-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrobomaster.hpp
313 lines (264 loc) · 9.44 KB
/
robomaster.hpp
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
#ifndef __ROBOMAS_HPP__
#define __ROBOMAS_HPP__
#include "mbed.h"
#include "math.h"
class robomaster {
public:
typedef enum {
current,
speed,
position
} control_type;
typedef enum {
c610,
c620,
gm6020
} esc_type;
typedef struct gain_s {
float Kp = 0;
float Ki = 0;
float Kd = 0;
} gain;
typedef struct data_s {
int counts = 0;
int rpm = 0;
int current = 0;
int prev_counts = -1;
int position_counts = 0;
float position = 0;
control_type control_type = control_type::current;
esc_type esc_type = esc_type::c610;
float target_rpm;
float target_position;
int send_current = 0;
int max_current = 0;
gain gain_speed;
gain gain_position;
bool stop = true;
bool emg = false;
float prev_error_speed;
float neutral = 0;
float last_position = -1;
float prev_error_position;
float error_integral_position;
float prev_input_input_delay;
float prev_output_input_delay;
float prev_ampare;
float prev_output;
float prev_error_mec;
float Kp_mec = 1;
float Ki_mec = 0;
float Kd_mec = 0;
} robomaster_data;
robomaster(PinName rx, PinName tx) : bus(rx,tx,hz) {
receive_thread.start(callback(this,&robomaster::receive_loop));
bus.attach(callback(this,&robomaster::receive_irq), CAN::RxIrq);
}
int get_counts(int id) {
return data[id].counts;
}
int get_rpm(int id) {
return data[id].rpm;
}
int get_current(int id) {
return data[id].current;
}
float get_position(int id) {
return data[id].position;
}
robomaster_data get_robomaster_data(int id) {
return data[id];
}
void set_neutral(int id, float neutral) {
data[id].neutral = neutral;
}
void set_emg(int id, bool emg) {
data[id].emg = emg;
}
void send_current() {
CANMessage message;
message.id = 0x200;
for(int id = 0; id < 4; id++) {
int current = fix_current(id);
message.data[id*2] = (current >> 8) & 0xFF;
message.data[id*2+1] = current & 0xFF;
}
bus.write(message);
message.id = 0x1FF;
for(int id = 0; id < 4; id++) {
int current = fix_current(id+4);
message.data[id*2] = (current >> 8) & 0xFF;
message.data[id*2+1] = current & 0xFF;
}
bus.write(message);
}
void set_control_type(int id, control_type control_type) {
data[id].control_type = control_type;
data[id].stop = true;
}
template<esc_type esc_type>
void set_max_current(int id, int current);
template<>
void set_max_current<c610>(int id, int current) {
if(current < -max_current_c610) current = -max_current_c610;
if(max_current_c610 < current) current = max_current_c610;
data[id].max_current = current;
}
template<>
void set_max_current<c620>(int id, int current) {
if(current < -max_current_c620) current = -max_current_c620;
if(max_current_c620 < current) current = max_current_c620;
data[id].max_current = current;
}
template<>
void set_max_current<gm6020>(int id, int current) {
if(current < -max_current_gm6020) current = -max_current_gm6020;
if(max_current_gm6020 < current) current = max_current_gm6020;
data[id].max_current = current;
}
template<control_type control_type>
void set_gain(int id, gain gain);
template<>
void set_gain<speed>(int id, gain gain) {
data[id].gain_speed = gain;
}
template<>
void set_gain<position>(int id, gain gain) {
data[id].gain_position = gain;
}
template<control_type control_type>
void set_target(int id, float target) {
if(data[id].control_type == current) {
set_target<current>(id,target);
} else if(data[id].control_type == speed) {
set_target<speed>(id, target);
} else if(data[id].control_type == position) {
set_target<position>(id, target);
}
}
template<>
void set_target<current>(int id, float target) {
data[id].send_current = static_cast<int>(target);
}
template<>
void set_target<speed>(int id, float target) {
data[id].target_rpm = target;
data[id].stop = false;
}
template<>
void set_target<position>(int id, float target) {
data[id].target_position = target;
data[id].stop = false;
}
void calculate_pid(int id, int delta_t) {
float filtered_rpm = input_delay(id, data[id].target_rpm);
float need_rpm = filtered_rpm + pid_control(id, filtered_rpm - data[id].rpm, (delta_t/1000.));
float target_ampare = need_rpm * rpm_to_ampare;
float model = calculate_plant_model(id, target_ampare);
float input_ampare = target_ampare + compensator(id, model - data[id].rpm);
data[id].send_current = input_ampare;
}
void calculate_rpm_pid(int id, int delta_t) {
float error_position = data[id].target_position - data[id].position;
data[id].error_integral_position += (error_position + data[id].prev_error_position) / 2.0 * (delta_t/1000.);
float target_rpm = error_position * data[id].gain_position.Kp +
data[id].error_integral_position * data[id].gain_position.Ki +
(error_position - data[id].prev_error_position) * data[id].gain_position.Kd;
data[id].prev_error_position = error_position;
data[id].target_rpm = target_rpm;
}
private:
const int hz = 1000000;
const int ppr = 8192;
const int max_current_c610 = 10000;
const int max_current_c620 = 10000;
const int max_current_gm6020 = 16000;
const float rpm_to_ampare = 0.29411764705882354;
robomaster_data data[8];
RawCAN bus;
CircularBuffer<CANMessage, 32> queue;
Thread receive_thread;
void receive_loop() {
while(true) {
while(!queue.empty()) {
CANMessage message;
queue.pop(message);
int id = message.id - 0x201;
if(0 <= id && id < 8) {
data[id].counts =uint16_t((message.data[0] << 8) |
message.data[1]);
data[id].rpm = int16_t((message.data[2] << 8) |
message.data[3]);
data[id].current = int16_t((message.data[4] << 8) |
message.data[5]);
if(data[id].prev_counts == -1) {
data[id].prev_counts = data[id].counts;
}
int delta_counts = data[id].prev_counts - data[id].counts;
if(delta_counts > ppr/2) {
data[id].position_counts++;
}
if(delta_counts < -ppr/2) {
data[id].position_counts--;
}
data[id].prev_counts = data [id].counts;
data[id].position = data[id].position_counts + (float)data[id].counts/ ppr - data[id].neutral;
}
}
}
}
void receive_irq() {
CANMessage message;
if(bus.read(message)) {
queue.push(message);
}
}
int fix_current(int id) {
int current = data[id].send_current;
if(current < -data[id].max_current) {
current = -data[id].max_current;
}
if(data[id].max_current < current) {
current = data[id].max_current;
}
if(data[id].stop || data[id].emg) {
current = 0;
data[id].error_integral_position = 0;
data[id].prev_output = 0;
data[id].prev_error_speed = 0;
data[id].prev_input_input_delay = 0;
data[id].prev_output_input_delay = 0;
}
return current;
}
float input_delay(int id, float input) {
float Kdel=0.04761904761904762; //遅延計算の重み 変えるな
float output = Kdel*input + Kdel* data[id].prev_input_input_delay + (0.9+Kdel/10)* data[id].prev_output_input_delay;
data[id].prev_input_input_delay = input;
data[id].prev_output_input_delay = output;
return output;
}
float pid_control(int id, float error, int delta_t) {
float error_integral_speed = (error + data[id].prev_error_speed)/2 * (delta_t/1000.);
float output = error * data[id].gain_speed.Kp +
error_integral_speed * data[id].gain_speed.Ki +
(error - data[id].prev_error_speed) * data[id].gain_speed.Kd;
data[id].prev_error_speed = error;
return output;
}
float calculate_plant_model(int id, float target_ampare) {
float plant_model = 0.085 * target_ampare +
0.085 * data[id].prev_ampare +
0.95 * data[id].prev_output;
data[id].prev_ampare = target_ampare;
data[id].prev_output = plant_model;
return plant_model;
}
float compensator(int id, float error) {
float output = data[id].Kp_mec * error +
data[id].Kd_mec*(error - data[id].prev_error_mec);
data[id].prev_error_mec = error;
return output;
}
};
#endif//__ROBOMAS_HPP__