2 * Copyright Droids, Microb Technology (2009)
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.
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.
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
18 * Revision : $Id: strat.c,v 1.6 2009-11-08 17:24:33 zer0 Exp $
20 * Olivier MATZ <zer0@droids-corp.org>
28 #include <aversive/pgmspace.h>
29 #include <aversive/queue.h>
30 #include <aversive/wait.h>
31 #include <aversive/error.h>
36 #include <clock_time.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>
48 #include <obstacle_avoidance.h>
49 #include <blocking_detection_manager.h>
50 #include <robot_system.h>
51 #include <position_manager.h>
53 #include <diagnostic.h>
58 #include "../common/i2c_commands.h"
59 #include "i2c_protocol.h"
64 #include "strat_base.h"
65 #include "strat_corn.h"
66 #include "strat_utils.h"
67 #include "strat_avoid.h"
71 #define COL_DISP_MARGIN 400 /* stop 40 cm in front of dispenser */
72 #define COL_SCAN_PRE_MARGIN 250
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;
79 volatile uint8_t strat_opponent_lpack = 0;
80 volatile uint8_t strat_opponent_rpack = 0;
82 struct strat_conf strat_conf = {
89 /*************************************************************/
93 /*************************************************************/
95 /* called before each strat, and before the start switch */
96 void strat_preinit(void)
99 interrupt_traj_reset();
100 mainboard.flags = DO_ENCODERS | DO_CS | DO_RS |
101 DO_POS | DO_BD | DO_POWER;
104 strat_conf.prev_wait_obstacle = -5;
105 strat_conf_dump(__FUNCTION__);
106 strat_db_dump(__FUNCTION__);
109 void strat_conf_dump(const char *caller)
111 if (!strat_conf.dump_enabled)
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);
123 void strat_event_enable(void)
128 void strat_event_disable(void)
133 /* call it just before launching the strat */
134 void strat_init(void)
137 position_set(&mainboard.pos, 298.16,
138 COLOR_Y(308.78), COLOR_A(70.00));
141 /* we consider that the color is correctly set */
143 strat_set_speed(SPEED_DIST_FAST, SPEED_ANGLE_FAST);
145 interrupt_traj_reset();
147 i2c_cobboard_set_mode(I2C_COBBOARD_MODE_HARVEST);
148 i2c_ballboard_set_mode(I2C_BALLBOARD_MODE_HARVEST);
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;
155 /* call it after each strat */
156 void strat_exit(void)
163 mainboard.flags &= ~(DO_TIMER);
169 mainboard.flags &= ~(DO_CS);
171 pwm_ng_set(LEFT_PWM, 0);
172 pwm_ng_set(RIGHT_PWM, 0);
176 /* mark tomato as not present */
177 static void check_tomato(void)
181 static uint8_t prev_check_time;
184 /* check present tomatoes once per second */
185 cur_time = time_get_s();
186 if (cur_time != prev_check_time) {
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()) {
194 printf("remove tomato %d\n", k);
196 strat_db.tomato_table[k]->present = 0;
199 prev_check_time = cur_time;
202 x = position_get_x_s16(&mainboard.pos);
203 y = position_get_y_s16(&mainboard.pos);
205 if (xycoord_to_ijcoord(&x, &y, &i, &j) < 0)
208 if (strat_db.wp_table[i][j].type != WP_TYPE_TOMATO)
211 if (strat_db.wp_table[i][j].present == 0)
214 strat_db.wp_table[i][j].time_removed = time_get_s();
215 strat_db.wp_table[i][j].present = 0;
217 ballboard.ball_count ++;
218 printf("add ball %d,%d\n", i, j);
222 /* mark corn as not present and give correct commands to the cobboard
224 static void check_corn(void)
227 int8_t lcob_near, rcob_near;
230 static uint8_t prev_check_time;
232 uint8_t need_lpack, need_rpack;
234 /* read sensors from ballboard */
236 lcob = ballboard.lcob;
237 ballboard.lcob = I2C_COB_NONE;
238 rcob = ballboard.rcob;
239 ballboard.rcob = I2C_COB_NONE;
242 /* XXX take opponent position into account */
244 /* check present cobs once per second */
245 cur_time = time_get_s();
246 if (cur_time != prev_check_time) {
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()) {
254 printf("remove cob %d\n", i);
256 strat_db.corn_table[i]->present = 0;
259 prev_check_time = cur_time;
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);
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);
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) {
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
293 && cobboard.status == I2C_COBBOARD_STATUS_LBUSY
296 strat_db.corn_table[lidx]->time_removed = time_get_s();
298 cobboard.cob_count ++;
299 printf("add cob %d\n", lidx);
304 i2c_cobboard_deploy_nomove(I2C_LEFT_SIDE);
308 /* no cob near us, we can pack or deploy freely */
310 i2c_cobboard_pack_weak(I2C_LEFT_SIDE);
312 i2c_cobboard_deploy(I2C_LEFT_SIDE);
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) {
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
328 && cobboard.status == I2C_COBBOARD_STATUS_RBUSY
331 strat_db.corn_table[ridx]->time_removed = time_get_s();
333 cobboard.cob_count ++;
334 printf("add cob %d\n", ridx);
339 i2c_cobboard_deploy_nomove(I2C_RIGHT_SIDE);
343 /* no cob near us, we can pack or deploy freely */
345 i2c_cobboard_pack_weak(I2C_RIGHT_SIDE);
347 i2c_cobboard_deploy(I2C_RIGHT_SIDE);
351 /* check opponent position */
352 void check_opponent(void)
354 int16_t opp_x, opp_y;
355 int16_t opp_d, opp_a;
358 strat_opponent_lpack = 0;
359 strat_opponent_rpack = 0;
361 if (get_opponent_xyda(&opp_x, &opp_y, &opp_d, &opp_a) < 0)
364 /* pack spickles if opponent too close */
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;
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;
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;
388 /* malus for some tomatoes and cobs, visited by opponent */
389 if (xycoord_to_ijcoord(&opp_x, &opp_y, &i, &j) < 0)
392 strat_db.wp_table[i][j].opp_visited = 1;
395 /* called periodically (10ms) */
396 void strat_event(void *dummy)
398 /* ignore when strat is not running */
399 if (strat_running == 0)
406 /* limit speed when opponent is near */
407 /* disabled for 2010, we are already slow :) */
408 //strat_limit_speed();
411 /* check that we are on an eject line */
412 static uint8_t robot_is_on_eject_line(void)
417 x = position_get_x_s16(&mainboard.pos);
418 y = position_get_y_s16(&mainboard.pos);
420 if (xycoord_to_ijcoord_not_corn(&x, &y, &i, &j) < 0)
423 if (!wp_belongs_to_line(i, j, 5, LINE_UP) &&
424 !wp_belongs_to_line(i, j, 2, LINE_R_UP))
430 /* 0 = fast, 1 = slow */
431 static uint8_t eject_select_speed(void)
436 x = position_get_x_s16(&mainboard.pos);
437 y = position_get_y_s16(&mainboard.pos);
439 if (get_cob_count() >= 5) {
444 if (xycoord_to_ijcoord_not_corn(&x, &y, &i, &j) < 0) {
445 DEBUG(E_USER_STRAT, "%s(): cannot find waypoint at %d,%d",
450 if (corn_count_neigh(i, j) == 2)
456 /* called multiple times while we are waiting to reach the ejection
458 static uint8_t speedify_eject(void)
460 if (eject_select_speed())
461 strat_set_speed(SPEED_CLITOID_SLOW, SPEED_ANGLE_SLOW);
463 strat_set_speed(SPEED_CLITOID_FAST, SPEED_ANGLE_SLOW);
467 /* must be called from a terminal line */
468 static uint8_t strat_eject(void)
472 DEBUG(E_USER_STRAT, "%s() cob_count=%d ball_count=%d",
473 __FUNCTION__, get_cob_count(), get_ball_count());
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__);
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(),
485 /* err is never == 0 because speedify_eject() always return 0 */
486 if (!TRAJ_SUCCESS(err))
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);
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))
502 DEBUG(E_USER_STRAT, "%s():%d", __FUNCTION__, __LINE__);
507 WAIT_COND_OR_TIMEOUT(ballboard.status == I2C_BALLBOARD_STATUS_F_BUSY,
509 WAIT_COND_OR_TIMEOUT(ballboard.status == I2C_BALLBOARD_STATUS_F_READY,
515 trajectory_a_abs(&mainboard.traj, COLOR_A(-110));
516 err = wait_traj_end(END_INTR|END_TRAJ);
517 if (!TRAJ_SUCCESS(err))
521 trajectory_d_rel(&mainboard.traj, -70);
522 err = wait_traj_end(END_INTR|END_TRAJ);
523 if (!TRAJ_SUCCESS(err))
526 if (get_cob_count() > 0) {
527 i2c_cobboard_set_mode(I2C_COBBOARD_MODE_EJECT);
532 trajectory_d_rel(&mainboard.traj, 70);
533 err = wait_traj_end(END_INTR|END_TRAJ);
534 if (!TRAJ_SUCCESS(err))
537 strat_db_dump(__FUNCTION__);
541 strat_event_enable();
546 static uint8_t strat_beginning(uint8_t do_initturn)
550 strat_set_acc(ACC_DIST, ACC_ANGLE);
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),
559 strat_set_acc(ACC_DIST, ACC_ANGLE);
560 strat_set_speed(SPEED_CLITOID_SLOW, SPEED_ANGLE_SLOW);
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();
571 if (!TRAJ_SUCCESS(err))
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();
583 if (!TRAJ_SUCCESS(err)) {
590 static uint8_t strat_beginning2(uint8_t do_initturn)
594 strat_set_acc(ACC_DIST, ACC_ANGLE);
597 strat_set_speed(600, 90); /* OK */
598 trajectory_d_a_rel(&mainboard.traj, 1000, COLOR_A(-40));
599 err = WAIT_COND_OR_TRAJ_END(trajectory_angle_finished(&mainboard.traj),
605 trajectory_goto_forward_xy_abs(&mainboard.traj,
607 err = wait_traj_end(TRAJ_FLAGS_STD);
612 /* dump state (every 5 s max) */
613 #define DUMP_RATE_LIMIT(dump, last_print) \
615 if (time_get_s() - last_print > 5) { \
617 last_print = time_get_s(); \
623 /* return true if we need to grab some more elements */
624 static uint8_t need_more_elements(void)
626 if (time_get_s() <= 75) {
627 /* we have enough time left */
628 if (get_ball_count() >= 4)
630 if (get_cob_count() >= 4)
632 if ((get_ball_count() >= 2) &&
633 (get_cob_count() >= 2))
638 /* not much time remaining */
639 if ((get_ball_count() >= 1) &&
640 (get_cob_count() >= 1))
647 /* get tomatoes near our goals (12,5 and 12,3) */
648 uint8_t get_opp_oranges(void)
654 DEBUG(E_USER_STRAT, "%s()", __FUNCTION__);
656 /* only if oranges are present */
657 if (strat_db.opp_oranges_count == 0)
660 strat_db.opp_oranges_count = 0;
661 x = position_get_x_s16(&mainboard.pos);
662 y = position_get_y_s16(&mainboard.pos);
664 if (xycoord_to_ijcoord_not_corn(&x, &y, &i, &j) < 0)
667 /* not on eject point */
668 if (i != 11 || j != 6)
672 strat_set_speed(SPEED_DIST_FAST, SPEED_ANGLE_FAST);
674 /* turn in the correct direction */
675 trajectory_a_abs(&mainboard.traj, COLOR_A(-90));
676 err = wait_traj_end(TRAJ_FLAGS_NO_NEAR);
677 if (!TRAJ_SUCCESS(err))
680 trajectory_goto_forward_xy_abs(&mainboard.traj, 2625, COLOR_Y(597));
681 err = wait_traj_end(TRAJ_FLAGS_STD);
682 if (!TRAJ_SUCCESS(err))
685 strat_set_speed(SPEED_DIST_SLOW, SPEED_ANGLE_SLOW);
686 trajectory_goto_forward_xy_abs(&mainboard.traj, 2750, COLOR_Y(250));
687 err = wait_traj_end(TRAJ_FLAGS_NO_NEAR);
688 if (!TRAJ_SUCCESS(err))
691 err = run_to_the_hills(get_opponent_color());
698 /* get tomatoes near our goals (12,5 and 12,3) */
699 uint8_t get_orphan_tomatoes(void)
701 #define CLITOID_TOMATO_RADIUS 100.
702 #define TOMATO_BACK_X 2780
703 #define TOMATO_BACK_LEN 200
710 DEBUG(E_USER_STRAT, "%s()", __FUNCTION__);
712 /* only go if both tomatoes are present */
713 if (!strat_db.wp_table[12][5].present ||
714 !strat_db.wp_table[12][3].present) {
718 x = position_get_x_s16(&mainboard.pos);
719 y = position_get_y_s16(&mainboard.pos);
721 if (xycoord_to_ijcoord_not_corn(&x, &y, &i, &j) < 0)
724 /* not on eject point */
725 if (i != 11 || j != 6)
729 strat_set_speed(SPEED_CLITOID_FAST, SPEED_ANGLE_SLOW);
731 /* turn in the correct direction */
732 trajectory_a_abs(&mainboard.traj, COLOR_A(-90));
733 err = wait_traj_end(TRAJ_FLAGS_NO_NEAR);
735 /* clitoid to turn and take the first ball */
736 ret = trajectory_clitoid(&mainboard.traj, 2625, COLOR_Y(1847),
737 COLOR_A(-90), 150., COLOR_A(90), 0,
738 CLITOID_TOMATO_RADIUS, 3*125);
744 err = wait_traj_end(TRAJ_FLAGS_NO_NEAR);
745 if (!TRAJ_SUCCESS(err))
748 strat_set_speed(SPEED_CLITOID_SLOW, SPEED_ANGLE_SLOW);
749 err = strat_calib(300, END_TRAJ|END_BLOCKING);
750 a = position_get_a_deg_s16(&mainboard.pos);
752 strat_reset_pos(AREA_X - ROBOT_HALF_LENGTH_FRONT,
756 strat_set_speed(SPEED_CLITOID_FAST, SPEED_ANGLE_FAST);
757 trajectory_d_rel(&mainboard.traj, -250);
758 err = WAIT_COND_OR_TRAJ_END(!x_is_more_than(TOMATO_BACK_X),
761 if (err != 0 && !TRAJ_SUCCESS(err))
764 trajectory_d_a_rel(&mainboard.traj, -TOMATO_BACK_LEN, COLOR_A(-90));
765 err = wait_traj_end(TRAJ_FLAGS_NO_NEAR);
769 /* clitoid to turn and take the first ball */
770 strat_set_speed(SPEED_CLITOID_FAST, SPEED_ANGLE_SLOW);
771 ret = trajectory_clitoid(&mainboard.traj, 2625, COLOR_Y(1847),
772 COLOR_A(-90), 150., COLOR_A(90), 0,
773 CLITOID_TOMATO_RADIUS, 7*125);
778 err = wait_traj_end(TRAJ_FLAGS_NO_NEAR);
779 if (!TRAJ_SUCCESS(err))
783 strat_set_speed(SPEED_CLITOID_FAST, SPEED_ANGLE_FAST);
784 trajectory_d_rel(&mainboard.traj, -250);
785 err = WAIT_COND_OR_TRAJ_END(!x_is_more_than(TOMATO_BACK_X),
787 trajectory_d_a_rel(&mainboard.traj, -TOMATO_BACK_LEN, COLOR_A(-90));
788 err = wait_traj_end(TRAJ_FLAGS_NO_NEAR);
796 #define HILL_LEN 1000
799 #define HILL_POSY_YELLOW 310
800 #define HILL_POSY_BLUE 190
802 #define HILL_POSX_BALLS_DOWN1 830
803 #define HILL_POSX_BALLS_DOWN2 920
804 #define HILL_POSX_BALLS_DOWN3 730
805 #define HILL_START_POSX 580
807 uint8_t prepare_hill(uint8_t orange_color, int16_t posx)
809 int16_t startx, starty;
810 uint8_t our_color = get_color();
813 if (orange_color == I2C_COLOR_YELLOW)
814 starty = HILL_POSY_YELLOW;
816 starty = HILL_POSY_BLUE;
817 if (orange_color == our_color)
820 startx = AREA_X - posx;
821 trajectory_goto_forward_xy_abs(&mainboard.traj, startx, COLOR_Y(starty));
822 err = wait_traj_end(TRAJ_FLAGS_SMALL_DIST);
823 if (!TRAJ_SUCCESS(err))
826 /* turn to the hills */
827 if (orange_color == our_color)
828 trajectory_a_abs(&mainboard.traj, COLOR_A(HILL_ANGLE));
830 trajectory_a_abs(&mainboard.traj, COLOR_A(-180+HILL_ANGLE));
831 err = wait_traj_end(TRAJ_FLAGS_SMALL_DIST);
832 if (!TRAJ_SUCCESS(err))
838 /* get oranges, must be called near game area */
839 uint8_t run_to_the_hills(uint8_t orange_color)
844 uint8_t our_color = get_color();
845 int32_t p = pid_get_gain_P(&mainboard.angle.pid);
846 int32_t i = pid_get_gain_I(&mainboard.angle.pid);
847 int32_t d = pid_get_gain_D(&mainboard.angle.pid);
848 int32_t max_in = pid_get_max_in(&mainboard.angle.pid);
849 int32_t max_i = pid_get_max_I(&mainboard.angle.pid);
850 int32_t max_out = pid_get_max_out(&mainboard.angle.pid);
853 strat_get_acc(&ad, &aa);
854 strat_get_speed(&sd, &sa);
856 DEBUG(E_USER_STRAT, "%s()", __FUNCTION__);
858 strat_set_speed(SPEED_DIST_FAST, SPEED_ANGLE_FAST);
859 err = prepare_hill(orange_color, HILL_START_POSX);
860 if (!TRAJ_SUCCESS(err))
863 strat_set_acc(5, ACC_ANGLE);
864 strat_set_speed(300, SPEED_ANGLE_SLOW);
865 bd_set_current_thresholds(&mainboard.distance.bd, 500, 8000, 2000000, 80);
866 bd_set_current_thresholds(&mainboard.angle.bd, 500, 8000, 2000000, 80);
867 bd_set_speed_threshold(&mainboard.distance.bd, 10);
868 support_balls_pack();
869 i2c_ballboard_set_mode(I2C_BALLBOARD_MODE_PREP_FORK);
871 /* decrease angle gains */
872 pid_set_gains(&mainboard.angle.pid, 200, 0, 2000);
874 /* here it is difficult to handle return values, because we
876 i2c_cobboard_set_mode(I2C_COBBOARD_MODE_KICKSTAND_DOWN);
877 trajectory_d_rel(&mainboard.traj, HILL_LEN);
878 err = WAIT_COND_OR_TRAJ_END(position_get_x_s16(&mainboard.pos) >
879 HILL_POSX_BALLS_DOWN1,
880 TRAJ_FLAGS_SMALL_DIST);
881 DEBUG(E_USER_STRAT, "deploy support balls");
882 support_balls_deploy();
883 i2c_cobboard_set_mode(I2C_COBBOARD_MODE_KICKSTAND_UP);
884 trajectory_only_a_rel(&mainboard.traj, 2);
885 err = WAIT_COND_OR_TE_TO(0, 0, 2200);
887 i2c_cobboard_set_mode(I2C_COBBOARD_MODE_KICKSTAND_DOWN);
890 i2c_ballboard_set_mode(I2C_BALLBOARD_MODE_TAKE_FORK);
894 /* reach top, go down */
895 trajectory_d_rel(&mainboard.traj, -HILL_LEN);
897 err = WAIT_COND_OR_TRAJ_END(position_get_x_s16(&mainboard.pos) <
898 HILL_POSX_BALLS_DOWN2,
899 TRAJ_FLAGS_SMALL_DIST);
900 DEBUG(E_USER_STRAT, "pack support balls");
901 support_balls_pack();
902 err = WAIT_COND_OR_TRAJ_END(position_get_x_s16(&mainboard.pos) <
903 HILL_POSX_BALLS_DOWN3,
904 TRAJ_FLAGS_SMALL_DIST);
905 DEBUG(E_USER_STRAT, "deploy support balls");
906 strat_set_acc(ad, aa);
907 strat_set_speed(SPEED_DIST_FAST, SPEED_ANGLE_FAST);
908 support_balls_deploy();
909 err = wait_traj_end(TRAJ_FLAGS_SMALL_DIST);
910 i2c_cobboard_set_mode(I2C_COBBOARD_MODE_HARVEST);
912 /* wait to be near the wall */
913 err = WAIT_COND_OR_TRAJ_END(position_get_x_s16(&mainboard.pos) < 200,
914 TRAJ_FLAGS_SMALL_DIST);
916 i2c_cobboard_set_mode(I2C_COBBOARD_MODE_HARVEST);
918 /* restore BD coefs */
919 bd_set_current_thresholds(&mainboard.distance.bd, 500, 8000, 1000000, 20);
920 bd_set_current_thresholds(&mainboard.distance.bd, 500, 8000, 1000000, 20);
921 bd_set_speed_threshold(&mainboard.distance.bd, 60);
923 /* calibrate position on the wall */
924 strat_set_speed(SPEED_DIST_VERY_SLOW, SPEED_ANGLE_FAST);
925 err = strat_calib(-400, TRAJ_FLAGS_SMALL_DIST);
926 if (orange_color == our_color)
927 strat_reset_pos(ROBOT_HALF_LENGTH_REAR,
928 DO_NOT_SET_POS, COLOR_A(0));
930 strat_reset_pos(AREA_X - ROBOT_HALF_LENGTH_REAR,
931 DO_NOT_SET_POS, COLOR_A(180));
932 strat_set_speed(SPEED_DIST_FAST, SPEED_ANGLE_FAST);
934 trajectory_d_rel(&mainboard.traj, 250);
935 err = wait_traj_end(TRAJ_FLAGS_NO_NEAR);
936 if (orange_color == I2C_COLOR_YELLOW)
937 trajectory_a_rel(&mainboard.traj, 90);
939 trajectory_a_rel(&mainboard.traj, -90);
940 err = wait_traj_end(TRAJ_FLAGS_NO_NEAR);
943 strat_set_speed(SPEED_DIST_VERY_SLOW, SPEED_ANGLE_FAST);
944 err = strat_calib(-400, TRAJ_FLAGS_SMALL_DIST);
945 strat_reset_pos(DO_NOT_SET_POS,
946 COLOR_Y(ROBOT_HALF_LENGTH_REAR),
948 strat_set_speed(SPEED_DIST_FAST, SPEED_ANGLE_FAST);
950 trajectory_d_rel(&mainboard.traj, 250);
951 err = wait_traj_end(TRAJ_FLAGS_NO_NEAR);
953 /* revert acceleration and speed */
954 pid_set_gains(&mainboard.angle.pid, p, i, d);
955 pid_set_maximums(&mainboard.distance.pid, max_in, max_i, max_out);
957 strat_set_speed(sd, sa);
958 support_balls_deploy();
962 uint8_t strat_main(void)
964 uint8_t err, do_initturn = 1;
967 if (strat_conf.flags & STRAT_CONF_OUR_ORANGE) {
968 err = run_to_the_hills(get_color());
969 strat_db.our_oranges_count = 0;
973 /* harvest the first cobs + balls */
974 if (strat_conf.flags & STRAT_CONF_STRAIGHT_BEGIN)
975 err = strat_beginning2(do_initturn);
977 err = strat_beginning(do_initturn);
979 if (!TRAJ_SUCCESS(err))
984 /* choose circuit, and harvest on it */
987 DEBUG(E_USER_STRAT, "start main loop");
989 /* if it's time to get tomatoes, do it */
990 if (time_get_s() > strat_conf.orphan_tomato) {
991 err = get_orphan_tomatoes();
992 if (err == END_ERROR) {
994 "get_orphan_tomatoes returned END_ERROR");
996 else if (err == END_TIMER) {
997 DEBUG(E_USER_STRAT, "End of time");
1001 else if (!TRAJ_SUCCESS(err)) {
1002 /* don't retry these tomatoes if it failed */
1003 strat_conf.orphan_tomato = 90;
1008 /* if it's time to get opponent oranges, do it */
1009 if (time_get_s() > strat_conf.opp_orange) {
1010 err = get_opp_oranges();
1011 if (err == END_ERROR) {
1013 "get_opp_oranges returned END_ERROR");
1015 else if (err == END_TIMER) {
1016 DEBUG(E_USER_STRAT, "End of time");
1020 else if (!TRAJ_SUCCESS(err)) {
1021 /* don't retry oranges if it failed */
1022 strat_conf.opp_orange = 90;
1027 /**********************/
1028 /* harvest on circuit */
1029 /**********************/
1031 err = strat_harvest_circuit();
1032 if (err == END_TIMER) {
1033 DEBUG(E_USER_STRAT, "End of time");
1037 if (!TRAJ_SUCCESS(err)) {
1042 /***********************/
1043 /* eject game elements */
1044 /***********************/
1046 err = strat_eject();
1047 /* end of time exit ! */
1048 if (err == END_TIMER) {
1049 DEBUG(E_USER_STRAT, "End of time");
1053 if (!TRAJ_SUCCESS(err)) {