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#include "mbed.h"
5#include "algorithms/PID.h"
7#include <cmath>
8#include <string>
9
10constexpr double M3508_GEAR_RATIO = 3591.0 / 187.0;
11constexpr double M2006_GEAR_RATIO = 36.0;
12
13constexpr int TICKS_REVOLUTION = 8192;
14constexpr int TIMEOUT_MS = 400;
15constexpr int CAN_HANDLER_NUMBER = 2;
16
17constexpr int INT16_T_MAX = 32767;
18constexpr int INT15_T_MAX = 16383;
19
20static int s_sendIDs[3] = {0x200, 0x1FF, 0x2FF}; //IDs to send data
21static Thread s_motorFeedbackThread(osPriorityAboveNormal); //threading for Motor::tick()
22static Thread s_motorSendThread(osPriorityNormal); //threading for Motor::tick()
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
43 GIMBLY = 2,
44 GM6020 = 2,
45
46 M3508 = 3,
47
48 C610 = 4,
49 M2006 = 4,
50
52};
53
54class DJIMotor {
55
56private:
57
58 struct PidSettings{
59 float p;
60 float i;
61 float d;
62 float outCap;
63 float integralCap;
64 };
65
66 struct MotorSettings{
67 PidSettings pos;
68 PidSettings speed;
69 };
70
71 // Default motor PID settings
72
73 MotorSettings gimbly = {
74 {10.88,1.2,18.9,8000,500},
75 {0.13, 8.8, 0, 25000, 1000}
76 };
77
78 MotorSettings m3508 = {
79 {.48, 0.0137, 4.2, 3000, 300},
80 {1.79, 0.27, 10.57, 15000, 500}
81 };
82
83 MotorSettings m3508Flywheel = {
84 {.48, 0.0137, 4.2, 3000, 300},
85 {2.013, 0.319, 20.084, 15000, 500}
86 };
87
88 enum motorMoveMode{
89 OFF,
90 POS,
91 SPD,
92 POW,
93 ERR
94 };
95
96 static DJIMotor* s_allMotors [CAN_HANDLER_NUMBER][3][4];
97 static bool s_motorsExist [CAN_HANDLER_NUMBER][3][4];
98
99 static CANHandler* s_canHandlers[CAN_HANDLER_NUMBER];
101
102 short canID_0; // canID - 1, because canID is 1-8, arrays are 0-7
103 short motorID_0; // physical motorID - 1
104 motorMoveMode mode = OFF; // mode of the motor
105
106 // angle | velocity | torque | temperature
107 int16_t motorData[4] = {};
108 int value = 0;
109
110 int lastMotorAngle = 0;
111 int integratedAngle = 0;
112 long lastIntegrationTime = -1;
113
114 uint32_t timeOfLastFeedback = 0;
115 uint32_t timeOfLastPID = 0;
116
117 static void s_updateMultiTurnPosition();
118 static void s_sendOneID(CANHandler::CANBus canBus, short sendIDindex, bool debug = false);
119// static int s_calculateDeltaTicks(int target, int current);
120 void setOutput();
121
122public:
123
124 static int s_calculateDeltaPhase(int target, int current, int max = TICKS_REVOLUTION);
125 static float s_calculateDeltaPhaseF(float target, float current, float max);
126
127 static int motorCount;
129 std::string name = "NO_NAME";
130 int16_t powerOut = 0;
131
134
135 int multiTurn = 0;
137 bool useAbsEncoder = true;
138 bool printAngle = false;
139
141
142 explicit DJIMotor(bool isErroneousMotor = false);
143 DJIMotor(short motorID, CANHandler::CANBus canBus, motorType type = STANDARD, const std::string& name = "NO_NAME");
144 ~DJIMotor();
145
146 static void s_setCANHandlers(CANHandler* bus_1, CANHandler* bus_2, bool threadSend = true, bool threadFeedback = true);
147 static void s_getFeedback(bool debug = false);
148 static void s_sendValues(bool debug = false);
149 static void s_feedbackThread();
150 static void s_sendThread();
151
153 void printAllMotorData();
154
155 inline void setPower(int power){
156 value = power;
157 mode = POW;
158 powerOut = power;
159 }
160
161 inline void setSpeed(int speed){
162 value = speed;
163 mode = SPD;
164 }
165
166 inline void setPosition(int position){
167 value = position;
168 mode = POS;
169 }
170
171 inline motorMoveMode getMode(){
172 return mode;
173 }
174
175 __attribute__((unused)) inline bool isConnected() const {
176 return us_ticker_read() / 1000 - timeOfLastFeedback <= TIMEOUT_MS;
177 }
178 static bool s_theseConnected(DJIMotor* motors[], int size, bool debug = false);
179 static bool s_allConnected(bool debug = false);
180// static int s_calculateDeltaPhase(int target, int current, int max = TICKS_REVOLUTION);
181
182
183
184 inline int operator>>(motorDataType data) { return getData(data); }
185
186 inline void setPositionPID(float kP, float kI, float kD) { pidPosition.setPID(kP, kI, kD); }
187 inline void setPositionIntegralCap(double cap) { pidPosition.setIntegralCap((float)cap); }
188 inline void setPositionOutputCap(double cap) { pidPosition.setOutputCap((float)cap); }
189
190 inline void setSpeedPID(float kP, float kI, float kD) { pidSpeed.setPID(kP, kI, kD); }
191 inline void setSpeedIntegralCap(double cap) { pidSpeed.setIntegralCap((float)cap); }
192 inline void setSpeedDerivativeCap(double cap) { pidSpeed.setDerivativeCap((float)cap); }
193 inline void setSpeedOutputCap(double cap) { pidSpeed.setOutputCap((float)cap); }
194
195 inline int calculateSpeedPID(int desired, int current, double dt) { return pidSpeed.calculate(desired, current, dt); }
196 inline int calculatePositionPID(int desired, int current, double dt, int chassis_rpm = 0) { return (pidPosition.calculate(desired, current, dt) - chassis_rpm); }
197 inline int calculatePeriodicPosition(float dE, double dt, int chassis_rpm = 0) { return (pidPosition.calculatePeriodic(dE, dt) - chassis_rpm); }
198
199};
200
201#endif //TR_EMBEDDED_DJIMOTOR_H
int i
Definition BiCGSTAB_step_by_step.cpp:9
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 double M2006_GEAR_RATIO
Definition DJIMotor.h:11
constexpr int TIMEOUT_MS
Definition DJIMotor.h:14
motorType
Definition DJIMotor.h:39
@ M3508
Definition DJIMotor.h:46
@ GM6020
Definition DJIMotor.h:44
@ M3508_FLYWHEEL
Definition DJIMotor.h:51
@ GIMBLY
Definition DJIMotor.h:43
@ C610
Definition DJIMotor.h:48
@ M2006
Definition DJIMotor.h:49
@ STANDARD
Definition DJIMotor.h:41
@ NONE
Definition DJIMotor.h:40
constexpr int CAN_HANDLER_NUMBER
Definition DJIMotor.h:15
constexpr int INT15_T_MAX
Definition DJIMotor.h:18
constexpr int TICKS_REVOLUTION
Definition DJIMotor.h:13
constexpr int INT16_T_MAX
Definition DJIMotor.h:17
constexpr double M3508_GEAR_RATIO
Definition DJIMotor.h:10
int data[]
Definition Map_placement_new.cpp:1
float * p
Definition Tutorial_Map_using.cpp:9
Scalar Scalar int size
Definition benchVecAdd.cpp:17
Definition CANHandler.h:18
CANBus
Definition CANHandler.h:27
@ NOBUS
Definition CANHandler.h:27
Definition DJIMotor.h:54
static void s_sendValues(bool debug=false)
Definition DJIMotor.cpp:144
static bool s_theseConnected(DJIMotor *motors[], int size, bool debug=false)
Definition DJIMotor.cpp:218
static int s_calculateDeltaPhase(int target, int current, int max=TICKS_REVOLUTION)
Definition DJIMotor.cpp:282
void setPosition(int position)
Definition DJIMotor.h:166
static void s_setCANHandlers(CANHandler *bus_1, CANHandler *bus_2, bool threadSend=true, bool threadFeedback=true)
Definition DJIMotor.cpp:90
void setSpeedOutputCap(double cap)
Definition DJIMotor.h:193
void printAllMotorData()
Definition DJIMotor.cpp:278
int outputCap
Definition DJIMotor.h:136
static float s_calculateDeltaPhaseF(float target, float current, float max)
Definition DJIMotor.cpp:297
int operator>>(motorDataType data)
Definition DJIMotor.h:184
void setPositionPID(float kP, float kI, float kD)
Definition DJIMotor.h:186
void setPower(int power)
Definition DJIMotor.h:155
PID pidPosition
Definition DJIMotor.h:133
int16_t powerOut
Definition DJIMotor.h:130
motorMoveMode getMode()
Definition DJIMotor.h:171
void setSpeedIntegralCap(double cap)
Definition DJIMotor.h:191
int multiTurn
Definition DJIMotor.h:135
bool printAngle
Definition DJIMotor.h:138
motorType type
Definition DJIMotor.h:140
static void s_feedbackThread()
the thread that runs the ever-necessary DJIMotor::s_getFeedback()
Definition DJIMotor.cpp:376
void setSpeed(int speed)
Definition DJIMotor.h:161
int calculatePeriodicPosition(float dE, double dt, int chassis_rpm=0)
Definition DJIMotor.h:197
static bool initializedWarning
Definition DJIMotor.h:128
static void s_getFeedback(bool debug=false)
Definition DJIMotor.cpp:186
PID pidSpeed
Definition DJIMotor.h:132
static void s_sendThread()
the thread that runs the ever-necessary DJIMotor::s_sendValues()
Definition DJIMotor.cpp:387
int calculatePositionPID(int desired, int current, double dt, int chassis_rpm=0)
Definition DJIMotor.h:196
static bool s_allConnected(bool debug=false)
Definition DJIMotor.cpp:237
int calculateSpeedPID(int desired, int current, double dt)
Definition DJIMotor.h:195
bool useAbsEncoder
Definition DJIMotor.h:137
void setPositionIntegralCap(double cap)
Definition DJIMotor.h:187
static int motorCount
Definition DJIMotor.h:127
void setPositionOutputCap(double cap)
Definition DJIMotor.h:188
std::string name
Definition DJIMotor.h:129
~DJIMotor()
Definition DJIMotor.cpp:82
void setSpeedDerivativeCap(double cap)
Definition DJIMotor.h:192
int getData(motorDataType data)
Definition DJIMotor.cpp:261
void setSpeedPID(float kP, float kI, float kD)
Definition DJIMotor.h:190
__attribute__((unused)) inline bool isConnected() const
Definition DJIMotor.h:175
Definition PID.h:138
int calculatePeriodic(float error, double dt)
Definition PID.cpp:172
void setPID(float p, float i, float d, float integralCap=0, float outputCap=0)
Definition PID.cpp:139
void setOutputCap(float outCap)
Definition PID.cpp:230
int calculate(int desired, int current, double dt)
Definition PID.cpp:166
void setIntegralCap(float integralCap)
Definition PID.cpp:225
void setDerivativeCap(float derivativeCap)
#define max(a, b)
Definition datatypes.h:20