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