4955bbaea0e0e6a0efc14b7bd366b5003155cea8
[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 <trajectory_manager_utils.h>
44 #include <trajectory_manager_core.h>
45 #include <vect_base.h>
46 #include <lines.h>
47 #include <polygon.h>
48 #include <obstacle_avoidance.h>
49 #include <blocking_detection_manager.h>
50 #include <robot_system.h>
51 #include <position_manager.h>
52
53 #include <diagnostic.h>
54
55 #include <rdline.h>
56 #include <parse.h>
57
58 #include "../common/i2c_commands.h"
59 #include "i2c_protocol.h"
60 #include "main.h"
61 #include "cs.h"
62 #include "strat.h"
63 #include "strat_db.h"
64 #include "strat_base.h"
65 #include "strat_corn.h"
66 #include "strat_utils.h"
67 #include "strat_avoid.h"
68 #include "sensor.h"
69 #include "actuator.h"
70
71 #define COL_DISP_MARGIN 400 /* stop 40 cm in front of dispenser */
72 #define COL_SCAN_PRE_MARGIN 250
73
74 static volatile uint8_t strat_running = 0;
75 volatile uint8_t strat_want_pack = 0;
76 volatile uint8_t strat_lpack60 = 0;
77 volatile uint8_t strat_rpack60 = 0;
78
79 volatile uint8_t strat_opponent_lpack = 0;
80 volatile uint8_t strat_opponent_rpack = 0;
81
82 struct strat_conf strat_conf = {
83         .dump_enabled = 0,
84         .opp_orange = 90,
85         .orphan_tomato = 50,
86         .flags = 0,
87 };
88
89 /*************************************************************/
90
91 /*                  INIT                                     */
92
93 /*************************************************************/
94
95 /* called before each strat, and before the start switch */
96 void strat_preinit(void)
97 {
98         time_reset();
99         interrupt_traj_reset();
100         mainboard.flags =  DO_ENCODERS | DO_CS | DO_RS |
101                 DO_POS | DO_BD | DO_POWER;
102
103         strat_db_init();
104         strat_conf.prev_wait_obstacle = -5;
105         strat_conf_dump(__FUNCTION__);
106         strat_db_dump(__FUNCTION__);
107 }
108
109 void strat_conf_dump(const char *caller)
110 {
111         if (!strat_conf.dump_enabled)
112                 return;
113
114         printf_P(PSTR("-- conf --\r\n"));
115         printf_P(PSTR("our_orange = %s\n"),
116                  (strat_conf.flags & STRAT_CONF_OUR_ORANGE) ? "y":"n");
117         printf_P(PSTR("wait_obstacle = %s\n"),
118                  (strat_conf.flags & STRAT_CONF_WAIT_OBSTACLE) ? "y":"n");
119         printf_P(PSTR("opp_orange = %d\n"), strat_conf.opp_orange);
120         printf_P(PSTR("orphan_tomato = %d\n"), strat_conf.orphan_tomato);
121 }
122
123 void strat_event_enable(void)
124 {
125         strat_running = 1;
126 }
127
128 void strat_event_disable(void)
129 {
130         strat_running = 0;
131 }
132
133 /* call it just before launching the strat */
134 void strat_init(void)
135 {
136 #ifdef HOST_VERSION
137         position_set(&mainboard.pos, 298.16,
138                      COLOR_Y(308.78), COLOR_A(70.00));
139 #endif
140
141         /* we consider that the color is correctly set */
142         strat_running = 1;
143         strat_set_speed(SPEED_DIST_FAST, SPEED_ANGLE_FAST);
144         time_reset();
145         interrupt_traj_reset();
146
147         i2c_cobboard_set_mode(I2C_COBBOARD_MODE_HARVEST);
148         i2c_ballboard_set_mode(I2C_BALLBOARD_MODE_HARVEST);
149
150         /* used in strat_base for END_TIMER */
151         mainboard.flags = DO_ENCODERS | DO_CS | DO_RS |
152                 DO_POS | DO_BD | DO_TIMER | DO_POWER;
153 }
154
155 /* call it after each strat */
156 void strat_exit(void)
157 {
158 #ifndef HOST_VERSION
159         uint8_t flags;
160 #endif
161
162         strat_running = 0;
163         mainboard.flags &= ~(DO_TIMER);
164         strat_hardstop();
165         time_reset();
166         wait_ms(100);
167 #ifndef HOST_VERSION
168         IRQ_LOCK(flags);
169         mainboard.flags &= ~(DO_CS);
170         IRQ_UNLOCK(flags);
171         pwm_ng_set(LEFT_PWM, 0);
172         pwm_ng_set(RIGHT_PWM, 0);
173 #endif
174 }
175
176 /* mark tomato as not present */
177 static void check_tomato(void)
178 {
179         int16_t x, y;
180         uint8_t i, j;
181         static uint8_t prev_check_time;
182         uint8_t cur_time;
183
184         /* check present tomatoes once per second */
185         cur_time = time_get_s();
186         if (cur_time != prev_check_time) {
187                 uint8_t k;
188
189                 for (k = 0; k < TOMATO_NB; k++) {
190                         if (strat_db.tomato_table[k]->present == 1 &&
191                             strat_db.tomato_table[k]->time_removed != -1 &&
192                             strat_db.tomato_table[k]->time_removed + 2 <= time_get_s()) {
193 #ifdef HOST_VERSION
194                                 printf("remove tomato %d\n", k);
195 #endif
196                                 strat_db.tomato_table[k]->present = 0;
197                         }
198                 }
199                 prev_check_time = cur_time;
200         }
201
202         x = position_get_x_s16(&mainboard.pos);
203         y = position_get_y_s16(&mainboard.pos);
204
205         if (xycoord_to_ijcoord(&x, &y, &i, &j) < 0)
206                 return;
207
208         if (strat_db.wp_table[i][j].type != WP_TYPE_TOMATO)
209                 return;
210
211         if (strat_db.wp_table[i][j].present == 0)
212                 return;
213
214         strat_db.wp_table[i][j].time_removed = time_get_s();
215         strat_db.wp_table[i][j].present = 0;
216 #ifdef HOST_VERSION
217         ballboard.ball_count ++;
218         printf("add ball %d,%d\n", i, j);
219 #endif
220 }
221
222 /* mark corn as not present and give correct commands to the cobboard
223  * for spickles */
224 static void check_corn(void)
225 {
226         uint8_t flags;
227         int8_t lcob_near, rcob_near;
228         uint8_t lcob, rcob;
229         uint8_t lidx, ridx;
230         static uint8_t prev_check_time;
231         uint8_t cur_time;
232         uint8_t need_lpack, need_rpack;
233
234         /* read sensors from ballboard */
235         IRQ_LOCK(flags);
236         lcob = ballboard.lcob;
237         ballboard.lcob = I2C_COB_NONE;
238         rcob = ballboard.rcob;
239         ballboard.rcob = I2C_COB_NONE;
240         IRQ_UNLOCK(flags);
241
242         /* XXX take opponent position into account */
243
244         /* check present cobs once per second */
245         cur_time = time_get_s();
246         if (cur_time != prev_check_time) {
247                 uint8_t i;
248
249                 for (i = 0; i < CORN_NB; i++) {
250                         if (strat_db.corn_table[i]->present == 1 &&
251                             strat_db.corn_table[i]->time_removed != -1 &&
252                             strat_db.corn_table[i]->time_removed + 2 <= time_get_s()) {
253 #ifdef HOST_VERSION
254                                 printf("remove cob %d\n", i);
255 #endif
256                                 strat_db.corn_table[i]->present = 0;
257                         }
258                 }
259                 prev_check_time = cur_time;
260         }
261
262         /* detect cob on left side */
263         lcob_near = corn_is_near(&lidx, I2C_LEFT_SIDE);
264         if (lcob_near && lcob != I2C_COB_NONE) {
265                 if (strat_db.corn_table[lidx]->corn.color == I2C_COB_UNKNOWN)
266                         DEBUG(E_USER_STRAT, "lcob %s %d",
267                               lcob == I2C_COB_WHITE ? "white" : "black", lidx);
268                 corn_set_color(strat_db.corn_table[lidx], lcob);
269         }
270
271         /* detect cob on right side */
272         rcob_near = corn_is_near(&ridx, I2C_RIGHT_SIDE);
273         if (rcob_near && rcob != I2C_COB_NONE) {
274                 if (strat_db.corn_table[ridx]->corn.color == I2C_COB_UNKNOWN)
275                         DEBUG(E_USER_STRAT, "rcob %s %d",
276                               rcob == I2C_COB_WHITE ? "white" : "black", ridx);
277                 corn_set_color(strat_db.corn_table[ridx], rcob);
278         }
279
280         /* control the cobboard mode for left spickle */
281         need_lpack = get_cob_count() >= 5 || strat_want_pack ||
282                 strat_lpack60 || strat_opponent_lpack;
283         if (lcob_near && strat_db.corn_table[lidx]->present) {
284                 if (need_lpack) {
285                         /* nothing  */
286                 }
287                 else {
288                         /* deploy spickle and harvest white ones */
289                         if (strat_db.corn_table[lidx]->corn.color == I2C_COB_WHITE) {
290                                 i2c_cobboard_autoharvest_nomove(I2C_LEFT_SIDE);
291                                 if (strat_db.corn_table[lidx]->time_removed == -1
292 #ifndef HOST_VERSION
293                                     && cobboard.status == I2C_COBBOARD_STATUS_LBUSY
294 #endif
295                                     ) {
296                                         strat_db.corn_table[lidx]->time_removed = time_get_s();
297 #ifdef HOST_VERSION
298                                         cobboard.cob_count ++;
299                                         printf("add cob %d\n", lidx);
300 #endif
301                                 }
302                         }
303                         else
304                                 i2c_cobboard_deploy_nomove(I2C_LEFT_SIDE);
305                 }
306         }
307         else {
308                 /* no cob near us, we can pack or deploy freely */
309                 if (need_lpack)
310                         i2c_cobboard_pack_weak(I2C_LEFT_SIDE);
311                 else
312                         i2c_cobboard_deploy(I2C_LEFT_SIDE);
313         }
314
315         /* control the cobboard mode for right spickle */
316         need_rpack = get_cob_count() >= 5 || strat_want_pack ||
317                 strat_rpack60 || strat_opponent_rpack;
318         if (rcob_near && strat_db.corn_table[ridx]->present) {
319                 if (need_rpack) {
320                         /* nothing */
321                 }
322                 else {
323                         /* deploy spickle and harvest white ones */
324                         if (strat_db.corn_table[ridx]->corn.color == I2C_COB_WHITE) {
325                                 i2c_cobboard_autoharvest_nomove(I2C_RIGHT_SIDE);
326                                 if (strat_db.corn_table[ridx]->time_removed == -1
327 #ifndef HOST_VERSION
328                                     && cobboard.status == I2C_COBBOARD_STATUS_RBUSY
329 #endif
330                                     ) {
331                                         strat_db.corn_table[ridx]->time_removed = time_get_s();
332 #ifdef HOST_VERSION
333                                         cobboard.cob_count ++;
334                                         printf("add cob %d\n", ridx);
335 #endif
336                                 }
337                         }
338                         else
339                                 i2c_cobboard_deploy_nomove(I2C_RIGHT_SIDE);
340                 }
341         }
342         else {
343                 /* no cob near us, we can pack or deploy freely */
344                 if (need_rpack)
345                         i2c_cobboard_pack_weak(I2C_RIGHT_SIDE);
346                 else
347                         i2c_cobboard_deploy(I2C_RIGHT_SIDE);
348         }
349 }
350
351 /* check opponent position */
352 void check_opponent(void)
353 {
354         int16_t opp_x, opp_y;
355         int16_t opp_d, opp_a;
356         uint8_t i, j;
357
358         strat_opponent_lpack = 0;
359         strat_opponent_rpack = 0;
360
361         if (get_opponent_xyda(&opp_x, &opp_y, &opp_d, &opp_a) < 0)
362                 return;
363
364         /* pack spickles if opponent too close */
365         if (opp_d < 600) {
366                 if (opp_a > 45 && opp_a < 135)
367                         strat_opponent_lpack = 1;
368                 if (opp_a > 225 && opp_a < 315)
369                         strat_opponent_rpack = 1;
370         }
371
372         /* check for oranges after 5 seconds */
373         if (time_get_s() > 5) {
374                 if (mainboard.our_color == I2C_COLOR_YELLOW) {
375                         if (opp_y < 500 && opp_x < 500)
376                                 strat_db.our_oranges_count = 0;
377                         if (opp_y < 500 && opp_x > AREA_X - 500)
378                                 strat_db.opp_oranges_count = 0;
379                 }
380                 else {
381                         if (opp_y > AREA_Y - 500 && opp_x < 500)
382                                 strat_db.our_oranges_count = 0;
383                         if (opp_y > AREA_Y - 500 && opp_x > AREA_X - 500)
384                                 strat_db.opp_oranges_count = 0;
385                 }
386         }
387
388         /* malus for some tomatoes and cobs, visited by opponent */
389         if (xycoord_to_ijcoord(&opp_x, &opp_y, &i, &j) < 0)
390                 return;
391
392         strat_db.wp_table[i][j].opp_visited = 1;
393 }
394
395 /* called periodically (10ms) */
396 void strat_event(void *dummy)
397 {
398         /* ignore when strat is not running */
399         if (strat_running == 0)
400                 return;
401
402         check_opponent();
403         check_tomato();
404         check_corn();
405
406         /* limit speed when opponent is near */
407         /* disabled for 2010, we are already slow :) */
408         //strat_limit_speed();
409 }
410
411 /* check that we are on an eject line */
412 static uint8_t robot_is_on_eject_line(void)
413 {
414         int16_t x, y;
415         uint8_t i, j;
416
417         x = position_get_x_s16(&mainboard.pos);
418         y = position_get_y_s16(&mainboard.pos);
419
420         if (xycoord_to_ijcoord_not_corn(&x, &y, &i, &j) < 0)
421                 return 0;
422
423         if (!wp_belongs_to_line(i, j, 5, LINE_UP) &&
424             !wp_belongs_to_line(i, j, 2, LINE_R_UP))
425                 return 0;
426
427         return 1;
428 }
429
430 /* 0 = fast, 1 = slow */
431 static uint8_t eject_select_speed(void)
432 {
433         int16_t x, y;
434         uint8_t i, j;
435
436         x = position_get_x_s16(&mainboard.pos);
437         y = position_get_y_s16(&mainboard.pos);
438
439         if (get_cob_count() >= 5) {
440                 strat_want_pack = 1;
441                 return 0; /* fast */
442         }
443
444         if (xycoord_to_ijcoord_not_corn(&x, &y, &i, &j) < 0) {
445                 DEBUG(E_USER_STRAT, "%s(): cannot find waypoint at %d,%d",
446                       __FUNCTION__, x, y);
447                 return 1; /* slow */
448         }
449
450         if (corn_count_neigh(i, j) == 2)
451                 return 1; /* slow */
452
453         return 0; /* fast */
454 }
455
456 /* called multiple times while we are waiting to reach the ejection
457  * point */
458 static uint8_t speedify_eject(void)
459 {
460         if (eject_select_speed())
461                 strat_set_speed(SPEED_CLITOID_SLOW, SPEED_ANGLE_SLOW);
462         else
463                 strat_set_speed(SPEED_CLITOID_FAST, SPEED_ANGLE_SLOW);
464         return 0;
465 }
466
467 /* must be called from a terminal line */
468 static uint8_t strat_eject(void)
469 {
470         uint8_t err;
471
472         DEBUG(E_USER_STRAT, "%s() cob_count=%d ball_count=%d",
473               __FUNCTION__, get_cob_count(), get_ball_count());
474
475         /* check that we are called from an eject line */
476         if (!robot_is_on_eject_line()) {
477                 DEBUG(E_USER_STRAT, "%s() not on eject line", __FUNCTION__);
478                 return END_ERROR;
479         }
480
481         /* go to eject point */
482         trajectory_goto_forward_xy_abs(&mainboard.traj, 2625, COLOR_Y(1847));
483         err = WAIT_COND_OR_TRAJ_END(speedify_eject(),
484                                     TRAJ_FLAGS_NO_NEAR);
485         /* err is never == 0 because speedify_eject() always return 0 */
486         if (!TRAJ_SUCCESS(err))
487                 return err;
488
489         /* pack arms (force), and disable strat_event */
490         strat_event_disable();
491         i2c_cobboard_pack_weak(I2C_LEFT_SIDE);
492         i2c_cobboard_pack_weak(I2C_RIGHT_SIDE);
493
494         /* ball ejection */
495         if (get_ball_count() > 0) {
496                 i2c_ballboard_set_mode(I2C_BALLBOARD_MODE_EJECT);
497                 trajectory_a_abs(&mainboard.traj, COLOR_A(70));
498                 err = wait_traj_end(TRAJ_FLAGS_NO_NEAR);
499                 if (!TRAJ_SUCCESS(err))
500                         goto fail;
501
502                 DEBUG(E_USER_STRAT, "%s():%d", __FUNCTION__, __LINE__);
503                 strat_hardstop();
504 #ifdef HOST_VERSION
505                 time_wait_ms(2000);
506 #else
507                 WAIT_COND_OR_TIMEOUT(ballboard.status == I2C_BALLBOARD_STATUS_F_BUSY,
508                                      2000);
509                 WAIT_COND_OR_TIMEOUT(ballboard.status == I2C_BALLBOARD_STATUS_F_READY,
510                                      2000);
511 #endif
512         }
513
514         /* half turn */
515         trajectory_a_abs(&mainboard.traj, COLOR_A(-110));
516         err = wait_traj_end(END_INTR|END_TRAJ);
517         if (!TRAJ_SUCCESS(err))
518                 goto fail;
519
520         /* cob ejection */
521         trajectory_d_rel(&mainboard.traj, -70);
522         err = wait_traj_end(END_INTR|END_TRAJ);
523         if (!TRAJ_SUCCESS(err))
524                 goto fail;
525
526         if (get_cob_count() > 0) {
527                 i2c_cobboard_set_mode(I2C_COBBOARD_MODE_EJECT);
528                 time_wait_ms(2000);
529         }
530
531         /* cob ejection */
532         trajectory_d_rel(&mainboard.traj, 70);
533         err = wait_traj_end(END_INTR|END_TRAJ);
534         if (!TRAJ_SUCCESS(err))
535                 goto fail;
536
537         strat_db_dump(__FUNCTION__);
538         err = END_TRAJ;
539
540  fail:
541         strat_event_enable();
542         strat_want_pack = 0;
543         return err;
544 }
545
546 static uint8_t strat_beginning(uint8_t do_initturn)
547 {
548         uint8_t err;
549
550         strat_set_acc(ACC_DIST, ACC_ANGLE);
551
552         if (do_initturn) {
553                 strat_set_speed(600, 60); /* OK */
554                 trajectory_d_a_rel(&mainboard.traj, 1000, COLOR_A(20));
555                 err = WAIT_COND_OR_TRAJ_END(trajectory_angle_finished(&mainboard.traj),
556                                             TRAJ_FLAGS_STD);
557         }
558
559         strat_set_acc(ACC_DIST, ACC_ANGLE);
560         strat_set_speed(SPEED_CLITOID_SLOW, SPEED_ANGLE_SLOW);
561
562  l1:
563         err = line2line(0, LINE_UP, 2, LINE_R_DOWN, TRAJ_FLAGS_NO_NEAR);
564         if (err == END_OBSTACLE &&
565             strat_conf.flags & STRAT_CONF_WAIT_OBSTACLE &&
566             time_get_s() > strat_conf.prev_wait_obstacle + 5) {
567                 strat_conf.prev_wait_obstacle = time_get_s();
568                 time_wait_ms(2000);
569                 goto l1;
570         }
571         if (!TRAJ_SUCCESS(err))
572                 return err;
573
574  l2:
575         err = line2line(2, LINE_R_DOWN, 2, LINE_R_UP, TRAJ_FLAGS_NO_NEAR);
576         if (err == END_OBSTACLE &&
577             strat_conf.flags & STRAT_CONF_WAIT_OBSTACLE &&
578             time_get_s() > strat_conf.prev_wait_obstacle + 5) {
579                 strat_conf.prev_wait_obstacle = time_get_s();
580                 time_wait_ms(2000);
581                 goto l2;
582         }
583         if (!TRAJ_SUCCESS(err)) {
584                 return err;
585         }
586
587         return END_TRAJ;
588 }
589
590 /* dump state (every 5 s max) */
591 #define DUMP_RATE_LIMIT(dump, last_print)               \
592         do {                                            \
593                 if (time_get_s() - last_print > 5) {    \
594                         dump();                         \
595                         last_print = time_get_s();      \
596                 }                                       \
597         } while (0)
598
599
600 #if 0
601 /* return true if we need to grab some more elements */
602 static uint8_t need_more_elements(void)
603 {
604         if (time_get_s() <= 75) {
605                 /* we have enough time left */
606                 if (get_ball_count() >= 4)
607                         return 0;
608                 if (get_cob_count() >= 4)
609                         return 0;
610                 if ((get_ball_count() >= 2) &&
611                     (get_cob_count() >= 2))
612                         return 0;
613                 return 1;
614         }
615         else {
616                 /* not much time remaining */
617                 if ((get_ball_count() >= 1) &&
618                     (get_cob_count() >= 1))
619                         return 0;
620                 return 1;
621         }
622 }
623 #endif
624
625 /* get tomatoes near our goals (12,5 and 12,3) */
626 uint8_t get_opp_oranges(void)
627 {
628         int16_t x, y;
629         uint8_t i, j;
630         uint8_t err;
631
632         DEBUG(E_USER_STRAT, "%s()", __FUNCTION__);
633
634         /* only if oranges are present */
635         if (strat_db.opp_oranges_count == 0)
636                 return END_TRAJ;
637
638         strat_db.opp_oranges_count = 0;
639         x = position_get_x_s16(&mainboard.pos);
640         y = position_get_y_s16(&mainboard.pos);
641
642         if (xycoord_to_ijcoord_not_corn(&x, &y, &i, &j) < 0)
643                 return END_ERROR;
644
645         /* not on eject point */
646         if (i != 11 || j != 6)
647                 return END_ERROR;
648
649         strat_want_pack = 1;
650         strat_set_speed(SPEED_DIST_FAST, SPEED_ANGLE_FAST);
651
652         /* turn in the correct direction */
653         trajectory_a_abs(&mainboard.traj, COLOR_A(-90));
654         err = wait_traj_end(TRAJ_FLAGS_NO_NEAR);
655         if (!TRAJ_SUCCESS(err))
656                 goto fail;
657
658         trajectory_goto_forward_xy_abs(&mainboard.traj, 2625, COLOR_Y(597));
659         err = wait_traj_end(TRAJ_FLAGS_STD);
660         if (!TRAJ_SUCCESS(err))
661                 goto fail;
662
663         strat_set_speed(SPEED_DIST_SLOW, SPEED_ANGLE_SLOW);
664         trajectory_goto_forward_xy_abs(&mainboard.traj, 2750, COLOR_Y(250));
665         err = wait_traj_end(TRAJ_FLAGS_NO_NEAR);
666         if (!TRAJ_SUCCESS(err))
667                 goto fail;
668
669         err = run_to_the_hills(get_opponent_color());
670
671  fail:
672         strat_want_pack = 0;
673         return err;
674 }
675
676 /* get tomatoes near our goals (12,5 and 12,3) */
677 uint8_t get_orphan_tomatoes(void)
678 {
679 #define CLITOID_TOMATO_RADIUS 100.
680 #define TOMATO_BACK_X 2760
681 #define TOMATO_BACK_LEN 200
682
683         int16_t x, y, a;
684         uint8_t i, j;
685         uint8_t err;
686         int8_t ret;
687
688         DEBUG(E_USER_STRAT, "%s()", __FUNCTION__);
689
690         /* only go if both tomatoes are present */
691         if (!strat_db.wp_table[12][5].present ||
692             !strat_db.wp_table[12][3].present) {
693                 return END_TRAJ;
694         }
695
696         x = position_get_x_s16(&mainboard.pos);
697         y = position_get_y_s16(&mainboard.pos);
698
699         if (xycoord_to_ijcoord_not_corn(&x, &y, &i, &j) < 0)
700                 return END_ERROR;
701
702         /* not on eject point */
703         if (i != 11 || j != 6)
704                 return END_ERROR;
705
706         strat_want_pack = 1;
707         strat_set_speed(SPEED_CLITOID_FAST, SPEED_ANGLE_SLOW);
708
709         /* turn in the correct direction */
710         trajectory_a_abs(&mainboard.traj, COLOR_A(-90));
711         err = wait_traj_end(TRAJ_FLAGS_NO_NEAR);
712
713         /* clitoid to turn and take the first ball */
714         ret = trajectory_clitoid(&mainboard.traj, 2625, COLOR_Y(1847),
715                                  COLOR_A(-90), 150., COLOR_A(90), 0,
716                                  CLITOID_TOMATO_RADIUS, 3*125);
717         if (ret < 0) {
718                 err = END_ERROR;
719                 goto fail;
720         }
721
722         err = wait_traj_end(TRAJ_FLAGS_NO_NEAR);
723         if (!TRAJ_SUCCESS(err))
724                 goto fail;
725
726         strat_set_speed(SPEED_CLITOID_SLOW, SPEED_ANGLE_SLOW);
727         err = strat_calib(300, END_TRAJ|END_BLOCKING);
728         a = position_get_a_deg_s16(&mainboard.pos);
729         if (ABS(a) < 10)
730                 strat_reset_pos(AREA_X - ROBOT_HALF_LENGTH_FRONT,
731                                 DO_NOT_SET_POS,
732                                 COLOR_A(0));
733
734         strat_set_speed(SPEED_CLITOID_FAST, SPEED_ANGLE_FAST);
735         trajectory_d_rel(&mainboard.traj, -250);
736         err = WAIT_COND_OR_TRAJ_END(!x_is_more_than(TOMATO_BACK_X),
737                                     TRAJ_FLAGS_NO_NEAR);
738
739         if (err != 0 && !TRAJ_SUCCESS(err))
740                 goto fail;
741
742         trajectory_d_a_rel(&mainboard.traj, -TOMATO_BACK_LEN, COLOR_A(-90));
743         err = wait_traj_end(TRAJ_FLAGS_NO_NEAR);
744
745         /* next ball */
746
747         /* clitoid to turn and take the first ball */
748         strat_set_speed(SPEED_CLITOID_FAST, SPEED_ANGLE_SLOW);
749         ret = trajectory_clitoid(&mainboard.traj, 2625, COLOR_Y(1847),
750                                  COLOR_A(-90), 150., COLOR_A(90), 0,
751                                  CLITOID_TOMATO_RADIUS, 7*125);
752         if (ret < 0) {
753                 err = END_ERROR;
754                 goto fail;
755         }
756         err = wait_traj_end(TRAJ_FLAGS_NO_NEAR);
757         if (!TRAJ_SUCCESS(err))
758                 goto fail;
759
760         strat_hardstop();
761         strat_set_speed(SPEED_CLITOID_FAST, SPEED_ANGLE_FAST);
762         trajectory_d_rel(&mainboard.traj, -250);
763         err = WAIT_COND_OR_TRAJ_END(!x_is_more_than(TOMATO_BACK_X),
764                                     TRAJ_FLAGS_NO_NEAR);
765         trajectory_d_a_rel(&mainboard.traj, -TOMATO_BACK_LEN, COLOR_A(-90));
766         err = wait_traj_end(TRAJ_FLAGS_NO_NEAR);
767
768 fail:
769         strat_want_pack = 0;
770         return err;
771 }
772
773
774 #define HILL_LEN 1000
775
776 #define HILL_ANGLE 0
777 #define HILL_POSY_YELLOW 310
778 #define HILL_POSY_BLUE 190
779
780 #define HILL_POSX_BALLS_DOWN1 830
781 #define HILL_POSX_BALLS_DOWN2 920
782 #define HILL_POSX_BALLS_DOWN3 730
783 #define HILL_START_POSX 580
784
785 uint8_t prepare_hill(uint8_t orange_color, int16_t posx)
786 {
787         int16_t startx, starty;
788         uint8_t our_color = get_color();
789         uint8_t err;
790
791         if (orange_color == I2C_COLOR_YELLOW)
792                 starty = HILL_POSY_YELLOW;
793         else
794                 starty = HILL_POSY_BLUE;
795         if (orange_color == our_color)
796                 startx = posx;
797         else
798                 startx = AREA_X - posx;
799         trajectory_goto_forward_xy_abs(&mainboard.traj, startx, COLOR_Y(starty));
800         err = wait_traj_end(TRAJ_FLAGS_SMALL_DIST);
801         if (!TRAJ_SUCCESS(err))
802                 return err;
803
804         /* turn to the hills */
805         if (orange_color == our_color)
806                 trajectory_a_abs(&mainboard.traj, COLOR_A(HILL_ANGLE));
807         else
808                 trajectory_a_abs(&mainboard.traj, COLOR_A(-180+HILL_ANGLE));
809         err = wait_traj_end(TRAJ_FLAGS_SMALL_DIST);
810         if (!TRAJ_SUCCESS(err))
811                 return err;
812
813         return END_TRAJ;
814 }
815
816 /* get oranges, must be called near game area */
817 uint8_t run_to_the_hills(uint8_t orange_color)
818 {
819         double aa, ad;
820         uint16_t sa, sd;
821         uint8_t err;
822         uint8_t our_color = get_color();
823         int32_t p = pid_get_gain_P(&mainboard.angle.pid);
824         int32_t i = pid_get_gain_I(&mainboard.angle.pid);
825         int32_t d = pid_get_gain_D(&mainboard.angle.pid);
826         int32_t max_in = pid_get_max_in(&mainboard.angle.pid);
827         int32_t max_i = pid_get_max_I(&mainboard.angle.pid);
828         int32_t max_out = pid_get_max_out(&mainboard.angle.pid);
829
830         strat_want_pack = 1;
831         strat_get_acc(&ad, &aa);
832         strat_get_speed(&sd, &sa);
833
834         DEBUG(E_USER_STRAT, "%s()", __FUNCTION__);
835
836         strat_set_speed(SPEED_DIST_FAST, SPEED_ANGLE_FAST);
837         err = prepare_hill(orange_color, HILL_START_POSX);
838         if (!TRAJ_SUCCESS(err))
839                 return err;
840
841         strat_set_acc(5, ACC_ANGLE);
842         strat_set_speed(300, SPEED_ANGLE_SLOW);
843         bd_set_current_thresholds(&mainboard.distance.bd, 500, 8000, 2000000, 80);
844         bd_set_current_thresholds(&mainboard.angle.bd, 500, 8000, 2000000, 80);
845         bd_set_speed_threshold(&mainboard.distance.bd, 10);
846         support_balls_pack();
847         i2c_ballboard_set_mode(I2C_BALLBOARD_MODE_PREP_FORK);
848
849         /* decrease angle gains */
850         pid_set_gains(&mainboard.angle.pid, 200, 0, 2000);
851
852         /* here it is difficult to handle return values, because we
853          * are on the hill */
854         i2c_cobboard_set_mode(I2C_COBBOARD_MODE_KICKSTAND_DOWN);
855         trajectory_d_rel(&mainboard.traj, HILL_LEN);
856         err = WAIT_COND_OR_TRAJ_END(position_get_x_s16(&mainboard.pos) >
857                                     HILL_POSX_BALLS_DOWN1,
858                                     TRAJ_FLAGS_SMALL_DIST);
859         DEBUG(E_USER_STRAT, "deploy support balls");
860         support_balls_deploy();
861         i2c_cobboard_set_mode(I2C_COBBOARD_MODE_KICKSTAND_UP);
862         trajectory_only_a_rel(&mainboard.traj, 2);
863         err = WAIT_COND_OR_TE_TO(0, 0, 2200);
864
865         i2c_cobboard_set_mode(I2C_COBBOARD_MODE_KICKSTAND_DOWN);
866         strat_set_acc(3, 3);
867         strat_hardstop();
868         i2c_ballboard_set_mode(I2C_BALLBOARD_MODE_TAKE_FORK);
869
870         time_wait_ms(1800);
871
872         /* reach top, go down */
873         trajectory_d_rel(&mainboard.traj, -HILL_LEN);
874
875         err = WAIT_COND_OR_TRAJ_END(position_get_x_s16(&mainboard.pos) <
876                                     HILL_POSX_BALLS_DOWN2,
877                                     TRAJ_FLAGS_SMALL_DIST);
878         DEBUG(E_USER_STRAT, "pack support balls");
879         support_balls_pack();
880         err = WAIT_COND_OR_TRAJ_END(position_get_x_s16(&mainboard.pos) <
881                                     HILL_POSX_BALLS_DOWN3,
882                                     TRAJ_FLAGS_SMALL_DIST);
883         DEBUG(E_USER_STRAT, "deploy support balls");
884         strat_set_acc(ad, aa);
885         strat_set_speed(SPEED_DIST_FAST, SPEED_ANGLE_FAST);
886         support_balls_deploy();
887         err = wait_traj_end(TRAJ_FLAGS_SMALL_DIST);
888         i2c_cobboard_set_mode(I2C_COBBOARD_MODE_HARVEST);
889
890         /* wait to be near the wall */
891         err = WAIT_COND_OR_TRAJ_END(position_get_x_s16(&mainboard.pos) < 200,
892                                     TRAJ_FLAGS_SMALL_DIST);
893
894         i2c_cobboard_set_mode(I2C_COBBOARD_MODE_HARVEST);
895
896         /* restore BD coefs */
897         bd_set_current_thresholds(&mainboard.distance.bd, 500, 8000, 1000000, 20);
898         bd_set_current_thresholds(&mainboard.distance.bd, 500, 8000, 1000000, 20);
899         bd_set_speed_threshold(&mainboard.distance.bd, 60);
900
901         /* calibrate position on the wall */
902         strat_set_speed(SPEED_DIST_VERY_SLOW, SPEED_ANGLE_FAST);
903         err = strat_calib(-400, TRAJ_FLAGS_SMALL_DIST);
904         if (orange_color == our_color)
905                 strat_reset_pos(ROBOT_HALF_LENGTH_REAR,
906                              DO_NOT_SET_POS, COLOR_A(0));
907         else
908                 strat_reset_pos(AREA_X - ROBOT_HALF_LENGTH_REAR,
909                              DO_NOT_SET_POS, COLOR_A(180));
910         strat_set_speed(SPEED_DIST_FAST, SPEED_ANGLE_FAST);
911
912         trajectory_d_rel(&mainboard.traj, 250);
913         err = wait_traj_end(TRAJ_FLAGS_NO_NEAR);
914         if (orange_color == I2C_COLOR_YELLOW)
915                 trajectory_a_rel(&mainboard.traj, 90);
916         else
917                 trajectory_a_rel(&mainboard.traj, -90);
918         err = wait_traj_end(TRAJ_FLAGS_NO_NEAR);
919         time_wait_ms(200);
920
921         strat_set_speed(SPEED_DIST_VERY_SLOW, SPEED_ANGLE_FAST);
922         err = strat_calib(-400, TRAJ_FLAGS_SMALL_DIST);
923         strat_reset_pos(DO_NOT_SET_POS,
924                         COLOR_Y(ROBOT_HALF_LENGTH_REAR),
925                         COLOR_A(90));
926         strat_set_speed(SPEED_DIST_FAST, SPEED_ANGLE_FAST);
927
928         trajectory_d_rel(&mainboard.traj, 250);
929         err = wait_traj_end(TRAJ_FLAGS_NO_NEAR);
930
931         /* revert acceleration and speed */
932         pid_set_gains(&mainboard.angle.pid, p, i, d);
933         pid_set_maximums(&mainboard.distance.pid, max_in, max_i, max_out);
934         strat_want_pack = 0;
935         strat_set_speed(sd, sa);
936         support_balls_deploy();
937         return err;
938 }
939
940 uint8_t strat_main(void)
941 {
942         uint8_t err, do_initturn = 1;
943
944         /* get oranges */
945         if (strat_conf.flags & STRAT_CONF_OUR_ORANGE) {
946                 err = run_to_the_hills(get_color());
947                 strat_db.our_oranges_count = 0;
948                 do_initturn = 0;
949         }
950
951         /* harvest the first cobs + balls */
952         err = strat_beginning(do_initturn);
953
954         if (!TRAJ_SUCCESS(err))
955                 strat_unblock();
956         else
957                 err = strat_eject();
958
959         /* choose circuit, and harvest on it */
960         while (1) {
961
962                 DEBUG(E_USER_STRAT, "start main loop");
963
964                 /* if it's time to get tomatoes, do it */
965                 if (time_get_s() > strat_conf.orphan_tomato) {
966                         err = get_orphan_tomatoes();
967                         if (err == END_ERROR) {
968                                 DEBUG(E_USER_STRAT,
969                                       "get_orphan_tomatoes returned END_ERROR");
970                         }
971                         else if (err == END_TIMER) {
972                                 DEBUG(E_USER_STRAT, "End of time");
973                                 strat_exit();
974                                 break;
975                         }
976                         else if (!TRAJ_SUCCESS(err)) {
977                                 /* don't retry these tomatoes if it failed */
978                                 strat_conf.orphan_tomato = 90;
979                                 strat_unblock();
980                         }
981                 }
982
983                 /* if it's time to get opponent oranges, do it */
984                 if (time_get_s() > strat_conf.opp_orange) {
985                         err = get_opp_oranges();
986                         if (err == END_ERROR) {
987                                 DEBUG(E_USER_STRAT,
988                                       "get_opp_oranges returned END_ERROR");
989                         }
990                         else if (err == END_TIMER) {
991                                 DEBUG(E_USER_STRAT, "End of time");
992                                 strat_exit();
993                                 break;
994                         }
995                         else if (!TRAJ_SUCCESS(err)) {
996                                 /* don't retry oranges if it failed */
997                                 strat_conf.opp_orange = 90;
998                                 strat_unblock();
999                         }
1000                 }
1001
1002                 /**********************/
1003                 /* harvest on circuit */
1004                 /**********************/
1005
1006                 err = strat_harvest_circuit();
1007                 if (err == END_TIMER) {
1008                         DEBUG(E_USER_STRAT, "End of time");
1009                         strat_exit();
1010                         break;
1011                 }
1012                 if (!TRAJ_SUCCESS(err)) {
1013                         strat_unblock();
1014                         continue;
1015                 }
1016
1017                 /***********************/
1018                 /* eject game elements */
1019                 /***********************/
1020
1021                 err = strat_eject();
1022                 /* end of time exit ! */
1023                 if (err == END_TIMER) {
1024                         DEBUG(E_USER_STRAT, "End of time");
1025                         strat_exit();
1026                         break;
1027                 }
1028                 if (!TRAJ_SUCCESS(err)) {
1029                         strat_unblock();
1030                         continue;
1031                 }
1032         }
1033
1034         return err;
1035 }