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"
64 #include "strat_avoid.h"
68 #define COL_DISP_MARGIN 400 /* stop 40 cm in front of dispenser */
69 #define COL_SCAN_PRE_MARGIN 250
71 static volatile uint8_t strat_running = 0;
72 static volatile uint8_t want_pack = 0;
73 struct strat_conf strat_conf;
75 /*************************************************************/
79 /*************************************************************/
81 /* called before each strat, and before the start switch */
82 void strat_preinit(void)
85 interrupt_traj_reset();
86 mainboard.flags = DO_ENCODERS | DO_CS | DO_RS |
87 DO_POS | DO_BD | DO_POWER;
90 strat_conf_dump(__FUNCTION__);
91 strat_db_dump(__FUNCTION__);
94 void strat_conf_dump(const char *caller)
96 if (!strat_conf.dump_enabled)
99 printf_P(PSTR("-- conf --\r\n"));
103 void strat_event_enable(void)
108 void strat_event_disable(void)
113 /* call it just before launching the strat */
114 void strat_init(void)
117 position_set(&mainboard.pos, 298.16,
118 COLOR_Y(308.78), COLOR_A(70.00));
121 /* we consider that the color is correctly set */
123 strat_set_speed(SPEED_DIST_FAST, SPEED_ANGLE_FAST);
125 interrupt_traj_reset();
127 i2c_cobboard_set_mode(I2C_COBBOARD_MODE_HARVEST);
128 i2c_ballboard_set_mode(I2C_BALLBOARD_MODE_HARVEST);
130 /* used in strat_base for END_TIMER */
131 mainboard.flags = DO_ENCODERS | DO_CS | DO_RS |
132 DO_POS | DO_BD | DO_TIMER | DO_POWER;
135 /* call it after each strat */
136 void strat_exit(void)
143 mainboard.flags &= ~(DO_TIMER);
149 mainboard.flags &= ~(DO_CS);
151 pwm_ng_set(LEFT_PWM, 0);
152 pwm_ng_set(RIGHT_PWM, 0);
156 /* called periodically (10ms) */
157 void strat_event(void *dummy)
160 int8_t lcob_near, rcob_near;
164 /* ignore when strat is not running */
165 if (strat_running == 0)
168 /* read sensors from ballboard */
170 lcob = ballboard.lcob;
171 ballboard.lcob = I2C_COB_NONE;
172 rcob = ballboard.rcob;
173 ballboard.rcob = I2C_COB_NONE;
176 /* if (lcob != I2C_COB_NONE) */
177 /* DEBUG(E_USER_STRAT, "XXX lcob %s", */
178 /* lcob == I2C_COB_WHITE ? "white" : "black"); */
179 /* if (rcob != I2C_COB_NONE) */
180 /* DEBUG(E_USER_STRAT, "XXX rcob %s", */
181 /* rcob == I2C_COB_WHITE ? "white" : "black"); */
182 /* XXX take opponent position into account */
185 if (time_get_s() == 15)
186 cobboard.cob_count = 5;
187 if (time_get_s() == 16)
188 cobboard.cob_count = 0;
189 if (time_get_s() == 25)
190 cobboard.cob_count = 5;
193 /* detect cob on left side */
194 lcob_near = corn_is_near(&lidx, I2C_LEFT_SIDE);
195 if (lcob_near && lcob != I2C_COB_NONE) {
196 if (strat_db.corn_table[lidx]->corn.color == I2C_COB_UNKNOWN)
197 DEBUG(E_USER_STRAT, "lcob %s %d",
198 lcob == I2C_COB_WHITE ? "white" : "black", lidx);
199 corn_set_color(strat_db.corn_table[lidx], lcob);
202 /* detect cob on right side */
203 rcob_near = corn_is_near(&ridx, I2C_RIGHT_SIDE);
204 if (rcob_near && rcob != I2C_COB_NONE) {
205 if (strat_db.corn_table[ridx]->corn.color == I2C_COB_UNKNOWN)
206 DEBUG(E_USER_STRAT, "rcob %s %d",
207 rcob == I2C_COB_WHITE ? "white" : "black", ridx);
208 corn_set_color(strat_db.corn_table[ridx], rcob);
211 /* control the cobboard mode for left spickle */
213 if (get_cob_count() >= 5 || want_pack) {
214 if (strat_db.corn_table[lidx]->corn.color != I2C_COB_WHITE &&
215 strat_db.corn_table[lidx]->corn.color != I2C_COB_BLACK)
216 i2c_cobboard_pack(I2C_LEFT_SIDE);
219 if (strat_db.corn_table[lidx]->corn.color == I2C_COB_WHITE)
220 i2c_cobboard_autoharvest(I2C_LEFT_SIDE);
221 else if (strat_db.corn_table[lidx]->corn.color == I2C_COB_BLACK)
222 i2c_cobboard_deploy_nomove(I2C_LEFT_SIDE);
224 i2c_cobboard_deploy(I2C_LEFT_SIDE);
228 if (get_cob_count() >= 5 || want_pack)
229 i2c_cobboard_pack(I2C_LEFT_SIDE);
231 i2c_cobboard_deploy(I2C_LEFT_SIDE);
234 /* control the cobboard mode for right spickle */
236 if (get_cob_count() >= 5 || want_pack) {
237 if (strat_db.corn_table[ridx]->corn.color != I2C_COB_WHITE &&
238 strat_db.corn_table[ridx]->corn.color != I2C_COB_BLACK)
239 i2c_cobboard_pack(I2C_RIGHT_SIDE);
242 if (strat_db.corn_table[ridx]->corn.color == I2C_COB_WHITE)
243 i2c_cobboard_autoharvest(I2C_RIGHT_SIDE);
244 else if (strat_db.corn_table[ridx]->corn.color == I2C_COB_BLACK)
245 i2c_cobboard_deploy_nomove(I2C_RIGHT_SIDE);
247 i2c_cobboard_deploy(I2C_RIGHT_SIDE);
251 if (get_cob_count() >= 5 || want_pack)
252 i2c_cobboard_pack(I2C_RIGHT_SIDE);
254 i2c_cobboard_deploy(I2C_RIGHT_SIDE);
257 /* limit speed when opponent is near */
262 static uint8_t strat_harvest(void)
267 static uint8_t strat_eject(void)
272 strat_set_speed(600, SPEED_ANGLE_SLOW);
275 trajectory_goto_xy_abs(&mainboard.traj, 2625, COLOR_Y(1847));
276 err = wait_traj_end(TRAJ_FLAGS_NO_NEAR);
279 i2c_ballboard_set_mode(I2C_BALLBOARD_MODE_EJECT);
280 trajectory_a_abs(&mainboard.traj, COLOR_A(70));
281 err = wait_traj_end(TRAJ_FLAGS_NO_NEAR);
283 DEBUG(E_USER_STRAT, "%s():%d", __FUNCTION__, __LINE__);
289 strat_event_disable();
290 i2c_cobboard_pack(I2C_LEFT_SIDE);
291 i2c_cobboard_pack(I2C_RIGHT_SIDE);
292 trajectory_a_rel(&mainboard.traj, COLOR_A(180));
293 err = wait_traj_end(END_INTR|END_TRAJ);
296 trajectory_d_rel(&mainboard.traj, -70);
297 err = wait_traj_end(END_INTR|END_TRAJ);
299 i2c_cobboard_set_mode(I2C_COBBOARD_MODE_EJECT);
300 strat_db_dump(__FUNCTION__);
303 strat_event_enable();
308 static uint8_t strat_beginning(void)
312 strat_set_acc(ACC_DIST, ACC_ANGLE);
314 strat_set_speed(600, SPEED_ANGLE_FAST);
317 strat_set_speed(250, SPEED_ANGLE_FAST);
320 // strat_set_speed(600, 60); /* OK */
321 strat_set_speed(250, 28); /* OK */
323 trajectory_d_a_rel(&mainboard.traj, 1000, COLOR_A(20));
324 err = WAIT_COND_OR_TRAJ_END(trajectory_angle_finished(&mainboard.traj),
327 strat_set_acc(ACC_DIST, ACC_ANGLE);
328 strat_set_speed(250, SPEED_ANGLE_SLOW);
332 DEBUG(E_USER_STRAT, "%s():%d count=%d", __FUNCTION__, __LINE__, get_cob_count());
333 if (get_cob_count() >= 5)
334 strat_set_speed(600, SPEED_ANGLE_FAST);
336 err = line2line(0, LINE_UP, 2, LINE_R_DOWN);
337 if (!TRAJ_SUCCESS(err)) {
344 DEBUG(E_USER_STRAT, "%s():%d count=%d", __FUNCTION__, __LINE__, get_cob_count());
345 if (get_cob_count() >= 5)
346 strat_set_speed(600, SPEED_ANGLE_FAST);
348 err = line2line(2, LINE_R_DOWN, 2, LINE_R_UP);
349 if (!TRAJ_SUCCESS(err)) {
356 /* strat_set_speed(600, SPEED_ANGLE_FAST); */
357 /* err = line2line(0, LINE_UP, 3, LINE_R_DOWN); */
358 /* err = line2line(3, LINE_R_DOWN, 2, LINE_R_UP); */
359 /* err = line2line(2, LINE_R_UP, 2, LINE_R_DOWN); */
360 /* err = line2line(2, LINE_R_DOWN, 3, LINE_R_UP); */
361 /* err = line2line(3, LINE_R_UP, 5, LINE_UP); */
362 /* err = line2line(5, LINE_UP, 2, LINE_L_DOWN); */
363 /* err = line2line(2, LINE_L_DOWN, 1, LINE_L_UP); */
364 /* err = line2line(1, LINE_L_UP, 1, LINE_L_DOWN); */
365 /* err = line2line(1, LINE_L_DOWN, 0, LINE_DOWN); */
367 strat_set_speed(300, SPEED_ANGLE_FAST);
368 err = line2line(0, LINE_UP, 0, LINE_R_UP);
369 err = line2line(0, LINE_R_UP, 1, LINE_DOWN);
370 err = line2line(1, LINE_DOWN, 1, LINE_L_DOWN);
371 err = line2line(1, LINE_L_DOWN, 0, LINE_DOWN);
379 strat_set_speed(250, SPEED_ANGLE_FAST);
383 DEBUG(E_USER_STRAT, "%s():%d count=%d", __FUNCTION__, __LINE__, get_cob_count());
384 if (get_cob_count() >= 5)
385 strat_set_speed(600, SPEED_ANGLE_FAST);
387 err = line2line(5, LINE_DOWN, 2, LINE_L_UP);
388 if (!TRAJ_SUCCESS(err)) {
395 DEBUG(E_USER_STRAT, "%s():%d count=%d", __FUNCTION__, __LINE__, get_cob_count());
396 if (get_cob_count() >= 5)
397 strat_set_speed(600, SPEED_ANGLE_FAST);
399 err = line2line(2, LINE_L_UP, 2, LINE_R_UP);
400 if (!TRAJ_SUCCESS(err)) {
406 DEBUG(E_USER_STRAT, "%s():%d count=%d", __FUNCTION__, __LINE__, get_cob_count());
407 if (get_cob_count() >= 5)
408 strat_set_speed(600, SPEED_ANGLE_FAST);
410 strat_harvest_circuit();
413 WAIT_COND_OR_TRAJ_END(distance_from_robot(2625, COLOR_Y(1847)) < 100,
420 /* dump state (every 5 s max) */
421 #define DUMP_RATE_LIMIT(dump, last_print) \
423 if (time_get_s() - last_print > 5) { \
425 last_print = time_get_s(); \
430 /* return true if we need to grab some more elements */
431 static uint8_t need_more_elements(void)
433 if (time_get_s() <= 75) {
434 /* we have enough time left */
435 if (get_ball_count() >= 4)
437 if (get_cob_count() >= 4)
439 if ((get_ball_count() >= 2) &&
440 (get_cob_count() >= 2))
445 /* not much time remaining */
446 if ((get_ball_count() >= 1) &&
447 (get_cob_count() >= 1))
453 uint8_t strat_main(void)
457 /* harvest the first cobs + balls */
458 err = strat_beginning();
461 /* end of time exit ! */
462 if (err == END_TIMER) {
463 DEBUG(E_USER_STRAT, "End of time");
468 if (need_more_elements() == 0) {
469 /* we have enough elements, go to eject */
471 if (!TRAJ_SUCCESS(err))
476 err = strat_harvest();
477 if (!TRAJ_SUCCESS(err))