high level strat
[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         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);
111
112         while ((ABS(mainboard.speed_d) > 200) ||
113                (ABS(mainboard.speed_a) > 200))
114
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);
120 }
121
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
125  * times. */
126 uint8_t strat_goto_xy_force(int16_t x, int16_t y)
127 {
128         uint8_t i, err;
129
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))
134                         return END_TRAJ;
135                 if (err == END_BLOCKING) {
136                         time_wait_ms(500);
137                         strat_hardstop();
138                 }
139         }
140         return END_BLOCKING;
141 }
142
143 /* reset position */
144 void strat_reset_pos(int16_t x, int16_t y, int16_t a)
145 {
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);
149
150         if (x == DO_NOT_SET_POS)
151                 x = posx;
152         if (y == DO_NOT_SET_POS)
153                 y = posy;
154         if (a == DO_NOT_SET_POS)
155                 a = posa;
156
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__);
163 }
164
165 /*
166  * decrease gain on angle PID, and go forward until we reach the
167  * border.
168  */
169 uint8_t strat_calib(int16_t dist, uint8_t flags)
170 {
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);
177         uint8_t err;
178
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);
185         return err;
186 }
187
188 static void strat_update_traj_speed(void)
189 {
190         uint16_t d, a;
191
192         d = strat_speed_d;
193         if (strat_limit_speed_d && d > strat_limit_speed_d)
194                 d = strat_limit_speed_d;
195         a = strat_speed_a;
196         if (strat_limit_speed_a && a > strat_limit_speed_a)
197                 a = strat_limit_speed_a;
198
199         trajectory_set_speed(&mainboard.traj, d, a);
200 }
201
202 void strat_set_speed(uint16_t d, uint16_t a)
203 {
204         uint8_t flags;
205         IRQ_LOCK(flags);
206         strat_speed_d = d;
207         strat_speed_a = a;
208         strat_update_traj_speed();
209         IRQ_UNLOCK(flags);
210 }
211
212 void strat_get_speed(uint16_t *d, uint16_t *a)
213 {
214         uint8_t flags;
215         IRQ_LOCK(flags);
216         *d = strat_speed_d;
217         *a = strat_speed_a;
218         IRQ_UNLOCK(flags);
219 }
220
221 void strat_limit_speed_enable(void)
222 {
223         strat_limit_speed_enabled = 1;
224 }
225
226 void strat_limit_speed_disable(void)
227 {
228         strat_limit_speed_enabled = 0;
229 }
230
231 /* called periodically */
232 void strat_limit_speed(void)
233 {
234         uint16_t lim_d = 0, lim_a = 0;
235         int16_t opp_d, opp_a;
236
237         if (strat_limit_speed_enabled == 0)
238                 goto update;
239
240         if (get_opponent_da(&opp_d, &opp_a) != 0)
241                 goto update;
242
243         if (opp_d < 500) {
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;
247                 }
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;
251                 }
252                 else {
253                         lim_d = SPEED_DIST_SLOW;
254                         lim_a = SPEED_ANGLE_VERY_SLOW;
255                 }
256         }
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;
261                 }
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;
265                 }
266         }
267
268  update:
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();
275         }
276 }
277
278 /* start the strat */
279 void strat_start(void)
280 {
281         uint8_t err;
282
283         strat_preinit();
284
285 #ifndef HOST_VERSION
286         /* if start sw not plugged */
287         if (sensor_get(S_START_SWITCH)) {
288                 int8_t i;
289
290                 printf_P(PSTR("No start switch, press a key or plug it\r\n"));
291
292                 /* while start sw not plugged */
293                 while (sensor_get(S_START_SWITCH)) {
294                         if (! cmdline_keypressed())
295                                 continue;
296
297                         for (i=3; i>0; i--) {
298                                 printf_P(PSTR("%d\r\n"), i);
299                                 time_wait_ms(1000);
300                         }
301                         break;
302                 }
303         }
304
305         /* if start sw plugged */
306         if (!sensor_get(S_START_SWITCH)) {
307                 printf_P(PSTR("Ready, unplug start switch to start\r\n"));
308                 /* while start sw plugged */
309                 while (!sensor_get(S_START_SWITCH));
310         }
311 #endif
312
313         strat_init();
314         err = strat_main();
315         printf("coucou\n");
316         NOTICE(E_USER_STRAT, "Finished !! returned %s", get_err(err));
317         strat_exit();
318 }
319
320 /* return true if we have to brake due to an obstacle */
321 uint8_t strat_obstacle(void)
322 {
323 #if 0
324         int16_t x_rel, y_rel;
325         int16_t opp_x, opp_y, opp_d, opp_a;
326
327         /* too slow */
328         if (ABS(mainboard.speed_d) < 150)
329                 return 0;
330
331         /* no opponent detected */
332         if (get_opponent_xyda(&opp_x, &opp_y,
333                               &opp_d, &opp_a))
334                 return 0;
335
336         /* save obstacle position */
337         opponent_obstacle.x = opp_x;
338         opponent_obstacle.y = opp_y;
339         opponent_obstacle.d = opp_d;
340         opponent_obstacle.a = opp_a;
341 #else /* belgium cup only */
342         int16_t x_rel, y_rel;
343         int16_t opp_d, opp_a;
344         double opp_x, opp_y;
345
346 #ifdef HOST_VERSION
347         if (time_get_s() >= 12 && time_get_s() <= 30)
348                 return 1;
349 #endif
350         if (!sensor_get(S_RCOB_WHITE))
351                 return 0;
352
353         opp_a = 0;
354         opp_d = 300;
355
356         rel_da_to_abs_xy(opp_d, RAD(opp_a), &opp_x, &opp_y);
357         if (!is_in_area(opp_x, opp_y, 250))
358                 return 0;
359 #endif
360
361         /* sensor are temporarily disabled */
362         if (sensor_obstacle_is_disabled())
363                 return 0;
364
365         /* relative position */
366         x_rel = cos(RAD(opp_a)) * (double)opp_d;
367         y_rel = sin(RAD(opp_a)) * (double)opp_d;
368
369         /* opponent too far */
370         if (opp_d > 600)
371                 return 0;
372
373         /* opponent is in front of us */
374         if (mainboard.speed_d > 0 && (opp_a > 325 || opp_a < 35)) {
375                 DEBUG(E_USER_STRAT, "opponent front d=%d, a=%d "
376                       "xrel=%d yrel=%d (speed_d=%d)",
377                       opp_d, opp_a, x_rel, y_rel, mainboard.speed_d);
378                 sensor_obstacle_disable();
379                 return 1;
380         }
381         /* opponent is behind us */
382         if (mainboard.speed_d < 0 && (opp_a < 215 && opp_a > 145)) {
383                 DEBUG(E_USER_STRAT, "opponent behind d=%d, a=%d xrel=%d yrel=%d",
384                       opp_d, opp_a, x_rel, y_rel);
385                 sensor_obstacle_disable();
386                 return 1;
387         }
388
389         return 0;
390 }
391
392 void interrupt_traj(void)
393 {
394         traj_intr = 1;
395 }
396
397 void interrupt_traj_reset(void)
398 {
399         traj_intr = 0;
400 }
401
402 uint8_t test_traj_end(uint8_t why)
403 {
404         uint16_t cur_timer;
405         point_t robot_pt;
406
407         robot_pt.x = position_get_x_s16(&mainboard.pos);
408         robot_pt.y = position_get_y_s16(&mainboard.pos);
409
410         /* trigger an event at 3 sec before the end of the match if we
411          * have balls in the barrel */
412         cur_timer = time_get_s();
413
414         if ((mainboard.flags & DO_TIMER) && (why & END_TIMER)) {
415                 /* end of match */
416                 if (cur_timer >= MATCH_TIME)
417                         return END_TIMER;
418         }
419
420         if ((why & END_INTR) && traj_intr) {
421                 interrupt_traj_reset();
422                 return END_INTR;
423         }
424
425         if ((why & END_TRAJ) && trajectory_finished(&mainboard.traj))
426                 return END_TRAJ;
427
428         /* we are near the destination point (depends on current
429          * speed) AND the robot is in the area bounding box. */
430         if (why & END_NEAR) {
431                 int16_t d_near = 100;
432
433                 if (mainboard.speed_d > 2000)
434                         d_near = 150;
435
436                 if (trajectory_in_window(&mainboard.traj, d_near, RAD(5.0)) &&
437                     is_in_boundingbox(&robot_pt))
438                         return END_NEAR;
439         }
440
441         if ((why & END_BLOCKING) && bd_get(&mainboard.angle.bd)) {
442                 strat_hardstop();
443                 return END_BLOCKING;
444         }
445
446         if ((why & END_BLOCKING) && bd_get(&mainboard.distance.bd)) {
447                 strat_hardstop();
448                 return END_BLOCKING;
449         }
450
451         if ((why & END_OBSTACLE) && strat_obstacle()) {
452                 strat_hardstop();
453                 return END_OBSTACLE;
454         }
455
456         return 0;
457 }
458
459 uint8_t __wait_traj_end_debug(uint8_t why, uint16_t line)
460 {
461         uint8_t ret = 0;
462         int16_t opp_x, opp_y, opp_d, opp_a;
463
464         while (ret == 0)
465                 ret = test_traj_end(why);
466
467         if (ret == END_OBSTACLE) {
468                 if (get_opponent_xyda(&opp_x, &opp_y,
469                                       &opp_d, &opp_a) == 0)
470                         DEBUG(E_USER_STRAT, "Got %s at line %d"
471                               " xy=(%d,%d) da=(%d,%d)", get_err(ret),
472                               line, opp_x, opp_y, opp_d, opp_a);
473         }
474         else {
475                 DEBUG(E_USER_STRAT, "Got %s at line %d",
476                       get_err(ret), line);
477         }
478         return ret;
479 }