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_dump(__FUNCTION__);
105 strat_db_dump(__FUNCTION__);
108 void strat_conf_dump(const char *caller)
110 if (!strat_conf.dump_enabled)
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");
120 void strat_event_enable(void)
125 void strat_event_disable(void)
130 /* call it just before launching the strat */
131 void strat_init(void)
134 position_set(&mainboard.pos, 298.16,
135 COLOR_Y(308.78), COLOR_A(70.00));
138 /* we consider that the color is correctly set */
140 strat_set_speed(SPEED_DIST_FAST, SPEED_ANGLE_FAST);
142 interrupt_traj_reset();
144 i2c_cobboard_set_mode(I2C_COBBOARD_MODE_HARVEST);
145 i2c_ballboard_set_mode(I2C_BALLBOARD_MODE_HARVEST);
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;
152 /* call it after each strat */
153 void strat_exit(void)
160 mainboard.flags &= ~(DO_TIMER);
166 mainboard.flags &= ~(DO_CS);
168 pwm_ng_set(LEFT_PWM, 0);
169 pwm_ng_set(RIGHT_PWM, 0);
173 /* mark tomato as not present */
174 static void check_tomato(void)
178 static uint8_t prev_check_time;
181 /* check present tomatoes once per second */
182 cur_time = time_get_s();
183 if (cur_time != prev_check_time) {
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()) {
191 printf("remove tomato %d\n", k);
193 strat_db.tomato_table[k]->present = 0;
196 prev_check_time = cur_time;
199 x = position_get_x_s16(&mainboard.pos);
200 y = position_get_y_s16(&mainboard.pos);
202 if (xycoord_to_ijcoord(&x, &y, &i, &j) < 0)
205 if (strat_db.wp_table[i][j].type != WP_TYPE_TOMATO)
208 if (strat_db.wp_table[i][j].present == 0)
211 strat_db.wp_table[i][j].time_removed = time_get_s();
212 strat_db.wp_table[i][j].present = 0;
214 ballboard.ball_count ++;
215 printf("add ball %d,%d\n", i, j);
219 /* mark corn as not present and give correct commands to the cobboard
221 static void check_corn(void)
224 int8_t lcob_near, rcob_near;
227 static uint8_t prev_check_time;
229 uint8_t need_lpack, need_rpack;
231 /* read sensors from ballboard */
233 lcob = ballboard.lcob;
234 ballboard.lcob = I2C_COB_NONE;
235 rcob = ballboard.rcob;
236 ballboard.rcob = I2C_COB_NONE;
239 /* XXX take opponent position into account */
241 /* check present cobs once per second */
242 cur_time = time_get_s();
243 if (cur_time != prev_check_time) {
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()) {
251 printf("remove cob %d\n", i);
253 strat_db.corn_table[i]->present = 0;
256 prev_check_time = cur_time;
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);
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);
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) {
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
290 && cobboard.status == I2C_COBBOARD_STATUS_LBUSY
293 strat_db.corn_table[lidx]->time_removed = time_get_s();
295 cobboard.cob_count ++;
296 printf("add cob %d\n", lidx);
301 i2c_cobboard_deploy_nomove(I2C_LEFT_SIDE);
305 /* no cob near us, we can pack or deploy freely */
307 i2c_cobboard_pack_weak(I2C_LEFT_SIDE);
309 i2c_cobboard_deploy(I2C_LEFT_SIDE);
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) {
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
325 && cobboard.status == I2C_COBBOARD_STATUS_RBUSY
328 strat_db.corn_table[ridx]->time_removed = time_get_s();
330 cobboard.cob_count ++;
331 printf("add cob %d\n", ridx);
336 i2c_cobboard_deploy_nomove(I2C_RIGHT_SIDE);
340 /* no cob near us, we can pack or deploy freely */
342 i2c_cobboard_pack_weak(I2C_RIGHT_SIDE);
344 i2c_cobboard_deploy(I2C_RIGHT_SIDE);
348 /* check opponent position */
349 void check_opponent(void)
351 int16_t opp_x, opp_y;
352 int16_t opp_d, opp_a;
355 strat_opponent_lpack = 0;
356 strat_opponent_rpack = 0;
358 if (get_opponent_xyda(&opp_x, &opp_y, &opp_d, &opp_a) < 0)
361 /* pack spickles if opponent too close */
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;
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;
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;
385 /* malus for some tomatoes and cobs, visited by opponent */
386 if (xycoord_to_ijcoord(&opp_x, &opp_y, &i, &j) < 0)
389 strat_db.wp_table[i][j].opp_visited = 1;
392 /* called periodically (10ms) */
393 void strat_event(void *dummy)
395 /* ignore when strat is not running */
396 if (strat_running == 0)
403 /* limit speed when opponent is near */
404 /* disabled for 2010, we are already slow :) */
405 //strat_limit_speed();
408 /* check that we are on an eject line */
409 static uint8_t robot_is_on_eject_line(void)
414 x = position_get_x_s16(&mainboard.pos);
415 y = position_get_y_s16(&mainboard.pos);
417 if (xycoord_to_ijcoord_not_corn(&x, &y, &i, &j) < 0)
420 if (!wp_belongs_to_line(i, j, 5, LINE_UP) &&
421 !wp_belongs_to_line(i, j, 2, LINE_R_UP))
427 /* 0 = fast, 1 = slow */
428 static uint8_t eject_select_speed(void)
433 x = position_get_x_s16(&mainboard.pos);
434 y = position_get_y_s16(&mainboard.pos);
436 if (get_cob_count() >= 5) {
441 if (xycoord_to_ijcoord_not_corn(&x, &y, &i, &j) < 0) {
442 DEBUG(E_USER_STRAT, "%s(): cannot find waypoint at %d,%d",
447 if (corn_count_neigh(i, j) == 2)
453 /* called multiple times while we are waiting to reach the ejection
455 static uint8_t speedify_eject(void)
457 if (eject_select_speed())
458 strat_set_speed(SPEED_CLITOID_SLOW, SPEED_ANGLE_SLOW);
460 strat_set_speed(SPEED_CLITOID_FAST, SPEED_ANGLE_SLOW);
464 /* must be called from a terminal line */
465 static uint8_t strat_eject(void)
469 DEBUG(E_USER_STRAT, "%s() cob_count=%d ball_count=%d",
470 __FUNCTION__, get_cob_count(), get_ball_count());
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__);
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(),
482 /* err is never == 0 because speedify_eject() always return 0 */
483 if (!TRAJ_SUCCESS(err))
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);
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))
499 DEBUG(E_USER_STRAT, "%s():%d", __FUNCTION__, __LINE__);
504 WAIT_COND_OR_TIMEOUT(ballboard.status == I2C_BALLBOARD_STATUS_F_BUSY,
506 WAIT_COND_OR_TIMEOUT(ballboard.status == I2C_BALLBOARD_STATUS_F_READY,
512 trajectory_a_abs(&mainboard.traj, COLOR_A(-110));
513 err = wait_traj_end(END_INTR|END_TRAJ);
514 if (!TRAJ_SUCCESS(err))
518 trajectory_d_rel(&mainboard.traj, -70);
519 err = wait_traj_end(END_INTR|END_TRAJ);
520 if (!TRAJ_SUCCESS(err))
523 if (get_cob_count() > 0) {
524 i2c_cobboard_set_mode(I2C_COBBOARD_MODE_EJECT);
529 trajectory_d_rel(&mainboard.traj, 70);
530 err = wait_traj_end(END_INTR|END_TRAJ);
531 if (!TRAJ_SUCCESS(err))
534 strat_db_dump(__FUNCTION__);
538 strat_event_enable();
543 static uint8_t strat_beginning(uint8_t do_initturn)
547 strat_set_acc(ACC_DIST, ACC_ANGLE);
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),
556 strat_set_acc(ACC_DIST, ACC_ANGLE);
557 strat_set_speed(SPEED_CLITOID_SLOW, SPEED_ANGLE_SLOW);
559 err = line2line(0, LINE_UP, 2, LINE_R_DOWN, TRAJ_FLAGS_NO_NEAR);
560 if (!TRAJ_SUCCESS(err))
563 err = line2line(2, LINE_R_DOWN, 2, LINE_R_UP, TRAJ_FLAGS_NO_NEAR);
564 if (!TRAJ_SUCCESS(err)) {
571 /* dump state (every 5 s max) */
572 #define DUMP_RATE_LIMIT(dump, last_print) \
574 if (time_get_s() - last_print > 5) { \
576 last_print = time_get_s(); \
582 /* return true if we need to grab some more elements */
583 static uint8_t need_more_elements(void)
585 if (time_get_s() <= 75) {
586 /* we have enough time left */
587 if (get_ball_count() >= 4)
589 if (get_cob_count() >= 4)
591 if ((get_ball_count() >= 2) &&
592 (get_cob_count() >= 2))
597 /* not much time remaining */
598 if ((get_ball_count() >= 1) &&
599 (get_cob_count() >= 1))
606 /* get tomatoes near our goals (12,5 and 12,3) */
607 uint8_t get_opp_oranges(void)
613 DEBUG(E_USER_STRAT, "%s()", __FUNCTION__);
615 /* only if oranges are present */
616 if (strat_db.opp_oranges_count == 0)
619 strat_db.opp_oranges_count = 0;
620 x = position_get_x_s16(&mainboard.pos);
621 y = position_get_y_s16(&mainboard.pos);
623 if (xycoord_to_ijcoord_not_corn(&x, &y, &i, &j) < 0)
626 /* not on eject point */
627 if (i != 11 || j != 6)
631 strat_set_speed(SPEED_DIST_FAST, SPEED_ANGLE_FAST);
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))
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))
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))
650 err = run_to_the_hills(get_opponent_color());
657 /* get tomatoes near our goals (12,5 and 12,3) */
658 uint8_t get_orphan_tomatoes(void)
660 #define CLITOID_TOMATO_RADIUS 100.
661 #define TOMATO_BACK_X 2760
662 #define TOMATO_BACK_LEN 200
669 DEBUG(E_USER_STRAT, "%s()", __FUNCTION__);
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) {
677 x = position_get_x_s16(&mainboard.pos);
678 y = position_get_y_s16(&mainboard.pos);
680 if (xycoord_to_ijcoord_not_corn(&x, &y, &i, &j) < 0)
683 /* not on eject point */
684 if (i != 11 || j != 6)
688 strat_set_speed(SPEED_CLITOID_FAST, SPEED_ANGLE_SLOW);
690 /* turn in the correct direction */
691 trajectory_a_abs(&mainboard.traj, COLOR_A(-90));
692 err = wait_traj_end(TRAJ_FLAGS_NO_NEAR);
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);
703 err = wait_traj_end(TRAJ_FLAGS_NO_NEAR);
704 if (!TRAJ_SUCCESS(err))
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);
711 strat_reset_pos(AREA_X - ROBOT_HALF_LENGTH_FRONT,
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),
720 if (err != 0 && !TRAJ_SUCCESS(err))
723 trajectory_d_a_rel(&mainboard.traj, -TOMATO_BACK_LEN, COLOR_A(-90));
724 err = wait_traj_end(TRAJ_FLAGS_NO_NEAR);
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);
737 err = wait_traj_end(TRAJ_FLAGS_NO_NEAR);
738 if (!TRAJ_SUCCESS(err))
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),
746 trajectory_d_a_rel(&mainboard.traj, -TOMATO_BACK_LEN, COLOR_A(-90));
747 err = wait_traj_end(TRAJ_FLAGS_NO_NEAR);
755 #define HILL_LEN 1000
758 #define HILL_POSY_YELLOW 310
759 #define HILL_POSY_BLUE 190
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
766 uint8_t prepare_hill(uint8_t orange_color, int16_t posx)
768 int16_t startx, starty;
769 uint8_t our_color = get_color();
772 if (orange_color == I2C_COLOR_YELLOW)
773 starty = HILL_POSY_YELLOW;
775 starty = HILL_POSY_BLUE;
776 if (orange_color == our_color)
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))
785 /* turn to the hills */
786 if (orange_color == our_color)
787 trajectory_a_abs(&mainboard.traj, COLOR_A(HILL_ANGLE));
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))
797 /* get oranges, must be called near game area */
798 uint8_t run_to_the_hills(uint8_t orange_color)
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);
812 strat_get_acc(&ad, &aa);
813 strat_get_speed(&sd, &sa);
815 DEBUG(E_USER_STRAT, "%s()", __FUNCTION__);
817 strat_set_speed(SPEED_DIST_FAST, SPEED_ANGLE_FAST);
818 err = prepare_hill(orange_color, HILL_START_POSX);
819 if (!TRAJ_SUCCESS(err))
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);
830 /* decrease angle gains */
831 pid_set_gains(&mainboard.angle.pid, 200, 0, 2000);
833 /* here it is difficult to handle return values, because we
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);
846 i2c_cobboard_set_mode(I2C_COBBOARD_MODE_KICKSTAND_DOWN);
849 i2c_ballboard_set_mode(I2C_BALLBOARD_MODE_TAKE_FORK);
853 /* reach top, go down */
854 trajectory_d_rel(&mainboard.traj, -HILL_LEN);
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);
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);
875 i2c_cobboard_set_mode(I2C_COBBOARD_MODE_HARVEST);
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);
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));
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);
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);
898 trajectory_a_rel(&mainboard.traj, -90);
899 err = wait_traj_end(TRAJ_FLAGS_NO_NEAR);
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),
907 strat_set_speed(SPEED_DIST_FAST, SPEED_ANGLE_FAST);
909 trajectory_d_rel(&mainboard.traj, 250);
910 err = wait_traj_end(TRAJ_FLAGS_NO_NEAR);
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);
916 strat_set_speed(sd, sa);
917 support_balls_deploy();
921 uint8_t strat_main(void)
923 uint8_t err, do_initturn = 1;
926 if (strat_conf.flags & STRAT_CONF_OUR_ORANGE) {
927 err = run_to_the_hills(get_color());
928 strat_db.our_oranges_count = 0;
932 /* harvest the first cobs + balls */
933 err = strat_beginning(do_initturn);
935 if (!TRAJ_SUCCESS(err))
940 /* choose circuit, and harvest on it */
943 DEBUG(E_USER_STRAT, "start main loop");
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) {
950 "get_orphan_tomatoes returned END_ERROR");
952 else if (err == END_TIMER) {
953 DEBUG(E_USER_STRAT, "End of time");
957 else if (!TRAJ_SUCCESS(err)) {
958 /* don't retry these tomatoes if it failed */
959 strat_conf.orphan_tomato = 90;
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) {
969 "get_opp_oranges returned END_ERROR");
971 else if (err == END_TIMER) {
972 DEBUG(E_USER_STRAT, "End of time");
976 else if (!TRAJ_SUCCESS(err)) {
977 /* don't retry oranges if it failed */
978 strat_conf.opp_orange = 90;
983 /**********************/
984 /* harvest on circuit */
985 /**********************/
987 err = strat_harvest_circuit();
988 if (err == END_TIMER) {
989 DEBUG(E_USER_STRAT, "End of time");
993 if (!TRAJ_SUCCESS(err)) {
998 /***********************/
999 /* eject game elements */
1000 /***********************/
1002 err = strat_eject();
1003 /* end of time exit ! */
1004 if (err == END_TIMER) {
1005 DEBUG(E_USER_STRAT, "End of time");
1009 if (!TRAJ_SUCCESS(err)) {