-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest.hpp
430 lines (348 loc) · 14 KB
/
test.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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
#include "aci_comm_engine.hpp"
#include "aci_comm_uav.hpp"
#include "utilities/synchronizer.hpp"
/////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////
void
testcase() {
using namespace acc;
Engine<SerialBus>* ae;
try {
std::string port = "/dev/tty.usbserial-A504DRSI";
//std::string port = "/dev/tty.usbserial-AL00XNUW";
// Create an Engine with a SerialBus.
// The init arguments are the SerialBus
// args. *ae* is a pointer to the singleton
// instance of Engine.
ae = &Engine<SerialBus>::init(port, B57600);
// Add variables to read, to packet 1.
ae->add_read(0, Var::motor_rpm_1, Var::motor_rpm_2, Var::motor_rpm_3, Var::motor_rpm_4);
// ae->add_read(0, Var::acc_x, Var::acc_y, Var::acc_z);
// Add commands to write, packet 0.
ae->add_write(0, Cmd::DIMC_motor_1,
Cmd::DIMC_motor_2,
Cmd::DIMC_motor_3,
Cmd::DIMC_motor_4,
Cmd::ctrl_mode,
Cmd::ctrl_enabled,
Cmd::disable_motor_onoff_by_stick);
// Start the engine.
// Returns when the variables and
// commands callbacks are executed.
// Default engine params: 100, 10
ae->start(1000, 100);
// For each key_write, write the
// specified value.
ae->write(Cmd::ctrl_mode, 0);
ae->write(Cmd::ctrl_enabled, 1);
ae->write(Cmd::disable_motor_onoff_by_stick, 1);
// sleep(5);
std::cout << "READY TO ACC" << std::endl;
ae->write(Cmd::DIMC_motor_1, DIMC_motor_write_conv(1100));
// std::cout << "ACC" << std::endl;
sleep(2);
int i = 0;
while(i < 1000) {
// auto yaw = uav.attitude.yaw.read();
// auto pitch = uav.attitude.pitch.read();
// auto roll = uav.attitude.roll.read();
// std::cout << ae->read(Var::acc_x) << "\t" << ae->read(Var::acc_y) << "\t" << ae->read(Var::acc_z) << std::endl;
// ae->write(Cmd::DIMC_motor_1, 10);
if(i == 500) ae->write(Cmd::DIMC_motor_1, DIMC_motor_write_conv(2000));
std::cout << motor_rpm_read_conv(ae->read(Var::motor_rpm_1)) << std::endl;
i++;
usleep(10000);
}
std::cout << "STOP" << std::endl;
ae->write(Cmd::DIMC_motor_1, 0);
std::cout << "STOP" << std::endl;
sleep(5);
} catch (std::runtime_error e) {
std::cout << "Exception: " << e.what() << std::endl;
ae->stop();
} catch (...) {
std::cout << "!!! Unexpected error !!!" << std::endl;
ae->stop();
}
}
/////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////
void
testcase2() {
using namespace acc;
std::string port = "/dev/tty.usbserial-A504DRSI";
UAV uav(port, B57600, CTRL_MODE::DIMC);
try {
// Enable
//auto pitch = uav.angles.pitch;
//pitch.enable_read(0).enable_write(0);
//uav.angles.pitch.enable_write(0).enable_read(0);
// uav.attitude.yaw.enable_read(0);
// uav.attitude.pitch.enable_read(0);
// uav.attitude.roll.enable_read(0);
for (int i = 0; i < 4; i++) {
uav.motors[i].enable_read(0);
uav.motors[i].enable_write(0);
}
uav.start().control_enable(true);
// Write
//uav.angles.pitch.write(30.0);
//uav.angles.roll.write(30.0);
sleep(2);
std::cout << "READY" << std::endl;
// Read
for (int i = 0; i < 4; i++) {
uav.motors[i].write(1500);
sleep(2);
uav.motors[i].write(1075);
sleep(2);
}
} catch (std::runtime_error e) {
std::cout << "Exception: " << e.what() << std::endl;
uav.stop();
} catch (...) {
std::cout << "!!! Unexpected error !!!" << std::endl;
uav.stop();
}
}
/////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////
void
testcase3() {
using namespace acc;
std::string port = "/dev/tty.usbserial-A504DRSI";
UAV uav(port, B57600, CTRL_MODE::DIMC);
try {
uav.motors.enable_read(0).enable_write(0);
uav.start().control_enable(true);
// sleep(2);
// uav.motors.write({1500, 1500, 1500, 1500});
std::cout << "START" << std::endl;
uav.motors.start();
sleep(3);
uav.motors.stop();
std::cout << "STOP" << std::endl;
// uav.motors.write(std::array<FLOATING_POINT_PRECISION, 4>{{1075, 1075, 1075, 1075}});
} catch (std::runtime_error e) {
std::cout << "Exception: " << e.what() << std::endl;
uav.stop();
} catch (...) {
std::cout << "!!! Unexpected error !!!" << std::endl;
uav.stop();
}
}
/////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////
void
testcase_read() {
using namespace acc;
using namespace std;
// std::string port = "/dev/ttyUSB0";
std::string port = "/dev/tty.usbserial-A504DRSI";
UAV uav(port, B57600, CTRL_MODE::READ_ONLY);
// uav.orientation = UAV_Z::DOWNWARD;
uav.orientation = UAV_Z::UPWARD;
Logger lg(std::cout);
lg.floating_point_digits = 2;
std::ofstream myfile;
myfile.open ("./utilities/data.csv");
Logger lg_file(myfile);
lg_file.separator = ",\t";
lg_file.log("time", "roll", "pitch", "yaw", "eta", "eps1", "eps2", "eps3", "norm");
try {
// frame read test
uav.frame.enable_read_angles(0);
// uav.frame.enable_read_angles_d(1);
// uav.frame.enable_read_acc(1);
//magnetometer
// uav.magnetometer.enable_read(0);
// enable read info
// uav.info.status.enable_read(1);
// uav.info.flight_time.enable_read(1);
// uav.info.battery_voltage.enable_read(1);
// uav.info.cpu_load.enable_read(1);
// uav.info.up_time.enable_read(1);
// enable read RC channels
// uav.rc_ch[0].enable_read(1);
// uav.rc_ch[1].enable_read(1);
// uav.rc_ch[2].enable_read(1);
// uav.rc_ch[3].enable_read(1);
// uav.rc_ch[4].enable_read(1);
// uav.rc_ch[5].enable_read(1);
// uav.rc_ch[6].enable_read(1);
// uav.rc_ch[7].enable_read(1);
uav.start();
double freq = 100;
lg.timer.reset_start_time(); // set timer in logger to 0
// while(lg.timer.time() < 10.0) {
while(true) {
// print angles
lg.log(lg.timer.time(), uav.frame.roll.read(), uav.frame.pitch.read(), uav.frame.yaw.read());
auto q = uav.frame.quaternion();
lg_file.log(lg.timer.time(), uav.frame.roll.read(), uav.frame.pitch.read(), uav.frame.yaw.read(), q[0], q[1], q[2], q[3], sqrt(pow(q[0], 2) + pow(q[1], 2) + pow(q[2], 2) + pow(q[3], 2)));
// print angles dot
// lg.log(lg.time(), uav.frame.roll_d.read(), uav.frame.pitch_d.read(), uav.frame.yaw_d.read());
// print acc
// lg.log(lg.timer.time(), uav.frame.x_dd.read(), uav.frame.y_dd.read(), uav.frame.z_dd.read());
// print magentometer
// lg.log(lg.timer.time(), uav.magnetometer.x.read(), uav.magnetometer.y.read(), uav.magnetometer.z.read());
// print infos
// lg.log(lg.timer.time(), uav.info.status.read(), uav.info.flight_time.read(), uav.info.battery_voltage.read(), uav.info.cpu_load.read(), uav.info.up_time.read());
// print RC channels
// lg.log(lg.timer.time(), uav.rc_ch[0].read(), uav.rc_ch[1].read(), uav.rc_ch[2].read(), uav.rc_ch[3].read(), uav.rc_ch[4].read(), uav.rc_ch[5].read(), uav.rc_ch[6].read(), uav.rc_ch[7].read());
usleep(1E6/freq);
}
} catch (std::runtime_error e) {
std::cout << "Exception: " << e.what() << std::endl;
uav.stop();
} catch (...) {
std::cout << "!!! Unexpected error !!!" << std::endl;
uav.stop();
}
}
/////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////
void
testcase_motors_start_stop() {
using namespace acc;
std::string port = "/dev/tty.usbserial-A504DRSI";
UAV uav(port, B57600, CTRL_MODE::DIMC);
try {
uav.motors.enable_write(0);
uav.start().control_enable(true);
sleep(2);
std::cout << "START" << std::endl;
uav.motors.start();
sleep(4);
std::cout << "STOP" << std::endl;
uav.motors.stop();
} catch (std::runtime_error e) {
std::cout << "Exception: " << e.what() << std::endl;
uav.stop();
} catch (...) {
std::cout << "!!! Unexpected error !!!" << std::endl;
uav.stop();
}
}
/////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////
void
testcase_motors_dynamics() {
using namespace acc;
std::string port = "/dev/tty.usbserial-A504DRSI";
UAV uav(port, B57600, CTRL_MODE::DIMC);
std::ofstream myfile;
myfile.open ("./utilities/motors_dynamics_log.csv");
Logger lg_file(myfile);
lg_file.separator = ",\t";
lg_file.log("time", "rpm_d", "rmp");
Logger lg_console(std::cout);
double freq = 300;
mb::Syncronizer syncro(freq);
double rpm_d = 0.0;
try {
uav.motors[0].enable_read(0);
// uav.motors[1].enable_read(0);
uav.motors.enable_write(0);
uav.start(900, 1000, 3000, 2);
uav.control_enable(true);
sleep(1);
std::cout << "STOP MOTORS" << std::endl;
uav.motors.stop();
sleep(2);
std::cout << "START MOTORS" << std::endl;
uav.motors[0].start();
// uav.motors[1].start();
sleep(4);
lg_file.timer.reset_start_time(); // set timer in logger to 0
std::cout << "START WHILE" << std::endl;
while(lg_file.timer.time() < 60.0) {
syncro.start();
rpm_d = mb::signal_builder(lg_file.timer.time());
uav.motors[0].write(rpm_d);
// uav.motors[1].write(rpm_d);
int motor_read = uav.motors[0].read();
// int motor_read2 = uav.motors[1].read();
lg_file.log(lg_file.timer.time(), rpm_d, motor_read);
// lg_console.log(lg_file.timer.time(), rpm_d, motor_read);
syncro.stop();
}
std::cout << "EXIT WHILE" << std::endl;
uav.motors[0].start(); // to take the motor to the min speed
// uav.motors[1].start(); // to take the motor to the min speed
sleep(2);
std::cout << "STOP" << std::endl;
uav.motors[0].stop();
// uav.motors[1].stop();
} catch (std::runtime_error e) {
std::cout << "Exception: " << e.what() << std::endl;
uav.stop();
} catch (...) {
std::cout << "!!! Unexpected error !!!" << std::endl;
uav.stop();
}
}
/////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////
void
testcase_motors_dynamics_engine() {
using namespace acc;
std::string port = "/dev/tty.usbserial-A504DRSI";
Engine<SerialBus>* ae;
std::ofstream myfile;
myfile.open ("./utilities/motors_dynamics_log.csv");
Logger lg_file(myfile);
lg_file.separator = ",\t";
lg_file.log("time", "rpm_d", "rmp");
Logger lg_console(std::cout);
double freq = 200;
mb::Syncronizer syncro(freq);
double rpm_d = 0.0;
try {
ae = &Engine<SerialBus>::init(port, B57600);
ae->add_read(0, Var::motor_rpm_1);
ae->add_write(0, Cmd::DIMC_motor_1,
Cmd::DIMC_motor_2,
Cmd::DIMC_motor_3,
Cmd::DIMC_motor_4,
Cmd::ctrl_mode,
Cmd::ctrl_enabled,
Cmd::disable_motor_onoff_by_stick);
ae->start(1000, 10);
ae->write(Cmd::ctrl_mode, 0);
ae->write(Cmd::ctrl_enabled, 1);
ae->write(Cmd::disable_motor_onoff_by_stick, 0);
sleep(1);
std::cout << "STOP MOTORS" << std::endl;
ae->write(Cmd::DIMC_motor_1, 0,
Cmd::DIMC_motor_2, 0,
Cmd::DIMC_motor_3, 0,
Cmd::DIMC_motor_4, 0); // motors stop
sleep(2);
std::cout << "START MOTORS" << std::endl;
ae->write(Cmd::DIMC_motor_1, 1);
sleep(4);
lg_file.timer.reset_start_time(); // set timer in logger to 0
std::cout << "START WHILE" << std::endl;
while(lg_file.timer.time() < 10.0) {
syncro.start();
rpm_d = mb::signal_builder(lg_file.timer.time());
ae->write(Cmd::DIMC_motor_1, DIMC_motor_write_conv(rpm_d));
int motor_read = motor_rpm_read_conv(ae->read(Var::motor_rpm_1));
lg_file.log(lg_file.timer.time(), rpm_d, motor_read);
lg_console.log(lg_file.timer.time(), rpm_d, motor_read);
syncro.stop();
}
std::cout << "EXIT WHILE" << std::endl;
ae->write(Cmd::DIMC_motor_1, 1);
sleep(2);
std::cout << "STOP" << std::endl;
ae->write(Cmd::DIMC_motor_1, 0);
} catch (std::runtime_error e) {
std::cout << "Exception: " << e.what() << std::endl;
ae->stop();
} catch (...) {
std::cout << "!!! Unexpected error !!!" << std::endl;
ae->stop();
}
}