forked from Rhoban/Metabot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
333 lines (286 loc) · 6.98 KB
/
main.cpp
File metadata and controls
333 lines (286 loc) · 6.98 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
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
#include <stdlib.h>
#include <wirish/wirish.h>
#include <servos.h>
#include <terminal.h>
#include <main.h>
#include <math.h>
#include <dxl.h>
#include <function.h>
#include <commands.h>
#include <rc.h>
#include <rhock.h>
#include "motors.h"
#include "voltage.h"
#include "buzzer.h"
#include "distance.h"
#include "config.h"
#include "motion.h"
#include "leds.h"
#include "mapping.h"
#include "behavior.h"
#include "imu.h"
#include "bt.h"
bool isUSB = false;
// Time
TERMINAL_PARAMETER_FLOAT(t, "Time", 0.0);
TERMINAL_COMMAND(version, "Getting firmware version")
{
terminal_io()->print("version=");
terminal_io()->println(METABOT_VERSION);
}
TERMINAL_COMMAND(started, "Is the robot started?")
{
terminal_io()->print("started=");
terminal_io()->println(started);
}
TERMINAL_COMMAND(rc, "Go to RC mode")
{
RC.begin(BT_BAUD);
terminal_init(&RC);
isUSB = false;
}
// Enabling/disabling move
TERMINAL_PARAMETER_BOOL(move, "Enable/Disable move", true);
// This destroys the fuse (used in dev)
/*
TERMINAL_COMMAND(suicide, "Lit the fuse")
{
digitalWrite(LIT, HIGH);
}
*/
// Setting the flag, called @50hz
bool flag = false;
void setFlag()
{
flag = true;
}
/**
* Checks wether there is enough voltage to start the motors
*/
bool can_start()
{
if (voltage_current() < 6 || voltage_error()) {
buzzer_play(MELODY_WARNING);
return false;
}
return true;
}
/**
* Initializing
*/
void setup()
{
// Input button
pinMode(BOARD_BUTTON_PIN, INPUT);
// Initializing terminal on the RC port
RC.begin(BT_BAUD);
terminal_init(&RC);
// Lit pin is output low
digitalWrite(LIT, LOW);
pinMode(LIT, OUTPUT);
// Initializing bluetooth module
bt_init();
// Initializing
motion_init();
// Initializing voltage measurement
voltage_init();
// Initializing the DXL bus
delay(500);
dxl_init();
// Initializing config (see config.h)
config_init();
// initializing distance
distance_init();
// Initializing the IMU
imu_init();
// Initializing positions to 0
for (int i=0; i<12; i++) {
dxl_set_position(i+1, 0.0);
}
for (int i=0; i<4; i++) {
l1[i] = l2[i] = l3[i] = 0;
}
// Configuring board LED as output
pinMode(BOARD_LED_PIN, OUTPUT);
digitalWrite(BOARD_LED_PIN, LOW);
// Initializing the buzzer, and playing the start-up melody
buzzer_init();
buzzer_play(MELODY_BOOT);
// Enable 50hz ticking
servos_init();
servos_attach_interrupt(setFlag);
RC.begin(BT_BAUD);
}
/**
* Computing the servo values
*/
void tick()
{
static bool wasMoving = false;
if (voltage_error()) {
// If there is a voltage error, blinks the LEDs orange and
// stop any motor activity
dxl_disable_all();
#ifdef RHOCK
rhock_killall();
#endif
if (t < 0.5) {
led_set_all(LED_R|LED_G);
} else {
led_set_all(0);
}
t += 0.02;
if (t > 1.0) t -= 1.0;
return;
}
// The robot is disabled
if (!move || !started) {
motors_read();
t = 0.0;
return;
}
// Incrementing and normalizing t
t += motion_get_f()*0.02;
if (t > 1.0) {
t -= 1.0;
colorize();
}
if (t < 0.0) t += 1.0;
if (!wasMoving && motion_is_moving()) {
t = 0.0;
}
wasMoving = motion_is_moving();
// Robot behavior
behavior_tick(0.02);
motion_tick(t);
// Sending order to servos
dxl_set_position(mapping[0], l1[0]);
dxl_set_position(mapping[3], l1[1]);
dxl_set_position(mapping[6], l1[2]);
dxl_set_position(mapping[9], l1[3]);
dxl_set_position(mapping[1], l2[0]);
dxl_set_position(mapping[4], l2[1]);
dxl_set_position(mapping[7], l2[2]);
dxl_set_position(mapping[10], l2[3]);
dxl_set_position(mapping[2], l3[0]);
dxl_set_position(mapping[5], l3[1]);
dxl_set_position(mapping[8], l3[2]);
dxl_set_position(mapping[11], l3[3]);
}
static int btnLast = 0;
static bool btn = false;
static int id = 13;
void buttonPress()
{
btnLast = millis();
}
void idAudio()
{
for (int k=0; k<id; k++) {
buzzer_beep(400, 50);
buzzer_wait_play();
delay(150);
}
}
/**
* The button is used to do some configurations, for more information
* please refer to the official documentation
*/
void buttonRelease()
{
if (millis()-btnLast > 6000) {
// Pressed more than 6s, configuring bluetooth
buzzer_play(MELODY_BEGIN);
buzzer_wait_play();
bt_set_config("Metabot", "1234");
buzzer_play(MELODY_BEGIN);
buzzer_wait_play();
} else if (millis()-btnLast > 2000) {
// Pressed more than 2s, entering ID sequence
id = 1;
buzzer_play(MELODY_BEGIN);
buzzer_wait_play();
} else {
// Next id sequence
if (id <= 12) {
int success = 0;
// Checking that there is exactly one servo on the bus
int discover = 0;
for (int k=1; k<=12; k++) {
bool ok = false;
for (int t=0; t<3; t++) {
if (dxl_ping(k)) {
ok = true;
}
delay(3);
}
if (ok) {
discover++;
}
}
// Configuring the servo on the bus to the target ID
// (using broadcast as address)
if (discover == 1) {
for (int k=0; k<3; k++) {
dxl_configure(DXL_BROADCAST, id);
dxl_write_byte(id, DXL_LED, LED_G);
delay(30);
// Checking that the servo now is correct
if (dxl_ping(id)) {
success++;
}
}
}
// Next ID
if (success > 0) {
idAudio();
id++;
if (id > 12) {
// It's over
delay(500);
buzzer_play(MELODY_BEGIN);
buzzer_wait_play();
}
} else {
buzzer_play(MELODY_WARNING);
buzzer_wait_play();
}
}
}
}
void loop()
{
bool btnNew = digitalRead(BOARD_BUTTON_PIN);
if (btnNew != btn) {
if (btnNew) buttonPress();
else buttonRelease();
btn = btnNew;
}
// Buzzer update
buzzer_tick();
// IMU ticking
imu_tick();
// Sampling the voltage
voltage_tick();
// Sampling the distance
distance_tick();
// Updating the terminal
terminal_tick();
#if defined(RHOCK)
rhock_tick();
#endif
if (SerialUSB.available() && !isUSB) {
isUSB = true;
terminal_init(&SerialUSB);
}
if (RC.available() && isUSB) {
isUSB = false;
terminal_init(&RC);
}
// Calling user motion tick
if (flag) {
flag = false;
tick();
dxl_flush();
}
}