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 volatile uint8_t strat_lpack60 = 0;
74 volatile uint8_t strat_rpack60 = 0;
75 struct strat_conf strat_conf;
77 /*************************************************************/
81 /*************************************************************/
83 /* called before each strat, and before the start switch */
84 void strat_preinit(void)
87 interrupt_traj_reset();
88 mainboard.flags = DO_ENCODERS | DO_CS | DO_RS |
89 DO_POS | DO_BD | DO_POWER;
92 strat_conf_dump(__FUNCTION__);
93 strat_db_dump(__FUNCTION__);
96 void strat_conf_dump(const char *caller)
98 if (!strat_conf.dump_enabled)
101 printf_P(PSTR("-- conf --\r\n"));
105 void strat_event_enable(void)
110 void strat_event_disable(void)
115 /* call it just before launching the strat */
116 void strat_init(void)
119 position_set(&mainboard.pos, 298.16,
120 COLOR_Y(308.78), COLOR_A(70.00));
123 /* we consider that the color is correctly set */
125 strat_set_speed(SPEED_DIST_FAST, SPEED_ANGLE_FAST);
127 interrupt_traj_reset();
129 i2c_cobboard_set_mode(I2C_COBBOARD_MODE_HARVEST);
130 i2c_ballboard_set_mode(I2C_BALLBOARD_MODE_HARVEST);
132 /* used in strat_base for END_TIMER */
133 mainboard.flags = DO_ENCODERS | DO_CS | DO_RS |
134 DO_POS | DO_BD | DO_TIMER | DO_POWER;
137 /* call it after each strat */
138 void strat_exit(void)
145 mainboard.flags &= ~(DO_TIMER);
151 mainboard.flags &= ~(DO_CS);
153 pwm_ng_set(LEFT_PWM, 0);
154 pwm_ng_set(RIGHT_PWM, 0);
158 /* called periodically (10ms) */
159 void strat_event(void *dummy)
162 int8_t lcob_near, rcob_near;
166 /* ignore when strat is not running */
167 if (strat_running == 0)
170 /* read sensors from ballboard */
172 lcob = ballboard.lcob;
173 ballboard.lcob = I2C_COB_NONE;
174 rcob = ballboard.rcob;
175 ballboard.rcob = I2C_COB_NONE;
178 /* if (lcob != I2C_COB_NONE) */
179 /* DEBUG(E_USER_STRAT, "XXX lcob %s", */
180 /* lcob == I2C_COB_WHITE ? "white" : "black"); */
181 /* if (rcob != I2C_COB_NONE) */
182 /* DEBUG(E_USER_STRAT, "XXX rcob %s", */
183 /* rcob == I2C_COB_WHITE ? "white" : "black"); */
184 /* XXX take opponent position into account */
187 if (time_get_s() == 15)
188 cobboard.cob_count = 5;
189 if (time_get_s() == 16)
190 cobboard.cob_count = 0;
191 if (time_get_s() == 25)
192 cobboard.cob_count = 5;
195 /* detect cob on left side */
196 lcob_near = corn_is_near(&lidx, I2C_LEFT_SIDE);
197 if (lcob_near && lcob != I2C_COB_NONE) {
198 if (strat_db.corn_table[lidx]->corn.color == I2C_COB_UNKNOWN)
199 DEBUG(E_USER_STRAT, "lcob %s %d",
200 lcob == I2C_COB_WHITE ? "white" : "black", lidx);
201 corn_set_color(strat_db.corn_table[lidx], lcob);
204 /* detect cob on right side */
205 rcob_near = corn_is_near(&ridx, I2C_RIGHT_SIDE);
206 if (rcob_near && rcob != I2C_COB_NONE) {
207 if (strat_db.corn_table[ridx]->corn.color == I2C_COB_UNKNOWN)
208 DEBUG(E_USER_STRAT, "rcob %s %d",
209 rcob == I2C_COB_WHITE ? "white" : "black", ridx);
210 corn_set_color(strat_db.corn_table[ridx], rcob);
213 /* control the cobboard mode for left spickle */
214 if (lcob_near && strat_db.corn_table[lidx]->present) {
215 if (get_cob_count() >= 5 || want_pack || strat_lpack60) {
219 /* deploy spickle and harvest white ones */
220 if (strat_db.corn_table[lidx]->corn.color == I2C_COB_WHITE) {
221 i2c_cobboard_autoharvest_nomove(I2C_LEFT_SIDE);
222 if (cobboard.status == I2C_COBBOARD_STATUS_LBUSY)
223 strat_db.corn_table[lidx]->present = 0;
226 i2c_cobboard_deploy_nomove(I2C_LEFT_SIDE);
230 /* no cob near us, we can pack or deploy freely */
231 if (get_cob_count() >= 5 || want_pack || strat_lpack60)
232 i2c_cobboard_pack(I2C_LEFT_SIDE);
234 i2c_cobboard_deploy(I2C_LEFT_SIDE);
237 /* control the cobboard mode for right spickle */
238 if (rcob_near && strat_db.corn_table[ridx]->present) {
239 if (get_cob_count() >= 5 || want_pack || strat_rpack60) {
243 /* deploy spickle and harvest white ones */
244 if (strat_db.corn_table[ridx]->corn.color == I2C_COB_WHITE) {
245 i2c_cobboard_autoharvest_nomove(I2C_RIGHT_SIDE);
246 if (cobboard.status == I2C_COBBOARD_STATUS_RBUSY)
247 strat_db.corn_table[ridx]->present = 0;
250 i2c_cobboard_deploy_nomove(I2C_RIGHT_SIDE);
254 /* no cob near us, we can pack or deploy freely */
255 if (get_cob_count() >= 5 || want_pack || strat_rpack60)
256 i2c_cobboard_pack(I2C_RIGHT_SIDE);
258 i2c_cobboard_deploy(I2C_RIGHT_SIDE);
261 /* limit speed when opponent is near */
266 static uint8_t strat_harvest(void)
271 /* must be called from a terminal line */
272 static uint8_t strat_eject(void)
277 /* go to eject point */
278 trajectory_goto_xy_abs(&mainboard.traj, 2625, COLOR_Y(1847));
279 err = WAIT_COND_OR_TRAJ_END(get_cob_count() >= 5,
283 strat_set_speed(600, SPEED_ANGLE_SLOW);
284 err = wait_traj_end(TRAJ_FLAGS_NO_NEAR);
288 strat_event_disable();
289 i2c_cobboard_pack(I2C_LEFT_SIDE);
290 i2c_cobboard_pack(I2C_RIGHT_SIDE);
293 i2c_ballboard_set_mode(I2C_BALLBOARD_MODE_EJECT);
294 trajectory_a_abs(&mainboard.traj, COLOR_A(70));
295 err = wait_traj_end(TRAJ_FLAGS_NO_NEAR);
297 DEBUG(E_USER_STRAT, "%s():%d", __FUNCTION__, __LINE__);
302 trajectory_a_rel(&mainboard.traj, COLOR_A(180));
303 err = wait_traj_end(END_INTR|END_TRAJ);
306 trajectory_d_rel(&mainboard.traj, -70);
307 err = wait_traj_end(END_INTR|END_TRAJ);
309 i2c_cobboard_set_mode(I2C_COBBOARD_MODE_EJECT);
310 strat_db_dump(__FUNCTION__);
313 strat_event_enable();
318 static uint8_t strat_beginning(void)
322 strat_set_acc(ACC_DIST, ACC_ANGLE);
324 strat_set_speed(600, SPEED_ANGLE_FAST);
327 strat_set_speed(250, SPEED_ANGLE_FAST);
330 strat_set_speed(600, 60); /* OK */
331 //strat_set_speed(250, 28); /* OK */
333 trajectory_d_a_rel(&mainboard.traj, 1000, COLOR_A(20));
334 err = WAIT_COND_OR_TRAJ_END(trajectory_angle_finished(&mainboard.traj),
337 strat_set_acc(ACC_DIST, ACC_ANGLE);
338 strat_set_speed(250, SPEED_ANGLE_SLOW);
341 DEBUG(E_USER_STRAT, "%s():%d count=%d", __FUNCTION__, __LINE__, get_cob_count());
342 if (get_cob_count() >= 5)
343 strat_set_speed(600, SPEED_ANGLE_FAST);
345 err = line2line(0, LINE_UP, 2, LINE_R_DOWN);
346 if (!TRAJ_SUCCESS(err)) {
353 DEBUG(E_USER_STRAT, "%s():%d count=%d", __FUNCTION__, __LINE__, get_cob_count());
354 if (get_cob_count() >= 5)
355 strat_set_speed(600, SPEED_ANGLE_FAST);
357 err = line2line(2, LINE_R_DOWN, 2, LINE_R_UP);
358 if (!TRAJ_SUCCESS(err)) {
367 strat_set_speed(250, SPEED_ANGLE_FAST);
368 strat_harvest_circuit();
375 /* dump state (every 5 s max) */
376 #define DUMP_RATE_LIMIT(dump, last_print) \
378 if (time_get_s() - last_print > 5) { \
380 last_print = time_get_s(); \
385 /* return true if we need to grab some more elements */
386 static uint8_t need_more_elements(void)
388 if (time_get_s() <= 75) {
389 /* we have enough time left */
390 if (get_ball_count() >= 4)
392 if (get_cob_count() >= 4)
394 if ((get_ball_count() >= 2) &&
395 (get_cob_count() >= 2))
400 /* not much time remaining */
401 if ((get_ball_count() >= 1) &&
402 (get_cob_count() >= 1))
408 uint8_t strat_main(void)
412 /* harvest the first cobs + balls */
413 err = strat_beginning();
416 /* end of time exit ! */
417 if (err == END_TIMER) {
418 DEBUG(E_USER_STRAT, "End of time");
423 if (need_more_elements() == 0) {
424 /* we have enough elements, go to eject */
426 if (!TRAJ_SUCCESS(err))
431 err = strat_harvest();
432 if (!TRAJ_SUCCESS(err))