2 * Copyright Droids Corporation, 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_base.c,v 1.8 2009-11-08 17:24:33 zer0 Exp $
27 #include <aversive/pgmspace.h>
28 #include <aversive/wait.h>
29 #include <aversive/error.h>
34 #include <clock_time.h>
39 #include <control_system_manager.h>
40 #include <trajectory_manager.h>
41 #include <trajectory_manager_utils.h>
42 #include <vect_base.h>
45 #include <obstacle_avoidance.h>
46 #include <blocking_detection_manager.h>
47 #include <robot_system.h>
48 #include <position_manager.h>
53 #include "../common/i2c_commands.h"
57 #include "strat_utils.h"
58 #include "strat_base.h"
61 #include "i2c_protocol.h"
63 /* true if we want to interrupt a trajectory */
64 static uint8_t traj_intr=0;
66 /* filled when a END_OBSTACLE is returned */
67 struct opponent_obstacle opponent_obstacle;
70 static volatile int16_t strat_speed_a = SPEED_DIST_FAST;
71 static volatile int16_t strat_speed_d = SPEED_ANGLE_FAST;
72 static volatile uint16_t strat_limit_speed_a = 0; /* no limit */
73 static volatile uint16_t strat_limit_speed_d = 0;
75 static volatile uint8_t strat_limit_speed_enabled = 1;
78 /* Strings that match the end traj cause */
79 /* /!\ keep it sync with stat_base.h */
80 const char *err_tab []= {
91 /* return string from end traj type num */
92 const char *get_err(uint8_t err)
97 for (i=0 ; i<8; i++) {
101 return "END_UNKNOWN";
104 void strat_hardstop(void)
106 trajectory_hardstop(&mainboard.traj);
107 pid_reset(&mainboard.angle.pid);
108 pid_reset(&mainboard.distance.pid);
109 bd_reset(&mainboard.angle.bd);
110 bd_reset(&mainboard.distance.bd);
112 while ((ABS(mainboard.speed_d) > 200) ||
113 (ABS(mainboard.speed_a) > 200))
115 trajectory_hardstop(&mainboard.traj);
116 pid_reset(&mainboard.angle.pid);
117 pid_reset(&mainboard.distance.pid);
118 bd_reset(&mainboard.angle.bd);
119 bd_reset(&mainboard.distance.bd);
122 /* go to an x,y point without checking for obstacle or blocking. It
123 * should be used for very small dist only. Return END_TRAJ if we
124 * reach destination, or END_BLOCKING if the robot blocked more than 3
126 uint8_t strat_goto_xy_force(int16_t x, int16_t y)
130 for (i=0; i<3; i++) {
131 trajectory_goto_xy_abs(&mainboard.traj, x, y);
132 err = wait_traj_end(TRAJ_FLAGS_SMALL_DIST);
133 if (TRAJ_SUCCESS(err))
135 if (err == END_BLOCKING) {
144 void strat_reset_pos(int16_t x, int16_t y, int16_t a)
146 int16_t posx = position_get_x_s16(&mainboard.pos);
147 int16_t posy = position_get_y_s16(&mainboard.pos);
148 int16_t posa = position_get_a_deg_s16(&mainboard.pos);
150 if (x == DO_NOT_SET_POS)
152 if (y == DO_NOT_SET_POS)
154 if (a == DO_NOT_SET_POS)
157 DEBUG(E_USER_STRAT, "reset pos (%s%s%s)",
158 x == DO_NOT_SET_POS ? "" : "x",
159 y == DO_NOT_SET_POS ? "" : "y",
160 a == DO_NOT_SET_POS ? "" : "a");
161 position_set(&mainboard.pos, x, y, a);
162 DEBUG(E_USER_STRAT, "pos resetted", __FUNCTION__);
166 * decrease gain on angle PID, and go forward until we reach the
169 uint8_t strat_calib(int16_t dist, uint8_t flags)
171 int32_t p = pid_get_gain_P(&mainboard.angle.pid);
172 int32_t i = pid_get_gain_I(&mainboard.angle.pid);
173 int32_t d = pid_get_gain_D(&mainboard.angle.pid);
174 int32_t max_in = pid_get_max_in(&mainboard.angle.pid);
175 int32_t max_i = pid_get_max_I(&mainboard.angle.pid);
176 int32_t max_out = pid_get_max_out(&mainboard.angle.pid);
179 pid_set_maximums(&mainboard.distance.pid, 0, 20000, 1000);
180 pid_set_gains(&mainboard.angle.pid, 200, 0, 2000);
181 trajectory_d_rel(&mainboard.traj, dist);
182 err = wait_traj_end(flags);
183 pid_set_gains(&mainboard.angle.pid, p, i, d);
184 pid_set_maximums(&mainboard.distance.pid, max_in, max_i, max_out);
188 static void strat_update_traj_speed(void)
193 if (strat_limit_speed_d && d > strat_limit_speed_d)
194 d = strat_limit_speed_d;
196 if (strat_limit_speed_a && a > strat_limit_speed_a)
197 a = strat_limit_speed_a;
199 trajectory_set_speed(&mainboard.traj, d, a);
202 void strat_set_speed(uint16_t d, uint16_t a)
208 strat_update_traj_speed();
212 void strat_get_speed(uint16_t *d, uint16_t *a)
221 void strat_limit_speed_enable(void)
223 strat_limit_speed_enabled = 1;
226 void strat_limit_speed_disable(void)
228 strat_limit_speed_enabled = 0;
231 /* called periodically */
232 void strat_limit_speed(void)
234 uint16_t lim_d = 0, lim_a = 0;
235 int16_t opp_d, opp_a;
237 if (strat_limit_speed_enabled == 0)
240 if (get_opponent_da(&opp_d, &opp_a) != 0)
244 if (mainboard.speed_d > 0 && (opp_a > 290 || opp_a < 70)) {
245 lim_d = SPEED_DIST_VERY_SLOW;
246 lim_a = SPEED_ANGLE_VERY_SLOW;
248 else if (mainboard.speed_d < 0 && (opp_a < 250 && opp_a > 110)) {
249 lim_d = SPEED_DIST_VERY_SLOW;
250 lim_a = SPEED_ANGLE_VERY_SLOW;
253 lim_d = SPEED_DIST_SLOW;
254 lim_a = SPEED_ANGLE_VERY_SLOW;
257 else if (opp_d < 800) {
258 if (mainboard.speed_d > 0 && (opp_a > 290 || opp_a < 70)) {
259 lim_d = SPEED_DIST_SLOW;
260 lim_a = SPEED_ANGLE_SLOW;
262 else if (mainboard.speed_d < 0 && (opp_a < 250 && opp_a > 110)) {
263 lim_d = SPEED_DIST_SLOW;
264 lim_a = SPEED_ANGLE_SLOW;
269 if (lim_d != strat_limit_speed_d ||
270 lim_a != strat_limit_speed_a) {
271 strat_limit_speed_d = lim_d;
272 strat_limit_speed_a = lim_a;
273 DEBUG(E_USER_STRAT, "new speed limit da=%d,%d", lim_d, lim_a);
274 strat_update_traj_speed();
278 /* start the strat */
279 void strat_start(void)
286 /* if start sw not plugged */
287 if (sensor_get(S_START_SWITCH)) {
290 printf_P(PSTR("No start switch, press a key or plug it\r\n"));
292 /* while start sw not plugged */
293 while (sensor_get(S_START_SWITCH)) {
294 if (! cmdline_keypressed())
297 for (i=3; i>0; i--) {
298 printf_P(PSTR("%d\r\n"), i);
305 /* if start sw plugged */
306 if (!sensor_get(S_START_SWITCH)) {
307 printf_P(PSTR("Ready, unplug start switch to start\r\n"));
308 /* while start sw plugged */
309 while (!sensor_get(S_START_SWITCH));
316 NOTICE(E_USER_STRAT, "Finished !! returned %s", get_err(err));
320 /* return true if we have to brake due to an obstacle */
321 uint8_t strat_obstacle(void)
324 int16_t x_rel, y_rel;
325 int16_t opp_x, opp_y, opp_d, opp_a;
328 if (ABS(mainboard.speed_d) < 150)
331 /* no opponent detected */
332 if (get_opponent_xyda(&opp_x, &opp_y,
336 /* save obstacle position */
337 opponent_obstacle.x = opp_x;
338 opponent_obstacle.y = opp_y;
339 opponent_obstacle.d = opp_d;
340 opponent_obstacle.a = opp_a;
341 #else /* belgium cup only */
342 int16_t x_rel, y_rel;
343 int16_t opp_d, opp_a;
347 if (time_get_s() >= 12 && time_get_s() <= 30)
350 if (!sensor_get(S_RCOB_WHITE))
356 rel_da_to_abs_xy(opp_d, RAD(opp_a), &opp_x, &opp_y);
357 if (!is_in_area(opp_x, opp_y, 250))
361 /* sensor are temporarily disabled */
362 if (sensor_obstacle_is_disabled())
365 /* relative position */
366 x_rel = cos(RAD(opp_a)) * (double)opp_d;
367 y_rel = sin(RAD(opp_a)) * (double)opp_d;
369 /* opponent too far */
373 /* opponent is in front of us */
374 if (mainboard.speed_d > 0 && (opp_a > 325 || opp_a < 35)) {
375 DEBUG(E_USER_STRAT, "opponent front d=%d, a=%d "
376 "xrel=%d yrel=%d (speed_d=%d)",
377 opp_d, opp_a, x_rel, y_rel, mainboard.speed_d);
378 sensor_obstacle_disable();
381 /* opponent is behind us */
382 if (mainboard.speed_d < 0 && (opp_a < 215 && opp_a > 145)) {
383 DEBUG(E_USER_STRAT, "opponent behind d=%d, a=%d xrel=%d yrel=%d",
384 opp_d, opp_a, x_rel, y_rel);
385 sensor_obstacle_disable();
392 void interrupt_traj(void)
397 void interrupt_traj_reset(void)
402 uint8_t test_traj_end(uint8_t why)
407 robot_pt.x = position_get_x_s16(&mainboard.pos);
408 robot_pt.y = position_get_y_s16(&mainboard.pos);
410 /* trigger an event at 3 sec before the end of the match if we
411 * have balls in the barrel */
412 cur_timer = time_get_s();
414 if ((mainboard.flags & DO_TIMER) && (why & END_TIMER)) {
416 if (cur_timer >= MATCH_TIME)
420 if ((why & END_INTR) && traj_intr) {
421 interrupt_traj_reset();
425 if ((why & END_TRAJ) && trajectory_finished(&mainboard.traj))
428 /* we are near the destination point (depends on current
429 * speed) AND the robot is in the area bounding box. */
430 if (why & END_NEAR) {
431 int16_t d_near = 100;
433 if (mainboard.speed_d > 2000)
436 if (trajectory_in_window(&mainboard.traj, d_near, RAD(5.0)) &&
437 is_in_boundingbox(&robot_pt))
441 if ((why & END_BLOCKING) && bd_get(&mainboard.angle.bd)) {
446 if ((why & END_BLOCKING) && bd_get(&mainboard.distance.bd)) {
451 if ((why & END_OBSTACLE) && strat_obstacle()) {
459 uint8_t __wait_traj_end_debug(uint8_t why, uint16_t line)
462 int16_t opp_x, opp_y, opp_d, opp_a;
465 ret = test_traj_end(why);
467 if (ret == END_OBSTACLE) {
468 if (get_opponent_xyda(&opp_x, &opp_y,
469 &opp_d, &opp_a) == 0)
470 DEBUG(E_USER_STRAT, "Got %s at line %d"
471 " xy=(%d,%d) da=(%d,%d)", get_err(ret),
472 line, opp_x, opp_y, opp_d, opp_a);
475 DEBUG(E_USER_STRAT, "Got %s at line %d",