2 * Copyright Droids, Microb Technology (2009)
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 * Revision : $Id: strat.c,v 1.6 2009-11-08 17:24:33 zer0 Exp $
20 * Olivier MATZ <zer0@droids-corp.org>
28 #include <aversive/pgmspace.h>
29 #include <aversive/queue.h>
30 #include <aversive/wait.h>
31 #include <aversive/error.h>
36 #include <clock_time.h>
41 #include <control_system_manager.h>
42 #include <trajectory_manager.h>
43 #include <trajectory_manager_utils.h>
44 #include <trajectory_manager_core.h>
45 #include <vect_base.h>
48 #include <obstacle_avoidance.h>
49 #include <blocking_detection_manager.h>
50 #include <robot_system.h>
51 #include <position_manager.h>
53 #include <diagnostic.h>
58 #include "../common/i2c_commands.h"
59 #include "i2c_protocol.h"
64 #include "strat_base.h"
65 #include "strat_corn.h"
66 #include "strat_utils.h"
67 #include "strat_avoid.h"
71 #define COL_DISP_MARGIN 400 /* stop 40 cm in front of dispenser */
72 #define COL_SCAN_PRE_MARGIN 250
74 static volatile uint8_t strat_running = 0;
75 volatile uint8_t strat_want_pack = 0;
76 volatile uint8_t strat_lpack60 = 0;
77 volatile uint8_t strat_rpack60 = 0;
79 volatile uint8_t strat_opponent_lpack = 0;
80 volatile uint8_t strat_opponent_rpack = 0;
82 struct strat_conf strat_conf = {
89 /*************************************************************/
93 /*************************************************************/
95 /* called before each strat, and before the start switch */
96 void strat_preinit(void)
99 interrupt_traj_reset();
100 mainboard.flags = DO_ENCODERS | DO_CS | DO_RS |
101 DO_POS | DO_BD | DO_POWER;
104 strat_conf.prev_wait_obstacle = -5;
105 strat_conf_dump(__FUNCTION__);
106 strat_db_dump(__FUNCTION__);
109 void strat_conf_dump(const char *caller)
111 if (!strat_conf.dump_enabled)
114 printf_P(PSTR("-- conf --\r\n"));
115 printf_P(PSTR("our_orange = %s\n"),
116 (strat_conf.flags & STRAT_CONF_OUR_ORANGE) ? "y":"n");
117 printf_P(PSTR("wait_obstacle = %s\n"),
118 (strat_conf.flags & STRAT_CONF_WAIT_OBSTACLE) ? "y":"n");
119 printf_P(PSTR("opp_orange = %d\n"), strat_conf.opp_orange);
120 printf_P(PSTR("orphan_tomato = %d\n"), strat_conf.orphan_tomato);
123 void strat_event_enable(void)
128 void strat_event_disable(void)
133 /* call it just before launching the strat */
134 void strat_init(void)
137 position_set(&mainboard.pos, 298.16,
138 COLOR_Y(308.78), COLOR_A(70.00));
141 /* we consider that the color is correctly set */
143 strat_set_speed(SPEED_DIST_FAST, SPEED_ANGLE_FAST);
145 interrupt_traj_reset();
147 i2c_cobboard_set_mode(I2C_COBBOARD_MODE_HARVEST);
148 i2c_ballboard_set_mode(I2C_BALLBOARD_MODE_HARVEST);
150 /* used in strat_base for END_TIMER */
151 mainboard.flags = DO_ENCODERS | DO_CS | DO_RS |
152 DO_POS | DO_BD | DO_TIMER | DO_POWER;
155 /* call it after each strat */
156 void strat_exit(void)
163 mainboard.flags &= ~(DO_TIMER);
169 mainboard.flags &= ~(DO_CS);
171 pwm_ng_set(LEFT_PWM, 0);
172 pwm_ng_set(RIGHT_PWM, 0);
176 /* mark tomato as not present */
177 static void check_tomato(void)
181 static uint8_t prev_check_time;
184 /* check present tomatoes once per second */
185 cur_time = time_get_s();
186 if (cur_time != prev_check_time) {
189 for (k = 0; k < TOMATO_NB; k++) {
190 if (strat_db.tomato_table[k]->present == 1 &&
191 strat_db.tomato_table[k]->time_removed != -1 &&
192 strat_db.tomato_table[k]->time_removed + 2 <= time_get_s()) {
194 printf("remove tomato %d\n", k);
196 strat_db.tomato_table[k]->present = 0;
199 prev_check_time = cur_time;
202 x = position_get_x_s16(&mainboard.pos);
203 y = position_get_y_s16(&mainboard.pos);
205 if (xycoord_to_ijcoord(&x, &y, &i, &j) < 0)
208 if (strat_db.wp_table[i][j].type != WP_TYPE_TOMATO)
211 if (strat_db.wp_table[i][j].present == 0)
214 strat_db.wp_table[i][j].time_removed = time_get_s();
215 strat_db.wp_table[i][j].present = 0;
217 ballboard.ball_count ++;
218 printf("add ball %d,%d\n", i, j);
222 /* mark corn as not present and give correct commands to the cobboard
224 static void check_corn(void)
227 int8_t lcob_near, rcob_near;
230 static uint8_t prev_check_time;
232 uint8_t need_lpack, need_rpack;
234 /* read sensors from ballboard */
236 lcob = ballboard.lcob;
237 ballboard.lcob = I2C_COB_NONE;
238 rcob = ballboard.rcob;
239 ballboard.rcob = I2C_COB_NONE;
242 /* XXX take opponent position into account */
244 /* check present cobs once per second */
245 cur_time = time_get_s();
246 if (cur_time != prev_check_time) {
249 for (i = 0; i < CORN_NB; i++) {
250 if (strat_db.corn_table[i]->present == 1 &&
251 strat_db.corn_table[i]->time_removed != -1 &&
252 strat_db.corn_table[i]->time_removed + 2 <= time_get_s()) {
254 printf("remove cob %d\n", i);
256 strat_db.corn_table[i]->present = 0;
259 prev_check_time = cur_time;
262 /* detect cob on left side */
263 lcob_near = corn_is_near(&lidx, I2C_LEFT_SIDE);
264 if (lcob_near && lcob != I2C_COB_NONE) {
265 if (strat_db.corn_table[lidx]->corn.color == I2C_COB_UNKNOWN)
266 DEBUG(E_USER_STRAT, "lcob %s %d",
267 lcob == I2C_COB_WHITE ? "white" : "black", lidx);
268 corn_set_color(strat_db.corn_table[lidx], lcob);
271 /* detect cob on right side */
272 rcob_near = corn_is_near(&ridx, I2C_RIGHT_SIDE);
273 if (rcob_near && rcob != I2C_COB_NONE) {
274 if (strat_db.corn_table[ridx]->corn.color == I2C_COB_UNKNOWN)
275 DEBUG(E_USER_STRAT, "rcob %s %d",
276 rcob == I2C_COB_WHITE ? "white" : "black", ridx);
277 corn_set_color(strat_db.corn_table[ridx], rcob);
280 /* control the cobboard mode for left spickle */
281 need_lpack = get_cob_count() >= 5 || strat_want_pack ||
282 strat_lpack60 || strat_opponent_lpack;
283 if (lcob_near && strat_db.corn_table[lidx]->present) {
288 /* deploy spickle and harvest white ones */
289 if (strat_db.corn_table[lidx]->corn.color == I2C_COB_WHITE) {
290 i2c_cobboard_autoharvest_nomove(I2C_LEFT_SIDE);
291 if (strat_db.corn_table[lidx]->time_removed == -1
293 && cobboard.status == I2C_COBBOARD_STATUS_LBUSY
296 strat_db.corn_table[lidx]->time_removed = time_get_s();
298 cobboard.cob_count ++;
299 printf("add cob %d\n", lidx);
304 i2c_cobboard_deploy_nomove(I2C_LEFT_SIDE);
308 /* no cob near us, we can pack or deploy freely */
310 i2c_cobboard_pack_weak(I2C_LEFT_SIDE);
312 i2c_cobboard_deploy(I2C_LEFT_SIDE);
315 /* control the cobboard mode for right spickle */
316 need_rpack = get_cob_count() >= 5 || strat_want_pack ||
317 strat_rpack60 || strat_opponent_rpack;
318 if (rcob_near && strat_db.corn_table[ridx]->present) {
323 /* deploy spickle and harvest white ones */
324 if (strat_db.corn_table[ridx]->corn.color == I2C_COB_WHITE) {
325 i2c_cobboard_autoharvest_nomove(I2C_RIGHT_SIDE);
326 if (strat_db.corn_table[ridx]->time_removed == -1
328 && cobboard.status == I2C_COBBOARD_STATUS_RBUSY
331 strat_db.corn_table[ridx]->time_removed = time_get_s();
333 cobboard.cob_count ++;
334 printf("add cob %d\n", ridx);
339 i2c_cobboard_deploy_nomove(I2C_RIGHT_SIDE);
343 /* no cob near us, we can pack or deploy freely */
345 i2c_cobboard_pack_weak(I2C_RIGHT_SIDE);
347 i2c_cobboard_deploy(I2C_RIGHT_SIDE);
351 /* check opponent position */
352 void check_opponent(void)
354 int16_t opp_x, opp_y;
355 int16_t opp_d, opp_a;
358 strat_opponent_lpack = 0;
359 strat_opponent_rpack = 0;
361 if (get_opponent_xyda(&opp_x, &opp_y, &opp_d, &opp_a) < 0)
364 /* pack spickles if opponent too close */
366 if (opp_a > 45 && opp_a < 135)
367 strat_opponent_lpack = 1;
368 if (opp_a > 225 && opp_a < 315)
369 strat_opponent_rpack = 1;
372 /* check for oranges after 5 seconds */
373 if (time_get_s() > 5) {
374 if (mainboard.our_color == I2C_COLOR_YELLOW) {
375 if (opp_y < 500 && opp_x < 500)
376 strat_db.our_oranges_count = 0;
377 if (opp_y < 500 && opp_x > AREA_X - 500)
378 strat_db.opp_oranges_count = 0;
381 if (opp_y > AREA_Y - 500 && opp_x < 500)
382 strat_db.our_oranges_count = 0;
383 if (opp_y > AREA_Y - 500 && opp_x > AREA_X - 500)
384 strat_db.opp_oranges_count = 0;
388 /* malus for some tomatoes and cobs, visited by opponent */
389 if (xycoord_to_ijcoord(&opp_x, &opp_y, &i, &j) < 0)
392 strat_db.wp_table[i][j].opp_visited = 1;
395 /* called periodically (10ms) */
396 void strat_event(void *dummy)
398 /* ignore when strat is not running */
399 if (strat_running == 0)
406 /* limit speed when opponent is near */
407 /* disabled for 2010, we are already slow :) */
408 //strat_limit_speed();
411 /* check that we are on an eject line */
412 static uint8_t robot_is_on_eject_line(void)
417 x = position_get_x_s16(&mainboard.pos);
418 y = position_get_y_s16(&mainboard.pos);
420 if (xycoord_to_ijcoord_not_corn(&x, &y, &i, &j) < 0)
423 if (!wp_belongs_to_line(i, j, 5, LINE_UP) &&
424 !wp_belongs_to_line(i, j, 2, LINE_R_UP))
430 /* 0 = fast, 1 = slow */
431 static uint8_t eject_select_speed(void)
436 x = position_get_x_s16(&mainboard.pos);
437 y = position_get_y_s16(&mainboard.pos);
439 if (get_cob_count() >= 5) {
444 if (xycoord_to_ijcoord_not_corn(&x, &y, &i, &j) < 0) {
445 DEBUG(E_USER_STRAT, "%s(): cannot find waypoint at %d,%d",
450 if (corn_count_neigh(i, j) == 2)
456 /* called multiple times while we are waiting to reach the ejection
458 static uint8_t speedify_eject(void)
460 if (eject_select_speed())
461 strat_set_speed(SPEED_CLITOID_SLOW, SPEED_ANGLE_SLOW);
463 strat_set_speed(SPEED_CLITOID_FAST, SPEED_ANGLE_SLOW);
467 /* must be called from a terminal line */
468 static uint8_t strat_eject(void)
472 DEBUG(E_USER_STRAT, "%s() cob_count=%d ball_count=%d",
473 __FUNCTION__, get_cob_count(), get_ball_count());
475 /* check that we are called from an eject line */
476 if (!robot_is_on_eject_line()) {
477 DEBUG(E_USER_STRAT, "%s() not on eject line", __FUNCTION__);
481 /* go to eject point */
482 trajectory_goto_forward_xy_abs(&mainboard.traj, 2625, COLOR_Y(1847));
483 err = WAIT_COND_OR_TRAJ_END(speedify_eject(),
485 /* err is never == 0 because speedify_eject() always return 0 */
486 if (!TRAJ_SUCCESS(err))
489 /* pack arms (force), and disable strat_event */
490 strat_event_disable();
491 i2c_cobboard_pack_weak(I2C_LEFT_SIDE);
492 i2c_cobboard_pack_weak(I2C_RIGHT_SIDE);
495 if (get_ball_count() > 0) {
496 i2c_ballboard_set_mode(I2C_BALLBOARD_MODE_EJECT);
497 trajectory_a_abs(&mainboard.traj, COLOR_A(70));
498 err = wait_traj_end(TRAJ_FLAGS_NO_NEAR);
499 if (!TRAJ_SUCCESS(err))
502 DEBUG(E_USER_STRAT, "%s():%d", __FUNCTION__, __LINE__);
507 WAIT_COND_OR_TIMEOUT(ballboard.status == I2C_BALLBOARD_STATUS_F_BUSY,
509 WAIT_COND_OR_TIMEOUT(ballboard.status == I2C_BALLBOARD_STATUS_F_READY,
515 trajectory_a_abs(&mainboard.traj, COLOR_A(-110));
516 err = wait_traj_end(END_INTR|END_TRAJ);
517 if (!TRAJ_SUCCESS(err))
521 trajectory_d_rel(&mainboard.traj, -70);
522 err = wait_traj_end(END_INTR|END_TRAJ);
523 if (!TRAJ_SUCCESS(err))
526 if (get_cob_count() > 0) {
527 i2c_cobboard_set_mode(I2C_COBBOARD_MODE_EJECT);
532 trajectory_d_rel(&mainboard.traj, 70);
533 err = wait_traj_end(END_INTR|END_TRAJ);
534 if (!TRAJ_SUCCESS(err))
537 strat_db_dump(__FUNCTION__);
541 strat_event_enable();
546 static uint8_t strat_beginning(uint8_t do_initturn)
550 strat_set_acc(ACC_DIST, ACC_ANGLE);
553 strat_set_speed(600, 60); /* OK */
554 trajectory_d_a_rel(&mainboard.traj, 1000, COLOR_A(20));
555 err = WAIT_COND_OR_TRAJ_END(trajectory_angle_finished(&mainboard.traj),
559 strat_set_acc(ACC_DIST, ACC_ANGLE);
560 strat_set_speed(SPEED_CLITOID_SLOW, SPEED_ANGLE_SLOW);
563 err = line2line(0, LINE_UP, 2, LINE_R_DOWN, TRAJ_FLAGS_NO_NEAR);
564 if (err == END_OBSTACLE &&
565 strat_conf.flags & STRAT_CONF_WAIT_OBSTACLE &&
566 time_get_s() > strat_conf.prev_wait_obstacle + 5) {
567 strat_conf.prev_wait_obstacle = time_get_s();
571 if (!TRAJ_SUCCESS(err))
575 err = line2line(2, LINE_R_DOWN, 2, LINE_R_UP, TRAJ_FLAGS_NO_NEAR);
576 if (err == END_OBSTACLE &&
577 strat_conf.flags & STRAT_CONF_WAIT_OBSTACLE &&
578 time_get_s() > strat_conf.prev_wait_obstacle + 5) {
579 strat_conf.prev_wait_obstacle = time_get_s();
583 if (!TRAJ_SUCCESS(err)) {
590 /* dump state (every 5 s max) */
591 #define DUMP_RATE_LIMIT(dump, last_print) \
593 if (time_get_s() - last_print > 5) { \
595 last_print = time_get_s(); \
601 /* return true if we need to grab some more elements */
602 static uint8_t need_more_elements(void)
604 if (time_get_s() <= 75) {
605 /* we have enough time left */
606 if (get_ball_count() >= 4)
608 if (get_cob_count() >= 4)
610 if ((get_ball_count() >= 2) &&
611 (get_cob_count() >= 2))
616 /* not much time remaining */
617 if ((get_ball_count() >= 1) &&
618 (get_cob_count() >= 1))
625 /* get tomatoes near our goals (12,5 and 12,3) */
626 uint8_t get_opp_oranges(void)
632 DEBUG(E_USER_STRAT, "%s()", __FUNCTION__);
634 /* only if oranges are present */
635 if (strat_db.opp_oranges_count == 0)
638 strat_db.opp_oranges_count = 0;
639 x = position_get_x_s16(&mainboard.pos);
640 y = position_get_y_s16(&mainboard.pos);
642 if (xycoord_to_ijcoord_not_corn(&x, &y, &i, &j) < 0)
645 /* not on eject point */
646 if (i != 11 || j != 6)
650 strat_set_speed(SPEED_DIST_FAST, SPEED_ANGLE_FAST);
652 /* turn in the correct direction */
653 trajectory_a_abs(&mainboard.traj, COLOR_A(-90));
654 err = wait_traj_end(TRAJ_FLAGS_NO_NEAR);
655 if (!TRAJ_SUCCESS(err))
658 trajectory_goto_forward_xy_abs(&mainboard.traj, 2625, COLOR_Y(597));
659 err = wait_traj_end(TRAJ_FLAGS_STD);
660 if (!TRAJ_SUCCESS(err))
663 strat_set_speed(SPEED_DIST_SLOW, SPEED_ANGLE_SLOW);
664 trajectory_goto_forward_xy_abs(&mainboard.traj, 2750, COLOR_Y(250));
665 err = wait_traj_end(TRAJ_FLAGS_NO_NEAR);
666 if (!TRAJ_SUCCESS(err))
669 err = run_to_the_hills(get_opponent_color());
676 /* get tomatoes near our goals (12,5 and 12,3) */
677 uint8_t get_orphan_tomatoes(void)
679 #define CLITOID_TOMATO_RADIUS 100.
680 #define TOMATO_BACK_X 2760
681 #define TOMATO_BACK_LEN 200
688 DEBUG(E_USER_STRAT, "%s()", __FUNCTION__);
690 /* only go if both tomatoes are present */
691 if (!strat_db.wp_table[12][5].present ||
692 !strat_db.wp_table[12][3].present) {
696 x = position_get_x_s16(&mainboard.pos);
697 y = position_get_y_s16(&mainboard.pos);
699 if (xycoord_to_ijcoord_not_corn(&x, &y, &i, &j) < 0)
702 /* not on eject point */
703 if (i != 11 || j != 6)
707 strat_set_speed(SPEED_CLITOID_FAST, SPEED_ANGLE_SLOW);
709 /* turn in the correct direction */
710 trajectory_a_abs(&mainboard.traj, COLOR_A(-90));
711 err = wait_traj_end(TRAJ_FLAGS_NO_NEAR);
713 /* clitoid to turn and take the first ball */
714 ret = trajectory_clitoid(&mainboard.traj, 2625, COLOR_Y(1847),
715 COLOR_A(-90), 150., COLOR_A(90), 0,
716 CLITOID_TOMATO_RADIUS, 3*125);
722 err = wait_traj_end(TRAJ_FLAGS_NO_NEAR);
723 if (!TRAJ_SUCCESS(err))
726 strat_set_speed(SPEED_CLITOID_SLOW, SPEED_ANGLE_SLOW);
727 err = strat_calib(300, END_TRAJ|END_BLOCKING);
728 a = position_get_a_deg_s16(&mainboard.pos);
730 strat_reset_pos(AREA_X - ROBOT_HALF_LENGTH_FRONT,
734 strat_set_speed(SPEED_CLITOID_FAST, SPEED_ANGLE_FAST);
735 trajectory_d_rel(&mainboard.traj, -250);
736 err = WAIT_COND_OR_TRAJ_END(!x_is_more_than(TOMATO_BACK_X),
739 if (err != 0 && !TRAJ_SUCCESS(err))
742 trajectory_d_a_rel(&mainboard.traj, -TOMATO_BACK_LEN, COLOR_A(-90));
743 err = wait_traj_end(TRAJ_FLAGS_NO_NEAR);
747 /* clitoid to turn and take the first ball */
748 strat_set_speed(SPEED_CLITOID_FAST, SPEED_ANGLE_SLOW);
749 ret = trajectory_clitoid(&mainboard.traj, 2625, COLOR_Y(1847),
750 COLOR_A(-90), 150., COLOR_A(90), 0,
751 CLITOID_TOMATO_RADIUS, 7*125);
756 err = wait_traj_end(TRAJ_FLAGS_NO_NEAR);
757 if (!TRAJ_SUCCESS(err))
761 strat_set_speed(SPEED_CLITOID_FAST, SPEED_ANGLE_FAST);
762 trajectory_d_rel(&mainboard.traj, -250);
763 err = WAIT_COND_OR_TRAJ_END(!x_is_more_than(TOMATO_BACK_X),
765 trajectory_d_a_rel(&mainboard.traj, -TOMATO_BACK_LEN, COLOR_A(-90));
766 err = wait_traj_end(TRAJ_FLAGS_NO_NEAR);
774 #define HILL_LEN 1000
777 #define HILL_POSY_YELLOW 310
778 #define HILL_POSY_BLUE 190
780 #define HILL_POSX_BALLS_DOWN1 830
781 #define HILL_POSX_BALLS_DOWN2 920
782 #define HILL_POSX_BALLS_DOWN3 730
783 #define HILL_START_POSX 580
785 uint8_t prepare_hill(uint8_t orange_color, int16_t posx)
787 int16_t startx, starty;
788 uint8_t our_color = get_color();
791 if (orange_color == I2C_COLOR_YELLOW)
792 starty = HILL_POSY_YELLOW;
794 starty = HILL_POSY_BLUE;
795 if (orange_color == our_color)
798 startx = AREA_X - posx;
799 trajectory_goto_forward_xy_abs(&mainboard.traj, startx, COLOR_Y(starty));
800 err = wait_traj_end(TRAJ_FLAGS_SMALL_DIST);
801 if (!TRAJ_SUCCESS(err))
804 /* turn to the hills */
805 if (orange_color == our_color)
806 trajectory_a_abs(&mainboard.traj, COLOR_A(HILL_ANGLE));
808 trajectory_a_abs(&mainboard.traj, COLOR_A(-180+HILL_ANGLE));
809 err = wait_traj_end(TRAJ_FLAGS_SMALL_DIST);
810 if (!TRAJ_SUCCESS(err))
816 /* get oranges, must be called near game area */
817 uint8_t run_to_the_hills(uint8_t orange_color)
822 uint8_t our_color = get_color();
823 int32_t p = pid_get_gain_P(&mainboard.angle.pid);
824 int32_t i = pid_get_gain_I(&mainboard.angle.pid);
825 int32_t d = pid_get_gain_D(&mainboard.angle.pid);
826 int32_t max_in = pid_get_max_in(&mainboard.angle.pid);
827 int32_t max_i = pid_get_max_I(&mainboard.angle.pid);
828 int32_t max_out = pid_get_max_out(&mainboard.angle.pid);
831 strat_get_acc(&ad, &aa);
832 strat_get_speed(&sd, &sa);
834 DEBUG(E_USER_STRAT, "%s()", __FUNCTION__);
836 strat_set_speed(SPEED_DIST_FAST, SPEED_ANGLE_FAST);
837 err = prepare_hill(orange_color, HILL_START_POSX);
838 if (!TRAJ_SUCCESS(err))
841 strat_set_acc(5, ACC_ANGLE);
842 strat_set_speed(300, SPEED_ANGLE_SLOW);
843 bd_set_current_thresholds(&mainboard.distance.bd, 500, 8000, 2000000, 80);
844 bd_set_current_thresholds(&mainboard.angle.bd, 500, 8000, 2000000, 80);
845 bd_set_speed_threshold(&mainboard.distance.bd, 10);
846 support_balls_pack();
847 i2c_ballboard_set_mode(I2C_BALLBOARD_MODE_PREP_FORK);
849 /* decrease angle gains */
850 pid_set_gains(&mainboard.angle.pid, 200, 0, 2000);
852 /* here it is difficult to handle return values, because we
854 i2c_cobboard_set_mode(I2C_COBBOARD_MODE_KICKSTAND_DOWN);
855 trajectory_d_rel(&mainboard.traj, HILL_LEN);
856 err = WAIT_COND_OR_TRAJ_END(position_get_x_s16(&mainboard.pos) >
857 HILL_POSX_BALLS_DOWN1,
858 TRAJ_FLAGS_SMALL_DIST);
859 DEBUG(E_USER_STRAT, "deploy support balls");
860 support_balls_deploy();
861 i2c_cobboard_set_mode(I2C_COBBOARD_MODE_KICKSTAND_UP);
862 trajectory_only_a_rel(&mainboard.traj, 2);
863 err = WAIT_COND_OR_TE_TO(0, 0, 2200);
865 i2c_cobboard_set_mode(I2C_COBBOARD_MODE_KICKSTAND_DOWN);
868 i2c_ballboard_set_mode(I2C_BALLBOARD_MODE_TAKE_FORK);
872 /* reach top, go down */
873 trajectory_d_rel(&mainboard.traj, -HILL_LEN);
875 err = WAIT_COND_OR_TRAJ_END(position_get_x_s16(&mainboard.pos) <
876 HILL_POSX_BALLS_DOWN2,
877 TRAJ_FLAGS_SMALL_DIST);
878 DEBUG(E_USER_STRAT, "pack support balls");
879 support_balls_pack();
880 err = WAIT_COND_OR_TRAJ_END(position_get_x_s16(&mainboard.pos) <
881 HILL_POSX_BALLS_DOWN3,
882 TRAJ_FLAGS_SMALL_DIST);
883 DEBUG(E_USER_STRAT, "deploy support balls");
884 strat_set_acc(ad, aa);
885 strat_set_speed(SPEED_DIST_FAST, SPEED_ANGLE_FAST);
886 support_balls_deploy();
887 err = wait_traj_end(TRAJ_FLAGS_SMALL_DIST);
888 i2c_cobboard_set_mode(I2C_COBBOARD_MODE_HARVEST);
890 /* wait to be near the wall */
891 err = WAIT_COND_OR_TRAJ_END(position_get_x_s16(&mainboard.pos) < 200,
892 TRAJ_FLAGS_SMALL_DIST);
894 i2c_cobboard_set_mode(I2C_COBBOARD_MODE_HARVEST);
896 /* restore BD coefs */
897 bd_set_current_thresholds(&mainboard.distance.bd, 500, 8000, 1000000, 20);
898 bd_set_current_thresholds(&mainboard.distance.bd, 500, 8000, 1000000, 20);
899 bd_set_speed_threshold(&mainboard.distance.bd, 60);
901 /* calibrate position on the wall */
902 strat_set_speed(SPEED_DIST_VERY_SLOW, SPEED_ANGLE_FAST);
903 err = strat_calib(-400, TRAJ_FLAGS_SMALL_DIST);
904 if (orange_color == our_color)
905 strat_reset_pos(ROBOT_HALF_LENGTH_REAR,
906 DO_NOT_SET_POS, COLOR_A(0));
908 strat_reset_pos(AREA_X - ROBOT_HALF_LENGTH_REAR,
909 DO_NOT_SET_POS, COLOR_A(180));
910 strat_set_speed(SPEED_DIST_FAST, SPEED_ANGLE_FAST);
912 trajectory_d_rel(&mainboard.traj, 250);
913 err = wait_traj_end(TRAJ_FLAGS_NO_NEAR);
914 if (orange_color == I2C_COLOR_YELLOW)
915 trajectory_a_rel(&mainboard.traj, 90);
917 trajectory_a_rel(&mainboard.traj, -90);
918 err = wait_traj_end(TRAJ_FLAGS_NO_NEAR);
921 strat_set_speed(SPEED_DIST_VERY_SLOW, SPEED_ANGLE_FAST);
922 err = strat_calib(-400, TRAJ_FLAGS_SMALL_DIST);
923 strat_reset_pos(DO_NOT_SET_POS,
924 COLOR_Y(ROBOT_HALF_LENGTH_REAR),
926 strat_set_speed(SPEED_DIST_FAST, SPEED_ANGLE_FAST);
928 trajectory_d_rel(&mainboard.traj, 250);
929 err = wait_traj_end(TRAJ_FLAGS_NO_NEAR);
931 /* revert acceleration and speed */
932 pid_set_gains(&mainboard.angle.pid, p, i, d);
933 pid_set_maximums(&mainboard.distance.pid, max_in, max_i, max_out);
935 strat_set_speed(sd, sa);
936 support_balls_deploy();
940 uint8_t strat_main(void)
942 uint8_t err, do_initturn = 1;
945 if (strat_conf.flags & STRAT_CONF_OUR_ORANGE) {
946 err = run_to_the_hills(get_color());
947 strat_db.our_oranges_count = 0;
951 /* harvest the first cobs + balls */
952 err = strat_beginning(do_initturn);
954 if (!TRAJ_SUCCESS(err))
959 /* choose circuit, and harvest on it */
962 DEBUG(E_USER_STRAT, "start main loop");
964 /* if it's time to get tomatoes, do it */
965 if (time_get_s() > strat_conf.orphan_tomato) {
966 err = get_orphan_tomatoes();
967 if (err == END_ERROR) {
969 "get_orphan_tomatoes 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 these tomatoes if it failed */
978 strat_conf.orphan_tomato = 90;
983 /* if it's time to get opponent oranges, do it */
984 if (time_get_s() > strat_conf.opp_orange) {
985 err = get_opp_oranges();
986 if (err == END_ERROR) {
988 "get_opp_oranges returned END_ERROR");
990 else if (err == END_TIMER) {
991 DEBUG(E_USER_STRAT, "End of time");
995 else if (!TRAJ_SUCCESS(err)) {
996 /* don't retry oranges if it failed */
997 strat_conf.opp_orange = 90;
1002 /**********************/
1003 /* harvest on circuit */
1004 /**********************/
1006 err = strat_harvest_circuit();
1007 if (err == END_TIMER) {
1008 DEBUG(E_USER_STRAT, "End of time");
1012 if (!TRAJ_SUCCESS(err)) {
1017 /***********************/
1018 /* eject game elements */
1019 /***********************/
1021 err = strat_eject();
1022 /* end of time exit ! */
1023 if (err == END_TIMER) {
1024 DEBUG(E_USER_STRAT, "End of time");
1028 if (!TRAJ_SUCCESS(err)) {