cob detection
[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 #if 0
70 /* return 1 if there is a corn near, and fill the index ptr */
71 uint8_t corn_is_near(int8_t *corn_idx, uint8_t side)
72 {
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->corn.idx < 0)
95                 return 0;
96         return 1;
97 }
98 #endif
99
100 /*
101  * - send the correct commands to the spickles
102  * - return 1 if we need to stop (cobboard is stucked)
103 */
104 static uint8_t handle_spickles(void)
105 {
106         return 0;
107 #if 0
108         int8_t corn_idx;
109
110         if (!corn_is_near(&corn_idx, I2C_LEFT_SIDE))
111                 i2c_cobboard_mode_deploy(I2C_LEFT_SIDE);
112         else {
113                 if (corn_table[corn_idx] == TYPE_WHITE_CORN)
114                         i2c_cobboard_mode_harvest(I2C_LEFT_SIDE);
115                 else
116                         i2c_cobboard_mode_pack(I2C_LEFT_SIDE);
117         }
118 /*      printf("%d %d\n", corn_idx, corn_table[corn_idx]); */
119 /*      time_wait_ms(100); */
120
121         if (!corn_is_near(&corn_idx, I2C_RIGHT_SIDE))
122                 i2c_cobboard_mode_deploy(I2C_RIGHT_SIDE);
123         else {
124                 if (corn_table[corn_idx] == TYPE_WHITE_CORN)
125                         i2c_cobboard_mode_harvest(I2C_RIGHT_SIDE);
126                 else
127                         i2c_cobboard_mode_pack(I2C_RIGHT_SIDE);
128         }
129
130         return 0;
131 #endif
132 }
133
134 uint8_t line2line(uint8_t dir1, uint8_t num1,
135                   uint8_t dir2, uint8_t num2)
136 {
137         double line1_a_rad, line1_a_deg, line2_a_rad;
138         double diff_a_deg, diff_a_deg_abs, beta_deg;
139         double radius;
140         struct line_2pts l1, l2;
141         line_t ll1, ll2;
142         point_t p;
143         uint8_t err;
144         uint16_t a_speed, d_speed;
145
146         /* convert to 2 points */
147         num2line(&l1, dir1, num1);
148         num2line(&l2, dir2, num2);
149
150         DEBUG(E_USER_STRAT, "line1: (%2.2f, %2.2f) -> (%2.2f, %2.2f)",
151               l1.p1.x, l1.p1.y, l1.p2.x, l1.p2.y);
152         DEBUG(E_USER_STRAT, "line2: (%2.2f, %2.2f) -> (%2.2f, %2.2f)",
153               l2.p1.x, l2.p1.y, l2.p2.x, l2.p2.y);
154
155         /* convert to line eq and find intersection */
156         pts2line(&l1.p1, &l1.p2, &ll1);
157         pts2line(&l2.p1, &l2.p2, &ll2);
158         intersect_line(&ll1, &ll2, &p);
159
160         line1_a_rad = atan2(l1.p2.y - l1.p1.y,
161                             l1.p2.x - l1.p1.x);
162         line1_a_deg = DEG(line1_a_rad);
163         line2_a_rad = atan2(l2.p2.y - l2.p1.y,
164                             l2.p2.x - l2.p1.x);
165         diff_a_deg = DEG(line2_a_rad - line1_a_rad);
166         if (diff_a_deg < -180) {
167                 diff_a_deg += 360;
168         }
169         else if (diff_a_deg > 180) {
170                 diff_a_deg -= 360;
171         }
172         diff_a_deg_abs = fabs(diff_a_deg);
173
174 /*      printf_P(PSTR("diff_a_deg=%2.2f\r\n"), diff_a_deg_abs); */
175 /*      printf_P(PSTR("inter=%2.2f,%2.2f\r\n"), p.x, p.y); */
176
177         if (diff_a_deg_abs < 70.) {
178                 radius = 200;
179                 if (diff_a_deg > 0)
180                         beta_deg = 40;
181                 else
182                         beta_deg = -40;
183         }
184         else if (diff_a_deg_abs < 100.) {
185                 radius = 100;
186                 if (diff_a_deg > 0)
187                         beta_deg = 40;
188                 else
189                         beta_deg = -40;
190         }
191         else {
192                 radius = 120;
193                 if (diff_a_deg > 0)
194                         beta_deg = 60;
195                 else
196                         beta_deg = -60;
197         }
198
199         /* XXX check return value !! */
200         trajectory_clitoid(&mainboard.traj, l1.p1.x, l1.p1.y,
201                            line1_a_deg, 150., diff_a_deg, beta_deg,
202                            radius, xy_norm(l1.p1.x, l1.p1.y,
203                                            p.x, p.y));
204         /* disabled */
205         if (0) {
206                 err = 0;
207                 while (err == 0) {
208                         err = WAIT_COND_OR_TRAJ_END(handle_spickles(), 0xFF);
209                         if (err == 0) {
210                                 /* cobboard is stucked */
211                                 trajectory_hardstop(&mainboard.traj);
212                                 return err; /* XXX do something */
213                         }
214                         err = test_traj_end(0xFF);
215                 }
216         }
217
218         err = WAIT_COND_OR_TRAJ_END(get_cob_count() == 5, 0xFF);
219         strat_get_speed(&d_speed, &a_speed);
220
221         /* XXX 600 -> cste */
222         if (err == 0 && d_speed < 600 &&
223             mainboard.traj.state == RUNNING_CLITOID_LINE)
224                 strat_set_speed(600, SPEED_ANGLE_FAST);
225         err = wait_traj_end(0xFF);
226
227         return err;
228 }
229
230 void num2line(struct line_2pts *l, uint8_t dir, uint8_t num)
231 {
232         float n = num;
233
234         switch (dir) {
235
236         case LINE_UP:
237                 l->p1.x = n * 450 + 375;
238                 l->p1.y = COLOR_Y(0);
239                 l->p2.x = n * 450 + 375;
240                 l->p2.y = COLOR_Y(2100);
241                 break;
242         case LINE_DOWN:
243                 l->p1.x = n * 450 + 375;
244                 l->p1.y = COLOR_Y(2100);
245                 l->p2.x = n * 450 + 375;
246                 l->p2.y = COLOR_Y(0);
247                 break;
248         case LINE_R_UP:
249                 l->p1.x = 150;
250                 l->p1.y = COLOR_Y(-n * 500 + 1472);
251                 l->p2.x = 2850;
252                 l->p2.y = COLOR_Y((-n + 4) * 500 + 972);
253                 break;
254         case LINE_L_DOWN:
255                 l->p1.x = 2850;
256                 l->p1.y = COLOR_Y((-n + 4) * 500 + 972);
257                 l->p2.x = 150;
258                 l->p2.y = COLOR_Y(-n * 500 + 1472);
259                 break;
260         case LINE_L_UP:
261                 l->p1.x = 2850;
262                 l->p1.y = COLOR_Y(-n * 500 + 1472);
263                 l->p2.x = 150;
264                 l->p2.y = COLOR_Y((-n + 4) * 500 + 972);
265                 break;
266         case LINE_R_DOWN:
267                 l->p1.x = 150;
268                 l->p1.y = COLOR_Y((-n + 4) * 500 + 972);
269                 l->p2.x = 2850;
270                 l->p2.y = COLOR_Y(-n * 500 + 1472);
271                 break;
272         default:
273                 break;
274         }
275 }