9b581965d13ad0c78bc1b8ee5f65ceb29632e26b
[aversive.git] / projects / microb2010 / mainboard / strat.c
1 /*
2  *  Copyright Droids, Microb Technology (2009)
3  *
4  *  This program is free software; you can redistribute it and/or modify
5  *  it under the terms of the GNU General Public License as published by
6  *  the Free Software Foundation; either version 2 of the License, or
7  *  (at your option) any later version.
8  *
9  *  This program is distributed in the hope that it will be useful,
10  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  *  GNU General Public License for more details.
13  *
14  *  You should have received a copy of the GNU General Public License
15  *  along with this program; if not, write to the Free Software
16  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17  *
18  *  Revision : $Id: strat.c,v 1.6 2009-11-08 17:24:33 zer0 Exp $
19  *
20  *  Olivier MATZ <zer0@droids-corp.org>
21  */
22
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <math.h>
27
28 #include <aversive/pgmspace.h>
29 #include <aversive/queue.h>
30 #include <aversive/wait.h>
31 #include <aversive/error.h>
32
33 #include <ax12.h>
34 #include <uart.h>
35 #include <pwm_ng.h>
36 #include <clock_time.h>
37 #include <spi.h>
38
39 #include <pid.h>
40 #include <quadramp.h>
41 #include <control_system_manager.h>
42 #include <trajectory_manager.h>
43 #include <vect_base.h>
44 #include <lines.h>
45 #include <polygon.h>
46 #include <obstacle_avoidance.h>
47 #include <blocking_detection_manager.h>
48 #include <robot_system.h>
49 #include <position_manager.h>
50
51 #include <diagnostic.h>
52
53 #include <rdline.h>
54 #include <parse.h>
55
56 #include "../common/i2c_commands.h"
57 #include "i2c_protocol.h"
58 #include "main.h"
59 #include "strat.h"
60 #include "strat_db.h"
61 #include "strat_base.h"
62 #include "strat_corn.h"
63 #include "strat_utils.h"
64 #include "strat_avoid.h"
65 #include "sensor.h"
66 #include "actuator.h"
67
68 #define COL_DISP_MARGIN 400 /* stop 40 cm in front of dispenser */
69 #define COL_SCAN_PRE_MARGIN 250
70
71 static volatile uint8_t strat_running = 0;
72 volatile uint8_t strat_want_pack = 0;
73 volatile uint8_t strat_lpack60 = 0;
74 volatile uint8_t strat_rpack60 = 0;
75 struct strat_conf strat_conf;
76
77 /*************************************************************/
78
79 /*                  INIT                                     */
80
81 /*************************************************************/
82
83 /* called before each strat, and before the start switch */
84 void strat_preinit(void)
85 {
86         time_reset();
87         interrupt_traj_reset();
88         mainboard.flags =  DO_ENCODERS | DO_CS | DO_RS |
89                 DO_POS | DO_BD | DO_POWER;
90
91         strat_db_init();
92         strat_conf_dump(__FUNCTION__);
93         strat_db_dump(__FUNCTION__);
94 }
95
96 void strat_conf_dump(const char *caller)
97 {
98         if (!strat_conf.dump_enabled)
99                 return;
100
101         printf_P(PSTR("-- conf --\r\n"));
102
103 }
104
105 void strat_event_enable(void)
106 {
107         strat_running = 1;
108 }
109
110 void strat_event_disable(void)
111 {
112         strat_running = 0;
113 }
114
115 /* call it just before launching the strat */
116 void strat_init(void)
117 {
118 #ifdef HOST_VERSION
119         position_set(&mainboard.pos, 298.16,
120                      COLOR_Y(308.78), COLOR_A(70.00));
121 #endif
122
123         /* we consider that the color is correctly set */
124         strat_running = 1;
125         strat_set_speed(SPEED_DIST_FAST, SPEED_ANGLE_FAST);
126         time_reset();
127         interrupt_traj_reset();
128
129         i2c_cobboard_set_mode(I2C_COBBOARD_MODE_HARVEST);
130         i2c_ballboard_set_mode(I2C_BALLBOARD_MODE_HARVEST);
131
132         /* used in strat_base for END_TIMER */
133         mainboard.flags = DO_ENCODERS | DO_CS | DO_RS |
134                 DO_POS | DO_BD | DO_TIMER | DO_POWER;
135 }
136
137 /* call it after each strat */
138 void strat_exit(void)
139 {
140 #ifndef HOST_VERSION
141         uint8_t flags;
142 #endif
143
144         strat_running = 0;
145         mainboard.flags &= ~(DO_TIMER);
146         strat_hardstop();
147         time_reset();
148         wait_ms(100);
149 #ifndef HOST_VERSION
150         IRQ_LOCK(flags);
151         mainboard.flags &= ~(DO_CS);
152         IRQ_UNLOCK(flags);
153         pwm_ng_set(LEFT_PWM, 0);
154         pwm_ng_set(RIGHT_PWM, 0);
155 #endif
156 }
157
158 /* mark tomato as not present */
159 static void check_tomato(void)
160 {
161         int16_t x, y;
162         uint8_t i, j;
163
164         x = position_get_x_s16(&mainboard.pos);
165         y = position_get_y_s16(&mainboard.pos);
166
167         if (xycoord_to_ijcoord(&x, &y, &i, &j) < 0)
168                 return;
169
170         if (strat_db.wp_table[i][j].type != WP_TYPE_TOMATO)
171                 return;
172
173         strat_db.wp_table[i][j].present = 0;
174 }
175
176 /* mark corn as not present and give correct commands to the cobboard
177  * for spickles */
178 static void check_corn(void)
179 {
180         uint8_t flags;
181         int8_t lcob_near, rcob_near;
182         uint8_t lcob, rcob;
183         uint8_t lidx, ridx;
184
185         /* read sensors from ballboard */
186         IRQ_LOCK(flags);
187         lcob = ballboard.lcob;
188         ballboard.lcob = I2C_COB_NONE;
189         rcob = ballboard.rcob;
190         ballboard.rcob = I2C_COB_NONE;
191         IRQ_UNLOCK(flags);
192
193 /*      if (lcob != I2C_COB_NONE) */
194 /*              DEBUG(E_USER_STRAT, "XXX lcob %s", */
195 /*                    lcob == I2C_COB_WHITE ? "white" : "black"); */
196 /*      if (rcob != I2C_COB_NONE) */
197 /*              DEBUG(E_USER_STRAT, "XXX rcob %s", */
198 /*                    rcob == I2C_COB_WHITE ? "white" : "black"); */
199         /* XXX take opponent position into account */
200
201 #ifdef HOST_VERSION
202         if (time_get_s() == 15)
203                 cobboard.cob_count = 5;
204         if (time_get_s() == 16)
205                 cobboard.cob_count = 0;
206 #endif
207
208         /* detect cob on left side */
209         lcob_near = corn_is_near(&lidx, I2C_LEFT_SIDE);
210         if (lcob_near && lcob != I2C_COB_NONE) {
211                 if (strat_db.corn_table[lidx]->corn.color == I2C_COB_UNKNOWN)
212                         DEBUG(E_USER_STRAT, "lcob %s %d",
213                               lcob == I2C_COB_WHITE ? "white" : "black", lidx);
214                 corn_set_color(strat_db.corn_table[lidx], lcob);
215         }
216
217         /* detect cob on right side */
218         rcob_near = corn_is_near(&ridx, I2C_RIGHT_SIDE);
219         if (rcob_near && rcob != I2C_COB_NONE) {
220                 if (strat_db.corn_table[ridx]->corn.color == I2C_COB_UNKNOWN)
221                         DEBUG(E_USER_STRAT, "rcob %s %d",
222                               rcob == I2C_COB_WHITE ? "white" : "black", ridx);
223                 corn_set_color(strat_db.corn_table[ridx], rcob);
224         }
225
226         /* control the cobboard mode for left spickle */
227         if (lcob_near && strat_db.corn_table[lidx]->present) {
228                 if (get_cob_count() >= 5 || strat_want_pack || strat_lpack60) {
229                         /* nothing  */
230                 }
231                 else {
232                         /* deploy spickle and harvest white ones */
233                         if (strat_db.corn_table[lidx]->corn.color == I2C_COB_WHITE) {
234                                 i2c_cobboard_autoharvest_nomove(I2C_LEFT_SIDE);
235                                 if (cobboard.status == I2C_COBBOARD_STATUS_LBUSY)
236                                         strat_db.corn_table[lidx]->present = 0;
237                         }
238                         else
239                                 i2c_cobboard_deploy_nomove(I2C_LEFT_SIDE);
240                 }
241         }
242         else {
243                 /* no cob near us, we can pack or deploy freely */
244                 if (get_cob_count() >= 5 || strat_want_pack || strat_lpack60)
245                         i2c_cobboard_pack_weak(I2C_LEFT_SIDE);
246                 else
247                         i2c_cobboard_deploy(I2C_LEFT_SIDE);
248         }
249
250         /* control the cobboard mode for right spickle */
251         if (rcob_near && strat_db.corn_table[ridx]->present) {
252                 if (get_cob_count() >= 5 || strat_want_pack || strat_rpack60) {
253                         /* nothing */
254                 }
255                 else {
256                         /* deploy spickle and harvest white ones */
257                         if (strat_db.corn_table[ridx]->corn.color == I2C_COB_WHITE) {
258                                 i2c_cobboard_autoharvest_nomove(I2C_RIGHT_SIDE);
259                                 if (cobboard.status == I2C_COBBOARD_STATUS_RBUSY)
260                                         strat_db.corn_table[ridx]->present = 0;
261                         }
262                         else
263                                 i2c_cobboard_deploy_nomove(I2C_RIGHT_SIDE);
264                 }
265         }
266         else {
267                 /* no cob near us, we can pack or deploy freely */
268                 if (get_cob_count() >= 5 || strat_want_pack || strat_rpack60)
269                         i2c_cobboard_pack_weak(I2C_RIGHT_SIDE);
270                 else
271                         i2c_cobboard_deploy(I2C_RIGHT_SIDE);
272         }
273 }
274
275 /* called periodically (10ms) */
276 void strat_event(void *dummy)
277 {
278         /* ignore when strat is not running */
279         if (strat_running == 0)
280                 return;
281
282         check_tomato();
283         check_corn();
284
285         /* limit speed when opponent is near */
286         /* disabled for 2010, we are already slow :) */
287         //strat_limit_speed();
288 }
289
290 /* check that we are on an eject line */
291 static uint8_t robot_is_on_eject_line(void)
292 {
293         int16_t x, y;
294         uint8_t i, j;
295
296         x = position_get_x_s16(&mainboard.pos);
297         y = position_get_y_s16(&mainboard.pos);
298
299         if (xycoord_to_ijcoord(&x, &y, &i, &j) < 0)
300                 return 0;
301
302         if (!wp_belongs_to_line(i, j, 5, LINE_UP) &&
303             !wp_belongs_to_line(i, j, 2, LINE_R_UP))
304                 return 0;
305
306         return 1;
307 }
308
309 /* 0 = fast, 1 = slow */
310 static uint8_t eject_select_speed(void)
311 {
312         int16_t x, y;
313         uint8_t i, j;
314
315         x = position_get_x_s16(&mainboard.pos);
316         y = position_get_y_s16(&mainboard.pos);
317
318         if (get_cob_count() >= 5) {
319                 strat_want_pack = 1;
320                 return 0; /* fast */
321         }
322
323         if (xycoord_to_ijcoord(&x, &y, &i, &j) < 0) {
324                 DEBUG(E_USER_STRAT, "%s(): cannot find waypoint at %d,%d",
325                       __FUNCTION__, x, y);
326                 return 1; /* slow */
327         }
328
329         if (corn_count_neigh(i, j) == 2)
330                 return 1; /* slow */
331
332         return 0; /* fast */
333 }
334
335 /* called multiple times while we are waiting to reach the ejection
336  * point */
337 static uint8_t speedify_eject(void)
338 {
339         if (eject_select_speed())
340                 strat_set_speed(SPEED_CLITOID_SLOW, SPEED_ANGLE_SLOW);
341         else
342                 strat_set_speed(SPEED_CLITOID_FAST, SPEED_ANGLE_SLOW);
343         return 0;
344 }
345
346 /* must be called from a terminal line */
347 static uint8_t strat_eject(void)
348 {
349         uint8_t err;
350
351         DEBUG(E_USER_STRAT, "%s()", __FUNCTION__);
352
353         /* check that we are called from an eject line */
354         if (!robot_is_on_eject_line()) {
355                 DEBUG(E_USER_STRAT, "%s() not on eject line", __FUNCTION__);
356                 return END_ERROR;
357         }
358
359         /* go to eject point */
360         trajectory_goto_xy_abs(&mainboard.traj, 2625, COLOR_Y(1847));
361         err = WAIT_COND_OR_TRAJ_END(speedify_eject(),
362                                     TRAJ_FLAGS_NO_NEAR);
363         /* err is never == 0 because speedify_eject() always return 0 */
364         if (!TRAJ_SUCCESS(err))
365                 return err;
366
367         /* pack arms (force), and disable strat_event */
368         strat_event_disable();
369         i2c_cobboard_pack_weak(I2C_LEFT_SIDE);
370         i2c_cobboard_pack_weak(I2C_RIGHT_SIDE);
371
372         /* ball ejection */
373         if (get_ball_count() > 0) {
374                 i2c_ballboard_set_mode(I2C_BALLBOARD_MODE_EJECT);
375                 trajectory_a_abs(&mainboard.traj, COLOR_A(70));
376                 err = wait_traj_end(TRAJ_FLAGS_NO_NEAR);
377                 if (!TRAJ_SUCCESS(err))
378                         goto fail;
379
380                 DEBUG(E_USER_STRAT, "%s():%d", __FUNCTION__, __LINE__);
381                 strat_hardstop();
382 #ifdef HOST_VERSION
383                 time_wait_ms(2000);
384 #else
385                 WAIT_COND_OR_TIMEOUT(ballboard.status == I2C_BALLBOARD_STATUS_F_BUSY,
386                                      2000);
387                 WAIT_COND_OR_TIMEOUT(ballboard.status == I2C_BALLBOARD_STATUS_F_READY,
388                                      2000);
389 #endif
390         }
391
392         /* half turn */
393         trajectory_a_abs(&mainboard.traj, COLOR_A(-110));
394         err = wait_traj_end(END_INTR|END_TRAJ);
395         if (!TRAJ_SUCCESS(err))
396                 goto fail;
397
398         /* cob ejection */
399         trajectory_d_rel(&mainboard.traj, -70);
400         err = wait_traj_end(END_INTR|END_TRAJ);
401         if (!TRAJ_SUCCESS(err))
402                 goto fail;
403
404         if (get_cob_count() > 0) {
405                 i2c_cobboard_set_mode(I2C_COBBOARD_MODE_EJECT);
406                 strat_db_dump(__FUNCTION__);
407                 time_wait_ms(2000);
408         }
409
410         err = END_TRAJ;
411
412  fail:
413         strat_event_enable();
414         strat_want_pack = 0;
415         return err;
416 }
417
418 static uint8_t strat_beginning(void)
419 {
420         uint8_t err;
421
422         strat_set_acc(ACC_DIST, ACC_ANGLE);
423         strat_set_speed(600, 60); /* OK */
424
425         trajectory_d_a_rel(&mainboard.traj, 1000, COLOR_A(20));
426         err = WAIT_COND_OR_TRAJ_END(trajectory_angle_finished(&mainboard.traj),
427                                     TRAJ_FLAGS_STD);
428
429         strat_set_acc(ACC_DIST, ACC_ANGLE);
430         strat_set_speed(SPEED_CLITOID_SLOW, SPEED_ANGLE_SLOW);
431
432         err = line2line(0, LINE_UP, 2, LINE_R_DOWN, TRAJ_FLAGS_NO_NEAR);
433         if (!TRAJ_SUCCESS(err))
434                 return err;
435
436         err = line2line(2, LINE_R_DOWN, 2, LINE_R_UP, TRAJ_FLAGS_NO_NEAR);
437         if (!TRAJ_SUCCESS(err)) {
438                 return err;
439         }
440
441         return END_TRAJ;
442 }
443
444 /* dump state (every 5 s max) */
445 #define DUMP_RATE_LIMIT(dump, last_print)               \
446         do {                                            \
447                 if (time_get_s() - last_print > 5) {    \
448                         dump();                         \
449                         last_print = time_get_s();      \
450                 }                                       \
451         } while (0)
452
453
454 #if 0
455 /* return true if we need to grab some more elements */
456 static uint8_t need_more_elements(void)
457 {
458         if (time_get_s() <= 75) {
459                 /* we have enough time left */
460                 if (get_ball_count() >= 4)
461                         return 0;
462                 if (get_cob_count() >= 4)
463                         return 0;
464                 if ((get_ball_count() >= 2) &&
465                     (get_cob_count() >= 2))
466                         return 0;
467                 return 1;
468         }
469         else {
470                 /* not much time remaining */
471                 if ((get_ball_count() >= 1) &&
472                     (get_cob_count() >= 1))
473                         return 0;
474                 return 1;
475         }
476 }
477 #endif
478
479 uint8_t strat_main(void)
480 {
481         uint8_t err;
482
483         /* harvest the first cobs + balls */
484         err = strat_beginning();
485         if (!TRAJ_SUCCESS(err))
486                 strat_unblock();
487         else
488                 err = strat_eject();
489
490         while (1) {
491
492                 /**********************/
493                 /* harvest on circuit */
494                 /**********************/
495
496                 DEBUG(E_USER_STRAT, "start main loop");
497
498                 err = strat_harvest_circuit();
499                 if (err == END_TIMER) {
500                         DEBUG(E_USER_STRAT, "End of time");
501                         strat_exit();
502                         break;
503                 }
504                 if (!TRAJ_SUCCESS(err)) {
505                         strat_unblock();
506                         continue;
507                 }
508
509                 /***********************/
510                 /* eject game elements */
511                 /***********************/
512
513                 err = strat_eject();
514                 /* end of time exit ! */
515                 if (err == END_TIMER) {
516                         DEBUG(E_USER_STRAT, "End of time");
517                         strat_exit();
518                         break;
519                 }
520                 if (!TRAJ_SUCCESS(err)) {
521                         strat_unblock();
522                         continue;
523                 }
524         }
525
526         return err;
527 }