acded681383da332ef6aa8d40410b10e9bd2575f
[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_get_acc(double *d, double *a)
229 {
230         uint8_t flags;
231         IRQ_LOCK(flags);
232         *d = mainboard.traj.d_acc;
233         *a = mainboard.traj.a_acc;
234         IRQ_UNLOCK(flags);
235 }
236
237 void strat_limit_speed_enable(void)
238 {
239         strat_limit_speed_enabled = 1;
240 }
241
242 void strat_limit_speed_disable(void)
243 {
244         strat_limit_speed_enabled = 0;
245 }
246
247 /* called periodically (note: disabled in 2010) */
248 void strat_limit_speed(void)
249 {
250         uint16_t lim_d = 0, lim_a = 0;
251         int16_t opp_d, opp_a;
252
253         if (strat_limit_speed_enabled == 0)
254                 goto update;
255
256         if (get_opponent_da(&opp_d, &opp_a) != 0)
257                 goto update;
258
259         if (opp_d < 500) {
260                 if (mainboard.speed_d > 0 && (opp_a > 290 || opp_a < 70)) {
261                         lim_d = SPEED_DIST_VERY_SLOW;
262                         lim_a = SPEED_ANGLE_VERY_SLOW;
263                 }
264                 else if (mainboard.speed_d < 0 && (opp_a < 250 && opp_a > 110)) {
265                         lim_d = SPEED_DIST_VERY_SLOW;
266                         lim_a = SPEED_ANGLE_VERY_SLOW;
267                 }
268                 else {
269                         lim_d = SPEED_DIST_SLOW;
270                         lim_a = SPEED_ANGLE_VERY_SLOW;
271                 }
272         }
273         else if (opp_d < 800) {
274                 if (mainboard.speed_d > 0 && (opp_a > 290 || opp_a < 70)) {
275                         lim_d = SPEED_DIST_SLOW;
276                         lim_a = SPEED_ANGLE_SLOW;
277                 }
278                 else if (mainboard.speed_d < 0 && (opp_a < 250 && opp_a > 110)) {
279                         lim_d = SPEED_DIST_SLOW;
280                         lim_a = SPEED_ANGLE_SLOW;
281                 }
282         }
283
284  update:
285         if (lim_d != strat_limit_speed_d ||
286             lim_a != strat_limit_speed_a) {
287                 strat_limit_speed_d = lim_d;
288                 strat_limit_speed_a = lim_a;
289                 DEBUG(E_USER_STRAT, "new speed limit da=%d,%d", lim_d, lim_a);
290                 strat_update_traj_speed();
291         }
292 }
293
294 /* start the strat */
295 void strat_start(void)
296 {
297         uint8_t err;
298
299         strat_preinit();
300
301 #ifndef HOST_VERSION
302         /* if start sw not plugged */
303         if (sensor_get(S_START_SWITCH)) {
304                 int8_t i;
305
306                 printf_P(PSTR("No start switch, press a key or plug it\r\n"));
307
308                 /* while start sw not plugged */
309                 while (sensor_get(S_START_SWITCH)) {
310                         if (! cmdline_keypressed())
311                                 continue;
312
313                         for (i=3; i>0; i--) {
314                                 printf_P(PSTR("%d\r\n"), i);
315                                 time_wait_ms(1000);
316                         }
317                         break;
318                 }
319         }
320
321         /* if start sw plugged */
322         if (!sensor_get(S_START_SWITCH)) {
323                 printf_P(PSTR("Ready, unplug start switch to start\r\n"));
324                 /* while start sw plugged */
325                 while (!sensor_get(S_START_SWITCH));
326         }
327 #endif
328
329         strat_init();
330         err = strat_main();
331         NOTICE(E_USER_STRAT, "Finished !! returned %s", get_err(err));
332         strat_exit();
333 }
334
335 /* return true if we have to brake due to an obstacle */
336 uint8_t strat_obstacle(void)
337 {
338         int16_t x_rel, y_rel;
339         int16_t opp_x, opp_y, opp_d, opp_a;
340
341         /* too slow */
342         if (ABS(mainboard.speed_d) < 150)
343                 return 0;
344
345         /* no opponent detected */
346         if (get_opponent_xyda(&opp_x, &opp_y, &opp_d, &opp_a))
347                 return 0;
348
349         /* save obstacle position */
350         opponent_obstacle.x = opp_x;
351         opponent_obstacle.y = opp_y;
352         opponent_obstacle.d = opp_d;
353         opponent_obstacle.a = opp_a;
354
355         if (!is_in_area(opp_x, opp_y, 250))
356                 return 0;
357
358         /* sensor are temporarily disabled */
359         if (sensor_obstacle_is_disabled())
360                 return 0;
361
362         /* relative position */
363         x_rel = cos(RAD(opp_a)) * (double)opp_d;
364         y_rel = sin(RAD(opp_a)) * (double)opp_d;
365
366         /* opponent too far */
367         if (opp_d > 500)
368                 return 0;
369
370         /* opponent is in front of us */
371         if (mainboard.speed_d > 0 && (opp_a > 325 || opp_a < 35)) {
372                 DEBUG(E_USER_STRAT, "opponent front d=%d, a=%d "
373                       "xrel=%d yrel=%d (speed_d=%d)",
374                       opp_d, opp_a, x_rel, y_rel, mainboard.speed_d);
375                 sensor_obstacle_disable();
376                 return 1;
377         }
378         /* opponent is behind us */
379         if (mainboard.speed_d < 0 && (opp_a < 215 && opp_a > 145)) {
380                 DEBUG(E_USER_STRAT, "opponent behind d=%d, a=%d xrel=%d yrel=%d",
381                       opp_d, opp_a, x_rel, y_rel);
382                 sensor_obstacle_disable();
383                 return 1;
384         }
385
386         return 0;
387 }
388
389 void interrupt_traj(void)
390 {
391         traj_intr = 1;
392 }
393
394 void interrupt_traj_reset(void)
395 {
396         traj_intr = 0;
397 }
398
399 uint8_t test_traj_end(uint8_t why)
400 {
401         uint16_t cur_timer;
402         point_t robot_pt;
403
404         robot_pt.x = position_get_x_s16(&mainboard.pos);
405         robot_pt.y = position_get_y_s16(&mainboard.pos);
406
407         /* trigger an event at 3 sec before the end of the match if we
408          * have balls in the barrel */
409         cur_timer = time_get_s();
410
411         if ((mainboard.flags & DO_TIMER) && (why & END_TIMER)) {
412                 /* end of match */
413                 if (cur_timer >= MATCH_TIME)
414                         return END_TIMER;
415         }
416
417         if ((why & END_INTR) && traj_intr) {
418                 interrupt_traj_reset();
419                 return END_INTR;
420         }
421
422         if ((why & END_TRAJ) && trajectory_finished(&mainboard.traj))
423                 return END_TRAJ;
424
425         /* we are near the destination point (depends on current
426          * speed) AND the robot is in the area bounding box. */
427         if (why & END_NEAR) {
428                 int16_t d_near = 100;
429
430                 if (mainboard.speed_d > 2000)
431                         d_near = 150;
432
433                 if (trajectory_in_window(&mainboard.traj, d_near, RAD(5.0)) &&
434                     is_in_boundingbox(&robot_pt))
435                         return END_NEAR;
436         }
437
438         if ((why & END_BLOCKING) && bd_get(&mainboard.angle.bd)) {
439                 strat_hardstop();
440                 return END_BLOCKING;
441         }
442
443         if ((why & END_BLOCKING) && bd_get(&mainboard.distance.bd)) {
444                 strat_hardstop();
445                 return END_BLOCKING;
446         }
447
448         if ((why & END_OBSTACLE) && strat_obstacle()) {
449                 strat_hardstop();
450                 return END_OBSTACLE;
451         }
452
453         return 0;
454 }
455
456 uint8_t __wait_traj_end_debug(uint8_t why, uint16_t line)
457 {
458         uint8_t ret = 0;
459         int16_t opp_x, opp_y, opp_d, opp_a;
460
461         while (ret == 0)
462                 ret = test_traj_end(why);
463
464         if (ret == END_OBSTACLE) {
465                 if (get_opponent_xyda(&opp_x, &opp_y,
466                                       &opp_d, &opp_a) == 0)
467                         DEBUG(E_USER_STRAT, "Got %s at line %d"
468                               " xy=(%d,%d) da=(%d,%d)", get_err(ret),
469                               line, opp_x, opp_y, opp_d, opp_a);
470         }
471         else {
472                 DEBUG(E_USER_STRAT, "Got %s at line %d",
473                       get_err(ret), line);
474         }
475         return ret;
476 }