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);
176 pid_set_gains(&mainboard.angle.pid, 150, 0, 2000);
177 trajectory_d_rel(&mainboard.traj, dist);
178 err = wait_traj_end(flags);
179 pid_set_gains(&mainboard.angle.pid, p, i, d);
183 static void strat_update_traj_speed(void)
188 if (strat_limit_speed_d && d > strat_limit_speed_d)
189 d = strat_limit_speed_d;
191 if (strat_limit_speed_a && a > strat_limit_speed_a)
192 a = strat_limit_speed_a;
194 trajectory_set_speed(&mainboard.traj, d, a);
197 void strat_set_speed(uint16_t d, uint16_t a)
203 strat_update_traj_speed();
207 void strat_get_speed(uint16_t *d, uint16_t *a)
216 void strat_limit_speed_enable(void)
218 strat_limit_speed_enabled = 1;
221 void strat_limit_speed_disable(void)
223 strat_limit_speed_enabled = 0;
226 /* called periodically */
227 void strat_limit_speed(void)
229 uint16_t lim_d = 0, lim_a = 0;
230 int16_t opp_d, opp_a;
232 if (strat_limit_speed_enabled == 0)
235 if (get_opponent_da(&opp_d, &opp_a) != 0)
239 if (mainboard.speed_d > 0 && (opp_a > 290 || opp_a < 70)) {
240 lim_d = SPEED_DIST_VERY_SLOW;
241 lim_a = SPEED_ANGLE_VERY_SLOW;
243 else if (mainboard.speed_d < 0 && (opp_a < 250 && opp_a > 110)) {
244 lim_d = SPEED_DIST_VERY_SLOW;
245 lim_a = SPEED_ANGLE_VERY_SLOW;
248 lim_d = SPEED_DIST_SLOW;
249 lim_a = SPEED_ANGLE_VERY_SLOW;
252 else if (opp_d < 800) {
253 if (mainboard.speed_d > 0 && (opp_a > 290 || opp_a < 70)) {
254 lim_d = SPEED_DIST_SLOW;
255 lim_a = SPEED_ANGLE_SLOW;
257 else if (mainboard.speed_d < 0 && (opp_a < 250 && opp_a > 110)) {
258 lim_d = SPEED_DIST_SLOW;
259 lim_a = SPEED_ANGLE_SLOW;
264 if (lim_d != strat_limit_speed_d ||
265 lim_a != strat_limit_speed_a) {
266 strat_limit_speed_d = lim_d;
267 strat_limit_speed_a = lim_a;
268 DEBUG(E_USER_STRAT, "new speed limit da=%d,%d", lim_d, lim_a);
269 strat_update_traj_speed();
273 /* start the strat */
274 void strat_start(void)
281 /* if start sw not plugged */
282 if (sensor_get(S_START_SWITCH)) {
283 printf_P(PSTR("No start switch, press a key or plug it\r\n"));
285 /* while start sw not plugged */
286 while (sensor_get(S_START_SWITCH)) {
287 if (! cmdline_keypressed())
290 for (i=3; i>0; i--) {
291 printf_P(PSTR("%d\r\n"), i);
298 /* if start sw plugged */
299 if (!sensor_get(S_START_SWITCH)) {
300 printf_P(PSTR("Ready, unplug start switch to start\r\n"));
301 /* while start sw plugged */
302 while (!sensor_get(S_START_SWITCH));
307 NOTICE(E_USER_STRAT, "Finished !! returned %s", get_err(err));
311 /* return true if we have to brake due to an obstacle */
312 uint8_t strat_obstacle(void)
314 int16_t x_rel, y_rel;
315 int16_t opp_x, opp_y, opp_d, opp_a;
318 if (ABS(mainboard.speed_d) < 150)
321 /* no opponent detected */
322 if (get_opponent_xyda(&opp_x, &opp_y,
326 /* save obstacle position */
327 opponent_obstacle.x = opp_x;
328 opponent_obstacle.y = opp_y;
329 opponent_obstacle.d = opp_d;
330 opponent_obstacle.a = opp_a;
332 /* sensor are temporarily disabled */
333 if (sensor_obstacle_is_disabled())
336 /* relative position */
337 x_rel = cos(RAD(opp_a)) * (double)opp_d;
338 y_rel = sin(RAD(opp_a)) * (double)opp_d;
340 /* opponent too far */
344 /* opponent is in front of us */
345 if (mainboard.speed_d > 0 && (opp_a > 325 || opp_a < 35)) {
346 DEBUG(E_USER_STRAT, "opponent front d=%d, a=%d "
347 "xrel=%d yrel=%d (speed_d=%d)",
348 opp_d, opp_a, x_rel, y_rel, mainboard.speed_d);
349 sensor_obstacle_disable();
352 /* opponent is behind us */
353 if (mainboard.speed_d < 0 && (opp_a < 215 && opp_a > 145)) {
354 DEBUG(E_USER_STRAT, "opponent behind d=%d, a=%d xrel=%d yrel=%d",
355 opp_d, opp_a, x_rel, y_rel);
356 sensor_obstacle_disable();
363 void interrupt_traj(void)
368 void interrupt_traj_reset(void)
373 uint8_t test_traj_end(uint8_t why)
378 robot_pt.x = position_get_x_s16(&mainboard.pos);
379 robot_pt.y = position_get_y_s16(&mainboard.pos);
381 /* trigger an event at 3 sec before the end of the match if we
382 * have balls in the barrel */
383 cur_timer = time_get_s();
385 if ((mainboard.flags & DO_TIMER) && (why & END_TIMER)) {
387 if (cur_timer >= MATCH_TIME)
391 if ((why & END_INTR) && traj_intr) {
392 interrupt_traj_reset();
396 if ((why & END_TRAJ) && trajectory_finished(&mainboard.traj))
399 /* we are near the destination point (depends on current
400 * speed) AND the robot is in the area bounding box. */
401 if (why & END_NEAR) {
402 int16_t d_near = 100;
404 if (mainboard.speed_d > 2000)
407 if (trajectory_in_window(&mainboard.traj, d_near, RAD(5.0)) &&
408 is_in_boundingbox(&robot_pt))
412 if ((why & END_BLOCKING) && bd_get(&mainboard.angle.bd)) {
417 if ((why & END_BLOCKING) && bd_get(&mainboard.distance.bd)) {
422 if ((why & END_OBSTACLE) && strat_obstacle()) {
430 uint8_t __wait_traj_end_debug(uint8_t why, uint16_t line)
433 int16_t opp_x, opp_y, opp_d, opp_a;
436 ret = test_traj_end(why);
438 if (ret == END_OBSTACLE) {
439 if (get_opponent_xyda(&opp_x, &opp_y,
440 &opp_d, &opp_a) == 0)
441 DEBUG(E_USER_STRAT, "Got %s at line %d"
442 " xy=(%d,%d) da=(%d,%d)", get_err(ret),
443 line, opp_x, opp_y, opp_d, opp_a);
446 DEBUG(E_USER_STRAT, "Got %s at line %d",