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\r\n"),
116 (strat_conf.flags & STRAT_CONF_OUR_ORANGE) ? "y":"n");
117 printf_P(PSTR("wait_obstacle = %s\r\n"),
118 (strat_conf.flags & STRAT_CONF_WAIT_OBSTACLE) ? "y":"n");
119 printf_P(PSTR("straight begin = %s\r\n"),
120 (strat_conf.flags & STRAT_CONF_STRAIGHT_BEGIN) ? "y":"n");
121 printf_P(PSTR("opp_orange = %d\r\n"), strat_conf.opp_orange);
122 printf_P(PSTR("orphan_tomato = %d\r\n"), strat_conf.orphan_tomato);
125 void strat_event_enable(void)
130 void strat_event_disable(void)
135 /* call it just before launching the strat */
136 void strat_init(void)
139 position_set(&mainboard.pos, 298.16,
140 COLOR_Y(308.78), COLOR_A(70.00));
143 /* we consider that the color is correctly set */
145 strat_set_speed(SPEED_DIST_FAST, SPEED_ANGLE_FAST);
147 interrupt_traj_reset();
149 i2c_cobboard_deploy(I2C_LEFT_SIDE);
150 i2c_cobboard_deploy(I2C_RIGHT_SIDE);
151 i2c_cobboard_set_mode(I2C_COBBOARD_MODE_HARVEST);
152 i2c_ballboard_set_mode(I2C_BALLBOARD_MODE_HARVEST);
154 /* used in strat_base for END_TIMER */
155 mainboard.flags = DO_ENCODERS | DO_CS | DO_RS |
156 DO_POS | DO_BD | DO_TIMER | DO_POWER;
159 /* call it after each strat */
160 void strat_exit(void)
167 mainboard.flags &= ~(DO_TIMER);
173 mainboard.flags &= ~(DO_CS);
175 pwm_ng_set(LEFT_PWM, 0);
176 pwm_ng_set(RIGHT_PWM, 0);
180 /* mark tomato as not present */
181 static void check_tomato(void)
185 static uint8_t prev_check_time;
188 /* check present tomatoes once per second */
189 cur_time = time_get_s();
190 if (cur_time != prev_check_time) {
193 for (k = 0; k < TOMATO_NB; k++) {
194 if (strat_db.tomato_table[k]->present == 1 &&
195 strat_db.tomato_table[k]->time_removed != -1 &&
196 strat_db.tomato_table[k]->time_removed + 2 <= time_get_s()) {
198 printf("remove tomato %d\n", k);
200 strat_db.tomato_table[k]->present = 0;
203 prev_check_time = cur_time;
206 x = position_get_x_s16(&mainboard.pos);
207 y = position_get_y_s16(&mainboard.pos);
209 if (xycoord_to_ijcoord(&x, &y, &i, &j) < 0)
212 if (strat_db.wp_table[i][j].type != WP_TYPE_TOMATO)
215 if (strat_db.wp_table[i][j].present == 0)
218 strat_db.wp_table[i][j].time_removed = time_get_s();
219 strat_db.wp_table[i][j].present = 0;
221 ballboard.ball_count ++;
222 printf("add ball %d,%d\n", i, j);
226 /* mark corn as not present and give correct commands to the cobboard
228 static void check_corn(void)
231 int8_t lcob_near, rcob_near;
234 static uint8_t prev_check_time;
236 uint8_t need_lpack, need_rpack;
237 int16_t l_xspickle, l_yspickle;
238 int16_t r_xspickle, r_yspickle;
239 uint8_t l_y_too_high_pack = 0, r_y_too_high_pack = 0;
241 /* read sensors from ballboard */
243 lcob = ballboard.lcob;
244 ballboard.lcob = I2C_COB_NONE;
245 rcob = ballboard.rcob;
246 ballboard.rcob = I2C_COB_NONE;
249 /* check present cobs once per second */
250 cur_time = time_get_s();
251 if (cur_time != prev_check_time) {
254 /* only useful for simu */
255 for (i = 0; i < CORN_NB; i++) {
256 if (strat_db.corn_table[i]->present == 1 &&
257 strat_db.corn_table[i]->time_removed != -1 &&
258 strat_db.corn_table[i]->time_removed + 2 <= time_get_s()) {
260 printf("remove cob %d\n", i);
262 strat_db.corn_table[i]->present = 0;
265 prev_check_time = cur_time;
268 /* detect cob on left side */
269 lcob_near = corn_is_near(&lidx, I2C_LEFT_SIDE,
270 &l_xspickle, &l_yspickle);
271 if (lcob_near && lcob != I2C_COB_NONE) {
272 if (strat_db.corn_table[lidx]->corn.color == I2C_COB_UNKNOWN)
273 DEBUG(E_USER_STRAT, "lcob %s %d",
274 lcob == I2C_COB_WHITE ? "white" : "black", lidx);
275 corn_set_color(strat_db.corn_table[lidx], lcob);
277 if (!__y_is_more_than(l_yspickle, 600))
278 l_y_too_high_pack = 1;
280 /* detect cob on right side */
281 rcob_near = corn_is_near(&ridx, I2C_RIGHT_SIDE,
282 &r_xspickle, &r_yspickle);
283 if (rcob_near && rcob != I2C_COB_NONE) {
284 if (strat_db.corn_table[ridx]->corn.color == I2C_COB_UNKNOWN)
285 DEBUG(E_USER_STRAT, "rcob %s %d",
286 rcob == I2C_COB_WHITE ? "white" : "black", ridx);
287 corn_set_color(strat_db.corn_table[ridx], rcob);
289 if (!__y_is_more_than(r_yspickle, 600))
290 r_y_too_high_pack = 1;
292 /* control the cobboard mode for left spickle */
293 need_lpack = get_cob_count() >= 5 || strat_want_pack ||
294 strat_lpack60 || strat_opponent_lpack || l_y_too_high_pack;
296 if (lcob_near && strat_db.corn_table[lidx]->present) {
301 /* deploy spickle and harvest white ones */
302 if (strat_db.corn_table[lidx]->corn.color == I2C_COB_WHITE) {
303 i2c_cobboard_autoharvest_nomove(I2C_LEFT_SIDE);
304 if (strat_db.corn_table[lidx]->time_removed == -1
306 && cobboard.status == I2C_COBBOARD_STATUS_LBUSY
309 strat_db.corn_table[lidx]->time_removed = time_get_s();
311 cobboard.cob_count ++;
312 printf("add cob %d\n", lidx);
314 strat_db.corn_table[lidx]->present = 0;
319 i2c_cobboard_deploy_nomove(I2C_LEFT_SIDE);
323 /* no cob near us, we can pack or deploy freely */
325 i2c_cobboard_pack_weak(I2C_LEFT_SIDE);
327 i2c_cobboard_deploy(I2C_LEFT_SIDE);
330 /* control the cobboard mode for right spickle */
331 need_rpack = get_cob_count() >= 5 || strat_want_pack ||
332 strat_rpack60 || strat_opponent_rpack || r_y_too_high_pack;
333 if (rcob_near && strat_db.corn_table[ridx]->present) {
338 /* deploy spickle and harvest white ones */
339 if (strat_db.corn_table[ridx]->corn.color == I2C_COB_WHITE) {
340 i2c_cobboard_autoharvest_nomove(I2C_RIGHT_SIDE);
341 if (strat_db.corn_table[ridx]->time_removed == -1
343 && cobboard.status == I2C_COBBOARD_STATUS_RBUSY
346 strat_db.corn_table[ridx]->time_removed = time_get_s();
348 cobboard.cob_count ++;
349 printf("add cob %d\n", ridx);
351 strat_db.corn_table[ridx]->present = 0;
356 i2c_cobboard_deploy_nomove(I2C_RIGHT_SIDE);
360 /* no cob near us, we can pack or deploy freely */
362 i2c_cobboard_pack_weak(I2C_RIGHT_SIDE);
364 i2c_cobboard_deploy(I2C_RIGHT_SIDE);
368 /* check opponent position */
369 void check_opponent(void)
371 int16_t opp_x, opp_y;
372 int16_t opp_d, opp_a;
375 strat_opponent_lpack = 0;
376 strat_opponent_rpack = 0;
378 if (get_opponent_xyda(&opp_x, &opp_y, &opp_d, &opp_a) < 0)
381 /* pack spickles if opponent too close */
383 if (opp_a > 45 && opp_a < 135)
384 strat_opponent_lpack = 1;
385 if (opp_a > 225 && opp_a < 315)
386 strat_opponent_rpack = 1;
389 /* check for oranges after 5 seconds */
390 if (time_get_s() > 5) {
391 if (mainboard.our_color == I2C_COLOR_YELLOW) {
392 if (opp_y < 500 && opp_x < 500)
393 strat_db.our_oranges_count = 0;
394 if (opp_y < 500 && opp_x > AREA_X - 500)
395 strat_db.opp_oranges_count = 0;
398 if (opp_y > AREA_Y - 500 && opp_x < 500)
399 strat_db.our_oranges_count = 0;
400 if (opp_y > AREA_Y - 500 && opp_x > AREA_X - 500)
401 strat_db.opp_oranges_count = 0;
405 /* malus for some tomatoes and cobs, visited by opponent */
406 if (xycoord_to_ijcoord(&opp_x, &opp_y, &i, &j) < 0)
409 strat_db.wp_table[i][j].opp_visited = 1;
412 /* called periodically (10ms) */
413 void strat_event(void *dummy)
415 /* ignore when strat is not running */
416 if (strat_running == 0)
423 /* limit speed when opponent is near */
424 /* disabled for 2010, we are already slow :) */
425 //strat_limit_speed();
428 /* check that we are on an eject line */
429 static uint8_t robot_is_on_eject_line(void)
434 x = position_get_x_s16(&mainboard.pos);
435 y = position_get_y_s16(&mainboard.pos);
437 if (xycoord_to_ijcoord_not_corn(&x, &y, &i, &j) < 0)
440 if (!wp_belongs_to_line(i, j, 5, LINE_UP) &&
441 !wp_belongs_to_line(i, j, 2, LINE_R_UP))
447 /* 0 = fast, 1 = slow */
448 static uint8_t eject_select_speed(void)
453 x = position_get_x_s16(&mainboard.pos);
454 y = position_get_y_s16(&mainboard.pos);
456 if (get_cob_count() >= 5) {
461 if (xycoord_to_ijcoord_not_corn(&x, &y, &i, &j) < 0) {
462 DEBUG(E_USER_STRAT, "%s(): cannot find waypoint at %d,%d",
467 if (corn_count_neigh(i, j) == 2)
473 /* called multiple times while we are waiting to reach the ejection
475 static uint8_t speedify_eject(void)
477 if (eject_select_speed())
478 strat_set_speed(SPEED_CLITOID_SLOW, SPEED_ANGLE_SLOW);
480 strat_set_speed(SPEED_CLITOID_FAST, SPEED_ANGLE_SLOW);
484 /* must be called from a terminal line */
485 static uint8_t strat_eject(void)
489 DEBUG(E_USER_STRAT, "%s() cob_count=%d ball_count=%d",
490 __FUNCTION__, get_cob_count(), get_ball_count());
492 /* check that we are called from an eject line */
493 if (!robot_is_on_eject_line()) {
494 DEBUG(E_USER_STRAT, "%s() not on eject line", __FUNCTION__);
498 /* go to eject point */
499 trajectory_goto_forward_xy_abs(&mainboard.traj, 2625, COLOR_Y(1847));
500 err = WAIT_COND_OR_TRAJ_END(speedify_eject(),
502 /* err is never == 0 because speedify_eject() always return 0 */
503 if (!TRAJ_SUCCESS(err))
506 /* pack arms (force), and disable strat_event */
507 strat_event_disable();
508 i2c_cobboard_pack_weak(I2C_LEFT_SIDE);
509 i2c_cobboard_pack_weak(I2C_RIGHT_SIDE);
512 if (get_ball_count() > 0) {
513 i2c_ballboard_set_mode(I2C_BALLBOARD_MODE_EJECT);
515 trajectory_a_abs(&mainboard.traj, COLOR_A(70));
516 err = wait_traj_end(TRAJ_FLAGS_NO_NEAR);
517 if (!TRAJ_SUCCESS(err))
520 DEBUG(E_USER_STRAT, "%s():%d", __FUNCTION__, __LINE__);
525 WAIT_COND_OR_TIMEOUT(ballboard.status == I2C_BALLBOARD_STATUS_F_BUSY,
527 WAIT_COND_OR_TIMEOUT(ballboard.status == I2C_BALLBOARD_STATUS_F_READY,
532 /* to pack spickles */
536 if (get_cob_count() > 0) {
538 trajectory_a_abs(&mainboard.traj, COLOR_A(-110));
539 err = wait_traj_end(END_INTR|END_TRAJ);
540 if (!TRAJ_SUCCESS(err))
544 trajectory_d_rel(&mainboard.traj, -70);
545 err = wait_traj_end(END_INTR|END_TRAJ);
546 if (!TRAJ_SUCCESS(err))
549 i2c_cobboard_set_mode(I2C_COBBOARD_MODE_EJECT);
554 trajectory_d_rel(&mainboard.traj, 70);
555 err = wait_traj_end(END_INTR|END_TRAJ);
556 if (!TRAJ_SUCCESS(err))
560 strat_db_dump(__FUNCTION__);
564 strat_event_enable();
569 static uint8_t strat_beginning(uint8_t do_initturn)
573 strat_set_acc(ACC_DIST, ACC_ANGLE);
576 //strat_set_speed(600, 60);
577 strat_set_speed(450, 50);
578 trajectory_d_a_rel(&mainboard.traj, 1000, COLOR_A(20));
579 err = WAIT_COND_OR_TRAJ_END(trajectory_angle_finished(&mainboard.traj),
583 strat_set_acc(ACC_DIST, ACC_ANGLE);
584 strat_set_speed(SPEED_CLITOID_SLOW, SPEED_ANGLE_SLOW);
587 err = line2line(0, LINE_UP, 2, LINE_R_DOWN, TRAJ_FLAGS_NO_NEAR);
588 if (err == END_OBSTACLE &&
589 strat_conf.flags & STRAT_CONF_WAIT_OBSTACLE &&
590 time_get_s() > strat_conf.prev_wait_obstacle + 5) {
591 strat_conf.prev_wait_obstacle = time_get_s();
595 if (!TRAJ_SUCCESS(err))
599 err = line2line(2, LINE_R_DOWN, 2, LINE_R_UP, TRAJ_FLAGS_NO_NEAR);
600 if (err == END_OBSTACLE &&
601 strat_conf.flags & STRAT_CONF_WAIT_OBSTACLE &&
602 time_get_s() > strat_conf.prev_wait_obstacle + 5) {
603 strat_conf.prev_wait_obstacle = time_get_s();
607 if (!TRAJ_SUCCESS(err)) {
614 static uint8_t strat_beginning2(uint8_t do_initturn)
618 strat_set_acc(ACC_DIST, ACC_ANGLE);
621 strat_set_speed(600, 95); /* OK */
622 trajectory_d_a_rel(&mainboard.traj, 1000, COLOR_A(-40));
623 err = WAIT_COND_OR_TRAJ_END(trajectory_angle_finished(&mainboard.traj),
629 trajectory_goto_forward_xy_abs(&mainboard.traj,
631 err = wait_traj_end(TRAJ_FLAGS_STD);
636 /* dump state (every 5 s max) */
637 #define DUMP_RATE_LIMIT(dump, last_print) \
639 if (time_get_s() - last_print > 5) { \
641 last_print = time_get_s(); \
647 /* return true if we need to grab some more elements */
648 static uint8_t need_more_elements(void)
650 if (time_get_s() <= 75) {
651 /* we have enough time left */
652 if (get_ball_count() >= 4)
654 if (get_cob_count() >= 4)
656 if ((get_ball_count() >= 2) &&
657 (get_cob_count() >= 2))
662 /* not much time remaining */
663 if ((get_ball_count() >= 1) &&
664 (get_cob_count() >= 1))
671 /* get tomatoes near our goals (12,5 and 12,3) */
672 uint8_t get_opp_oranges(void)
678 DEBUG(E_USER_STRAT, "%s()", __FUNCTION__);
680 /* only if oranges are present */
681 if (strat_db.opp_oranges_count == 0)
684 strat_db.opp_oranges_count = 0;
685 x = position_get_x_s16(&mainboard.pos);
686 y = position_get_y_s16(&mainboard.pos);
688 if (xycoord_to_ijcoord_not_corn(&x, &y, &i, &j) < 0)
691 /* not on eject point */
692 if (i != 11 || j != 6)
696 strat_set_speed(SPEED_DIST_FAST, SPEED_ANGLE_FAST);
698 /* turn in the correct direction */
699 trajectory_a_abs(&mainboard.traj, COLOR_A(-90));
700 err = wait_traj_end(TRAJ_FLAGS_NO_NEAR);
701 if (!TRAJ_SUCCESS(err))
704 trajectory_goto_forward_xy_abs(&mainboard.traj, 2625, COLOR_Y(597));
705 err = wait_traj_end(TRAJ_FLAGS_STD);
706 if (!TRAJ_SUCCESS(err))
709 strat_set_speed(SPEED_DIST_SLOW, SPEED_ANGLE_SLOW);
710 trajectory_goto_forward_xy_abs(&mainboard.traj, 2750, COLOR_Y(250));
711 err = wait_traj_end(TRAJ_FLAGS_NO_NEAR);
712 if (!TRAJ_SUCCESS(err))
715 err = run_to_the_hills(get_opponent_color());
722 /* get tomatoes near our goals (12,5 and 12,3) */
723 uint8_t get_orphan_tomatoes(void)
725 #define CLITOID_TOMATO_RADIUS 100.
726 #define TOMATO_BACK_X 2780
727 #define TOMATO_BACK_LEN 200
734 DEBUG(E_USER_STRAT, "%s()", __FUNCTION__);
736 /* only go if both tomatoes are present */
737 if (!strat_db.wp_table[12][5].present ||
738 !strat_db.wp_table[12][3].present) {
742 x = position_get_x_s16(&mainboard.pos);
743 y = position_get_y_s16(&mainboard.pos);
745 if (xycoord_to_ijcoord_not_corn(&x, &y, &i, &j) < 0)
748 /* not on eject point */
749 if (i != 11 || j != 6)
753 strat_set_speed(SPEED_CLITOID_FAST, SPEED_ANGLE_SLOW);
755 /* turn in the correct direction */
756 trajectory_a_abs(&mainboard.traj, COLOR_A(-90));
757 err = wait_traj_end(TRAJ_FLAGS_NO_NEAR);
759 /* clitoid to turn and take the first ball */
760 ret = trajectory_clitoid(&mainboard.traj, 2625, COLOR_Y(1847),
761 COLOR_A(-90), 150., COLOR_A(90), 0,
762 CLITOID_TOMATO_RADIUS, 3*125);
768 err = wait_traj_end(TRAJ_FLAGS_NO_NEAR);
769 if (!TRAJ_SUCCESS(err))
772 strat_set_speed(SPEED_CLITOID_SLOW, SPEED_ANGLE_SLOW);
773 err = strat_calib(300, END_TRAJ|END_BLOCKING);
774 a = position_get_a_deg_s16(&mainboard.pos);
776 strat_reset_pos(AREA_X - ROBOT_HALF_LENGTH_FRONT,
778 COLOR_A(0) + ROBOT_ANGLE_FRONT);
780 strat_set_speed(SPEED_CLITOID_FAST, SPEED_ANGLE_FAST);
781 trajectory_d_rel(&mainboard.traj, -250);
782 err = WAIT_COND_OR_TRAJ_END(!x_is_more_than(TOMATO_BACK_X),
785 if (err != 0 && !TRAJ_SUCCESS(err))
788 trajectory_d_a_rel(&mainboard.traj, -TOMATO_BACK_LEN, COLOR_A(-90));
789 err = wait_traj_end(TRAJ_FLAGS_NO_NEAR);
793 /* clitoid to turn and take the first ball */
794 strat_set_speed(SPEED_CLITOID_FAST, SPEED_ANGLE_SLOW);
795 ret = trajectory_clitoid(&mainboard.traj, 2625, COLOR_Y(1847),
796 COLOR_A(-90), 150., COLOR_A(90), 0,
797 CLITOID_TOMATO_RADIUS, 7*125);
802 err = wait_traj_end(TRAJ_FLAGS_NO_NEAR);
803 if (!TRAJ_SUCCESS(err))
807 strat_set_speed(SPEED_CLITOID_FAST, SPEED_ANGLE_FAST);
808 trajectory_d_rel(&mainboard.traj, -250);
809 err = WAIT_COND_OR_TRAJ_END(!x_is_more_than(TOMATO_BACK_X),
811 trajectory_d_a_rel(&mainboard.traj, -TOMATO_BACK_LEN, COLOR_A(-90));
812 err = wait_traj_end(TRAJ_FLAGS_NO_NEAR);
820 #define HILL_LEN 1000
823 #define HILL_POSY_YELLOW 310
824 #define HILL_POSY_BLUE 190
826 #define HILL_POSX_BALLS_DOWN1 830
827 #define HILL_POSX_BALLS_DOWN2 920
828 #define HILL_POSX_BALLS_DOWN3 730
829 #define HILL_START_POSX 580
831 uint8_t prepare_hill(uint8_t orange_color, int16_t posx)
833 int16_t startx, starty;
834 uint8_t our_color = get_color();
837 strat_set_speed(SPEED_DIST_SLOW, SPEED_ANGLE_SLOW);
838 if (orange_color == I2C_COLOR_YELLOW)
839 starty = HILL_POSY_YELLOW;
841 starty = HILL_POSY_BLUE;
842 if (orange_color == our_color)
845 startx = AREA_X - posx;
846 trajectory_goto_forward_xy_abs(&mainboard.traj, startx, COLOR_Y(starty));
847 err = wait_traj_end(TRAJ_FLAGS_SMALL_DIST);
848 if (!TRAJ_SUCCESS(err))
851 /* turn to the hills */
852 if (orange_color == our_color)
853 trajectory_a_abs(&mainboard.traj, COLOR_A(HILL_ANGLE));
855 trajectory_a_abs(&mainboard.traj, COLOR_A(-180+HILL_ANGLE));
856 err = wait_traj_end(TRAJ_FLAGS_SMALL_DIST);
857 if (!TRAJ_SUCCESS(err))
863 /* get oranges, must be called near game area */
864 uint8_t run_to_the_hills(uint8_t orange_color)
869 uint8_t our_color = get_color();
870 int32_t p = pid_get_gain_P(&mainboard.angle.pid);
871 int32_t i = pid_get_gain_I(&mainboard.angle.pid);
872 int32_t d = pid_get_gain_D(&mainboard.angle.pid);
873 int32_t max_in = pid_get_max_in(&mainboard.angle.pid);
874 int32_t max_i = pid_get_max_I(&mainboard.angle.pid);
875 int32_t max_out = pid_get_max_out(&mainboard.angle.pid);
878 strat_get_acc(&ad, &aa);
879 strat_get_speed(&sd, &sa);
881 DEBUG(E_USER_STRAT, "%s()", __FUNCTION__);
883 strat_set_speed(SPEED_DIST_FAST, SPEED_ANGLE_FAST);
884 err = prepare_hill(orange_color, HILL_START_POSX);
885 if (!TRAJ_SUCCESS(err))
888 strat_set_acc(5, ACC_ANGLE);
889 strat_set_speed(300, SPEED_ANGLE_SLOW);
890 bd_set_current_thresholds(&mainboard.distance.bd, 500, 8000, 2000000, 80);
891 bd_set_current_thresholds(&mainboard.angle.bd, 500, 8000, 2000000, 80);
892 bd_set_speed_threshold(&mainboard.distance.bd, 10);
893 support_balls_pack();
895 /* decrease angle gains */
896 pid_set_gains(&mainboard.angle.pid, 200, 0, 2000);
898 /* here it is difficult to handle return values, because we
900 i2c_cobboard_set_mode(I2C_COBBOARD_MODE_KICKSTAND_DOWN);
901 trajectory_d_rel(&mainboard.traj, HILL_LEN);
902 if (orange_color == our_color)
903 err = WAIT_COND_OR_TRAJ_END(position_get_x_s16(&mainboard.pos) >
904 HILL_POSX_BALLS_DOWN1,
905 TRAJ_FLAGS_SMALL_DIST);
907 err = WAIT_COND_OR_TRAJ_END(position_get_x_s16(&mainboard.pos) <
908 AREA_X - HILL_POSX_BALLS_DOWN1,
909 TRAJ_FLAGS_SMALL_DIST);
910 DEBUG(E_USER_STRAT, "deploy support balls");
911 i2c_ballboard_set_mode(I2C_BALLBOARD_MODE_PREP_FORK);
912 support_balls_deploy();
913 i2c_cobboard_set_mode(I2C_COBBOARD_MODE_KICKSTAND_UP);
914 trajectory_only_a_rel(&mainboard.traj, 2);
915 err = WAIT_COND_OR_TE_TO(0, 0, 2200);
917 i2c_cobboard_set_mode(I2C_COBBOARD_MODE_KICKSTAND_DOWN);
920 i2c_ballboard_set_mode(I2C_BALLBOARD_MODE_TAKE_FORK);
924 trajectory_d_rel(&mainboard.traj, 15);
929 /* reach top, go down */
930 trajectory_d_rel(&mainboard.traj, -HILL_LEN);
932 if (orange_color == our_color)
933 err = WAIT_COND_OR_TRAJ_END(position_get_x_s16(&mainboard.pos) <
934 HILL_POSX_BALLS_DOWN2,
935 TRAJ_FLAGS_SMALL_DIST);
937 err = WAIT_COND_OR_TRAJ_END(position_get_x_s16(&mainboard.pos) >
938 AREA_X - HILL_POSX_BALLS_DOWN2,
939 TRAJ_FLAGS_SMALL_DIST);
940 DEBUG(E_USER_STRAT, "pack support balls");
941 support_balls_pack();
942 if (orange_color == our_color)
943 err = WAIT_COND_OR_TRAJ_END(position_get_x_s16(&mainboard.pos) <
944 HILL_POSX_BALLS_DOWN3,
945 TRAJ_FLAGS_SMALL_DIST);
947 err = WAIT_COND_OR_TRAJ_END(position_get_x_s16(&mainboard.pos) >
948 AREA_X - HILL_POSX_BALLS_DOWN3,
949 TRAJ_FLAGS_SMALL_DIST);
951 DEBUG(E_USER_STRAT, "deploy support balls");
952 strat_set_acc(ad, aa);
953 strat_set_speed(SPEED_DIST_FAST, SPEED_ANGLE_FAST);
954 support_balls_deploy();
956 /* wait to be near the wall */
957 if (orange_color == our_color)
958 err = WAIT_COND_OR_TRAJ_END(position_get_x_s16(&mainboard.pos) < 300,
959 TRAJ_FLAGS_SMALL_DIST);
961 err = WAIT_COND_OR_TRAJ_END(position_get_x_s16(&mainboard.pos) >
963 TRAJ_FLAGS_SMALL_DIST);
965 /* restore BD coefs */
966 bd_set_current_thresholds(&mainboard.distance.bd, 500, 8000, 1000000, 20);
967 bd_set_current_thresholds(&mainboard.distance.bd, 500, 8000, 1000000, 20);
968 bd_set_speed_threshold(&mainboard.distance.bd, 60);
970 /* calibrate position on the wall */
971 strat_set_speed(SPEED_DIST_VERY_SLOW, SPEED_ANGLE_SLOW);
973 i2c_cobboard_set_mode(I2C_COBBOARD_MODE_HARVEST);
975 trajectory_a_abs(&mainboard.traj, COLOR_A(-90));
976 err = wait_traj_end(TRAJ_FLAGS_NO_NEAR);
979 err = strat_calib(400, TRAJ_FLAGS_SMALL_DIST);
980 strat_reset_pos(DO_NOT_SET_POS,
981 COLOR_Y(ROBOT_HALF_LENGTH_FRONT),
982 COLOR_A(-90) + ROBOT_ANGLE_FRONT);
983 strat_set_speed(SPEED_DIST_FAST, SPEED_ANGLE_FAST);
985 trajectory_d_rel(&mainboard.traj, -250);
986 err = wait_traj_end(TRAJ_FLAGS_NO_NEAR);
987 if (orange_color == our_color)
988 trajectory_a_abs(&mainboard.traj, 180);
990 trajectory_a_abs(&mainboard.traj, 0);
991 err = wait_traj_end(TRAJ_FLAGS_NO_NEAR);
994 strat_set_speed(SPEED_DIST_VERY_SLOW, SPEED_ANGLE_FAST);
995 err = strat_calib(400, TRAJ_FLAGS_SMALL_DIST);
996 if (orange_color == our_color)
997 strat_reset_pos(ROBOT_HALF_LENGTH_FRONT,
999 180 + ROBOT_ANGLE_FRONT);
1001 strat_reset_pos(AREA_X - ROBOT_HALF_LENGTH_FRONT,
1003 0 + ROBOT_ANGLE_FRONT);
1004 strat_set_speed(SPEED_DIST_FAST, SPEED_ANGLE_FAST);
1006 trajectory_d_rel(&mainboard.traj, -250);
1007 err = wait_traj_end(TRAJ_FLAGS_NO_NEAR);
1009 /* revert acceleration and speed */
1010 pid_set_gains(&mainboard.angle.pid, p, i, d);
1011 pid_set_maximums(&mainboard.distance.pid, max_in, max_i, max_out);
1012 strat_want_pack = 0;
1013 strat_set_speed(sd, sa);
1014 support_balls_deploy();
1018 uint8_t strat_main(void)
1020 uint8_t err, do_initturn = 1;
1023 if (strat_conf.flags & STRAT_CONF_OUR_ORANGE) {
1024 err = run_to_the_hills(get_color());
1025 strat_db.our_oranges_count = 0;
1029 /* harvest the first cobs + balls */
1030 if (strat_conf.flags & STRAT_CONF_STRAIGHT_BEGIN)
1031 err = strat_beginning2(do_initturn);
1033 err = strat_beginning(do_initturn);
1035 if (!TRAJ_SUCCESS(err))
1038 err = strat_eject();
1040 /* choose circuit, and harvest on it */
1043 DEBUG(E_USER_STRAT, "start main loop");
1045 /* if it's time to get tomatoes, do it */
1046 if (time_get_s() > strat_conf.orphan_tomato) {
1047 err = get_orphan_tomatoes();
1048 if (err == END_ERROR) {
1050 "get_orphan_tomatoes returned END_ERROR");
1052 else if (err == END_TIMER) {
1053 DEBUG(E_USER_STRAT, "End of time");
1057 else if (!TRAJ_SUCCESS(err)) {
1058 /* don't retry these tomatoes if it failed */
1059 strat_conf.orphan_tomato = 90;
1064 /* if it's time to get opponent oranges, do it */
1065 if (time_get_s() > strat_conf.opp_orange) {
1066 err = get_opp_oranges();
1067 if (err == END_ERROR) {
1069 "get_opp_oranges returned END_ERROR");
1071 else if (err == END_TIMER) {
1072 DEBUG(E_USER_STRAT, "End of time");
1076 else if (!TRAJ_SUCCESS(err)) {
1077 /* don't retry oranges if it failed */
1078 strat_conf.opp_orange = 90;
1083 /**********************/
1084 /* harvest on circuit */
1085 /**********************/
1087 err = strat_harvest_circuit();
1088 if (err == END_TIMER) {
1089 DEBUG(E_USER_STRAT, "End of time");
1093 if (!TRAJ_SUCCESS(err)) {
1098 /***********************/
1099 /* eject game elements */
1100 /***********************/
1102 err = strat_eject();
1103 /* end of time exit ! */
1104 if (err == END_TIMER) {
1105 DEBUG(E_USER_STRAT, "End of time");
1109 if (!TRAJ_SUCCESS(err)) {