cob detection
[aversive.git] / projects / microb2010 / mainboard / strat_base.c
1 /*
2  *  Copyright Droids Corporation, Microb Technology (2009)
3  *
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.
8  *
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.
13  *
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
17  *
18  *  Revision : $Id: strat_base.c,v 1.8 2009-11-08 17:24:33 zer0 Exp $
19  *
20  */
21
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <math.h>
26
27 #include <aversive/pgmspace.h>
28 #include <aversive/wait.h>
29 #include <aversive/error.h>
30
31 #include <ax12.h>
32 #include <uart.h>
33 #include <pwm_ng.h>
34 #include <clock_time.h>
35 #include <spi.h>
36
37 #include <pid.h>
38 #include <quadramp.h>
39 #include <control_system_manager.h>
40 #include <trajectory_manager.h>
41 #include <trajectory_manager_utils.h>
42 #include <vect_base.h>
43 #include <lines.h>
44 #include <polygon.h>
45 #include <obstacle_avoidance.h>
46 #include <blocking_detection_manager.h>
47 #include <robot_system.h>
48 #include <position_manager.h>
49
50 #include <rdline.h>
51 #include <parse.h>
52
53 #include "../common/i2c_commands.h"
54
55 #include "main.h"
56 #include "cmdline.h"
57 #include "strat_utils.h"
58 #include "strat_base.h"
59 #include "strat.h"
60 #include "sensor.h"
61 #include "i2c_protocol.h"
62
63 /* true if we want to interrupt a trajectory */
64 static uint8_t traj_intr=0;
65
66 /* filled when a END_OBSTACLE is returned */
67 struct opponent_obstacle opponent_obstacle;
68
69 /* asked speed */
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;
74
75 static volatile uint8_t strat_limit_speed_enabled = 1;
76
77
78 /* Strings that match the end traj cause */
79 /* /!\ keep it sync with stat_base.h */
80 const char *err_tab []= {
81         "END_TRAJ",
82         "END_BLOCKING",
83         "END_NEAR",
84         "END_OBSTACLE",
85         "END_ERROR",
86         "END_INTR",
87         "END_TIMER",
88         "END_RESERVED",
89 };
90
91 /* return string from end traj type num */
92 const char *get_err(uint8_t err)
93 {
94         uint8_t i;
95         if (err == 0)
96                 return "SUCCESS";
97         for (i=0 ; i<8; i++) {
98                 if (err & (1 <<i))
99                         return err_tab[i];
100         }
101         return "END_UNKNOWN";
102 }
103
104 void strat_hardstop(void)
105 {
106         DEBUG(E_USER_STRAT, "strat_hardstop");
107
108         trajectory_hardstop(&mainboard.traj);
109         pid_reset(&mainboard.angle.pid);
110         pid_reset(&mainboard.distance.pid);
111         bd_reset(&mainboard.angle.bd);
112         bd_reset(&mainboard.distance.bd);
113
114         while ((ABS(mainboard.speed_d) > 200) ||
115                (ABS(mainboard.speed_a) > 200))
116
117         trajectory_hardstop(&mainboard.traj);
118         pid_reset(&mainboard.angle.pid);
119         pid_reset(&mainboard.distance.pid);
120         bd_reset(&mainboard.angle.bd);
121         bd_reset(&mainboard.distance.bd);
122 }
123
124 /* go to an x,y point without checking for obstacle or blocking. It
125  * should be used for very small dist only. Return END_TRAJ if we
126  * reach destination, or END_BLOCKING if the robot blocked more than 3
127  * times. */
128 uint8_t strat_goto_xy_force(int16_t x, int16_t y)
129 {
130         uint8_t i, err;
131
132         for (i=0; i<3; i++) {
133                 trajectory_goto_xy_abs(&mainboard.traj, x, y);
134                 err = wait_traj_end(TRAJ_FLAGS_SMALL_DIST);
135                 if (TRAJ_SUCCESS(err))
136                         return END_TRAJ;
137                 if (err == END_BLOCKING) {
138                         time_wait_ms(500);
139                         strat_hardstop();
140                 }
141         }
142         return END_BLOCKING;
143 }
144
145 /* reset position */
146 void strat_reset_pos(int16_t x, int16_t y, int16_t a)
147 {
148         int16_t posx = position_get_x_s16(&mainboard.pos);
149         int16_t posy = position_get_y_s16(&mainboard.pos);
150         int16_t posa = position_get_a_deg_s16(&mainboard.pos);
151
152         if (x == DO_NOT_SET_POS)
153                 x = posx;
154         if (y == DO_NOT_SET_POS)
155                 y = posy;
156         if (a == DO_NOT_SET_POS)
157                 a = posa;
158
159         DEBUG(E_USER_STRAT, "reset pos (%s%s%s)",
160               x == DO_NOT_SET_POS ? "" : "x",
161               y == DO_NOT_SET_POS ? "" : "y",
162               a == DO_NOT_SET_POS ? "" : "a");
163         position_set(&mainboard.pos, x, y, a);
164         DEBUG(E_USER_STRAT, "pos resetted", __FUNCTION__);
165 }
166
167 /*
168  * decrease gain on angle PID, and go forward until we reach the
169  * border.
170  */
171 uint8_t strat_calib(int16_t dist, uint8_t flags)
172 {
173         int32_t p = pid_get_gain_P(&mainboard.angle.pid);
174         int32_t i = pid_get_gain_I(&mainboard.angle.pid);
175         int32_t d = pid_get_gain_D(&mainboard.angle.pid);
176         int32_t max_in = pid_get_max_in(&mainboard.angle.pid);
177         int32_t max_i = pid_get_max_I(&mainboard.angle.pid);
178         int32_t max_out = pid_get_max_out(&mainboard.angle.pid);
179         uint8_t err;
180
181         pid_set_maximums(&mainboard.distance.pid, 0, 20000, 1000);
182         pid_set_gains(&mainboard.angle.pid, 200, 0, 2000);
183         trajectory_d_rel(&mainboard.traj, dist);
184         err = wait_traj_end(flags);
185         pid_set_gains(&mainboard.angle.pid, p, i, d);
186         pid_set_maximums(&mainboard.distance.pid, max_in, max_i, max_out);
187         return err;
188 }
189
190 static void strat_update_traj_speed(void)
191 {
192         uint16_t d, a;
193
194         d = strat_speed_d;
195         if (strat_limit_speed_d && d > strat_limit_speed_d)
196                 d = strat_limit_speed_d;
197         a = strat_speed_a;
198         if (strat_limit_speed_a && a > strat_limit_speed_a)
199                 a = strat_limit_speed_a;
200
201         trajectory_set_speed(&mainboard.traj, d, a);
202 }
203
204 void strat_set_speed(uint16_t d, uint16_t a)
205 {
206         uint8_t flags;
207         IRQ_LOCK(flags);
208         strat_speed_d = d;
209         strat_speed_a = a;
210         strat_update_traj_speed();
211         IRQ_UNLOCK(flags);
212 }
213
214 void strat_set_acc(double d, double a)
215 {
216         trajectory_set_acc(&mainboard.traj, d, a);
217 }
218
219 void strat_get_speed(uint16_t *d, uint16_t *a)
220 {
221         uint8_t flags;
222         IRQ_LOCK(flags);
223         *d = strat_speed_d;
224         *a = strat_speed_a;
225         IRQ_UNLOCK(flags);
226 }
227
228 void strat_limit_speed_enable(void)
229 {
230         strat_limit_speed_enabled = 1;
231 }
232
233 void strat_limit_speed_disable(void)
234 {
235         strat_limit_speed_enabled = 0;
236 }
237
238 /* called periodically */
239 void strat_limit_speed(void)
240 {
241         uint16_t lim_d = 0, lim_a = 0;
242         int16_t opp_d, opp_a;
243
244         if (strat_limit_speed_enabled == 0)
245                 goto update;
246
247         if (get_opponent_da(&opp_d, &opp_a) != 0)
248                 goto update;
249
250         if (opp_d < 500) {
251                 if (mainboard.speed_d > 0 && (opp_a > 290 || opp_a < 70)) {
252                         lim_d = SPEED_DIST_VERY_SLOW;
253                         lim_a = SPEED_ANGLE_VERY_SLOW;
254                 }
255                 else if (mainboard.speed_d < 0 && (opp_a < 250 && opp_a > 110)) {
256                         lim_d = SPEED_DIST_VERY_SLOW;
257                         lim_a = SPEED_ANGLE_VERY_SLOW;
258                 }
259                 else {
260                         lim_d = SPEED_DIST_SLOW;
261                         lim_a = SPEED_ANGLE_VERY_SLOW;
262                 }
263         }
264         else if (opp_d < 800) {
265                 if (mainboard.speed_d > 0 && (opp_a > 290 || opp_a < 70)) {
266                         lim_d = SPEED_DIST_SLOW;
267                         lim_a = SPEED_ANGLE_SLOW;
268                 }
269                 else if (mainboard.speed_d < 0 && (opp_a < 250 && opp_a > 110)) {
270                         lim_d = SPEED_DIST_SLOW;
271                         lim_a = SPEED_ANGLE_SLOW;
272                 }
273         }
274
275  update:
276         if (lim_d != strat_limit_speed_d ||
277             lim_a != strat_limit_speed_a) {
278                 strat_limit_speed_d = lim_d;
279                 strat_limit_speed_a = lim_a;
280                 DEBUG(E_USER_STRAT, "new speed limit da=%d,%d", lim_d, lim_a);
281                 strat_update_traj_speed();
282         }
283 }
284
285 /* start the strat */
286 void strat_start(void)
287 {
288         uint8_t err;
289
290         strat_preinit();
291
292 #ifndef HOST_VERSION
293         /* if start sw not plugged */
294         if (sensor_get(S_START_SWITCH)) {
295                 int8_t i;
296
297                 printf_P(PSTR("No start switch, press a key or plug it\r\n"));
298
299                 /* while start sw not plugged */
300                 while (sensor_get(S_START_SWITCH)) {
301                         if (! cmdline_keypressed())
302                                 continue;
303
304                         for (i=3; i>0; i--) {
305                                 printf_P(PSTR("%d\r\n"), i);
306                                 time_wait_ms(1000);
307                         }
308                         break;
309                 }
310         }
311
312         /* if start sw plugged */
313         if (!sensor_get(S_START_SWITCH)) {
314                 printf_P(PSTR("Ready, unplug start switch to start\r\n"));
315                 /* while start sw plugged */
316                 while (!sensor_get(S_START_SWITCH));
317         }
318 #endif
319
320         strat_init();
321         err = strat_main();
322         NOTICE(E_USER_STRAT, "Finished !! returned %s", get_err(err));
323         strat_exit();
324 }
325
326 /* return true if we have to brake due to an obstacle */
327 uint8_t strat_obstacle(void)
328 {
329 #if 0
330         int16_t x_rel, y_rel;
331         int16_t opp_x, opp_y, opp_d, opp_a;
332
333         /* too slow */
334         if (ABS(mainboard.speed_d) < 150)
335                 return 0;
336
337         /* no opponent detected */
338         if (get_opponent_xyda(&opp_x, &opp_y,
339                               &opp_d, &opp_a))
340                 return 0;
341
342         /* save obstacle position */
343         opponent_obstacle.x = opp_x;
344         opponent_obstacle.y = opp_y;
345         opponent_obstacle.d = opp_d;
346         opponent_obstacle.a = opp_a;
347 #else /* belgium cup only */
348         int16_t x_rel, y_rel;
349         int16_t opp_d, opp_a;
350         double opp_x, opp_y;
351
352 #ifdef HOST_VERSION
353         return 0;
354         if (time_get_s() >= 12 && time_get_s() <= 30)
355                 return 1;
356 #endif
357         return 0; /* XXX disabled */
358
359         if (!sensor_get(S_RCOB_WHITE))
360                 return 0;
361
362         opp_a = 0;
363         opp_d = 300;
364
365         rel_da_to_abs_xy(opp_d, RAD(opp_a), &opp_x, &opp_y);
366         if (!is_in_area(opp_x, opp_y, 250))
367                 return 0;
368 #endif
369
370         /* sensor are temporarily disabled */
371         if (sensor_obstacle_is_disabled())
372                 return 0;
373
374         /* relative position */
375         x_rel = cos(RAD(opp_a)) * (double)opp_d;
376         y_rel = sin(RAD(opp_a)) * (double)opp_d;
377
378         /* opponent too far */
379         if (opp_d > 600)
380                 return 0;
381
382         /* opponent is in front of us */
383         if (mainboard.speed_d > 0 && (opp_a > 325 || opp_a < 35)) {
384                 DEBUG(E_USER_STRAT, "opponent front d=%d, a=%d "
385                       "xrel=%d yrel=%d (speed_d=%d)",
386                       opp_d, opp_a, x_rel, y_rel, mainboard.speed_d);
387                 sensor_obstacle_disable();
388                 return 1;
389         }
390         /* opponent is behind us */
391         if (mainboard.speed_d < 0 && (opp_a < 215 && opp_a > 145)) {
392                 DEBUG(E_USER_STRAT, "opponent behind d=%d, a=%d xrel=%d yrel=%d",
393                       opp_d, opp_a, x_rel, y_rel);
394                 sensor_obstacle_disable();
395                 return 1;
396         }
397
398         return 0;
399 }
400
401 void interrupt_traj(void)
402 {
403         traj_intr = 1;
404 }
405
406 void interrupt_traj_reset(void)
407 {
408         traj_intr = 0;
409 }
410
411 uint8_t test_traj_end(uint8_t why)
412 {
413         uint16_t cur_timer;
414         point_t robot_pt;
415
416         robot_pt.x = position_get_x_s16(&mainboard.pos);
417         robot_pt.y = position_get_y_s16(&mainboard.pos);
418
419         /* trigger an event at 3 sec before the end of the match if we
420          * have balls in the barrel */
421         cur_timer = time_get_s();
422
423         if ((mainboard.flags & DO_TIMER) && (why & END_TIMER)) {
424                 /* end of match */
425                 if (cur_timer >= MATCH_TIME)
426                         return END_TIMER;
427         }
428
429         if ((why & END_INTR) && traj_intr) {
430                 interrupt_traj_reset();
431                 return END_INTR;
432         }
433
434         if ((why & END_TRAJ) && trajectory_finished(&mainboard.traj))
435                 return END_TRAJ;
436
437         /* we are near the destination point (depends on current
438          * speed) AND the robot is in the area bounding box. */
439         if (why & END_NEAR) {
440                 int16_t d_near = 100;
441
442                 if (mainboard.speed_d > 2000)
443                         d_near = 150;
444
445                 if (trajectory_in_window(&mainboard.traj, d_near, RAD(5.0)) &&
446                     is_in_boundingbox(&robot_pt))
447                         return END_NEAR;
448         }
449
450         if ((why & END_BLOCKING) && bd_get(&mainboard.angle.bd)) {
451                 strat_hardstop();
452                 return END_BLOCKING;
453         }
454
455         if ((why & END_BLOCKING) && bd_get(&mainboard.distance.bd)) {
456                 strat_hardstop();
457                 return END_BLOCKING;
458         }
459
460         if ((why & END_OBSTACLE) && strat_obstacle()) {
461                 strat_hardstop();
462                 return END_OBSTACLE;
463         }
464
465         return 0;
466 }
467
468 uint8_t __wait_traj_end_debug(uint8_t why, uint16_t line)
469 {
470         uint8_t ret = 0;
471         int16_t opp_x, opp_y, opp_d, opp_a;
472
473         while (ret == 0)
474                 ret = test_traj_end(why);
475
476         if (ret == END_OBSTACLE) {
477                 if (get_opponent_xyda(&opp_x, &opp_y,
478                                       &opp_d, &opp_a) == 0)
479                         DEBUG(E_USER_STRAT, "Got %s at line %d"
480                               " xy=(%d,%d) da=(%d,%d)", get_err(ret),
481                               line, opp_x, opp_y, opp_d, opp_a);
482         }
483         else {
484                 DEBUG(E_USER_STRAT, "Got %s at line %d",
485                       get_err(ret), line);
486         }
487         return ret;
488 }