forked from cvra/nastya
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmatch.c
489 lines (420 loc) · 13.5 KB
/
match.c
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
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <ucos_ii.h>
#include <control_system_manager/control_system_manager.h>
#include <pid/pid.h>
#include "tasks.h"
#include <uptime.h>
#include "control.h"
#include "position_integration.h"
#include "hardware.h"
#include "plot_task.h"
#include "util.h"
#include "drive.h"
#include "match.h"
#include "param.h"
OS_STK match_task_stk[MATCH_TASK_STACKSIZE];
bool next_match_team_red = false;
static bool restart_match = false;
timestamp_t match_start;
bool match_running = false;
static bool match_abort;
// mirror functions to change coordinates to the other team
float mirror_x(float x)
{
return MATCH_TABLE_LENGHT - x;
}
float mirror_y(float y)
{
return y;
}
float mirror_heading(float h)
{
return circular_range(M_PI - h);
}
int mirror_cannon(int c)
{
if (c <= 3) {
return c + 3;
} else {
return c - 3;
}
}
#define MAMMOTH_CAPTURE_SETUP_TIME 10000000 // [us]
#define MAX_NB_MATCH_ACTIONS 128
static match_action_t match_actions[MAX_NB_MATCH_ACTIONS] = {
{7, 2000.000000, 0.000000},
{1, 0.22, 0.45},
{1, 0.466437, 0.553406},
{2, 1.570796, 0.000000},
{1, 0.700000, 0.600000},
{5, 1.000000, 0.000000},
{1, 0.740000, 0.600000},
{5, 2.000000, 0.000000},
{5, 3.000000, 0.000000},
{1, 0.770000, 0.600000},
{5, 4.000000, 0.000000},
{5, 5.000000, 0.000000},
{1, 0.800000, 0.600000},
{5, 6.000000, 0.000000},
{1, 1.300000, 0.600000},
{2, -0.520000, 0.000000},
{1, 1.300000, 0.150000},
{4, 0.000000, 0.000000},
{1, 1.300000, 0.040000},
{1, 1.300000, 0.550000},
{2, -1.570796, 0.000000},
{1, 1.100000, 0.550000},
{1, 0.800000, 0.550000},
{1, 0.800000, 0.350000},
{0, 0.000000, 0.000000},
{0, 0.000000, 0.000000}
};
static void match_exec(bool team_red, match_action_t *a);
bool match_action_timeout(int time_to_end_of_match)
{
return (match_abort ||
(match_running && uptime_get() - match_start > MATCH_DURATION - time_to_end_of_match));
}
static bool wait_for_start(void)
{
return !(IORD(PIO_BASE, 0) & 0x1000);
}
#define RESET_POS_X 0.105
#define RESET_POS_Y 0.135
#define RESET_HEADING (- M_PI / 3)
#define START_POS_X 0.21569117903709
#define START_POS_Y 0.25893285870552
#define START_HEADING -4.0729007720947
void match_run(void)
{
emergency_stop_en = false;
match_running = false;
control_update_setpoint_vx(0);
control_update_setpoint_vy(0);
control_update_setpoint_omega(0);
nastya_cs.vx_control_enable = false;
nastya_cs.vy_control_enable = false;
nastya_cs.omega_control_enable = false;
enable_postion_control = false;
enable_heading_control = false;
hw_cannon_arm_all();
OSTimeDly(OS_TICKS_PER_SEC / 2);
while (!wait_for_start()) OSTimeDly(OS_TICKS_PER_SEC/100);
match_abort = false;
OSTimeDly(OS_TICKS_PER_SEC / 2);
bool team_red = next_match_team_red;
if (team_red) {
position_reset_to(mirror_x(RESET_POS_X), mirror_y(RESET_POS_Y), RESET_HEADING);
drive_set_dest(mirror_x(RESET_POS_X), mirror_y(RESET_POS_Y));
printf("reset match red %f %f %f\n", get_position_x(), get_position_y(), get_heading());
} else {
position_reset_to(RESET_POS_X, RESET_POS_Y, RESET_HEADING);
drive_set_dest(RESET_POS_X, RESET_POS_Y);
printf("reset match yellow %f %f %f\n", get_position_x(), get_position_y(), get_heading());
}
drive_disable_heading_ctrl();
enable_postion_control = true;
enable_heading_control = true;
nastya_cs.vx_control_enable = true;
nastya_cs.vy_control_enable = true;
nastya_cs.omega_control_enable = true;
OSTimeDly(OS_TICKS_PER_SEC * 2);
if (team_red) {
printf("start seq red\n");
drive_goto(mirror_x(0.19162034988403), mirror_y(0.19018436968327),0,false);
drive_goto(mirror_x(START_POS_X), mirror_y(START_POS_Y),0,false);
OSTimeDly(OS_TICKS_PER_SEC * 3);
drive_set_heading(mirror_heading(START_HEADING));
drive_sync_heading(0);
} else {
printf("start seq yellow\n");
drive_goto(0.19162034988403, 0.19018436968327,0,false);
drive_goto(START_POS_X, START_POS_Y,0,false);
OSTimeDly(OS_TICKS_PER_SEC * 3);
drive_set_heading(START_HEADING);
drive_sync_heading(0);
}
printf("seq term: %f %f %f\n", get_position_x(), get_position_y(), get_heading());
// wait for start signal
while (wait_for_start()) {
OSTimeDly(OS_TICKS_PER_SEC/100);
if (match_abort)
goto abort_match;
}
emergency_stop_en = true;
match_start = uptime_get();
match_running = true;
printf("much started [%d]\nwow\n", (int)match_start);
int i;
for (i = 0; i < MAX_NB_MATCH_ACTIONS; i++) {
match_exec(team_red, &match_actions[i]);
if (match_abort)
goto abort_match;
if (uptime_get() - match_start > MATCH_DURATION - MAMMOTH_CAPTURE_SETUP_TIME)
goto end_of_match;
}
end_of_match:
while (uptime_get() - match_start < MATCH_DURATION - MAMMOTH_CAPTURE_SETUP_TIME) {
if (match_abort)
goto abort_match;
OSTimeDly(OS_TICKS_PER_SEC/100);
}
// choose nearest mammoth
float pos_x, pos_y;
get_position(&pos_x, &pos_y);
float mammoth_x, mammoth_y = 0;
if (pos_x > MATCH_TABLE_LENGHT/2) {
mammoth_x = MATCH_TABLE_LENGHT - 0.8;
} else {
mammoth_x = 0.8;
}
// orient robot for mammoth capture
drive_set_look_at(mammoth_x, mammoth_y);
OSTimeDly(OS_TICKS_PER_SEC);
drive_set_dest(mammoth_x, mammoth_y + 0.35);
emergency_stop_en = false;
// wait until .5s after match
while (uptime_get() - match_start < MATCH_DURATION + 500000)
OSTimeDly(OS_TICKS_PER_SEC/100);
// fire
hw_set_net(1);
OSTimeDly(OS_TICKS_PER_SEC *3);
hw_set_net(0);
OSTimeDly(OS_TICKS_PER_SEC);
hw_set_net(1);
OSTimeDly(OS_TICKS_PER_SEC/2);
hw_set_net(0);
OSTimeDly(OS_TICKS_PER_SEC/2);
hw_set_net(1);
OSTimeDly(OS_TICKS_PER_SEC/2);
hw_set_net(0);
OSTimeDly(OS_TICKS_PER_SEC/2);
hw_set_net(1);
OSTimeDly(OS_TICKS_PER_SEC/2);
hw_set_net(0);
OSTimeDly(OS_TICKS_PER_SEC/2);
hw_set_net(1);
OSTimeDly(OS_TICKS_PER_SEC/2);
hw_set_net(0);
OSTimeDly(OS_TICKS_PER_SEC/2);
printf("captured mammoth\n");
abort_match:
control_update_setpoint_vx(0);
control_update_setpoint_vy(0);
control_update_setpoint_omega(0);
match_running = false;
match_abort = false;
while (wait_for_start()) OSTimeDly(OS_TICKS_PER_SEC/100);
return;
}
void match_task(void *arg)
{
while (!wait_for_start() && !restart_match) OSTimeDly(OS_TICKS_PER_SEC/100);
while (1) {
match_run();
}
}
void ready_for_match(void)
{
OSTaskCreateExt(match_task,
NULL,
&match_task_stk[MATCH_TASK_STACKSIZE-1],
MATCH_TASK_PRIORITY,
MATCH_TASK_PRIORITY,
&match_task_stk[0],
MATCH_TASK_STACKSIZE,
NULL, 0);
}
void match_set_red(void)
{
next_match_team_red = true;
}
void match_set_yellow(void)
{
next_match_team_red = false;
}
void match_restart(bool team_red)
{
match_abort = true;
restart_match = true;
next_match_team_red = team_red;
}
static void match_exec(bool team_red, match_action_t *a)
{
int wait_ms;
switch (a->cmd) {
case MATCH_ACTION_MOVE:
if (team_red) {
drive_goto(mirror_x(a->arg1), mirror_y(a->arg2),0,false);
} else {
drive_goto(a->arg1, a->arg2,0,false);
}
break;
case MATCH_ACTION_SET_HEADING:
if (team_red) {
drive_set_heading(mirror_heading(a->arg1));
} else {
drive_set_heading(a->arg1);
}
break;
case MATCH_ACTION_SET_LOOK_AT:
if (team_red) {
drive_set_look_at(mirror_x(a->arg1), mirror_y(a->arg2));
} else {
drive_set_look_at(a->arg1, a->arg2);
}
break;
case MATCH_ACTION_SYNC_HEADING:
drive_sync_heading(0);
break;
case MATCH_ACTION_FIRE_CANNON:
if (team_red) {
hw_cannon_fire(mirror_cannon(a->arg1));
} else {
hw_cannon_fire(a->arg1);
}
// printf("cannon %d: boooom!\n", (int)a->arg1);
break;
case MATCH_ACTION_SLEEP_MS:
wait_ms = a->arg1;
while (wait_ms > 0) {
OSTimeDly(OS_TICKS_PER_SEC / 100);
wait_ms -= 10;
if (match_action_timeout(MAMMOTH_CAPTURE_SETUP_TIME))
break;
}
break;
case MATCH_ACTION_WAIT_END_OF_MATCH:
while (!match_action_timeout(MAMMOTH_CAPTURE_SETUP_TIME))
OSTimeDly(OS_TICKS_PER_SEC / 100);
break;
}
}
int match_action_list(char* buffer, int buf_len)
{
int i, ret, remaining_sz = buf_len;
for(i = 0; i < MAX_NB_MATCH_ACTIONS; i++) {
switch (match_actions[i].cmd) {
case MATCH_ACTION_NOP:
if (i == 0 || (i != 0 && match_actions[i-1].cmd != MATCH_ACTION_NOP)){
ret = snprintf(buffer, remaining_sz, "[%3d] NOP\n", i);
} else {
ret = 0;
}
break;
case MATCH_ACTION_MOVE:
ret = snprintf(buffer, remaining_sz,
"[%3d] Move (%1.3f, %1.3f)\n",
i, match_actions[i].arg1, match_actions[i].arg2);
break;
case MATCH_ACTION_SET_HEADING:
ret = snprintf(buffer, remaining_sz,
"[%3d] Set heading to %+1.2f\n",
i, match_actions[i].arg1);
break;
case MATCH_ACTION_SET_LOOK_AT:
ret = snprintf(buffer, remaining_sz,
"[%3d] Look at (%1.3f, %1.3f)\n",
i, match_actions[i].arg1, match_actions[i].arg2);
break;
case MATCH_ACTION_SYNC_HEADING:
ret = snprintf(buffer, remaining_sz, "[%3d] Sync heading.\n", i);
break;
case MATCH_ACTION_FIRE_CANNON:
ret = snprintf(buffer, remaining_sz,
"[%3d] Fire cannon Nb. %1.0f Arrrr!\n",
i, match_actions[i].arg1);
break;
case MATCH_ACTION_WAIT_END_OF_MATCH:
ret = snprintf(buffer, remaining_sz, "[%3d] Wait end of match.\n", i);
break;
case MATCH_ACTION_SLEEP_MS:
ret = snprintf(buffer, remaining_sz, "[%3d] Sleep %.0f ms.\n",
i, match_actions[i].arg1);
break;
default:
ret = snprintf(buffer, remaining_sz, "[%3d] Unknown command.\n", i);
break;
}
if (ret < 0) {
// snprintf error
return -1;
}
if (ret > remaining_sz) {
// buffer overflow
return -2;
}
buffer += ret;
remaining_sz -= ret;
}
return 0;
}
void match_action_modify(int index, int cmd, float arg1, float arg2)
{
if (index < MAX_NB_MATCH_ACTIONS && index >= 0) {
match_actions[index].cmd = cmd;
match_actions[index].arg1 = arg1;
match_actions[index].arg2 = arg2;
}
}
void match_action_insert(int index) {
if (index < MAX_NB_MATCH_ACTIONS - 1 && index >= 0) {
memmove(&match_actions[index+1], &match_actions[index],
(MAX_NB_MATCH_ACTIONS - index - 1) * sizeof(match_action_t));
} else if (index != MAX_NB_MATCH_ACTIONS - 1){
return; // invalid index
}
match_action_modify(index, MATCH_ACTION_NOP, 0, 0);
}
void match_action_delete(int index)
{
if (index < MAX_NB_MATCH_ACTIONS - 1 && index >= 0) {
memmove(&match_actions[index], &match_actions[index+1],
(MAX_NB_MATCH_ACTIONS - index - 1) * sizeof(match_action_t));
} else if (index != MAX_NB_MATCH_ACTIONS - 1){
return; // invalid index
}
match_action_modify(MAX_NB_MATCH_ACTIONS - 1, MATCH_ACTION_NOP, 0, 0);
}
int match_action_save_as_c_code(char* buffer, int buf_len)
{
int i, ret, remaining_sz = buf_len;
// print first line
ret = snprintf(buffer, remaining_sz,
"static match_action_t match_actions[MAX_NB_MATCH_ACTIONS] = {\n");
if (ret < 0) return -1;
if (ret > remaining_sz) return -2;
buffer += ret;
remaining_sz -= ret;
for(i = 0; i < MAX_NB_MATCH_ACTIONS; i++) {
if (i != MAX_NB_MATCH_ACTIONS - 1) {
if (i > 0 && match_actions[i - 1].cmd == MATCH_ACTION_NOP &&
match_actions[i].cmd == MATCH_ACTION_NOP) {
ret = 0;
} else {
ret = snprintf(buffer, remaining_sz, "\t{%d, %f, %f},\n",
match_actions[i].cmd,
match_actions[i].arg1,
match_actions[i].arg2);
}
} else {
ret = snprintf(buffer, remaining_sz, "\t{%d, %f, %f}\n};\n",
match_actions[i].cmd,
match_actions[i].arg1,
match_actions[i].arg2);
}
if (ret < 0) {
// snprintf error
return -1;
}
if (ret > remaining_sz) {
// buffer overflow
return -2;
}
buffer += ret;
remaining_sz -= ret;
}
return 0;
}