TR-mbed 1.0
Loading...
Searching...
No Matches
DJIMotor.h
Go to the documentation of this file.
1#ifndef TR_EMBEDDED_DJIMOTOR_H
2#define TR_EMBEDDED_DJIMOTOR_H
3
4#pragma once
5#include "mbed.h"
9#include <cmath>
10#include <string>
11
12constexpr float M3508_GEAR_RATIO = 3591.0f / 187.0f;
13constexpr float M2006_GEAR_RATIO = 36.0f;
14
15constexpr int TICKS_REVOLUTION = 8192;
16constexpr int TIMEOUT_MS = 400;
17constexpr int CAN_HANDLER_NUMBER = 2;
18
19constexpr int INT16_T_MAX = 32767;
20constexpr int INT15_T_MAX = 16383;
21
22constexpr int s_sendIDs[3] = {0x200, 0x1FF, 0x2FF}; //IDs to send data
23
33
34
35//keep in mind that in the constructor, this is only used to
36//set the default pid values and gear ratio. The motorType will
37//be changed to STANDARD, because that's what the code uses.
38
40 NONE = 0,
41 STANDARD = 1, //identifier for all motors that use the standard can protocol, used by the C610 and C620
42 GM6020 = 2,
43 M3508 = 3,
44 M2006 = 4,
46};
47
48class DJIMotor {
49public:
57
67
68
69 static int motorCount;
70 static bool initializedWarning;
71 std::string name = "NO_NAME";
72 int16_t powerOut = 0;
73
76
77 int multiTurn = 0;
79 bool useAbsEncoder = false; // false for everything except gm6020s
80 bool printAngle = false;
81
83
84
85 explicit DJIMotor(bool isErroneousMotor = false);
86 DJIMotor(short motorID, CANHandler::CANBus canBus, motorType type = STANDARD, const std::string& name = "NO_NAME");
87 DJIMotor(config cfg);
88 ~DJIMotor();
89
90 static int calculateDeltaPhase(int target, int current, int max = TICKS_REVOLUTION);
91 static float calculateDeltaPhase(float target, float current, float max);
92
93 static void setCanHandlers(CANHandler * can_1, CANHandler * can_2);
94 static void getCan1Feedback(const CANMsg * msg);
95 static void getCan2Feedback(const CANMsg * msg);
96 static void sendValues(bool debug = false);
97
98 float getData(motorDataType data);
99
100 inline void setMotorOutput(int val, motorMoveMode mod)
101 {
102 value = val;
103 mode = mod;
104 }
105
106 inline void setPower(int power)
107 {
108 value = (float)power;
109 mode = POW;
110 powerOut = power;
111 }
112
113 inline void setSpeed(float speed){
114 value = speed;
115 mode = SPD;
116 }
117
118 inline void setPosition(float position){
119 value = position;
120 mode = POS;
121 }
122
124 return mode;
125 }
126
127 __attribute__((unused)) inline bool isConnected() const {
128 return us_ticker_read() / 1000 - timeOfLastFeedback <= TIMEOUT_MS;
129 }
130
131 inline int operator>>(motorDataType data) { return getData(data); }
132 DJIMotor(const DJIMotor&) = delete;
133 DJIMotor& operator=(const DJIMotor&) = delete;
134
135
136 inline void setPositionPID(float kP, float kI, float kD) { pidPosition.setPID(kP, kI, kD); }
137 inline void setPositionIntegralCap(double cap) { pidPosition.setIntegralCap((float)cap); }
138 inline void setPositionOutputCap(double cap) { pidPosition.setOutputCap((float)cap); }
139
140 inline void setSpeedPID(float kP, float kI, float kD) { pidSpeed.setPID(kP, kI, kD); }
141 inline void setSpeedIntegralCap(double cap) { pidSpeed.setIntegralCap((float)cap); }
142 inline void setSpeedDerivativeCap(double cap) { pidSpeed.setDerivativeCap((float)cap); }
143 inline void setSpeedOutputCap(double cap) { pidSpeed.setOutputCap((float)cap); }
144
145 inline float calculateSpeedPID(float desired, float current, double dt) { return pidSpeed.calculate(desired, current, dt); }
146 inline float calculatePositionPID(float desired, float current, double dt) { return pidSpeed.calculate(pidPosition.calculate(desired, current, dt), getData(VELOCITY), dt); }
147 inline float calculatePeriodicPosition(float dE, double dt) { return pidSpeed.calculate(pidPosition.calculatePeriodic(dE, dt), getData(VELOCITY), dt); }
148
149private:
150 static DJIMotor* s_allMotors [CAN_HANDLER_NUMBER][3][4];
151 static bool s_motorsExist [CAN_HANDLER_NUMBER][3][4];
152
153 static CANHandler* s_canHandlers[CAN_HANDLER_NUMBER];
155
156 short canID_0; // canID - 1, because canID is 1-8, arrays are 0-7
157 short motorID_0; // physical motorID - 1
158 motorMoveMode mode = OFF; // mode of the motor
159
160 // angle | velocity | torque | temperature
161 int16_t motorData[4] = {};
162 float value = 0;
163
164 int lastMotorAngle = 0;
165 int integratedAngle = 0;
166 long lastIntegrationTime = -1;
167
168 uint32_t timeOfLastFeedback = 0;
169 uint32_t timeOfLastPID = 0;
170
171 static void updateOneMultiTurn(int canBus, int can_line, int motor_id);
172 static void sendOneID(CANHandler::CANBus canBus, short sendIDindex, bool debug = false);
173 void setOutput();
174};
175
176#endif //TR_EMBEDDED_DJIMOTOR_H
constexpr float M3508_GEAR_RATIO
Definition DJIMotor.h:12
constexpr float M2006_GEAR_RATIO
Definition DJIMotor.h:13
motorDataType
Definition DJIMotor.h:24
@ ANGLE
Definition DJIMotor.h:25
@ VELOCITY
Definition DJIMotor.h:26
@ MULTITURNANGLE
Definition DJIMotor.h:29
@ POWEROUT
Definition DJIMotor.h:30
@ TORQUE
Definition DJIMotor.h:27
@ TEMPERATURE
Definition DJIMotor.h:28
@ VALUE
Definition DJIMotor.h:31
constexpr int TIMEOUT_MS
Definition DJIMotor.h:16
motorType
Definition DJIMotor.h:39
@ M3508
Definition DJIMotor.h:43
@ GM6020
Definition DJIMotor.h:42
@ M3508_FLYWHEEL
Definition DJIMotor.h:45
@ M2006
Definition DJIMotor.h:44
@ STANDARD
Definition DJIMotor.h:41
@ NONE
Definition DJIMotor.h:40
constexpr int CAN_HANDLER_NUMBER
Definition DJIMotor.h:17
constexpr int INT15_T_MAX
Definition DJIMotor.h:20
constexpr int TICKS_REVOLUTION
Definition DJIMotor.h:15
constexpr int s_sendIDs[3]
Definition DJIMotor.h:22
constexpr int INT16_T_MAX
Definition DJIMotor.h:19
Definition CANHandler.h:33
CANBus
Definition CANHandler.h:45
@ NOBUS
Definition CANHandler.h:45
Definition CANMsg.h:16
Definition DJIMotor.h:48
float calculatePeriodicPosition(float dE, double dt)
Definition DJIMotor.h:147
void setSpeedOutputCap(double cap)
Definition DJIMotor.h:143
int outputCap
Definition DJIMotor.h:78
int operator>>(motorDataType data)
Definition DJIMotor.h:131
static int calculateDeltaPhase(int target, int current, int max=TICKS_REVOLUTION)
Definition DJIMotor.cpp:341
void setPositionPID(float kP, float kI, float kD)
Definition DJIMotor.h:136
void setPower(int power)
Definition DJIMotor.h:106
PID pidPosition
Definition DJIMotor.h:75
int16_t powerOut
Definition DJIMotor.h:72
void setSpeed(float speed)
Definition DJIMotor.h:113
motorMoveMode getMode()
Definition DJIMotor.h:123
void setSpeedIntegralCap(double cap)
Definition DJIMotor.h:141
static void sendValues(bool debug=false)
Definition DJIMotor.cpp:127
int multiTurn
Definition DJIMotor.h:77
bool printAngle
Definition DJIMotor.h:80
motorType type
Definition DJIMotor.h:82
motorMoveMode
Definition DJIMotor.h:50
@ SPD
Definition DJIMotor.h:53
@ POS
Definition DJIMotor.h:52
@ OFF
Definition DJIMotor.h:51
@ POW
Definition DJIMotor.h:54
@ ERR
Definition DJIMotor.h:55
static bool initializedWarning
Definition DJIMotor.h:70
float getData(motorDataType data)
Definition DJIMotor.cpp:321
PID pidSpeed
Definition DJIMotor.h:74
DJIMotor(const DJIMotor &)=delete
bool useAbsEncoder
Definition DJIMotor.h:79
void setPositionIntegralCap(double cap)
Definition DJIMotor.h:137
static int motorCount
Definition DJIMotor.h:69
float calculateSpeedPID(float desired, float current, double dt)
Definition DJIMotor.h:145
void setPositionOutputCap(double cap)
Definition DJIMotor.h:138
void setPosition(float position)
Definition DJIMotor.h:118
float calculatePositionPID(float desired, float current, double dt)
Definition DJIMotor.h:146
std::string name
Definition DJIMotor.h:71
static void getCan2Feedback(const CANMsg *msg)
Definition DJIMotor.cpp:220
~DJIMotor()
Definition DJIMotor.cpp:71
void setSpeedDerivativeCap(double cap)
Definition DJIMotor.h:142
static void setCanHandlers(CANHandler *can_1, CANHandler *can_2)
Definition DJIMotor.cpp:175
void setSpeedPID(float kP, float kI, float kD)
Definition DJIMotor.h:140
static void getCan1Feedback(const CANMsg *msg)
Definition DJIMotor.cpp:181
DJIMotor & operator=(const DJIMotor &)=delete
__attribute__((unused)) inline bool isConnected() const
Definition DJIMotor.h:127
void setMotorOutput(int val, motorMoveMode mod)
Definition DJIMotor.h:100
Definition PID.h:138
float calculatePeriodic(float error, float dt_ms)
Definition PID.cpp:181
void setPID(float p, float i, float d, float integralCap=0, float outputCap=0)
Definition PID.cpp:148
void setOutputCap(float outCap)
Definition PID.cpp:238
float calculate(float desired, float current, float dt_ms)
Definition PID.cpp:175
void setIntegralCap(float integralCap)
Definition PID.cpp:233
void setDerivativeCap(float derivativeCap)
Definition DJIMotor.h:59
motorType type
Definition DJIMotor.h:62
PID::config pos_cfg
Definition DJIMotor.h:65
CANHandler::CANBus canBus
Definition DJIMotor.h:61
std::string name
Definition DJIMotor.h:63
PID::config vel_cfg
Definition DJIMotor.h:64
short motorID
Definition DJIMotor.h:60
Definition PID.h:154