3ec6a2c5aef56a444c71219334e57754267f6b03
[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_utils.h"
66 #include "sensor.h"
67 #include "actuator.h"
68
69 /* return 1 if there is a corn near, and fill the index ptr */
70 int8_t corn_is_near(uint8_t *corn_idx, uint8_t side)
71 {
72         /* XXX to be checked */
73 #define SENSOR_CORN_DIST  225
74 #define SENSOR_CORN_ANGLE 90
75         double x = position_get_x_double(&mainboard.pos);
76         double y = position_get_y_double(&mainboard.pos);
77         double a_rad = position_get_a_rad_double(&mainboard.pos);
78         double x_corn, y_corn;
79         int16_t x_corn_int, y_corn_int;
80         struct waypoint_db *wp;
81
82         if (side == I2C_LEFT_SIDE) {
83                 x_corn = x + cos(a_rad + RAD(SENSOR_CORN_ANGLE)) * SENSOR_CORN_DIST;
84                 y_corn = y + sin(a_rad + RAD(SENSOR_CORN_ANGLE)) * SENSOR_CORN_DIST;
85         }
86         else {
87                 x_corn = x + cos(a_rad + RAD(-SENSOR_CORN_ANGLE)) * SENSOR_CORN_DIST;
88                 y_corn = y + sin(a_rad + RAD(-SENSOR_CORN_ANGLE)) * SENSOR_CORN_DIST;
89         }
90         x_corn_int = x_corn;
91         y_corn_int = y_corn;
92
93         wp = xycoord_to_corn_idx(&x_corn_int, &y_corn_int);
94         if (wp == NULL)
95                 return 0;
96         *corn_idx = wp->corn.idx;
97         return 1;
98 }
99
100 /*
101  * return true if clitoid started
102  */
103 static uint8_t clitoid_started(void)
104 {
105         return trajectory_get_state(&mainboard.traj) == RUNNING_CLITOID_CURVE;
106 }
107
108 /* XXX passer les flags de traj */
109 uint8_t line2line(uint8_t num1, uint8_t dir1,
110                   uint8_t num2, uint8_t dir2)
111 {
112         double line1_a_rad, line1_a_deg, line2_a_rad;
113         double diff_a_deg, diff_a_deg_abs, beta_deg;
114         double radius;
115         struct line_2pts l1, l2;
116         line_t ll1, ll2;
117         point_t p;
118         uint8_t err;
119         int8_t ret;
120
121         /* convert to 2 points */
122         num2line(&l1, dir1, num1);
123         num2line(&l2, dir2, num2);
124
125         DEBUG(E_USER_STRAT, "line1: (%2.2f, %2.2f) -> (%2.2f, %2.2f)",
126               l1.p1.x, l1.p1.y, l1.p2.x, l1.p2.y);
127         DEBUG(E_USER_STRAT, "line2: (%2.2f, %2.2f) -> (%2.2f, %2.2f)",
128               l2.p1.x, l2.p1.y, l2.p2.x, l2.p2.y);
129
130         /* convert to line eq and find intersection */
131         pts2line(&l1.p1, &l1.p2, &ll1);
132         pts2line(&l2.p1, &l2.p2, &ll2);
133         intersect_line(&ll1, &ll2, &p);
134
135         line1_a_rad = atan2(l1.p2.y - l1.p1.y,
136                             l1.p2.x - l1.p1.x);
137         line1_a_deg = DEG(line1_a_rad);
138         line2_a_rad = atan2(l2.p2.y - l2.p1.y,
139                             l2.p2.x - l2.p1.x);
140         diff_a_deg = DEG(line2_a_rad - line1_a_rad);
141         if (diff_a_deg < -180) {
142                 diff_a_deg += 360;
143         }
144         else if (diff_a_deg > 180) {
145                 diff_a_deg -= 360;
146         }
147         diff_a_deg_abs = fabs(diff_a_deg);
148
149 /*      printf_P(PSTR("diff_a_deg=%2.2f\r\n"), diff_a_deg_abs); */
150 /*      printf_P(PSTR("inter=%2.2f,%2.2f\r\n"), p.x, p.y); */
151
152         /* small angle, 60 deg */
153         if (diff_a_deg_abs < 70.) {
154                 radius = 150;
155                 if (diff_a_deg > 0)
156                         beta_deg = 0;
157                 else
158                         beta_deg = 0;
159         }
160         /* double 90 deg for half turn -- not used */
161         else if (diff_a_deg_abs < 100.) {
162                 radius = 100;
163                 if (diff_a_deg > 0)
164                         beta_deg = 40;
165                 else
166                         beta_deg = -40;
167         }
168         /* hard turn, 120 deg */
169         else {
170                 radius = 75;
171                 if (diff_a_deg > 0)
172                         beta_deg = 0;
173                 else
174                         beta_deg = 0;
175         }
176
177         /* XXX check return value !! */
178         ret = trajectory_clitoid(&mainboard.traj, l1.p1.x, l1.p1.y,
179                                  line1_a_deg, 150., diff_a_deg, beta_deg,
180                                  radius, xy_norm(l1.p1.x, l1.p1.y,
181                                                  p.x, p.y));
182         if (ret < 0)
183                 DEBUG(E_USER_STRAT, "clitoid failed");
184
185         /* XXX what to do if cobboard is stucked */
186
187         err = WAIT_COND_OR_TRAJ_END(clitoid_started(), 0xFF);
188         DEBUG(E_USER_STRAT, "clitoid started err=%d diff_a_deg_abs=%2.2f diff_a_deg=%2.2f",
189               err, diff_a_deg_abs, diff_a_deg);
190
191         /* when clitoid starts and angle is 60 deg, pack external
192          * spickle */
193         if (diff_a_deg_abs < 70. && err == 0) {
194                 if (diff_a_deg > 0)
195                         strat_rpack60 = 1;
196                 else
197                         strat_lpack60 = 1;
198         }
199         if (err == 0)
200                 err = wait_traj_end(0xFF);
201
202         DEBUG(E_USER_STRAT, "clitoid finished");
203
204         /* XXX 600 -> cste */
205         /* XXX does not work, do better */
206 /*      if (err == 0 && d_speed < 600 && */
207 /*          mainboard.traj.state == RUNNING_CLITOID_LINE) */
208 /*              strat_set_speed(600, SPEED_ANGLE_FAST); */
209
210         strat_rpack60 = 0;
211         strat_lpack60 = 0;
212         return err;
213 }
214
215 void num2line(struct line_2pts *l, uint8_t dir, uint8_t num)
216 {
217         float n = num;
218
219         switch (dir) {
220
221         case LINE_UP:
222                 l->p1.x = n * 450 + 375;
223                 l->p1.y = COLOR_Y(0);
224                 l->p2.x = n * 450 + 375;
225                 l->p2.y = COLOR_Y(2100);
226                 break;
227         case LINE_DOWN:
228                 l->p1.x = n * 450 + 375;
229                 l->p1.y = COLOR_Y(2100);
230                 l->p2.x = n * 450 + 375;
231                 l->p2.y = COLOR_Y(0);
232                 break;
233         case LINE_R_UP:
234                 l->p1.x = 150;
235                 l->p1.y = COLOR_Y(-n * 500 + 1472);
236                 l->p2.x = 2850;
237                 l->p2.y = COLOR_Y((-n + 4) * 500 + 972);
238                 break;
239         case LINE_L_DOWN:
240                 l->p1.x = 2850;
241                 l->p1.y = COLOR_Y((-n + 4) * 500 + 972);
242                 l->p2.x = 150;
243                 l->p2.y = COLOR_Y(-n * 500 + 1472);
244                 break;
245         case LINE_L_UP:
246                 l->p1.x = 2850;
247                 l->p1.y = COLOR_Y(-n * 500 + 1472);
248                 l->p2.x = 150;
249                 l->p2.y = COLOR_Y((-n + 4) * 500 + 972);
250                 break;
251         case LINE_R_DOWN:
252                 l->p1.x = 150;
253                 l->p1.y = COLOR_Y((-n + 4) * 500 + 972);
254                 l->p2.x = 2850;
255                 l->p2.y = COLOR_Y(-n * 500 + 1472);
256                 break;
257         default:
258                 break;
259         }
260 }