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 static void remove_cob(uint8_t idx)
228 if (strat_db.corn_table[idx]->corn.color == I2C_COB_BLACK)
231 if (strat_db.corn_table[idx]->time_removed == -1) {
232 strat_db.corn_table[idx]->time_removed = time_get_s();
234 cobboard.cob_count ++;
235 printf("add cob %d\n", idx);
240 /* mark corn as not present and give correct commands to the cobboard
242 static void check_corn(void)
245 int8_t lcob_near, rcob_near;
248 static uint8_t prev_check_time;
250 uint8_t need_lpack, need_rpack;
251 int16_t l_xspickle, l_yspickle;
252 int16_t r_xspickle, r_yspickle;
253 uint8_t l_y_too_high_pack = 0, r_y_too_high_pack = 0;
255 /* read sensors from ballboard */
257 lcob = ballboard.lcob;
258 ballboard.lcob = I2C_COB_NONE;
259 rcob = ballboard.rcob;
260 ballboard.rcob = I2C_COB_NONE;
263 /* check present cobs once per second */
264 cur_time = time_get_s();
265 if (cur_time != prev_check_time) {
268 /* only useful for simu */
269 for (i = 0; i < CORN_NB; i++) {
270 if (strat_db.corn_table[i]->present == 1 &&
271 strat_db.corn_table[i]->time_removed != -1 &&
272 strat_db.corn_table[i]->time_removed + 2 <= time_get_s()) {
274 printf("remove cob %d\n", i);
276 strat_db.corn_table[i]->present = 0;
279 prev_check_time = cur_time;
282 /* detect cob on left side */
283 lcob_near = corn_is_near(&lidx, I2C_LEFT_SIDE,
284 &l_xspickle, &l_yspickle);
285 if (lcob_near && lcob != I2C_COB_NONE) {
286 if (strat_db.corn_table[lidx]->corn.color == I2C_COB_UNKNOWN)
287 DEBUG(E_USER_STRAT, "lcob %s %d",
288 lcob == I2C_COB_WHITE ? "white" : "black", lidx);
289 corn_set_color(strat_db.corn_table[lidx], lcob);
291 if (cur_time > 5 && !__y_is_more_than(l_yspickle, 600))
292 l_y_too_high_pack = 1;
294 /* detect cob on right side */
295 rcob_near = corn_is_near(&ridx, I2C_RIGHT_SIDE,
296 &r_xspickle, &r_yspickle);
297 if (rcob_near && rcob != I2C_COB_NONE) {
298 if (strat_db.corn_table[ridx]->corn.color == I2C_COB_UNKNOWN)
299 DEBUG(E_USER_STRAT, "rcob %s %d",
300 rcob == I2C_COB_WHITE ? "white" : "black", ridx);
301 corn_set_color(strat_db.corn_table[ridx], rcob);
303 if (cur_time > 5 && !__y_is_more_than(r_yspickle, 600))
304 r_y_too_high_pack = 1;
306 /* re-enable white cob */
307 if (lcob == I2C_COB_WHITE && lcob_near &&
308 strat_db.corn_table[lidx]->present == 0) {
309 strat_db.corn_table[lidx]->present = 1;
310 strat_db.corn_table[lidx]->time_removed = -1;
312 if (rcob == I2C_COB_WHITE && rcob_near &&
313 strat_db.corn_table[ridx]->present == 0) {
314 strat_db.corn_table[ridx]->present = 1;
315 strat_db.corn_table[ridx]->time_removed = -1;
318 /* control the cobboard mode for left spickle */
319 need_lpack = get_cob_count() >= 5 || strat_want_pack ||
320 strat_lpack60 || strat_opponent_lpack || l_y_too_high_pack;
323 (strat_db.corn_table[lidx]->present ||
324 strat_db.corn_table[lidx]->time_removed == -1)) {
329 /* deploy spickle and harvest white ones */
330 if (strat_db.corn_table[lidx]->corn.color == I2C_COB_WHITE)
331 i2c_cobboard_autoharvest_nomove(I2C_LEFT_SIDE);
333 i2c_cobboard_deploy_nomove(I2C_LEFT_SIDE);
338 /* no cob near us, we can pack or deploy freely */
340 i2c_cobboard_pack_weak(I2C_LEFT_SIDE);
342 i2c_cobboard_deploy(I2C_LEFT_SIDE);
345 /* control the cobboard mode for right spickle */
346 need_rpack = get_cob_count() >= 5 || strat_want_pack ||
347 strat_rpack60 || strat_opponent_rpack || r_y_too_high_pack;
349 (strat_db.corn_table[ridx]->present ||
350 strat_db.corn_table[ridx]->time_removed == -1)) {
355 /* deploy spickle and harvest white ones */
356 if (strat_db.corn_table[ridx]->corn.color == I2C_COB_WHITE)
357 i2c_cobboard_autoharvest_nomove(I2C_RIGHT_SIDE);
359 i2c_cobboard_deploy_nomove(I2C_RIGHT_SIDE);
364 /* no cob near us, we can pack or deploy freely */
366 i2c_cobboard_pack_weak(I2C_RIGHT_SIDE);
368 i2c_cobboard_deploy(I2C_RIGHT_SIDE);
372 /* check opponent position */
373 void check_opponent(void)
375 int16_t opp_x, opp_y;
376 int16_t opp_d, opp_a;
379 strat_opponent_lpack = 0;
380 strat_opponent_rpack = 0;
382 if (get_opponent_xyda(&opp_x, &opp_y, &opp_d, &opp_a) < 0)
385 /* pack spickles if opponent too close */
387 if (opp_a > 45 && opp_a < 135)
388 strat_opponent_lpack = 1;
389 if (opp_a > 225 && opp_a < 315)
390 strat_opponent_rpack = 1;
393 /* check for oranges after 5 seconds */
394 if (time_get_s() > 5) {
395 if (mainboard.our_color == I2C_COLOR_YELLOW) {
396 if (opp_y < 500 && opp_x < 500)
397 strat_db.our_oranges_count = 0;
398 if (opp_y < 500 && opp_x > AREA_X - 500)
399 strat_db.opp_oranges_count = 0;
402 if (opp_y > AREA_Y - 500 && opp_x < 500)
403 strat_db.our_oranges_count = 0;
404 if (opp_y > AREA_Y - 500 && opp_x > AREA_X - 500)
405 strat_db.opp_oranges_count = 0;
409 /* malus for some tomatoes and cobs, visited by opponent */
410 if (xycoord_to_ijcoord(&opp_x, &opp_y, &i, &j) < 0)
413 strat_db.wp_table[i][j].opp_visited = 1;
416 /* called periodically (10ms) */
417 void strat_event(void *dummy)
419 /* ignore when strat is not running */
420 if (strat_running == 0)
427 /* limit speed when opponent is near */
428 /* disabled for 2010, we are already slow :) */
429 //strat_limit_speed();
432 /* check that we are on an eject line */
433 static uint8_t robot_is_on_eject_line(void)
438 x = position_get_x_s16(&mainboard.pos);
439 y = position_get_y_s16(&mainboard.pos);
441 if (xycoord_to_ijcoord_not_corn(&x, &y, &i, &j) < 0)
444 if (!wp_belongs_to_line(i, j, 5, LINE_UP) &&
445 !wp_belongs_to_line(i, j, 2, LINE_R_UP))
451 /* 0 = fast, 1 = slow */
452 static uint8_t eject_select_speed(void)
457 x = position_get_x_s16(&mainboard.pos);
458 y = position_get_y_s16(&mainboard.pos);
460 if (get_cob_count() >= 5) {
465 if (xycoord_to_ijcoord_not_corn(&x, &y, &i, &j) < 0) {
466 DEBUG(E_USER_STRAT, "%s(): cannot find waypoint at %d,%d",
471 if (corn_count_neigh(i, j) == 2)
477 /* called multiple times while we are waiting to reach the ejection
479 static uint8_t speedify_eject(void)
481 if (eject_select_speed())
482 strat_set_speed(SPEED_CLITOID_SLOW, SPEED_ANGLE_SLOW);
484 strat_set_speed(SPEED_CLITOID_FAST, SPEED_ANGLE_SLOW);
488 /* must be called from a terminal line */
489 static uint8_t strat_eject(void)
493 DEBUG(E_USER_STRAT, "%s() cob_count=%d ball_count=%d",
494 __FUNCTION__, get_cob_count(), get_ball_count());
496 /* check that we are called from an eject line */
497 if (!robot_is_on_eject_line()) {
498 DEBUG(E_USER_STRAT, "%s() not on eject line", __FUNCTION__);
502 /* go to eject point */
503 trajectory_goto_forward_xy_abs(&mainboard.traj, 2625, COLOR_Y(1847));
504 err = WAIT_COND_OR_TRAJ_END(speedify_eject(),
506 /* err is never == 0 because speedify_eject() always return 0 */
507 if (!TRAJ_SUCCESS(err))
510 /* pack arms (force), and disable strat_event */
511 strat_event_disable();
512 i2c_cobboard_pack_weak(I2C_LEFT_SIDE);
513 i2c_cobboard_pack_weak(I2C_RIGHT_SIDE);
516 if (get_ball_count() > 0) {
517 i2c_ballboard_set_mode(I2C_BALLBOARD_MODE_EJECT);
519 trajectory_a_abs(&mainboard.traj, COLOR_A(70));
520 err = wait_traj_end(TRAJ_FLAGS_NO_NEAR);
521 if (!TRAJ_SUCCESS(err))
524 DEBUG(E_USER_STRAT, "%s():%d", __FUNCTION__, __LINE__);
529 WAIT_COND_OR_TIMEOUT(ballboard.status == I2C_BALLBOARD_STATUS_F_READY,
534 /* to pack spickles */
538 if (get_cob_count() > 0 ||
539 cobboard.status == I2C_COBBOARD_STATUS_LBUSY ||
540 cobboard.status == I2C_COBBOARD_STATUS_RBUSY) {
542 trajectory_a_abs(&mainboard.traj, COLOR_A(-110));
543 err = wait_traj_end(END_INTR|END_TRAJ);
544 if (!TRAJ_SUCCESS(err))
548 trajectory_d_rel(&mainboard.traj, -70);
549 err = wait_traj_end(END_INTR|END_TRAJ);
550 if (!TRAJ_SUCCESS(err))
553 i2c_cobboard_set_mode(I2C_COBBOARD_MODE_EJECT);
558 trajectory_d_rel(&mainboard.traj, 70);
559 err = wait_traj_end(END_INTR|END_TRAJ);
560 if (!TRAJ_SUCCESS(err))
564 strat_db_dump(__FUNCTION__);
568 strat_event_enable();
573 static uint8_t strat_beginning(uint8_t do_initturn)
577 strat_set_acc(ACC_DIST, ACC_ANGLE);
580 //strat_set_speed(600, 60);
581 //strat_set_speed(450, 50);
582 strat_set_speed(350, 40);
583 trajectory_d_a_rel(&mainboard.traj, 1000, COLOR_A(20));
584 err = WAIT_COND_OR_TRAJ_END(trajectory_angle_finished(&mainboard.traj),
588 strat_set_acc(ACC_DIST, ACC_ANGLE);
589 strat_set_speed(SPEED_CLITOID_SLOW, SPEED_ANGLE_SLOW);
592 err = line2line(0, LINE_UP, 2, LINE_R_DOWN, TRAJ_FLAGS_NO_NEAR);
593 if (err == END_OBSTACLE &&
594 strat_conf.flags & STRAT_CONF_WAIT_OBSTACLE &&
595 time_get_s() > strat_conf.prev_wait_obstacle + 5) {
596 strat_conf.prev_wait_obstacle = time_get_s();
600 if (!TRAJ_SUCCESS(err))
604 err = line2line(2, LINE_R_DOWN, 2, LINE_R_UP, TRAJ_FLAGS_NO_NEAR);
605 if (err == END_OBSTACLE &&
606 strat_conf.flags & STRAT_CONF_WAIT_OBSTACLE &&
607 time_get_s() > strat_conf.prev_wait_obstacle + 5) {
608 strat_conf.prev_wait_obstacle = time_get_s();
612 if (!TRAJ_SUCCESS(err)) {
619 static uint8_t strat_beginning2(uint8_t do_initturn)
623 strat_set_acc(ACC_DIST, ACC_ANGLE);
626 strat_set_speed(600, 95); /* OK */
627 trajectory_d_a_rel(&mainboard.traj, 1000, COLOR_A(-40));
628 err = WAIT_COND_OR_TRAJ_END(trajectory_angle_finished(&mainboard.traj),
634 trajectory_goto_forward_xy_abs(&mainboard.traj,
636 err = wait_traj_end(TRAJ_FLAGS_STD);
641 /* dump state (every 5 s max) */
642 #define DUMP_RATE_LIMIT(dump, last_print) \
644 if (time_get_s() - last_print > 5) { \
646 last_print = time_get_s(); \
652 /* return true if we need to grab some more elements */
653 static uint8_t need_more_elements(void)
655 if (time_get_s() <= 75) {
656 /* we have enough time left */
657 if (get_ball_count() >= 4)
659 if (get_cob_count() >= 4)
661 if ((get_ball_count() >= 2) &&
662 (get_cob_count() >= 2))
667 /* not much time remaining */
668 if ((get_ball_count() >= 1) &&
669 (get_cob_count() >= 1))
676 /* get tomatoes near our goals (12,5 and 12,3) */
677 uint8_t get_opp_oranges(void)
683 DEBUG(E_USER_STRAT, "%s()", __FUNCTION__);
685 /* only if oranges are present */
686 if (strat_db.opp_oranges_count == 0)
689 strat_db.opp_oranges_count = 0;
690 x = position_get_x_s16(&mainboard.pos);
691 y = position_get_y_s16(&mainboard.pos);
693 if (xycoord_to_ijcoord_not_corn(&x, &y, &i, &j) < 0)
696 /* not on eject point */
697 if (i != 11 || j != 6)
701 strat_set_speed(SPEED_DIST_FAST, SPEED_ANGLE_FAST);
703 /* turn in the correct direction */
704 trajectory_a_abs(&mainboard.traj, COLOR_A(-90));
705 err = wait_traj_end(TRAJ_FLAGS_NO_NEAR);
706 if (!TRAJ_SUCCESS(err))
709 trajectory_goto_forward_xy_abs(&mainboard.traj, 2625, COLOR_Y(597));
710 err = wait_traj_end(TRAJ_FLAGS_STD);
711 if (!TRAJ_SUCCESS(err))
714 strat_set_speed(SPEED_DIST_SLOW, SPEED_ANGLE_SLOW);
715 trajectory_goto_forward_xy_abs(&mainboard.traj, 2750, COLOR_Y(250));
716 err = wait_traj_end(TRAJ_FLAGS_NO_NEAR);
717 if (!TRAJ_SUCCESS(err))
720 err = run_to_the_hills(get_opponent_color());
727 /* get tomatoes near our goals (12,5 and 12,3) */
728 uint8_t get_orphan_tomatoes(void)
730 #define CLITOID_TOMATO_RADIUS 100.
731 #define TOMATO_BACK_X 2780
732 #define TOMATO_BACK_LEN 200
739 DEBUG(E_USER_STRAT, "%s()", __FUNCTION__);
741 /* only go if both tomatoes are present */
742 if (!strat_db.wp_table[12][5].present ||
743 !strat_db.wp_table[12][3].present) {
747 x = position_get_x_s16(&mainboard.pos);
748 y = position_get_y_s16(&mainboard.pos);
750 if (xycoord_to_ijcoord_not_corn(&x, &y, &i, &j) < 0)
753 /* not on eject point */
754 if (i != 11 || j != 6)
758 strat_set_speed(SPEED_CLITOID_FAST, SPEED_ANGLE_SLOW);
760 /* turn in the correct direction */
761 trajectory_a_abs(&mainboard.traj, COLOR_A(-90));
762 err = wait_traj_end(TRAJ_FLAGS_NO_NEAR);
764 /* clitoid to turn and take the first ball */
765 ret = trajectory_clitoid(&mainboard.traj, 2625, COLOR_Y(1847),
766 COLOR_A(-90), 150., COLOR_A(90), 0,
767 CLITOID_TOMATO_RADIUS, 3*125);
773 err = wait_traj_end(TRAJ_FLAGS_NO_NEAR);
774 if (!TRAJ_SUCCESS(err))
777 strat_set_speed(SPEED_CLITOID_SLOW, SPEED_ANGLE_SLOW);
778 err = strat_calib(300, END_TRAJ|END_BLOCKING);
779 a = position_get_a_deg_s16(&mainboard.pos);
781 strat_reset_pos(AREA_X - ROBOT_HALF_LENGTH_FRONT,
783 COLOR_A(0) + ROBOT_ANGLE_FRONT);
785 strat_set_speed(SPEED_CLITOID_FAST, SPEED_ANGLE_FAST);
786 trajectory_d_rel(&mainboard.traj, -250);
787 err = WAIT_COND_OR_TRAJ_END(!x_is_more_than(TOMATO_BACK_X),
790 if (err != 0 && !TRAJ_SUCCESS(err))
793 trajectory_d_a_rel(&mainboard.traj, -TOMATO_BACK_LEN, COLOR_A(-90));
794 err = wait_traj_end(TRAJ_FLAGS_NO_NEAR);
798 /* clitoid to turn and take the first ball */
799 strat_set_speed(SPEED_CLITOID_FAST, SPEED_ANGLE_SLOW);
800 ret = trajectory_clitoid(&mainboard.traj, 2625, COLOR_Y(1847),
801 COLOR_A(-90), 150., COLOR_A(90), 0,
802 CLITOID_TOMATO_RADIUS, 7*125);
807 err = wait_traj_end(TRAJ_FLAGS_NO_NEAR);
808 if (!TRAJ_SUCCESS(err))
812 strat_set_speed(SPEED_CLITOID_FAST, SPEED_ANGLE_FAST);
813 trajectory_d_rel(&mainboard.traj, -250);
814 err = WAIT_COND_OR_TRAJ_END(!x_is_more_than(TOMATO_BACK_X),
816 trajectory_d_a_rel(&mainboard.traj, -TOMATO_BACK_LEN, COLOR_A(-90));
817 err = wait_traj_end(TRAJ_FLAGS_NO_NEAR);
825 #define HILL_LEN 1000
828 #define HILL_POSY_YELLOW 310
829 #define HILL_POSY_BLUE 190
831 #define HILL_POSX_BALLS_DOWN1 830
832 #define HILL_POSX_BALLS_DOWN2 920
833 #define HILL_POSX_BALLS_DOWN3 730
834 #define HILL_START_POSX 580
836 uint8_t prepare_hill(uint8_t orange_color, int16_t posx)
838 int16_t startx, starty;
839 uint8_t our_color = get_color();
842 strat_set_speed(SPEED_DIST_SLOW, SPEED_ANGLE_SLOW);
843 if (orange_color == I2C_COLOR_YELLOW)
844 starty = HILL_POSY_YELLOW;
846 starty = HILL_POSY_BLUE;
847 if (orange_color == our_color)
850 startx = AREA_X - posx;
851 trajectory_goto_forward_xy_abs(&mainboard.traj, startx, COLOR_Y(starty));
852 err = wait_traj_end(TRAJ_FLAGS_SMALL_DIST);
853 if (!TRAJ_SUCCESS(err))
856 /* turn to the hills */
857 if (orange_color == our_color)
858 trajectory_a_abs(&mainboard.traj, COLOR_A(HILL_ANGLE));
860 trajectory_a_abs(&mainboard.traj, COLOR_A(-180+HILL_ANGLE));
861 err = wait_traj_end(TRAJ_FLAGS_SMALL_DIST);
862 if (!TRAJ_SUCCESS(err))
868 /* get oranges, must be called near game area */
869 uint8_t run_to_the_hills(uint8_t orange_color)
874 uint8_t our_color = get_color();
875 int32_t p = pid_get_gain_P(&mainboard.angle.pid);
876 int32_t i = pid_get_gain_I(&mainboard.angle.pid);
877 int32_t d = pid_get_gain_D(&mainboard.angle.pid);
878 int32_t max_in = pid_get_max_in(&mainboard.angle.pid);
879 int32_t max_i = pid_get_max_I(&mainboard.angle.pid);
880 int32_t max_out = pid_get_max_out(&mainboard.angle.pid);
883 strat_get_acc(&ad, &aa);
884 strat_get_speed(&sd, &sa);
886 DEBUG(E_USER_STRAT, "%s()", __FUNCTION__);
888 strat_set_speed(SPEED_DIST_FAST, SPEED_ANGLE_FAST);
889 err = prepare_hill(orange_color, HILL_START_POSX);
890 if (!TRAJ_SUCCESS(err))
893 strat_set_acc(5, ACC_ANGLE);
894 strat_set_speed(300, SPEED_ANGLE_SLOW);
895 bd_set_current_thresholds(&mainboard.distance.bd, 500, 8000, 2000000, 80);
896 bd_set_current_thresholds(&mainboard.angle.bd, 500, 8000, 2000000, 80);
897 bd_set_speed_threshold(&mainboard.distance.bd, 10);
898 support_balls_pack();
900 /* decrease angle gains */
901 pid_set_gains(&mainboard.angle.pid, 200, 0, 2000);
903 /* here it is difficult to handle return values, because we
905 i2c_cobboard_set_mode(I2C_COBBOARD_MODE_KICKSTAND_DOWN);
906 trajectory_d_rel(&mainboard.traj, HILL_LEN);
907 if (orange_color == our_color)
908 err = WAIT_COND_OR_TRAJ_END(position_get_x_s16(&mainboard.pos) >
909 HILL_POSX_BALLS_DOWN1,
910 TRAJ_FLAGS_SMALL_DIST);
912 err = WAIT_COND_OR_TRAJ_END(position_get_x_s16(&mainboard.pos) <
913 AREA_X - HILL_POSX_BALLS_DOWN1,
914 TRAJ_FLAGS_SMALL_DIST);
915 DEBUG(E_USER_STRAT, "deploy support balls");
916 i2c_ballboard_set_mode(I2C_BALLBOARD_MODE_PREP_FORK);
917 support_balls_deploy();
918 i2c_cobboard_set_mode(I2C_COBBOARD_MODE_KICKSTAND_UP);
919 trajectory_only_a_rel(&mainboard.traj, 2);
920 err = WAIT_COND_OR_TE_TO(0, 0, 2200);
922 i2c_cobboard_set_mode(I2C_COBBOARD_MODE_KICKSTAND_DOWN);
925 i2c_ballboard_set_mode(I2C_BALLBOARD_MODE_TAKE_FORK);
929 trajectory_d_rel(&mainboard.traj, 15);
934 /* reach top, go down */
935 trajectory_d_a_rel(&mainboard.traj, -HILL_LEN, -2);
937 if (orange_color == our_color)
938 err = WAIT_COND_OR_TRAJ_END(position_get_x_s16(&mainboard.pos) <
939 HILL_POSX_BALLS_DOWN2,
940 TRAJ_FLAGS_SMALL_DIST);
942 err = WAIT_COND_OR_TRAJ_END(position_get_x_s16(&mainboard.pos) >
943 AREA_X - HILL_POSX_BALLS_DOWN2,
944 TRAJ_FLAGS_SMALL_DIST);
945 DEBUG(E_USER_STRAT, "pack support balls");
946 support_balls_pack();
947 if (orange_color == our_color)
948 err = WAIT_COND_OR_TRAJ_END(position_get_x_s16(&mainboard.pos) <
949 HILL_POSX_BALLS_DOWN3,
950 TRAJ_FLAGS_SMALL_DIST);
952 err = WAIT_COND_OR_TRAJ_END(position_get_x_s16(&mainboard.pos) >
953 AREA_X - HILL_POSX_BALLS_DOWN3,
954 TRAJ_FLAGS_SMALL_DIST);
956 DEBUG(E_USER_STRAT, "deploy support balls");
957 support_balls_deploy();
959 if (orange_color == I2C_COLOR_YELLOW) {
960 strat_set_acc(ad, aa);
961 strat_set_speed(SPEED_DIST_FAST, SPEED_ANGLE_FAST);
964 strat_set_acc(ad, 0.4);
965 strat_set_speed(SPEED_DIST_SLOW, SPEED_ANGLE_SLOW);
966 trajectory_d_a_rel(&mainboard.traj, -500, 20);
969 /* wait to be near the wall */
970 if (orange_color == our_color)
971 err = WAIT_COND_OR_TRAJ_END(position_get_x_s16(&mainboard.pos) < 300,
972 TRAJ_FLAGS_SMALL_DIST);
974 err = WAIT_COND_OR_TRAJ_END(position_get_x_s16(&mainboard.pos) >
976 TRAJ_FLAGS_SMALL_DIST);
978 strat_set_acc(ad, aa);
979 strat_set_speed(SPEED_DIST_FAST, SPEED_ANGLE_FAST);
981 /* restore BD coefs */
982 bd_set_current_thresholds(&mainboard.distance.bd, 500, 8000, 1000000, 20);
983 bd_set_current_thresholds(&mainboard.distance.bd, 500, 8000, 1000000, 20);
984 bd_set_speed_threshold(&mainboard.distance.bd, 60);
986 /* calibrate position on the wall */
987 strat_set_speed(SPEED_DIST_VERY_SLOW, SPEED_ANGLE_SLOW);
989 i2c_cobboard_set_mode(I2C_COBBOARD_MODE_HARVEST);
991 trajectory_a_abs(&mainboard.traj, COLOR_A(-90));
992 err = wait_traj_end(TRAJ_FLAGS_NO_NEAR);
995 err = strat_calib(400, TRAJ_FLAGS_SMALL_DIST);
996 strat_reset_pos(DO_NOT_SET_POS,
997 COLOR_Y(ROBOT_HALF_LENGTH_FRONT),
998 COLOR_A(-90) + ROBOT_ANGLE_FRONT);
999 strat_set_speed(SPEED_DIST_FAST, SPEED_ANGLE_FAST);
1001 trajectory_d_rel(&mainboard.traj, -250);
1002 err = wait_traj_end(TRAJ_FLAGS_NO_NEAR);
1004 if (orange_color == our_color)
1005 trajectory_a_abs(&mainboard.traj, 180);
1007 trajectory_a_abs(&mainboard.traj, 0);
1008 err = wait_traj_end(TRAJ_FLAGS_NO_NEAR);
1011 strat_set_speed(SPEED_DIST_VERY_SLOW, SPEED_ANGLE_FAST);
1012 err = strat_calib(400, TRAJ_FLAGS_SMALL_DIST);
1013 if (orange_color == our_color)
1014 strat_reset_pos(ROBOT_HALF_LENGTH_FRONT,
1016 180 + ROBOT_ANGLE_FRONT);
1018 strat_reset_pos(AREA_X - ROBOT_HALF_LENGTH_FRONT,
1020 0 + ROBOT_ANGLE_FRONT);
1021 strat_set_speed(SPEED_DIST_FAST, SPEED_ANGLE_FAST);
1023 trajectory_d_rel(&mainboard.traj, -250);
1024 err = wait_traj_end(TRAJ_FLAGS_NO_NEAR);
1026 /* revert acceleration and speed */
1027 pid_set_gains(&mainboard.angle.pid, p, i, d);
1028 pid_set_maximums(&mainboard.distance.pid, max_in, max_i, max_out);
1029 strat_want_pack = 0;
1030 strat_set_speed(sd, sa);
1031 support_balls_deploy();
1035 uint8_t strat_main(void)
1037 uint8_t err, do_initturn = 1;
1040 if (strat_conf.flags & STRAT_CONF_OUR_ORANGE) {
1041 err = run_to_the_hills(get_color());
1042 strat_db.our_oranges_count = 0;
1046 /* harvest the first cobs + balls */
1047 if (strat_conf.flags & STRAT_CONF_STRAIGHT_BEGIN)
1048 err = strat_beginning2(do_initturn);
1050 err = strat_beginning(do_initturn);
1052 if (!TRAJ_SUCCESS(err))
1055 err = strat_eject();
1057 /* choose circuit, and harvest on it */
1060 DEBUG(E_USER_STRAT, "start main loop");
1062 /* if it's time to get tomatoes, do it */
1063 if (time_get_s() > strat_conf.orphan_tomato) {
1064 err = get_orphan_tomatoes();
1065 if (err == END_ERROR) {
1067 "get_orphan_tomatoes returned END_ERROR");
1069 else if (err == END_TIMER) {
1070 DEBUG(E_USER_STRAT, "End of time");
1074 else if (!TRAJ_SUCCESS(err)) {
1075 /* don't retry these tomatoes if it failed */
1076 strat_conf.orphan_tomato = 90;
1081 /* if it's time to get opponent oranges, do it */
1082 if (time_get_s() > strat_conf.opp_orange) {
1083 err = get_opp_oranges();
1084 if (err == END_ERROR) {
1086 "get_opp_oranges returned END_ERROR");
1088 else if (err == END_TIMER) {
1089 DEBUG(E_USER_STRAT, "End of time");
1093 else if (!TRAJ_SUCCESS(err)) {
1094 /* don't retry oranges if it failed */
1095 strat_conf.opp_orange = 90;
1100 /**********************/
1101 /* harvest on circuit */
1102 /**********************/
1104 err = strat_harvest_circuit();
1105 if (err == END_TIMER) {
1106 DEBUG(E_USER_STRAT, "End of time");
1110 if (!TRAJ_SUCCESS(err)) {
1115 /***********************/
1116 /* eject game elements */
1117 /***********************/
1119 err = strat_eject();
1120 /* end of time exit ! */
1121 if (err == END_TIMER) {
1122 DEBUG(E_USER_STRAT, "End of time");
1126 if (!TRAJ_SUCCESS(err)) {