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 <vect_base.h>
44 #include <obstacle_avoidance.h>
45 #include <blocking_detection_manager.h>
46 #include <robot_system.h>
47 #include <position_manager.h>
52 #include "../common/i2c_commands.h"
56 #include "strat_utils.h"
57 #include "strat_base.h"
60 #include "i2c_protocol.h"
62 /* true if we want to interrupt a trajectory */
63 static uint8_t traj_intr=0;
65 /* filled when a END_OBSTACLE is returned */
66 struct opponent_obstacle opponent_obstacle;
69 static volatile int16_t strat_speed_a = SPEED_DIST_FAST;
70 static volatile int16_t strat_speed_d = SPEED_ANGLE_FAST;
71 static volatile uint16_t strat_limit_speed_a = 0; /* no limit */
72 static volatile uint16_t strat_limit_speed_d = 0;
74 static volatile uint8_t strat_limit_speed_enabled = 1;
77 /* Strings that match the end traj cause */
78 /* /!\ keep it sync with stat_base.h */
79 const char *err_tab []= {
90 /* return string from end traj type num */
91 const char *get_err(uint8_t err)
96 for (i=0 ; i<8; i++) {
100 return "END_UNKNOWN";
103 void strat_hardstop(void)
105 trajectory_hardstop(&mainboard.traj);
106 pid_reset(&mainboard.angle.pid);
107 pid_reset(&mainboard.distance.pid);
108 bd_reset(&mainboard.angle.bd);
109 bd_reset(&mainboard.distance.bd);
111 while ((ABS(mainboard.speed_d) > 200) ||
112 (ABS(mainboard.speed_a) > 200))
114 trajectory_hardstop(&mainboard.traj);
115 pid_reset(&mainboard.angle.pid);
116 pid_reset(&mainboard.distance.pid);
117 bd_reset(&mainboard.angle.bd);
118 bd_reset(&mainboard.distance.bd);
121 /* go to an x,y point without checking for obstacle or blocking. It
122 * should be used for very small dist only. Return END_TRAJ if we
123 * reach destination, or END_BLOCKING if the robot blocked more than 3
125 uint8_t strat_goto_xy_force(int16_t x, int16_t y)
129 for (i=0; i<3; i++) {
130 trajectory_goto_xy_abs(&mainboard.traj, x, y);
131 err = wait_traj_end(TRAJ_FLAGS_SMALL_DIST);
132 if (TRAJ_SUCCESS(err))
134 if (err == END_BLOCKING) {
143 void strat_reset_pos(int16_t x, int16_t y, int16_t a)
145 int16_t posx = position_get_x_s16(&mainboard.pos);
146 int16_t posy = position_get_y_s16(&mainboard.pos);
147 int16_t posa = position_get_a_deg_s16(&mainboard.pos);
149 if (x == DO_NOT_SET_POS)
151 if (y == DO_NOT_SET_POS)
153 if (a == DO_NOT_SET_POS)
156 DEBUG(E_USER_STRAT, "reset pos (%s%s%s)",
157 x == DO_NOT_SET_POS ? "" : "x",
158 y == DO_NOT_SET_POS ? "" : "y",
159 a == DO_NOT_SET_POS ? "" : "a");
160 position_set(&mainboard.pos, x, y, a);
161 DEBUG(E_USER_STRAT, "pos resetted", __FUNCTION__);
165 * decrease gain on angle PID, and go forward until we reach the
168 uint8_t strat_calib(int16_t dist, uint8_t flags)
170 int32_t p = pid_get_gain_P(&mainboard.angle.pid);
171 int32_t i = pid_get_gain_I(&mainboard.angle.pid);
172 int32_t d = pid_get_gain_D(&mainboard.angle.pid);
175 pid_set_gains(&mainboard.angle.pid, 150, 0, 2000);
176 trajectory_d_rel(&mainboard.traj, dist);
177 err = wait_traj_end(flags);
178 pid_set_gains(&mainboard.angle.pid, p, i, d);
182 static void strat_update_traj_speed(void)
187 if (strat_limit_speed_d && d > strat_limit_speed_d)
188 d = strat_limit_speed_d;
190 if (strat_limit_speed_a && a > strat_limit_speed_a)
191 a = strat_limit_speed_a;
193 trajectory_set_speed(&mainboard.traj, d, a);
196 void strat_set_speed(uint16_t d, uint16_t a)
202 strat_update_traj_speed();
206 void strat_get_speed(uint16_t *d, uint16_t *a)
215 void strat_limit_speed_enable(void)
217 strat_limit_speed_enabled = 1;
220 void strat_limit_speed_disable(void)
222 strat_limit_speed_enabled = 0;
225 /* called periodically */
226 void strat_limit_speed(void)
228 uint16_t lim_d = 0, lim_a = 0;
229 int16_t opp_d, opp_a;
231 if (strat_limit_speed_enabled == 0)
234 if (get_opponent_da(&opp_d, &opp_a) != 0)
238 if (mainboard.speed_d > 0 && (opp_a > 290 || opp_a < 70)) {
239 lim_d = SPEED_DIST_VERY_SLOW;
240 lim_a = SPEED_ANGLE_VERY_SLOW;
242 else if (mainboard.speed_d < 0 && (opp_a < 250 && opp_a > 110)) {
243 lim_d = SPEED_DIST_VERY_SLOW;
244 lim_a = SPEED_ANGLE_VERY_SLOW;
247 lim_d = SPEED_DIST_SLOW;
248 lim_a = SPEED_ANGLE_VERY_SLOW;
251 else if (opp_d < 800) {
252 if (mainboard.speed_d > 0 && (opp_a > 290 || opp_a < 70)) {
253 lim_d = SPEED_DIST_SLOW;
254 lim_a = SPEED_ANGLE_SLOW;
256 else if (mainboard.speed_d < 0 && (opp_a < 250 && opp_a > 110)) {
257 lim_d = SPEED_DIST_SLOW;
258 lim_a = SPEED_ANGLE_SLOW;
263 if (lim_d != strat_limit_speed_d ||
264 lim_a != strat_limit_speed_a) {
265 strat_limit_speed_d = lim_d;
266 strat_limit_speed_a = lim_a;
267 DEBUG(E_USER_STRAT, "new speed limit da=%d,%d", lim_d, lim_a);
268 strat_update_traj_speed();
272 /* start the strat */
273 void strat_start(void)
280 /* if start sw not plugged */
281 if (sensor_get(S_START_SWITCH)) {
282 printf_P(PSTR("No start switch, press a key or plug it\r\n"));
284 /* while start sw not plugged */
285 while (sensor_get(S_START_SWITCH)) {
286 if (! cmdline_keypressed())
289 for (i=3; i>0; i--) {
290 printf_P(PSTR("%d\r\n"), i);
297 /* if start sw plugged */
298 if (!sensor_get(S_START_SWITCH)) {
299 printf_P(PSTR("Ready, unplug start switch to start\r\n"));
300 /* while start sw plugged */
301 while (!sensor_get(S_START_SWITCH));
306 NOTICE(E_USER_STRAT, "Finished !! returned %s", get_err(err));
310 /* return true if we have to brake due to an obstacle */
311 uint8_t strat_obstacle(void)
313 int16_t x_rel, y_rel;
314 int16_t opp_x, opp_y, opp_d, opp_a;
317 if (ABS(mainboard.speed_d) < 150)
320 /* no opponent detected */
321 if (get_opponent_xyda(&opp_x, &opp_y,
325 /* save obstacle position */
326 opponent_obstacle.x = opp_x;
327 opponent_obstacle.y = opp_y;
328 opponent_obstacle.d = opp_d;
329 opponent_obstacle.a = opp_a;
331 /* sensor are temporarily disabled */
332 if (sensor_obstacle_is_disabled())
335 /* relative position */
336 x_rel = cos(RAD(opp_a)) * (double)opp_d;
337 y_rel = sin(RAD(opp_a)) * (double)opp_d;
339 /* opponent too far */
343 /* opponent is in front of us */
344 if (mainboard.speed_d > 0 && (opp_a > 325 || opp_a < 35)) {
345 DEBUG(E_USER_STRAT, "opponent front d=%d, a=%d "
346 "xrel=%d yrel=%d (speed_d=%d)",
347 opp_d, opp_a, x_rel, y_rel, mainboard.speed_d);
348 sensor_obstacle_disable();
351 /* opponent is behind us */
352 if (mainboard.speed_d < 0 && (opp_a < 215 && opp_a > 145)) {
353 DEBUG(E_USER_STRAT, "opponent behind d=%d, a=%d xrel=%d yrel=%d",
354 opp_d, opp_a, x_rel, y_rel);
355 sensor_obstacle_disable();
362 void interrupt_traj(void)
367 void interrupt_traj_reset(void)
372 uint8_t test_traj_end(uint8_t why)
377 robot_pt.x = position_get_x_s16(&mainboard.pos);
378 robot_pt.y = position_get_y_s16(&mainboard.pos);
380 /* trigger an event at 3 sec before the end of the match if we
381 * have balls in the barrel */
382 cur_timer = time_get_s();
384 if ((mainboard.flags & DO_TIMER) && (why & END_TIMER)) {
386 if (cur_timer >= MATCH_TIME)
390 if ((why & END_INTR) && traj_intr) {
391 interrupt_traj_reset();
395 if ((why & END_TRAJ) && trajectory_finished(&mainboard.traj))
398 /* we are near the destination point (depends on current
399 * speed) AND the robot is in the area bounding box. */
400 if (why & END_NEAR) {
401 int16_t d_near = 100;
403 if (mainboard.speed_d > 2000)
406 if (trajectory_in_window(&mainboard.traj, d_near, RAD(5.0)) &&
407 is_in_boundingbox(&robot_pt))
411 if ((why & END_BLOCKING) && bd_get(&mainboard.angle.bd)) {
416 if ((why & END_BLOCKING) && bd_get(&mainboard.distance.bd)) {
421 if ((why & END_OBSTACLE) && strat_obstacle()) {
429 uint8_t __wait_traj_end_debug(uint8_t why, uint16_t line)
432 int16_t opp_x, opp_y, opp_d, opp_a;
435 ret = test_traj_end(why);
437 if (ret == END_OBSTACLE) {
438 if (get_opponent_xyda(&opp_x, &opp_y,
439 &opp_d, &opp_a) == 0)
440 DEBUG(E_USER_STRAT, "Got %s at line %d"
441 " xy=(%d,%d) da=(%d,%d)", get_err(ret),
442 line, opp_x, opp_y, opp_d, opp_a);
445 DEBUG(E_USER_STRAT, "Got %s at line %d",