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>
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)
131 uint8_t hardstop = 0;
132 microseconds us = time_get_us2();
133 int16_t opp_a, opp_d, opp_x, opp_y;
136 serr = get_opponent_xyda(&opp_x, &opp_y,
146 if ((time_get_us2() - us) > 3000000L)
150 for (i=0; i<3; i++) {
151 trajectory_goto_xy_abs(&mainboard.traj, x, y);
152 err = wait_traj_end(TRAJ_FLAGS_SMALL_DIST);
153 if (TRAJ_SUCCESS(err))
155 if (err == END_BLOCKING) {
164 void strat_reset_pos(int16_t x, int16_t y, int16_t a)
166 int16_t posx = position_get_x_s16(&mainboard.pos);
167 int16_t posy = position_get_y_s16(&mainboard.pos);
168 int16_t posa = position_get_a_deg_s16(&mainboard.pos);
170 if (x == DO_NOT_SET_POS)
172 if (y == DO_NOT_SET_POS)
174 if (a == DO_NOT_SET_POS)
177 DEBUG(E_USER_STRAT, "reset pos (%s%s%s)",
178 x == DO_NOT_SET_POS ? "" : "x",
179 y == DO_NOT_SET_POS ? "" : "y",
180 a == DO_NOT_SET_POS ? "" : "a");
181 position_set(&mainboard.pos, x, y, a);
182 DEBUG(E_USER_STRAT, "pos resetted", __FUNCTION__);
186 * decrease gain on angle PID, and go forward until we reach the
189 uint8_t strat_calib(int16_t dist, uint8_t flags)
191 int32_t p = pid_get_gain_P(&mainboard.angle.pid);
192 int32_t i = pid_get_gain_I(&mainboard.angle.pid);
193 int32_t d = pid_get_gain_D(&mainboard.angle.pid);
196 pid_set_gains(&mainboard.angle.pid, 150, 0, 2000);
197 trajectory_d_rel(&mainboard.traj, dist);
198 err = wait_traj_end(flags);
199 pid_set_gains(&mainboard.angle.pid, p, i, d);
203 /* escape from zone, and don't brake, so we can continue with another
205 uint8_t strat_escape(struct build_zone *zone, uint8_t flags)
208 uint16_t old_spdd, old_spda;
210 strat_get_speed(&old_spdd, &old_spda);
212 err = WAIT_COND_OR_TIMEOUT(!opponent_is_behind(), 1000);
214 strat_set_speed(SPEED_DIST_VERY_SLOW, SPEED_ANGLE_FAST);
215 trajectory_d_rel(&mainboard.traj, -150);
216 err = wait_traj_end(TRAJ_FLAGS_SMALL_DIST);
217 strat_set_speed(old_spdd, old_spda);
221 strat_set_speed(SPEED_DIST_FAST, SPEED_ANGLE_FAST);
223 if (zone->flags & ZONE_F_DISC) {
224 trajectory_d_rel(&mainboard.traj, -350);
225 err = WAIT_COND_OR_TRAJ_END(!robot_is_near_disc(), flags);
228 trajectory_d_rel(&mainboard.traj, -300);
229 err = wait_traj_end(flags);
232 strat_set_speed(old_spdd, old_spda);
238 static void strat_update_traj_speed(void)
243 if (strat_limit_speed_d && d > strat_limit_speed_d)
244 d = strat_limit_speed_d;
246 if (strat_limit_speed_a && a > strat_limit_speed_a)
247 a = strat_limit_speed_a;
249 trajectory_set_speed(&mainboard.traj, d, a);
252 void strat_set_speed(uint16_t d, uint16_t a)
258 strat_update_traj_speed();
262 void strat_get_speed(uint16_t *d, uint16_t *a)
271 void strat_limit_speed_enable(void)
273 strat_limit_speed_enabled = 1;
276 void strat_limit_speed_disable(void)
278 strat_limit_speed_enabled = 0;
281 /* called periodically */
282 void strat_limit_speed(void)
284 uint16_t lim_d = 0, lim_a = 0;
285 int16_t opp_d, opp_a;
287 if (strat_limit_speed_enabled == 0)
290 if (get_opponent_da(&opp_d, &opp_a) != 0)
300 if (mainboard.speed_d > 0 && (opp_a > 290 || opp_a < 70)) {
301 lim_d = SPEED_DIST_VERY_SLOW;
302 lim_a = SPEED_ANGLE_VERY_SLOW;
304 else if (mainboard.speed_d < 0 && (opp_a < 250 && opp_a > 110)) {
305 lim_d = SPEED_DIST_VERY_SLOW;
306 lim_a = SPEED_ANGLE_VERY_SLOW;
309 lim_d = SPEED_DIST_SLOW;
310 lim_a = SPEED_ANGLE_VERY_SLOW;
314 else if (opp_d < 800) {
315 if (mainboard.speed_d > 0 && (opp_a > 290 || opp_a < 70)) {
316 lim_d = SPEED_DIST_SLOW;
317 lim_a = SPEED_ANGLE_SLOW;
319 else if (mainboard.speed_d < 0 && (opp_a < 250 && opp_a > 110)) {
320 lim_d = SPEED_DIST_SLOW;
321 lim_a = SPEED_ANGLE_SLOW;
326 if (lim_d != strat_limit_speed_d ||
327 lim_a != strat_limit_speed_a) {
328 strat_limit_speed_d = lim_d;
329 strat_limit_speed_a = lim_a;
330 DEBUG(E_USER_STRAT, "new speed limit da=%d,%d", lim_d, lim_a);
331 strat_update_traj_speed();
335 /* start the strat */
336 void strat_start(void)
343 /* if start sw not plugged */
344 if (sensor_get(S_START_SWITCH)) {
345 printf_P(PSTR("No start switch, press a key or plug it\r\n"));
347 /* while start sw not plugged */
348 while (sensor_get(S_START_SWITCH)) {
349 if (! cmdline_keypressed())
352 for (i=3; i>0; i--) {
353 printf_P(PSTR("%d\r\n"), i);
360 /* if start sw plugged */
361 if (!sensor_get(S_START_SWITCH)) {
362 printf_P(PSTR("Ready, unplug start switch to start\r\n"));
363 /* while start sw plugged */
364 while (!sensor_get(S_START_SWITCH));
369 NOTICE(E_USER_STRAT, "Finished !! returned %s", get_err(err));
373 /* return true if we have to brake due to an obstacle */
374 uint8_t strat_obstacle(void)
376 int16_t x_rel, y_rel;
377 int16_t opp_x, opp_y, opp_d, opp_a;
380 if (ABS(mainboard.speed_d) < 150)
383 /* no opponent detected */
384 if (get_opponent_xyda(&opp_x, &opp_y,
388 /* save obstacle position */
389 opponent_obstacle.x = opp_x;
390 opponent_obstacle.y = opp_y;
391 opponent_obstacle.d = opp_d;
392 opponent_obstacle.a = opp_a;
394 /* sensor are temporarily disabled */
395 if (sensor_obstacle_is_disabled())
398 /* relative position */
399 x_rel = cos(RAD(opp_a)) * (double)opp_d;
400 y_rel = sin(RAD(opp_a)) * (double)opp_d;
402 /* opponent too far */
406 /* opponent is in front of us */
407 if (mainboard.speed_d > 0 && (opp_a > 325 || opp_a < 35)) {
408 DEBUG(E_USER_STRAT, "opponent front d=%d, a=%d "
409 "xrel=%d yrel=%d (speed_d=%d)",
410 opp_d, opp_a, x_rel, y_rel, mainboard.speed_d);
411 sensor_obstacle_disable();
414 /* opponent is behind us */
415 if (mainboard.speed_d < 0 && (opp_a < 215 && opp_a > 145)) {
416 DEBUG(E_USER_STRAT, "opponent behind d=%d, a=%d xrel=%d yrel=%d",
417 opp_d, opp_a, x_rel, y_rel);
418 sensor_obstacle_disable();
425 void interrupt_traj(void)
430 void interrupt_traj_reset(void)
435 uint8_t test_traj_end(uint8_t why)
440 robot_pt.x = position_get_x_s16(&mainboard.pos);
441 robot_pt.y = position_get_y_s16(&mainboard.pos);
443 /* trigger an event at 3 sec before the end of the match if we
444 * have balls in the barrel */
445 cur_timer = time_get_s();
447 if ((mainboard.flags & DO_TIMER) && (why & END_TIMER)) {
449 if (cur_timer >= MATCH_TIME)
453 if ((why & END_INTR) && traj_intr) {
454 interrupt_traj_reset();
458 if ((why & END_TRAJ) && trajectory_finished(&mainboard.traj))
461 /* we are near the destination point (depends on current
462 * speed) AND the robot is in the area bounding box. */
463 if (why & END_NEAR) {
464 int16_t d_near = 100;
466 if (mainboard.speed_d > 2000)
469 if (trajectory_in_window(&mainboard.traj, d_near, RAD(5.0)) &&
470 is_in_boundingbox(&robot_pt))
474 if ((why & END_BLOCKING) && bd_get(&mainboard.angle.bd)) {
479 if ((why & END_BLOCKING) && bd_get(&mainboard.distance.bd)) {
484 if ((why & END_OBSTACLE) && strat_obstacle()) {
492 uint8_t __wait_traj_end_debug(uint8_t why, uint16_t line)
495 int16_t opp_x, opp_y, opp_d, opp_a;
498 ret = test_traj_end(why);
500 if (ret == END_OBSTACLE) {
501 if (get_opponent_xyda(&opp_x, &opp_y,
502 &opp_d, &opp_a) == 0)
503 DEBUG(E_USER_STRAT, "Got %s at line %d"
504 " xy=(%d,%d) da=(%d,%d)", get_err(ret),
505 line, opp_x, opp_y, opp_d, opp_a);
508 DEBUG(E_USER_STRAT, "Got %s at line %d",