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)) {
288 printf_P(PSTR("No start switch, press a key or plug it\r\n"));
290 /* while start sw not plugged */
291 while (sensor_get(S_START_SWITCH)) {
292 if (! cmdline_keypressed())
295 for (i=3; i>0; i--) {
296 printf_P(PSTR("%d\r\n"), i);
303 /* if start sw plugged */
304 if (!sensor_get(S_START_SWITCH)) {
305 printf_P(PSTR("Ready, unplug start switch to start\r\n"));
306 /* while start sw plugged */
307 while (!sensor_get(S_START_SWITCH));
312 NOTICE(E_USER_STRAT, "Finished !! returned %s", get_err(err));
316 /* return true if we have to brake due to an obstacle */
317 uint8_t strat_obstacle(void)
319 int16_t x_rel, y_rel;
320 int16_t opp_x, opp_y, opp_d, opp_a;
323 if (ABS(mainboard.speed_d) < 150)
326 /* no opponent detected */
327 if (get_opponent_xyda(&opp_x, &opp_y,
331 /* save obstacle position */
332 opponent_obstacle.x = opp_x;
333 opponent_obstacle.y = opp_y;
334 opponent_obstacle.d = opp_d;
335 opponent_obstacle.a = opp_a;
337 /* sensor are temporarily disabled */
338 if (sensor_obstacle_is_disabled())
341 /* relative position */
342 x_rel = cos(RAD(opp_a)) * (double)opp_d;
343 y_rel = sin(RAD(opp_a)) * (double)opp_d;
345 /* opponent too far */
349 /* opponent is in front of us */
350 if (mainboard.speed_d > 0 && (opp_a > 325 || opp_a < 35)) {
351 DEBUG(E_USER_STRAT, "opponent front d=%d, a=%d "
352 "xrel=%d yrel=%d (speed_d=%d)",
353 opp_d, opp_a, x_rel, y_rel, mainboard.speed_d);
354 sensor_obstacle_disable();
357 /* opponent is behind us */
358 if (mainboard.speed_d < 0 && (opp_a < 215 && opp_a > 145)) {
359 DEBUG(E_USER_STRAT, "opponent behind d=%d, a=%d xrel=%d yrel=%d",
360 opp_d, opp_a, x_rel, y_rel);
361 sensor_obstacle_disable();
368 void interrupt_traj(void)
373 void interrupt_traj_reset(void)
378 uint8_t test_traj_end(uint8_t why)
383 robot_pt.x = position_get_x_s16(&mainboard.pos);
384 robot_pt.y = position_get_y_s16(&mainboard.pos);
386 /* trigger an event at 3 sec before the end of the match if we
387 * have balls in the barrel */
388 cur_timer = time_get_s();
390 if ((mainboard.flags & DO_TIMER) && (why & END_TIMER)) {
392 if (cur_timer >= MATCH_TIME)
396 if ((why & END_INTR) && traj_intr) {
397 interrupt_traj_reset();
401 if ((why & END_TRAJ) && trajectory_finished(&mainboard.traj))
404 /* we are near the destination point (depends on current
405 * speed) AND the robot is in the area bounding box. */
406 if (why & END_NEAR) {
407 int16_t d_near = 100;
409 if (mainboard.speed_d > 2000)
412 if (trajectory_in_window(&mainboard.traj, d_near, RAD(5.0)) &&
413 is_in_boundingbox(&robot_pt))
417 if ((why & END_BLOCKING) && bd_get(&mainboard.angle.bd)) {
422 if ((why & END_BLOCKING) && bd_get(&mainboard.distance.bd)) {
427 if ((why & END_OBSTACLE) && strat_obstacle()) {
435 uint8_t __wait_traj_end_debug(uint8_t why, uint16_t line)
438 int16_t opp_x, opp_y, opp_d, opp_a;
441 ret = test_traj_end(why);
443 if (ret == END_OBSTACLE) {
444 if (get_opponent_xyda(&opp_x, &opp_y,
445 &opp_d, &opp_a) == 0)
446 DEBUG(E_USER_STRAT, "Got %s at line %d"
447 " xy=(%d,%d) da=(%d,%d)", get_err(ret),
448 line, opp_x, opp_y, opp_d, opp_a);
451 DEBUG(E_USER_STRAT, "Got %s at line %d",