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 <vect_base.h>
46 #include <obstacle_avoidance.h>
47 #include <blocking_detection_manager.h>
48 #include <robot_system.h>
49 #include <position_manager.h>
51 #include <diagnostic.h>
56 #include "../common/i2c_commands.h"
57 #include "i2c_protocol.h"
61 #include "strat_base.h"
62 #include "strat_corn.h"
63 #include "strat_utils.h"
67 #define COL_DISP_MARGIN 400 /* stop 40 cm in front of dispenser */
68 #define COL_SCAN_PRE_MARGIN 250
70 static uint8_t strat_running = 0;
71 struct strat_conf strat_conf;
73 /*************************************************************/
77 /*************************************************************/
79 /* called before each strat, and before the start switch */
80 void strat_preinit(void)
83 interrupt_traj_reset();
84 mainboard.flags = DO_ENCODERS | DO_CS | DO_RS |
85 DO_POS | DO_BD | DO_POWER;
87 //i2c_cobboard_mode_init();
88 strat_conf_dump(__FUNCTION__);
89 strat_db_dump(__FUNCTION__);
92 void strat_conf_dump(const char *caller)
94 if (!strat_conf.dump_enabled)
97 printf_P(PSTR("-- conf --\r\n"));
101 /* call it just before launching the strat */
102 void strat_init(void)
105 position_set(&mainboard.pos, 298.16,
106 COLOR_Y(308.78), COLOR_A(70.00));
109 /* we consider that the color is correctly set */
112 strat_set_speed(SPEED_DIST_FAST, SPEED_ANGLE_FAST);
114 interrupt_traj_reset();
116 i2c_cobboard_set_mode(I2C_COBBOARD_MODE_HARVEST);
117 i2c_ballboard_set_mode(I2C_BALLBOARD_MODE_HARVEST);
119 /* used in strat_base for END_TIMER */
120 mainboard.flags = DO_ENCODERS | DO_CS | DO_RS |
121 DO_POS | DO_BD | DO_TIMER | DO_POWER;
125 /* call it after each strat */
126 void strat_exit(void)
133 mainboard.flags &= ~(DO_TIMER);
139 mainboard.flags &= ~(DO_CS);
141 pwm_ng_set(LEFT_PWM, 0);
142 pwm_ng_set(RIGHT_PWM, 0);
146 /* called periodically (10ms) */
147 void strat_event(void *dummy)
153 /* ignore when strat is not running */
154 if (strat_running == 0)
158 lcob = ballboard.lcob;
159 ballboard.lcob = I2C_COB_NONE;
160 rcob = ballboard.rcob;
161 ballboard.rcob = I2C_COB_NONE;
164 /* XXX take opponent position into account */
167 /* detect cob on left side */
168 if (corn_is_near(&lidx, I2C_LEFT_SIDE)) {
169 if (lcob != I2C_COB_NONE) {
170 corn_set_color(strat_db.corn_table[lidx], lcob);
171 DEBUG(E_USER_STRAT, "lcob %s %d",
172 lcob == I2C_COB_WHITE ? "white" : "black", lidx);
174 if (strat_db.corn_table[lidx]->corn.color == I2C_COB_WHITE)
175 i2c_cobboard_autoharvest(I2C_LEFT_SIDE);
177 i2c_cobboard_deploy(I2C_LEFT_SIDE);
180 i2c_cobboard_deploy(I2C_LEFT_SIDE);
183 /* detect cob on right side */
184 if (corn_is_near(&ridx, I2C_RIGHT_SIDE)) {
185 if (rcob != I2C_COB_NONE) {
186 corn_set_color(strat_db.corn_table[ridx], rcob);
187 DEBUG(E_USER_STRAT, "rcob %s %d",
188 rcob == I2C_COB_WHITE ? "white" : "black", ridx);
190 if (strat_db.corn_table[ridx]->corn.color == I2C_COB_WHITE)
191 i2c_cobboard_autoharvest(I2C_RIGHT_SIDE);
193 i2c_cobboard_deploy(I2C_RIGHT_SIDE);
196 i2c_cobboard_deploy(I2C_RIGHT_SIDE);
200 /* limit speed when opponent is close */
204 static uint8_t strat_beginning(void)
208 strat_set_acc(ACC_DIST, ACC_ANGLE);
210 strat_set_speed(600, SPEED_ANGLE_FAST);
213 strat_set_speed(250, SPEED_ANGLE_FAST);
217 // strat_set_speed(600, 60); /* OK */
218 strat_set_speed(250, 28); /* OK */
220 trajectory_d_a_rel(&mainboard.traj, 1000, COLOR_A(20));
221 err = WAIT_COND_OR_TRAJ_END(trajectory_angle_finished(&mainboard.traj),
224 strat_set_acc(ACC_DIST, ACC_ANGLE);
228 if (get_cob_count() >= 5)
229 strat_set_speed(600, SPEED_ANGLE_FAST);
231 err = line2line(LINE_UP, 0, LINE_R_DOWN, 2);
232 if (!TRAJ_SUCCESS(err)) {
233 trajectory_hardstop(&mainboard.traj);
239 if (get_cob_count() >= 5)
240 strat_set_speed(600, SPEED_ANGLE_FAST);
242 err = line2line(LINE_R_DOWN, 2, LINE_R_UP, 2);
243 if (!TRAJ_SUCCESS(err)) {
244 trajectory_hardstop(&mainboard.traj);
250 if (get_cob_count() >= 5)
251 strat_set_speed(600, SPEED_ANGLE_FAST);
253 err = line2line(LINE_R_UP, 2, LINE_UP, 5);
254 if (!TRAJ_SUCCESS(err)) {
255 trajectory_hardstop(&mainboard.traj);
260 strat_set_speed(600, SPEED_ANGLE_FAST);
261 err = line2line(LINE_UP, 0, LINE_R_DOWN, 3);
262 err = line2line(LINE_R_DOWN, 3, LINE_R_UP, 2);
263 err = line2line(LINE_R_UP, 2, LINE_R_DOWN, 2);
264 err = line2line(LINE_R_DOWN, 2, LINE_R_UP, 3);
265 err = line2line(LINE_R_UP, 3, LINE_UP, 5);
266 err = line2line(LINE_UP, 5, LINE_L_DOWN, 2);
267 err = line2line(LINE_L_DOWN, 2, LINE_L_UP, 1);
268 err = line2line(LINE_L_UP, 1, LINE_L_DOWN, 1);
269 err = line2line(LINE_L_DOWN, 1, LINE_DOWN, 0);
271 trajectory_hardstop(&mainboard.traj);
275 trajectory_hardstop(&mainboard.traj);
278 trajectory_a_abs(&mainboard.traj, COLOR_A(90));
279 i2c_ballboard_set_mode(I2C_BALLBOARD_MODE_EJECT);
283 trajectory_goto_xy_abs(&mainboard.traj, 2625, COLOR_Y(1847));
284 err = wait_traj_end(END_INTR|END_TRAJ);
285 i2c_cobboard_pack(I2C_LEFT_SIDE);
286 i2c_cobboard_pack(I2C_RIGHT_SIDE);
287 trajectory_a_rel(&mainboard.traj, COLOR_A(180));
288 err = wait_traj_end(END_INTR|END_TRAJ);
291 trajectory_d_rel(&mainboard.traj, -100);
292 err = wait_traj_end(END_INTR|END_TRAJ);
293 i2c_cobboard_set_mode(I2C_COBBOARD_MODE_EJECT);
296 trajectory_hardstop(&mainboard.traj);
300 /* dump state (every 5 s max) */
301 #define DUMP_RATE_LIMIT(dump, last_print) \
303 if (time_get_s() - last_print > 5) { \
305 last_print = time_get_s(); \
310 /* return true if we need to grab some more elements */
311 static uint8_t need_more_elements(void)
313 if (time_get_s() <= 75) {
314 /* we have enough time left */
315 if (get_ball_count() >= 4)
317 if (get_cob_count() >= 4)
319 if ((get_ball_count() >= 2) &&
320 (get_cob_count() >= 2))
325 /* not much time remaining */
326 if ((get_ball_count() >= 1) &&
327 (get_cob_count() >= 1))
333 static uint8_t strat_harvest(void)
338 static uint8_t strat_eject(void)
343 uint8_t strat_main(void)
347 /* harvest the first cobs + balls */
348 err = strat_beginning();
351 /* end of time exit ! */
352 if (err == END_TIMER) {
353 DEBUG(E_USER_STRAT, "End of time");
358 if (need_more_elements() == 0) {
359 /* we have enough elements, go to eject */
361 if (!TRAJ_SUCCESS(err))
366 err = strat_harvest();
367 if (!TRAJ_SUCCESS(err))