77c9a51904e002ed48d23d9a21b76958ec02e09d
[aversive.git] / projects / microb2010 / mainboard / strat_corn.c
1 /*
2  *  Copyright Droids, Microb Technology (2010)
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.c,v 1.6 2009-11-08 17:24:33 zer0 Exp $
19  *
20  *  Olivier MATZ <zer0@droids-corp.org>
21  */
22
23
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <stdint.h>
27 #include <math.h>
28
29 #include <aversive.h>
30 #include <aversive/error.h>
31 #include <aversive/pgmspace.h>
32
33 #include <ax12.h>
34 #include <uart.h>
35 #include <pwm_ng.h>
36 #include <clock_time.h>
37 #include <spi.h>
38
39 #include <pid.h>
40 #include <quadramp.h>
41 #include <control_system_manager.h>
42 #include <trajectory_manager.h>
43 #include <trajectory_manager_utils.h>
44 #include <trajectory_manager_core.h>
45 #include <vect_base.h>
46 #include <lines.h>
47 #include <polygon.h>
48 #include <obstacle_avoidance.h>
49 #include <blocking_detection_manager.h>
50 #include <robot_system.h>
51 #include <position_manager.h>
52
53 #include <diagnostic.h>
54
55 #include <rdline.h>
56 #include <parse.h>
57
58 #include "../common/i2c_commands.h"
59 #include "i2c_protocol.h"
60 #include "main.h"
61 #include "strat.h"
62 #include "strat_db.h"
63 #include "strat_base.h"
64 #include "strat_corn.h"
65 #include "strat_avoid.h"
66 #include "strat_utils.h"
67 #include "sensor.h"
68 #include "actuator.h"
69
70 static volatile uint8_t clitoid_slow = 0;
71
72 /* return 1 if there is a corn near, and fill the index ptr */
73 int8_t corn_is_near(uint8_t *corn_idx, uint8_t side)
74 {
75         /* XXX to be checked */
76 #define SENSOR_CORN_DIST  225
77 #define SENSOR_CORN_ANGLE 90
78         double x = position_get_x_double(&mainboard.pos);
79         double y = position_get_y_double(&mainboard.pos);
80         double a_rad = position_get_a_rad_double(&mainboard.pos);
81         double x_corn, y_corn;
82         int16_t x_corn_int, y_corn_int;
83         struct waypoint_db *wp;
84
85         if (side == I2C_LEFT_SIDE) {
86                 x_corn = x + cos(a_rad + RAD(SENSOR_CORN_ANGLE)) * SENSOR_CORN_DIST;
87                 y_corn = y + sin(a_rad + RAD(SENSOR_CORN_ANGLE)) * SENSOR_CORN_DIST;
88         }
89         else {
90                 x_corn = x + cos(a_rad + RAD(-SENSOR_CORN_ANGLE)) * SENSOR_CORN_DIST;
91                 y_corn = y + sin(a_rad + RAD(-SENSOR_CORN_ANGLE)) * SENSOR_CORN_DIST;
92         }
93         x_corn_int = x_corn;
94         y_corn_int = y_corn;
95
96         wp = xycoord_to_corn_idx(&x_corn_int, &y_corn_int);
97         if (wp == NULL)
98                 return 0;
99         *corn_idx = wp->corn.idx;
100         return 1;
101 }
102
103 /* fill 2 points that are on the line (num, dir) */
104 static void num2line(struct line_2pts *l, uint8_t num, uint8_t dir)
105 {
106         float n = num;
107
108         switch (dir) {
109
110         case LINE_UP:
111                 l->p1.x = n * 450 + 375;
112                 l->p1.y = COLOR_Y(0);
113                 l->p2.x = n * 450 + 375;
114                 l->p2.y = COLOR_Y(2100);
115                 break;
116         case LINE_DOWN:
117                 l->p1.x = n * 450 + 375;
118                 l->p1.y = COLOR_Y(2100);
119                 l->p2.x = n * 450 + 375;
120                 l->p2.y = COLOR_Y(0);
121                 break;
122         case LINE_R_UP:
123                 l->p1.x = 150;
124                 l->p1.y = COLOR_Y(-n * 500 + 1472);
125                 l->p2.x = 2850;
126                 l->p2.y = COLOR_Y((-n + 4) * 500 + 972);
127                 break;
128         case LINE_L_DOWN:
129                 l->p1.x = 2850;
130                 l->p1.y = COLOR_Y((-n + 4) * 500 + 972);
131                 l->p2.x = 150;
132                 l->p2.y = COLOR_Y(-n * 500 + 1472);
133                 break;
134         case LINE_L_UP:
135                 l->p1.x = 2850;
136                 l->p1.y = COLOR_Y(-n * 500 + 1472);
137                 l->p2.x = 150;
138                 l->p2.y = COLOR_Y((-n + 4) * 500 + 972);
139                 break;
140         case LINE_R_DOWN:
141                 l->p1.x = 150;
142                 l->p1.y = COLOR_Y((-n + 4) * 500 + 972);
143                 l->p2.x = 2850;
144                 l->p2.y = COLOR_Y(-n * 500 + 1472);
145                 break;
146         default:
147                 break;
148         }
149 }
150
151 /* return true if we must go slow */
152 static uint8_t clitoid_select_speed(uint8_t num1, uint8_t dir1,
153                                     uint8_t num2, uint8_t dir2)
154 {
155         int16_t x, y;
156         uint8_t i, j;
157         uint8_t i2, i3, j2, j3; /* next wp */
158
159         x = position_get_x_s16(&mainboard.pos);
160         y = position_get_y_s16(&mainboard.pos);
161
162         if (get_cob_count() >= 5)
163                 return 0; /* fast */
164
165         if (xycoord_to_ijcoord(&x, &y, &i, &j) < 0) {
166                 DEBUG(E_USER_STRAT, "%s(): cannot find waypoint at %d,%d",
167                       __FUNCTION__, x, y);
168                 return 1;
169         }
170
171         if (corn_count_neigh(i, j) == 2)
172                 return 1;
173
174         /* we are on intersection, let's go slow... but as we enter in
175          * the curve-part of the clitoid, we should not go there */
176         if (wp_belongs_to_line(i, j, num2, dir2))
177                 return 0;
178
179         /* we can ge fast if it's a 60deg angle and if we checked the
180          * current point */
181         if (is_60deg(dir1, dir2))
182                 return 0;
183
184         /* get next point */
185         if (wp_get_neigh(i, j, &i2, &j2, dir1) < 0) {
186                 DEBUG(E_USER_STRAT, "%s(): cannot get neigh1",
187                       __FUNCTION__);
188                 return 1;
189         }
190
191         /* if (i2, j2) belongs to next line, check corns */
192         if (wp_belongs_to_line(i2, j2, num2, dir2)) {
193                 if (corn_count_neigh(i2, j2) > 0)
194                         return 1;
195                 else
196                         return 0;
197         }
198
199         /* get next point */
200         if (wp_get_neigh(i2, j2, &i3, &j3, dir1) < 0) {
201                 DEBUG(E_USER_STRAT, "%s(): cannot get neigh2",
202                       __FUNCTION__);
203                 return 1;
204         }
205
206         /* if (i3, j3) belongs to next line, check corns */
207         if (wp_belongs_to_line(i3, j3, num2, dir2)) {
208                 if (corn_count_neigh(i2, j2) > 0 ||
209                     corn_count_neigh(i3, j3) > 0)
210                         return 1;
211                 else
212                         return 0;
213         }
214
215         /* go fast */
216         return 0;
217 }
218
219 /*
220  * handle speed before clitoid (on the line), depending on strat_db.
221  * return true if clitoid started
222  */
223 #define NORETURN_DIST 300
224 static uint8_t speedify_clitoid(uint8_t num1, uint8_t dir1,
225                                 uint8_t num2, uint8_t dir2)
226 {
227         uint8_t slow;
228         double turnx, turny;
229
230         slow = clitoid_select_speed(num1, dir1, num2, dir2);
231         if (slow != clitoid_slow) {
232                 turnx = mainboard.traj.target.line.turn_pt.x;
233                 turny = mainboard.traj.target.line.turn_pt.y;
234                 if (distance_from_robot(turnx, turny) > NORETURN_DIST) {
235                         clitoid_slow = slow;
236                         return 1;
237                 }
238         }
239
240         return trajectory_get_state(&mainboard.traj) == RUNNING_CLITOID_CURVE;
241 }
242
243 /* process the clitoid parameters, return 0 on success or -1 if
244  * clitoid cannot be executed. pack_spickles is set to I2C_LEFT_SIDE,
245  * I2C_RIGHT_SIDE or I2C_NO_SIDE to tell if we need to pack a specific
246  * spickle. */
247 static int8_t strat_calc_clitoid(uint8_t num1, uint8_t dir1,
248                                  uint8_t num2, uint8_t dir2,
249                                  uint8_t *pack_spickles)
250 {
251         double line1_a_rad, line1_a_deg, line2_a_rad;
252         double diff_a_deg, diff_a_deg_abs, beta_deg;
253         double radius;
254         struct line_2pts l1, l2;
255         line_t ll1, ll2;
256         point_t p;
257         int8_t ret;
258
259         /* convert to 2 points */
260         num2line(&l1, num1, dir1);
261         num2line(&l2, num2, dir2);
262
263         DEBUG(E_USER_STRAT, "line1: (%2.2f, %2.2f) -> (%2.2f, %2.2f)",
264               l1.p1.x, l1.p1.y, l1.p2.x, l1.p2.y);
265         DEBUG(E_USER_STRAT, "line2: (%2.2f, %2.2f) -> (%2.2f, %2.2f)",
266               l2.p1.x, l2.p1.y, l2.p2.x, l2.p2.y);
267
268         /* convert to line eq and find intersection */
269         pts2line(&l1.p1, &l1.p2, &ll1);
270         pts2line(&l2.p1, &l2.p2, &ll2);
271         intersect_line(&ll1, &ll2, &p);
272
273         line1_a_rad = atan2(l1.p2.y - l1.p1.y,
274                             l1.p2.x - l1.p1.x);
275         line1_a_deg = DEG(line1_a_rad);
276         line2_a_rad = atan2(l2.p2.y - l2.p1.y,
277                             l2.p2.x - l2.p1.x);
278         diff_a_deg = DEG(line2_a_rad - line1_a_rad);
279         if (diff_a_deg < -180) {
280                 diff_a_deg += 360;
281         }
282         else if (diff_a_deg > 180) {
283                 diff_a_deg -= 360;
284         }
285         diff_a_deg_abs = fabs(diff_a_deg);
286
287 /*      printf_P(PSTR("diff_a_deg=%2.2f\r\n"), diff_a_deg_abs); */
288 /*      printf_P(PSTR("inter=%2.2f,%2.2f\r\n"), p.x, p.y); */
289
290         *pack_spickles = I2C_NO_SIDE;
291
292         /* small angle, 60 deg */
293         if (diff_a_deg_abs < 70.) {
294                 radius = 150;
295                 if (diff_a_deg > 0) {
296                         beta_deg = 0;
297                         *pack_spickles = I2C_RIGHT_SIDE;
298                 }
299                 else {
300                         beta_deg = 0;
301                         *pack_spickles = I2C_RIGHT_SIDE;
302                 }
303         }
304         /* double 90 deg for half turn -- not used */
305         else if (diff_a_deg_abs < 100.) {
306                 radius = 100;
307                 if (diff_a_deg > 0)
308                         beta_deg = 40;
309                 else
310                         beta_deg = -40;
311         }
312         /* hard turn, 120 deg */
313         else {
314                 radius = 75;
315                 if (diff_a_deg > 0)
316                         beta_deg = 0;
317                 else
318                         beta_deg = 0;
319         }
320
321         clitoid_slow = clitoid_select_speed(num1, dir1, num2, dir2);
322         if (clitoid_slow) {
323                 DEBUG(E_USER_STRAT, "slow clito");
324                 strat_set_speed(SPEED_CLITOID_SLOW, SPEED_ANGLE_SLOW);
325         }
326         else {
327                 DEBUG(E_USER_STRAT, "fast clito");
328                 strat_set_speed(SPEED_CLITOID_FAST, SPEED_ANGLE_SLOW);
329         }
330
331         ret = trajectory_clitoid(&mainboard.traj, l1.p1.x, l1.p1.y,
332                                  line1_a_deg, 150., diff_a_deg, beta_deg,
333                                  radius, xy_norm(l1.p1.x, l1.p1.y,
334                                                  p.x, p.y));
335         return ret;
336 }
337
338 /* go from line num1,dir1 to line num2,dir2. Uses trjectory flags
339  * specified as argument and return END_xxx condition */
340 uint8_t line2line(uint8_t num1, uint8_t dir1, uint8_t num2,
341                   uint8_t dir2, uint8_t flags)
342 {
343         int8_t ret;
344         uint8_t err, pack_spickles;
345
346  reprocess:
347         ret = strat_calc_clitoid(num1, dir1, num2, dir2, &pack_spickles);
348         if (ret < 0) {
349                 DEBUG(E_USER_STRAT, "clitoid failed");
350                 return END_ERROR;
351         }
352
353         /* XXX what to do if cobboard is stucked */
354
355         /* wait beginning of clitoid or changing of speed */
356         err = WAIT_COND_OR_TRAJ_END(speedify_clitoid(num1, dir1,
357                                                      num2, dir2),
358                                     flags);
359
360         /* error during traj, or traj finished */
361         if (err != 0)
362                 return err;
363
364         /* the speed has to change */
365         if (err == 0 &&
366             trajectory_get_state(&mainboard.traj) != RUNNING_CLITOID_CURVE)
367                 goto reprocess;
368
369         DEBUG(E_USER_STRAT, "clitoid started err=%d pack_spickles=%d",
370               err, pack_spickles);
371
372         /* when clitoid starts and angle is 60 deg, pack external
373          * spickle */
374         if (err == 0) {
375                 if (pack_spickles == I2C_LEFT_SIDE)
376                         strat_lpack60 = 1;
377                 else if (pack_spickles == I2C_RIGHT_SIDE)
378                         strat_rpack60 = 1;
379
380                 /* wait end of clitoid */
381                 err = wait_traj_end(flags);
382         }
383
384         DEBUG(E_USER_STRAT, "clitoid finished, err=%d", err);
385
386         strat_rpack60 = 0;
387         strat_lpack60 = 0;
388         return err;
389 }
390