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;
238 /* read sensors from ballboard */
240 lcob = ballboard.lcob;
241 ballboard.lcob = I2C_COB_NONE;
242 rcob = ballboard.rcob;
243 ballboard.rcob = I2C_COB_NONE;
246 /* check present cobs once per second */
247 cur_time = time_get_s();
248 if (cur_time != prev_check_time) {
251 /* only useful for simu */
252 for (i = 0; i < CORN_NB; i++) {
253 if (strat_db.corn_table[i]->present == 1 &&
254 strat_db.corn_table[i]->time_removed != -1 &&
255 strat_db.corn_table[i]->time_removed + 2 <= time_get_s()) {
257 printf("remove cob %d\n", i);
259 strat_db.corn_table[i]->present = 0;
262 prev_check_time = cur_time;
265 /* detect cob on left side */
266 lcob_near = corn_is_near(&lidx, I2C_LEFT_SIDE);
267 if (lcob_near && lcob != I2C_COB_NONE) {
268 if (strat_db.corn_table[lidx]->corn.color == I2C_COB_UNKNOWN)
269 DEBUG(E_USER_STRAT, "lcob %s %d",
270 lcob == I2C_COB_WHITE ? "white" : "black", lidx);
271 corn_set_color(strat_db.corn_table[lidx], lcob);
274 /* detect cob on right side */
275 rcob_near = corn_is_near(&ridx, I2C_RIGHT_SIDE);
276 if (rcob_near && rcob != I2C_COB_NONE) {
277 if (strat_db.corn_table[ridx]->corn.color == I2C_COB_UNKNOWN)
278 DEBUG(E_USER_STRAT, "rcob %s %d",
279 rcob == I2C_COB_WHITE ? "white" : "black", ridx);
280 corn_set_color(strat_db.corn_table[ridx], rcob);
283 /* control the cobboard mode for left spickle */
284 need_lpack = get_cob_count() >= 5 || strat_want_pack ||
285 strat_lpack60 || strat_opponent_lpack;
286 if (lcob_near && strat_db.corn_table[lidx]->present) {
291 /* deploy spickle and harvest white ones */
292 if (strat_db.corn_table[lidx]->corn.color == I2C_COB_WHITE) {
293 i2c_cobboard_autoharvest_nomove(I2C_LEFT_SIDE);
294 if (strat_db.corn_table[lidx]->time_removed == -1
296 && cobboard.status == I2C_COBBOARD_STATUS_LBUSY
299 strat_db.corn_table[lidx]->time_removed = time_get_s();
301 cobboard.cob_count ++;
302 printf("add cob %d\n", lidx);
304 strat_db.corn_table[lidx]->present = 0;
309 i2c_cobboard_deploy_nomove(I2C_LEFT_SIDE);
313 /* no cob near us, we can pack or deploy freely */
315 i2c_cobboard_pack_weak(I2C_LEFT_SIDE);
317 i2c_cobboard_deploy(I2C_LEFT_SIDE);
320 /* control the cobboard mode for right spickle */
321 need_rpack = get_cob_count() >= 5 || strat_want_pack ||
322 strat_rpack60 || strat_opponent_rpack;
323 if (rcob_near && strat_db.corn_table[ridx]->present) {
328 /* deploy spickle and harvest white ones */
329 if (strat_db.corn_table[ridx]->corn.color == I2C_COB_WHITE) {
330 i2c_cobboard_autoharvest_nomove(I2C_RIGHT_SIDE);
331 if (strat_db.corn_table[ridx]->time_removed == -1
333 && cobboard.status == I2C_COBBOARD_STATUS_RBUSY
336 strat_db.corn_table[ridx]->time_removed = time_get_s();
338 cobboard.cob_count ++;
339 printf("add cob %d\n", ridx);
341 strat_db.corn_table[ridx]->present = 0;
346 i2c_cobboard_deploy_nomove(I2C_RIGHT_SIDE);
350 /* no cob near us, we can pack or deploy freely */
352 i2c_cobboard_pack_weak(I2C_RIGHT_SIDE);
354 i2c_cobboard_deploy(I2C_RIGHT_SIDE);
358 /* check opponent position */
359 void check_opponent(void)
361 int16_t opp_x, opp_y;
362 int16_t opp_d, opp_a;
365 strat_opponent_lpack = 0;
366 strat_opponent_rpack = 0;
368 if (get_opponent_xyda(&opp_x, &opp_y, &opp_d, &opp_a) < 0)
371 /* pack spickles if opponent too close */
373 if (opp_a > 45 && opp_a < 135)
374 strat_opponent_lpack = 1;
375 if (opp_a > 225 && opp_a < 315)
376 strat_opponent_rpack = 1;
379 /* check for oranges after 5 seconds */
380 if (time_get_s() > 5) {
381 if (mainboard.our_color == I2C_COLOR_YELLOW) {
382 if (opp_y < 500 && opp_x < 500)
383 strat_db.our_oranges_count = 0;
384 if (opp_y < 500 && opp_x > AREA_X - 500)
385 strat_db.opp_oranges_count = 0;
388 if (opp_y > AREA_Y - 500 && opp_x < 500)
389 strat_db.our_oranges_count = 0;
390 if (opp_y > AREA_Y - 500 && opp_x > AREA_X - 500)
391 strat_db.opp_oranges_count = 0;
395 /* malus for some tomatoes and cobs, visited by opponent */
396 if (xycoord_to_ijcoord(&opp_x, &opp_y, &i, &j) < 0)
399 strat_db.wp_table[i][j].opp_visited = 1;
402 /* called periodically (10ms) */
403 void strat_event(void *dummy)
405 /* ignore when strat is not running */
406 if (strat_running == 0)
413 /* limit speed when opponent is near */
414 /* disabled for 2010, we are already slow :) */
415 //strat_limit_speed();
418 /* check that we are on an eject line */
419 static uint8_t robot_is_on_eject_line(void)
424 x = position_get_x_s16(&mainboard.pos);
425 y = position_get_y_s16(&mainboard.pos);
427 if (xycoord_to_ijcoord_not_corn(&x, &y, &i, &j) < 0)
430 if (!wp_belongs_to_line(i, j, 5, LINE_UP) &&
431 !wp_belongs_to_line(i, j, 2, LINE_R_UP))
437 /* 0 = fast, 1 = slow */
438 static uint8_t eject_select_speed(void)
443 x = position_get_x_s16(&mainboard.pos);
444 y = position_get_y_s16(&mainboard.pos);
446 if (get_cob_count() >= 5) {
451 if (xycoord_to_ijcoord_not_corn(&x, &y, &i, &j) < 0) {
452 DEBUG(E_USER_STRAT, "%s(): cannot find waypoint at %d,%d",
457 if (corn_count_neigh(i, j) == 2)
463 /* called multiple times while we are waiting to reach the ejection
465 static uint8_t speedify_eject(void)
467 if (eject_select_speed())
468 strat_set_speed(SPEED_CLITOID_SLOW, SPEED_ANGLE_SLOW);
470 strat_set_speed(SPEED_CLITOID_FAST, SPEED_ANGLE_SLOW);
474 /* must be called from a terminal line */
475 static uint8_t strat_eject(void)
479 DEBUG(E_USER_STRAT, "%s() cob_count=%d ball_count=%d",
480 __FUNCTION__, get_cob_count(), get_ball_count());
482 /* check that we are called from an eject line */
483 if (!robot_is_on_eject_line()) {
484 DEBUG(E_USER_STRAT, "%s() not on eject line", __FUNCTION__);
488 /* go to eject point */
489 trajectory_goto_forward_xy_abs(&mainboard.traj, 2625, COLOR_Y(1847));
490 err = WAIT_COND_OR_TRAJ_END(speedify_eject(),
492 /* err is never == 0 because speedify_eject() always return 0 */
493 if (!TRAJ_SUCCESS(err))
496 /* pack arms (force), and disable strat_event */
497 strat_event_disable();
498 i2c_cobboard_pack_weak(I2C_LEFT_SIDE);
499 i2c_cobboard_pack_weak(I2C_RIGHT_SIDE);
502 if (get_ball_count() > 0) {
503 i2c_ballboard_set_mode(I2C_BALLBOARD_MODE_EJECT);
504 trajectory_a_abs(&mainboard.traj, COLOR_A(70));
505 err = wait_traj_end(TRAJ_FLAGS_NO_NEAR);
506 if (!TRAJ_SUCCESS(err))
509 DEBUG(E_USER_STRAT, "%s():%d", __FUNCTION__, __LINE__);
514 WAIT_COND_OR_TIMEOUT(ballboard.status == I2C_BALLBOARD_STATUS_F_BUSY,
516 WAIT_COND_OR_TIMEOUT(ballboard.status == I2C_BALLBOARD_STATUS_F_READY,
521 if (get_cob_count() > 0) {
523 trajectory_a_abs(&mainboard.traj, COLOR_A(-110));
524 err = wait_traj_end(END_INTR|END_TRAJ);
525 if (!TRAJ_SUCCESS(err))
529 trajectory_d_rel(&mainboard.traj, -70);
530 err = wait_traj_end(END_INTR|END_TRAJ);
531 if (!TRAJ_SUCCESS(err))
534 i2c_cobboard_set_mode(I2C_COBBOARD_MODE_EJECT);
539 trajectory_d_rel(&mainboard.traj, 70);
540 err = wait_traj_end(END_INTR|END_TRAJ);
541 if (!TRAJ_SUCCESS(err))
545 strat_db_dump(__FUNCTION__);
549 strat_event_enable();
554 static uint8_t strat_beginning(uint8_t do_initturn)
558 strat_set_acc(ACC_DIST, ACC_ANGLE);
561 //strat_set_speed(600, 60);
562 strat_set_speed(450, 50);
563 trajectory_d_a_rel(&mainboard.traj, 1000, COLOR_A(20));
564 err = WAIT_COND_OR_TRAJ_END(trajectory_angle_finished(&mainboard.traj),
568 strat_set_acc(ACC_DIST, ACC_ANGLE);
569 strat_set_speed(SPEED_CLITOID_SLOW, SPEED_ANGLE_SLOW);
572 err = line2line(0, LINE_UP, 2, LINE_R_DOWN, TRAJ_FLAGS_NO_NEAR);
573 if (err == END_OBSTACLE &&
574 strat_conf.flags & STRAT_CONF_WAIT_OBSTACLE &&
575 time_get_s() > strat_conf.prev_wait_obstacle + 5) {
576 strat_conf.prev_wait_obstacle = time_get_s();
580 if (!TRAJ_SUCCESS(err))
584 err = line2line(2, LINE_R_DOWN, 2, LINE_R_UP, TRAJ_FLAGS_NO_NEAR);
585 if (err == END_OBSTACLE &&
586 strat_conf.flags & STRAT_CONF_WAIT_OBSTACLE &&
587 time_get_s() > strat_conf.prev_wait_obstacle + 5) {
588 strat_conf.prev_wait_obstacle = time_get_s();
592 if (!TRAJ_SUCCESS(err)) {
599 static uint8_t strat_beginning2(uint8_t do_initturn)
603 strat_set_acc(ACC_DIST, ACC_ANGLE);
606 strat_set_speed(600, 95); /* OK */
607 trajectory_d_a_rel(&mainboard.traj, 1000, COLOR_A(-40));
608 err = WAIT_COND_OR_TRAJ_END(trajectory_angle_finished(&mainboard.traj),
614 trajectory_goto_forward_xy_abs(&mainboard.traj,
616 err = wait_traj_end(TRAJ_FLAGS_STD);
621 /* dump state (every 5 s max) */
622 #define DUMP_RATE_LIMIT(dump, last_print) \
624 if (time_get_s() - last_print > 5) { \
626 last_print = time_get_s(); \
632 /* return true if we need to grab some more elements */
633 static uint8_t need_more_elements(void)
635 if (time_get_s() <= 75) {
636 /* we have enough time left */
637 if (get_ball_count() >= 4)
639 if (get_cob_count() >= 4)
641 if ((get_ball_count() >= 2) &&
642 (get_cob_count() >= 2))
647 /* not much time remaining */
648 if ((get_ball_count() >= 1) &&
649 (get_cob_count() >= 1))
656 /* get tomatoes near our goals (12,5 and 12,3) */
657 uint8_t get_opp_oranges(void)
663 DEBUG(E_USER_STRAT, "%s()", __FUNCTION__);
665 /* only if oranges are present */
666 if (strat_db.opp_oranges_count == 0)
669 strat_db.opp_oranges_count = 0;
670 x = position_get_x_s16(&mainboard.pos);
671 y = position_get_y_s16(&mainboard.pos);
673 if (xycoord_to_ijcoord_not_corn(&x, &y, &i, &j) < 0)
676 /* not on eject point */
677 if (i != 11 || j != 6)
681 strat_set_speed(SPEED_DIST_FAST, SPEED_ANGLE_FAST);
683 /* turn in the correct direction */
684 trajectory_a_abs(&mainboard.traj, COLOR_A(-90));
685 err = wait_traj_end(TRAJ_FLAGS_NO_NEAR);
686 if (!TRAJ_SUCCESS(err))
689 trajectory_goto_forward_xy_abs(&mainboard.traj, 2625, COLOR_Y(597));
690 err = wait_traj_end(TRAJ_FLAGS_STD);
691 if (!TRAJ_SUCCESS(err))
694 strat_set_speed(SPEED_DIST_SLOW, SPEED_ANGLE_SLOW);
695 trajectory_goto_forward_xy_abs(&mainboard.traj, 2750, COLOR_Y(250));
696 err = wait_traj_end(TRAJ_FLAGS_NO_NEAR);
697 if (!TRAJ_SUCCESS(err))
700 err = run_to_the_hills(get_opponent_color());
707 /* get tomatoes near our goals (12,5 and 12,3) */
708 uint8_t get_orphan_tomatoes(void)
710 #define CLITOID_TOMATO_RADIUS 100.
711 #define TOMATO_BACK_X 2780
712 #define TOMATO_BACK_LEN 200
719 DEBUG(E_USER_STRAT, "%s()", __FUNCTION__);
721 /* only go if both tomatoes are present */
722 if (!strat_db.wp_table[12][5].present ||
723 !strat_db.wp_table[12][3].present) {
727 x = position_get_x_s16(&mainboard.pos);
728 y = position_get_y_s16(&mainboard.pos);
730 if (xycoord_to_ijcoord_not_corn(&x, &y, &i, &j) < 0)
733 /* not on eject point */
734 if (i != 11 || j != 6)
738 strat_set_speed(SPEED_CLITOID_FAST, SPEED_ANGLE_SLOW);
740 /* turn in the correct direction */
741 trajectory_a_abs(&mainboard.traj, COLOR_A(-90));
742 err = wait_traj_end(TRAJ_FLAGS_NO_NEAR);
744 /* clitoid to turn and take the first ball */
745 ret = trajectory_clitoid(&mainboard.traj, 2625, COLOR_Y(1847),
746 COLOR_A(-90), 150., COLOR_A(90), 0,
747 CLITOID_TOMATO_RADIUS, 3*125);
753 err = wait_traj_end(TRAJ_FLAGS_NO_NEAR);
754 if (!TRAJ_SUCCESS(err))
757 strat_set_speed(SPEED_CLITOID_SLOW, SPEED_ANGLE_SLOW);
758 err = strat_calib(300, END_TRAJ|END_BLOCKING);
759 a = position_get_a_deg_s16(&mainboard.pos);
761 strat_reset_pos(AREA_X - ROBOT_HALF_LENGTH_FRONT,
763 COLOR_A(0) + ROBOT_ANGLE_FRONT);
765 strat_set_speed(SPEED_CLITOID_FAST, SPEED_ANGLE_FAST);
766 trajectory_d_rel(&mainboard.traj, -250);
767 err = WAIT_COND_OR_TRAJ_END(!x_is_more_than(TOMATO_BACK_X),
770 if (err != 0 && !TRAJ_SUCCESS(err))
773 trajectory_d_a_rel(&mainboard.traj, -TOMATO_BACK_LEN, COLOR_A(-90));
774 err = wait_traj_end(TRAJ_FLAGS_NO_NEAR);
778 /* clitoid to turn and take the first ball */
779 strat_set_speed(SPEED_CLITOID_FAST, SPEED_ANGLE_SLOW);
780 ret = trajectory_clitoid(&mainboard.traj, 2625, COLOR_Y(1847),
781 COLOR_A(-90), 150., COLOR_A(90), 0,
782 CLITOID_TOMATO_RADIUS, 7*125);
787 err = wait_traj_end(TRAJ_FLAGS_NO_NEAR);
788 if (!TRAJ_SUCCESS(err))
792 strat_set_speed(SPEED_CLITOID_FAST, SPEED_ANGLE_FAST);
793 trajectory_d_rel(&mainboard.traj, -250);
794 err = WAIT_COND_OR_TRAJ_END(!x_is_more_than(TOMATO_BACK_X),
796 trajectory_d_a_rel(&mainboard.traj, -TOMATO_BACK_LEN, COLOR_A(-90));
797 err = wait_traj_end(TRAJ_FLAGS_NO_NEAR);
805 #define HILL_LEN 1000
808 #define HILL_POSY_YELLOW 310
809 #define HILL_POSY_BLUE 190
811 #define HILL_POSX_BALLS_DOWN1 830
812 #define HILL_POSX_BALLS_DOWN2 920
813 #define HILL_POSX_BALLS_DOWN3 730
814 #define HILL_START_POSX 580
816 uint8_t prepare_hill(uint8_t orange_color, int16_t posx)
818 int16_t startx, starty;
819 uint8_t our_color = get_color();
822 strat_set_speed(SPEED_DIST_SLOW, SPEED_ANGLE_SLOW);
823 if (orange_color == I2C_COLOR_YELLOW)
824 starty = HILL_POSY_YELLOW;
826 starty = HILL_POSY_BLUE;
827 if (orange_color == our_color)
830 startx = AREA_X - posx;
831 trajectory_goto_forward_xy_abs(&mainboard.traj, startx, COLOR_Y(starty));
832 err = wait_traj_end(TRAJ_FLAGS_SMALL_DIST);
833 if (!TRAJ_SUCCESS(err))
836 /* turn to the hills */
837 if (orange_color == our_color)
838 trajectory_a_abs(&mainboard.traj, COLOR_A(HILL_ANGLE));
840 trajectory_a_abs(&mainboard.traj, COLOR_A(-180+HILL_ANGLE));
841 err = wait_traj_end(TRAJ_FLAGS_SMALL_DIST);
842 if (!TRAJ_SUCCESS(err))
848 /* get oranges, must be called near game area */
849 uint8_t run_to_the_hills(uint8_t orange_color)
854 uint8_t our_color = get_color();
855 int32_t p = pid_get_gain_P(&mainboard.angle.pid);
856 int32_t i = pid_get_gain_I(&mainboard.angle.pid);
857 int32_t d = pid_get_gain_D(&mainboard.angle.pid);
858 int32_t max_in = pid_get_max_in(&mainboard.angle.pid);
859 int32_t max_i = pid_get_max_I(&mainboard.angle.pid);
860 int32_t max_out = pid_get_max_out(&mainboard.angle.pid);
863 strat_get_acc(&ad, &aa);
864 strat_get_speed(&sd, &sa);
866 DEBUG(E_USER_STRAT, "%s()", __FUNCTION__);
868 strat_set_speed(SPEED_DIST_FAST, SPEED_ANGLE_FAST);
869 err = prepare_hill(orange_color, HILL_START_POSX);
870 if (!TRAJ_SUCCESS(err))
873 strat_set_acc(5, ACC_ANGLE);
874 strat_set_speed(300, SPEED_ANGLE_SLOW);
875 bd_set_current_thresholds(&mainboard.distance.bd, 500, 8000, 2000000, 80);
876 bd_set_current_thresholds(&mainboard.angle.bd, 500, 8000, 2000000, 80);
877 bd_set_speed_threshold(&mainboard.distance.bd, 10);
878 support_balls_pack();
880 /* decrease angle gains */
881 pid_set_gains(&mainboard.angle.pid, 200, 0, 2000);
883 /* here it is difficult to handle return values, because we
885 i2c_cobboard_set_mode(I2C_COBBOARD_MODE_KICKSTAND_DOWN);
886 trajectory_d_rel(&mainboard.traj, HILL_LEN);
887 if (orange_color == our_color)
888 err = WAIT_COND_OR_TRAJ_END(position_get_x_s16(&mainboard.pos) >
889 HILL_POSX_BALLS_DOWN1,
890 TRAJ_FLAGS_SMALL_DIST);
892 err = WAIT_COND_OR_TRAJ_END(position_get_x_s16(&mainboard.pos) <
893 AREA_X - HILL_POSX_BALLS_DOWN1,
894 TRAJ_FLAGS_SMALL_DIST);
895 DEBUG(E_USER_STRAT, "deploy support balls");
896 i2c_ballboard_set_mode(I2C_BALLBOARD_MODE_PREP_FORK);
897 support_balls_deploy();
898 i2c_cobboard_set_mode(I2C_COBBOARD_MODE_KICKSTAND_UP);
899 trajectory_only_a_rel(&mainboard.traj, 2);
900 err = WAIT_COND_OR_TE_TO(0, 0, 2200);
902 i2c_cobboard_set_mode(I2C_COBBOARD_MODE_KICKSTAND_DOWN);
905 i2c_ballboard_set_mode(I2C_BALLBOARD_MODE_TAKE_FORK);
909 /* reach top, go down */
910 trajectory_d_rel(&mainboard.traj, -HILL_LEN);
912 if (orange_color == our_color)
913 err = WAIT_COND_OR_TRAJ_END(position_get_x_s16(&mainboard.pos) <
914 HILL_POSX_BALLS_DOWN2,
915 TRAJ_FLAGS_SMALL_DIST);
917 err = WAIT_COND_OR_TRAJ_END(position_get_x_s16(&mainboard.pos) >
918 AREA_X - HILL_POSX_BALLS_DOWN2,
919 TRAJ_FLAGS_SMALL_DIST);
920 DEBUG(E_USER_STRAT, "pack support balls");
921 support_balls_pack();
922 if (orange_color == our_color)
923 err = WAIT_COND_OR_TRAJ_END(position_get_x_s16(&mainboard.pos) <
924 HILL_POSX_BALLS_DOWN3,
925 TRAJ_FLAGS_SMALL_DIST);
927 err = WAIT_COND_OR_TRAJ_END(position_get_x_s16(&mainboard.pos) >
928 AREA_X - HILL_POSX_BALLS_DOWN3,
929 TRAJ_FLAGS_SMALL_DIST);
931 DEBUG(E_USER_STRAT, "deploy support balls");
932 strat_set_acc(ad, aa);
933 strat_set_speed(SPEED_DIST_FAST, SPEED_ANGLE_FAST);
934 support_balls_deploy();
936 /* wait to be near the wall */
937 if (orange_color == our_color)
938 err = WAIT_COND_OR_TRAJ_END(position_get_x_s16(&mainboard.pos) < 300,
939 TRAJ_FLAGS_SMALL_DIST);
941 err = WAIT_COND_OR_TRAJ_END(position_get_x_s16(&mainboard.pos) >
943 TRAJ_FLAGS_SMALL_DIST);
945 /* restore BD coefs */
946 bd_set_current_thresholds(&mainboard.distance.bd, 500, 8000, 1000000, 20);
947 bd_set_current_thresholds(&mainboard.distance.bd, 500, 8000, 1000000, 20);
948 bd_set_speed_threshold(&mainboard.distance.bd, 60);
950 /* calibrate position on the wall */
951 strat_set_speed(SPEED_DIST_VERY_SLOW, SPEED_ANGLE_SLOW);
953 i2c_cobboard_set_mode(I2C_COBBOARD_MODE_HARVEST);
955 trajectory_a_abs(&mainboard.traj, COLOR_A(-90));
956 err = wait_traj_end(TRAJ_FLAGS_NO_NEAR);
959 err = strat_calib(400, TRAJ_FLAGS_SMALL_DIST);
960 strat_reset_pos(DO_NOT_SET_POS,
961 COLOR_Y(ROBOT_HALF_LENGTH_FRONT),
962 COLOR_A(-90) + ROBOT_ANGLE_FRONT);
963 strat_set_speed(SPEED_DIST_FAST, SPEED_ANGLE_FAST);
965 trajectory_d_rel(&mainboard.traj, -250);
966 err = wait_traj_end(TRAJ_FLAGS_NO_NEAR);
967 if (orange_color == our_color)
968 trajectory_a_abs(&mainboard.traj, 180);
970 trajectory_a_abs(&mainboard.traj, 0);
971 err = wait_traj_end(TRAJ_FLAGS_NO_NEAR);
974 strat_set_speed(SPEED_DIST_VERY_SLOW, SPEED_ANGLE_FAST);
975 err = strat_calib(400, TRAJ_FLAGS_SMALL_DIST);
976 if (orange_color == our_color)
977 strat_reset_pos(ROBOT_HALF_LENGTH_FRONT,
979 180 + ROBOT_ANGLE_FRONT);
981 strat_reset_pos(AREA_X - ROBOT_HALF_LENGTH_FRONT,
983 0 + ROBOT_ANGLE_FRONT);
984 strat_set_speed(SPEED_DIST_FAST, SPEED_ANGLE_FAST);
986 trajectory_d_rel(&mainboard.traj, -250);
987 err = wait_traj_end(TRAJ_FLAGS_NO_NEAR);
989 /* revert acceleration and speed */
990 pid_set_gains(&mainboard.angle.pid, p, i, d);
991 pid_set_maximums(&mainboard.distance.pid, max_in, max_i, max_out);
993 strat_set_speed(sd, sa);
994 support_balls_deploy();
998 uint8_t strat_main(void)
1000 uint8_t err, do_initturn = 1;
1003 if (strat_conf.flags & STRAT_CONF_OUR_ORANGE) {
1004 err = run_to_the_hills(get_color());
1005 strat_db.our_oranges_count = 0;
1009 /* harvest the first cobs + balls */
1010 if (strat_conf.flags & STRAT_CONF_STRAIGHT_BEGIN)
1011 err = strat_beginning2(do_initturn);
1013 err = strat_beginning(do_initturn);
1015 if (!TRAJ_SUCCESS(err))
1018 err = strat_eject();
1020 /* choose circuit, and harvest on it */
1023 DEBUG(E_USER_STRAT, "start main loop");
1025 /* if it's time to get tomatoes, do it */
1026 if (time_get_s() > strat_conf.orphan_tomato) {
1027 err = get_orphan_tomatoes();
1028 if (err == END_ERROR) {
1030 "get_orphan_tomatoes returned END_ERROR");
1032 else if (err == END_TIMER) {
1033 DEBUG(E_USER_STRAT, "End of time");
1037 else if (!TRAJ_SUCCESS(err)) {
1038 /* don't retry these tomatoes if it failed */
1039 strat_conf.orphan_tomato = 90;
1044 /* if it's time to get opponent oranges, do it */
1045 if (time_get_s() > strat_conf.opp_orange) {
1046 err = get_opp_oranges();
1047 if (err == END_ERROR) {
1049 "get_opp_oranges returned END_ERROR");
1051 else if (err == END_TIMER) {
1052 DEBUG(E_USER_STRAT, "End of time");
1056 else if (!TRAJ_SUCCESS(err)) {
1057 /* don't retry oranges if it failed */
1058 strat_conf.opp_orange = 90;
1063 /**********************/
1064 /* harvest on circuit */
1065 /**********************/
1067 err = strat_harvest_circuit();
1068 if (err == END_TIMER) {
1069 DEBUG(E_USER_STRAT, "End of time");
1073 if (!TRAJ_SUCCESS(err)) {
1078 /***********************/
1079 /* eject game elements */
1080 /***********************/
1082 err = strat_eject();
1083 /* end of time exit ! */
1084 if (err == END_TIMER) {
1085 DEBUG(E_USER_STRAT, "End of time");
1089 if (!TRAJ_SUCCESS(err)) {