traj update
[aversive.git] / projects / microb2010 / mainboard / strat.c
1 /*
2  *  Copyright Droids, 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.c,v 1.6 2009-11-08 17:24:33 zer0 Exp $
19  *
20  *  Olivier MATZ <zer0@droids-corp.org>
21  */
22
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <math.h>
27
28 #include <aversive/pgmspace.h>
29 #include <aversive/queue.h>
30 #include <aversive/wait.h>
31 #include <aversive/error.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 <vect_base.h>
44 #include <lines.h>
45 #include <polygon.h>
46 #include <obstacle_avoidance.h>
47 #include <blocking_detection_manager.h>
48 #include <robot_system.h>
49 #include <position_manager.h>
50
51 #include <diagnostic.h>
52
53 #include <rdline.h>
54 #include <parse.h>
55
56 #include "../common/i2c_commands.h"
57 #include "i2c_protocol.h"
58 #include "main.h"
59 #include "strat.h"
60 #include "strat_db.h"
61 #include "strat_base.h"
62 #include "strat_corn.h"
63 #include "strat_utils.h"
64 #include "sensor.h"
65 #include "actuator.h"
66
67 #define COL_DISP_MARGIN 400 /* stop 40 cm in front of dispenser */
68 #define COL_SCAN_PRE_MARGIN 250
69
70 static uint8_t strat_running = 0;
71 struct strat_conf strat_conf;
72
73 /*************************************************************/
74
75 /*                  INIT                                     */
76
77 /*************************************************************/
78
79 /* called before each strat, and before the start switch */
80 void strat_preinit(void)
81 {
82         time_reset();
83         interrupt_traj_reset();
84         mainboard.flags =  DO_ENCODERS | DO_CS | DO_RS |
85                 DO_POS | DO_BD | DO_POWER;
86
87         //i2c_cobboard_mode_init();
88         strat_conf_dump(__FUNCTION__);
89         strat_db_dump(__FUNCTION__);
90 }
91
92 void strat_conf_dump(const char *caller)
93 {
94         if (!strat_conf.dump_enabled)
95                 return;
96
97         printf_P(PSTR("-- conf --\r\n"));
98
99 }
100
101 /* call it just before launching the strat */
102 void strat_init(void)
103 {
104 #ifdef HOST_VERSION
105         position_set(&mainboard.pos, 298.16,
106                      COLOR_Y(308.78), COLOR_A(70.00));
107 #endif
108
109         /* we consider that the color is correctly set */
110         strat_running = 1;
111         strat_db_init();
112         strat_set_speed(SPEED_DIST_FAST, SPEED_ANGLE_FAST);
113         time_reset();
114         interrupt_traj_reset();
115
116         i2c_cobboard_set_mode(I2C_COBBOARD_MODE_HARVEST);
117         i2c_ballboard_set_mode(I2C_BALLBOARD_MODE_HARVEST);
118
119         /* used in strat_base for END_TIMER */
120         mainboard.flags = DO_ENCODERS | DO_CS | DO_RS |
121                 DO_POS | DO_BD | DO_TIMER | DO_POWER;
122 }
123
124
125 /* call it after each strat */
126 void strat_exit(void)
127 {
128 #ifndef HOST_VERSION
129         uint8_t flags;
130 #endif
131
132         strat_running = 0;
133         mainboard.flags &= ~(DO_TIMER);
134         strat_hardstop();
135         time_reset();
136         wait_ms(100);
137 #ifndef HOST_VERSION
138         IRQ_LOCK(flags);
139         mainboard.flags &= ~(DO_CS);
140         IRQ_UNLOCK(flags);
141         pwm_ng_set(LEFT_PWM, 0);
142         pwm_ng_set(RIGHT_PWM, 0);
143 #endif
144 }
145
146 /* called periodically (10ms) */
147 void strat_event(void *dummy)
148 {
149         uint8_t flags;
150         uint8_t lcob, rcob;
151         uint8_t lidx, ridx;
152
153         /* ignore when strat is not running */
154         if (strat_running == 0)
155                 return;
156
157         IRQ_LOCK(flags);
158         lcob = ballboard.lcob;
159         ballboard.lcob = I2C_COB_NONE;
160         rcob = ballboard.rcob;
161         ballboard.rcob = I2C_COB_NONE;
162         IRQ_UNLOCK(flags);
163
164         /* XXX take opponent position into account */
165
166 #ifdef HOST_VERSION
167         if (time_get_s() == 15)
168                 cobboard.cob_count = 5;
169         if (time_get_s() == 16)
170                 cobboard.cob_count = 0;
171         if (time_get_s() == 25)
172                 cobboard.cob_count = 5;
173 #endif
174
175         /* detect cob on left side */
176         if (corn_is_near(&lidx, I2C_LEFT_SIDE)) {
177                 if (lcob != I2C_COB_NONE) {
178                         corn_set_color(strat_db.corn_table[lidx], lcob);
179                         DEBUG(E_USER_STRAT, "lcob %s %d",
180                               lcob == I2C_COB_WHITE ? "white" : "black", lidx);
181                 }
182                 if (strat_db.corn_table[lidx]->corn.color == I2C_COB_WHITE)
183                         i2c_cobboard_autoharvest(I2C_LEFT_SIDE);
184                 else
185                         i2c_cobboard_deploy(I2C_LEFT_SIDE);
186         }
187         else {
188                 i2c_cobboard_deploy(I2C_LEFT_SIDE);
189         }
190
191         /* detect cob on right side */
192         if (corn_is_near(&ridx, I2C_RIGHT_SIDE)) {
193                 if (rcob != I2C_COB_NONE) {
194                         corn_set_color(strat_db.corn_table[ridx], rcob);
195                         DEBUG(E_USER_STRAT, "rcob %s %d",
196                               rcob == I2C_COB_WHITE ? "white" : "black", ridx);
197                 }
198                 if (strat_db.corn_table[ridx]->corn.color == I2C_COB_WHITE)
199                         i2c_cobboard_autoharvest(I2C_RIGHT_SIDE);
200                 else
201                         i2c_cobboard_deploy(I2C_RIGHT_SIDE);
202         }
203         else {
204                 i2c_cobboard_deploy(I2C_RIGHT_SIDE);
205         }
206
207
208         /* limit speed when opponent is close */
209         strat_limit_speed();
210 }
211
212 static uint8_t strat_beginning(void)
213 {
214         uint8_t err;
215
216         strat_set_acc(ACC_DIST, ACC_ANGLE);
217 #ifdef HOST_VERSION
218         strat_set_speed(600, SPEED_ANGLE_FAST);
219 #else
220         /* 250 */
221         strat_set_speed(250, SPEED_ANGLE_FAST);
222 #endif
223
224
225         // strat_set_speed(600, 60); /* OK */
226         strat_set_speed(250, 28); /* OK */
227
228         trajectory_d_a_rel(&mainboard.traj, 1000, COLOR_A(20));
229         err = WAIT_COND_OR_TRAJ_END(trajectory_angle_finished(&mainboard.traj),
230                                     TRAJ_FLAGS_STD);
231
232         strat_set_acc(ACC_DIST, ACC_ANGLE);
233
234 #if 1
235  l1:
236         DEBUG(E_USER_STRAT, "%s():%d", __FUNCTION__, __LINE__);
237         if (get_cob_count() >= 5)
238                 strat_set_speed(600, SPEED_ANGLE_FAST);
239
240         err = line2line(LINE_UP, 0, LINE_R_DOWN, 2);
241         if (!TRAJ_SUCCESS(err)) {
242                 strat_hardstop();
243                 time_wait_ms(2000);
244                 goto l1;
245         }
246
247  l2:
248         DEBUG(E_USER_STRAT, "%s():%d", __FUNCTION__, __LINE__);
249         if (get_cob_count() >= 5)
250                 strat_set_speed(600, SPEED_ANGLE_FAST);
251
252         err = line2line(LINE_R_DOWN, 2, LINE_R_UP, 2);
253         if (!TRAJ_SUCCESS(err)) {
254                 strat_hardstop();
255                 time_wait_ms(2000);
256                 goto l2;
257         }
258
259         WAIT_COND_OR_TRAJ_END(distance_from_robot(2625, COLOR_Y(1847)) < 100,
260                               TRAJ_FLAGS_STD);
261         trajectory_goto_xy_abs(&mainboard.traj, 2625, COLOR_Y(1847));
262         err = wait_traj_end(END_INTR|END_TRAJ);
263
264 #else
265         strat_set_speed(600, SPEED_ANGLE_FAST);
266         err = line2line(LINE_UP, 0, LINE_R_DOWN, 3);
267         err = line2line(LINE_R_DOWN, 3, LINE_R_UP, 2);
268         err = line2line(LINE_R_UP, 2, LINE_R_DOWN, 2);
269         err = line2line(LINE_R_DOWN, 2, LINE_R_UP, 3);
270         err = line2line(LINE_R_UP, 3, LINE_UP, 5);
271         err = line2line(LINE_UP, 5, LINE_L_DOWN, 2);
272         err = line2line(LINE_L_DOWN, 2, LINE_L_UP, 1);
273         err = line2line(LINE_L_UP, 1, LINE_L_DOWN, 1);
274         err = line2line(LINE_L_DOWN, 1, LINE_DOWN, 0);
275         wait_ms(500);
276         strat_hardstop();
277         return END_TRAJ;
278 #endif
279
280         DEBUG(E_USER_STRAT, "%s():%d", __FUNCTION__, __LINE__);
281         strat_hardstop();
282         strat_set_speed(600, SPEED_ANGLE_FAST);
283
284         /* ball ejection */
285         trajectory_a_abs(&mainboard.traj, COLOR_A(90));
286         i2c_ballboard_set_mode(I2C_BALLBOARD_MODE_EJECT);
287         time_wait_ms(2000);
288
289         /* half turn */
290         i2c_cobboard_pack(I2C_LEFT_SIDE);
291         i2c_cobboard_pack(I2C_RIGHT_SIDE);
292         trajectory_a_rel(&mainboard.traj, COLOR_A(180));
293         err = wait_traj_end(END_INTR|END_TRAJ);
294
295         /* cob ejection */
296         trajectory_d_rel(&mainboard.traj, -100);
297         err = wait_traj_end(END_INTR|END_TRAJ);
298         i2c_cobboard_set_mode(I2C_COBBOARD_MODE_EJECT);
299         time_wait_ms(2000);
300
301         strat_set_speed(250, SPEED_ANGLE_FAST);
302
303  l4:
304         DEBUG(E_USER_STRAT, "%s():%d", __FUNCTION__, __LINE__);
305         if (get_cob_count() >= 5)
306                 strat_set_speed(600, SPEED_ANGLE_FAST);
307
308         err = line2line(LINE_DOWN, 5, LINE_L_UP, 2);
309         if (!TRAJ_SUCCESS(err)) {
310                 strat_hardstop();
311                 time_wait_ms(2000);
312                 goto l4;
313         }
314
315  l5:
316         DEBUG(E_USER_STRAT, "%s():%d", __FUNCTION__, __LINE__);
317         if (get_cob_count() >= 5)
318                 strat_set_speed(600, SPEED_ANGLE_FAST);
319
320         err = line2line(LINE_L_UP, 2, LINE_R_UP, 2);
321         if (!TRAJ_SUCCESS(err)) {
322                 strat_hardstop();
323                 time_wait_ms(2000);
324                 goto l5;
325         }
326
327         DEBUG(E_USER_STRAT, "%s():%d", __FUNCTION__, __LINE__);
328         if (get_cob_count() >= 5)
329                 strat_set_speed(600, SPEED_ANGLE_FAST);
330
331         WAIT_COND_OR_TRAJ_END(distance_from_robot(2625, COLOR_Y(1847)) < 100,
332                               TRAJ_FLAGS_STD);
333         trajectory_goto_xy_abs(&mainboard.traj, 2625, COLOR_Y(1847));
334         err = wait_traj_end(END_INTR|END_TRAJ);
335
336         DEBUG(E_USER_STRAT, "%s():%d", __FUNCTION__, __LINE__);
337         strat_hardstop();
338         strat_set_speed(600, SPEED_ANGLE_FAST);
339
340         /* ball ejection */
341         trajectory_a_abs(&mainboard.traj, COLOR_A(90));
342         i2c_ballboard_set_mode(I2C_BALLBOARD_MODE_EJECT);
343         time_wait_ms(2000);
344
345         /* half turn */
346         i2c_cobboard_pack(I2C_LEFT_SIDE);
347         i2c_cobboard_pack(I2C_RIGHT_SIDE);
348         trajectory_a_rel(&mainboard.traj, COLOR_A(180));
349         err = wait_traj_end(END_INTR|END_TRAJ);
350
351         /* cob ejection */
352         trajectory_d_rel(&mainboard.traj, -100);
353         err = wait_traj_end(END_INTR|END_TRAJ);
354         i2c_cobboard_set_mode(I2C_COBBOARD_MODE_EJECT);
355         time_wait_ms(2000);
356
357         return END_TRAJ;
358 }
359
360 /* dump state (every 5 s max) */
361 #define DUMP_RATE_LIMIT(dump, last_print)               \
362         do {                                            \
363                 if (time_get_s() - last_print > 5) {    \
364                         dump();                         \
365                         last_print = time_get_s();      \
366                 }                                       \
367         } while (0)
368
369
370 /* return true if we need to grab some more elements */
371 static uint8_t need_more_elements(void)
372 {
373         if (time_get_s() <= 75) {
374                 /* we have enough time left */
375                 if (get_ball_count() >= 4)
376                         return 0;
377                 if (get_cob_count() >= 4)
378                         return 0;
379                 if ((get_ball_count() >= 2) &&
380                     (get_cob_count() >= 2))
381                         return 0;
382                 return 1;
383         }
384         else {
385                 /* not much time remaining */
386                 if ((get_ball_count() >= 1) &&
387                     (get_cob_count() >= 1))
388                         return 0;
389                 return 1;
390         }
391 }
392
393 static uint8_t strat_harvest(void)
394 {
395         return 0;
396 }
397
398 static uint8_t strat_eject(void)
399 {
400         return 0;
401 }
402
403 uint8_t strat_main(void)
404 {
405         uint8_t err;
406
407         /* harvest the first cobs + balls */
408         err = strat_beginning();
409
410         while (1) {
411                 /* end of time exit ! */
412                 if (err == END_TIMER) {
413                         DEBUG(E_USER_STRAT, "End of time");
414                         strat_exit();
415                         break;
416                 }
417
418                 if (need_more_elements() == 0) {
419                         /* we have enough elements, go to eject */
420                         err = strat_eject();
421                         if (!TRAJ_SUCCESS(err))
422                                 continue;
423                 }
424                 else {
425                         /* harvest */
426                         err = strat_harvest();
427                         if (!TRAJ_SUCCESS(err))
428                                 continue;
429                 }
430         }
431
432         return err;
433 }