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