-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmotorMove.cpp
More file actions
180 lines (136 loc) · 3.25 KB
/
motorMove.cpp
File metadata and controls
180 lines (136 loc) · 3.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
#include <string>
#include <cstdint>
#include <cstdio>
const int CAN_HANDLER_NUMBER = 2;
enum motorType {
NONE = 0,
STANDARD = 1,
GIMBLY = 2,
GM6020 = 2,
M3508 = 3,
C610 = 4,
M2006 = 4,
M3508_FLYWHEEL = 5,
};
enum motorDataType {
ANGLE,
VELOCITY,
TORQUE,
TEMPERATURE,
POWER,
};
class Remote {
public:
enum class SwitchState
{
UNKNOWN,
DOWN,
MID,
UP
};
};
class CANHandler{
public:
enum CANBus {CANBUS_1, CANBUS_2, NOBUS};
};
class DJIMotor{
private:
static DJIMotor* s_allMotors [CAN_HANDLER_NUMBER][3][4];
static bool s_motorsExist [CAN_HANDLER_NUMBER][3][4];
short motorID_0;
short canID_0;
motorType type;
CANHandler::CANBus canBus;
std::string name;
enum motorMoveMode{
OFF,
POS,
SPD,
POW,
ERR
};
int value = 0;
motorMoveMode mode = OFF;
int16_t motorData[5] = {};
public:
DJIMotor(short motorID, CANHandler::CANBus canBus, motorType type, const std::string& name) {
canID_0 = static_cast<short>(motorID - 1);
motorID_0 = canID_0;
this -> canBus = canBus;
this -> type = type;
this -> name = name;
if(type == GM6020)
canID_0 += 4;
s_allMotors[canBus][canID_0 / 4][canID_0 % 4] = this;
s_motorsExist[canBus][canID_0 / 4][canID_0 % 4] = true;
}
void setPower(int power){
mode = POW;
value = power;
}
void setSpeed(int speed){
mode = SPD;
value = speed;
}
void setPosition(int position){
mode = POS;
value = position;
}
void setOutput(){
if(mode == POW)
motorData[POWER] = static_cast<int16_t>(value);
else if(mode == SPD)
motorData[VELOCITY] = static_cast<int16_t>(value);
else if(mode == POS)
motorData[ANGLE] = static_cast<int16_t>(value);
}
static void s_sendValues(){
for(int canBus = 0; canBus < CAN_HANDLER_NUMBER; canBus ++)
for(int j = 0; j < 3; j ++)
for(int k = 0; k < 4; k ++)
if(s_motorsExist[canBus][j][k])
s_allMotors[canBus][j][k] -> setOutput();
}
static void s_getFeedback(){
// need for PID in real class
}
int getData(motorDataType data){
return motorData[data];
}
};
DJIMotor* DJIMotor::s_allMotors[2][3][4];
bool DJIMotor::s_motorsExist[2][3][4];
struct remote_{
private:
Remote::SwitchState lS;
int lX;
public:
Remote::SwitchState leftSwitch() const{
return lS;
}
int leftX() const{
return lX;
}
void read(bool debug = false){
static int count;
count %= 660;
if((count % 15) / 5 == 0)
lS = Remote::SwitchState::UP;
else if((count % 15) / 5 == 1)
lS = Remote::SwitchState::MID;
else if((count % 15) / 5 == 2)
lS = Remote::SwitchState::DOWN;
lX = count;
if(debug){
printf("lX:[%d] lS:[%d]\n",lX, (int)lS);
}
count++;
}
} remote;
///////////////
//main.cpp
///////////////
// ONLY ADD CODE BELOW
// Use prints and getData() for debugging purposes
int main(){
}