94b9b6f7c0416890e1ef18150379ef2d87c5f415
[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 volatile 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 void strat_event_enable(void)
102 {
103         strat_running = 1;
104 }
105
106 void strat_event_disable(void)
107 {
108         strat_running = 0;
109 }
110
111 /* call it just before launching the strat */
112 void strat_init(void)
113 {
114 #ifdef HOST_VERSION
115         position_set(&mainboard.pos, 298.16,
116                      COLOR_Y(308.78), COLOR_A(70.00));
117 #endif
118
119         /* we consider that the color is correctly set */
120         strat_running = 1;
121         strat_db_init();
122         strat_set_speed(SPEED_DIST_FAST, SPEED_ANGLE_FAST);
123         time_reset();
124         interrupt_traj_reset();
125
126         i2c_cobboard_set_mode(I2C_COBBOARD_MODE_HARVEST);
127         i2c_ballboard_set_mode(I2C_BALLBOARD_MODE_HARVEST);
128
129         /* used in strat_base for END_TIMER */
130         mainboard.flags = DO_ENCODERS | DO_CS | DO_RS |
131                 DO_POS | DO_BD | DO_TIMER | DO_POWER;
132 }
133
134 /* call it after each strat */
135 void strat_exit(void)
136 {
137 #ifndef HOST_VERSION
138         uint8_t flags;
139 #endif
140
141         strat_running = 0;
142         mainboard.flags &= ~(DO_TIMER);
143         strat_hardstop();
144         time_reset();
145         wait_ms(100);
146 #ifndef HOST_VERSION
147         IRQ_LOCK(flags);
148         mainboard.flags &= ~(DO_CS);
149         IRQ_UNLOCK(flags);
150         pwm_ng_set(LEFT_PWM, 0);
151         pwm_ng_set(RIGHT_PWM, 0);
152 #endif
153 }
154
155 /* called periodically (10ms) */
156 void strat_event(void *dummy)
157 {
158         uint8_t flags;
159         uint8_t lcob, rcob;
160         uint8_t lidx, ridx;
161
162         /* ignore when strat is not running */
163         if (strat_running == 0)
164                 return;
165
166         IRQ_LOCK(flags);
167         lcob = ballboard.lcob;
168         ballboard.lcob = I2C_COB_NONE;
169         rcob = ballboard.rcob;
170         ballboard.rcob = I2C_COB_NONE;
171         IRQ_UNLOCK(flags);
172
173         /* XXX take opponent position into account */
174
175 #ifdef HOST_VERSION
176         if (time_get_s() == 15)
177                 cobboard.cob_count = 5;
178         if (time_get_s() == 16)
179                 cobboard.cob_count = 0;
180         if (time_get_s() == 25)
181                 cobboard.cob_count = 5;
182 #endif
183
184         /* detect cob on left side */
185         if (corn_is_near(&lidx, I2C_LEFT_SIDE)) {
186                 if (lcob != I2C_COB_NONE)
187                         corn_set_color(strat_db.corn_table[lidx], lcob);
188
189                 if (strat_db.corn_table[lidx]->corn.color == I2C_COB_WHITE)
190                         i2c_cobboard_autoharvest(I2C_LEFT_SIDE);
191                 else
192                         i2c_cobboard_deploy(I2C_LEFT_SIDE);
193         }
194         else {
195                 i2c_cobboard_deploy(I2C_LEFT_SIDE);
196         }
197
198         /* detect cob on right side */
199         if (corn_is_near(&ridx, I2C_RIGHT_SIDE)) {
200                 if (rcob != I2C_COB_NONE)
201                         corn_set_color(strat_db.corn_table[ridx], rcob);
202
203                 if (strat_db.corn_table[ridx]->corn.color == I2C_COB_WHITE)
204                         i2c_cobboard_autoharvest(I2C_RIGHT_SIDE);
205                 else
206                         i2c_cobboard_deploy(I2C_RIGHT_SIDE);
207         }
208         else {
209                 i2c_cobboard_deploy(I2C_RIGHT_SIDE);
210         }
211
212
213         /* limit speed when opponent is close */
214         strat_limit_speed();
215 }
216
217
218 static uint8_t strat_harvest(void)
219 {
220         return 0;
221 }
222
223 static uint8_t strat_eject(void)
224 {
225         uint8_t err;
226
227         //XXX return vals
228         strat_set_speed(600, SPEED_ANGLE_SLOW);
229
230         trajectory_goto_xy_abs(&mainboard.traj, 2625, COLOR_Y(1847));
231         err = wait_traj_end(TRAJ_FLAGS_NO_NEAR);
232         trajectory_a_abs(&mainboard.traj, COLOR_A(70));
233         err = wait_traj_end(TRAJ_FLAGS_NO_NEAR);
234
235         DEBUG(E_USER_STRAT, "%s():%d", __FUNCTION__, __LINE__);
236         strat_hardstop();
237
238         /* ball ejection */
239         trajectory_a_abs(&mainboard.traj, COLOR_A(90));
240         i2c_ballboard_set_mode(I2C_BALLBOARD_MODE_EJECT);
241         time_wait_ms(2000);
242
243         /* half turn */
244         i2c_cobboard_pack(I2C_LEFT_SIDE);
245         i2c_cobboard_pack(I2C_RIGHT_SIDE);
246         trajectory_a_rel(&mainboard.traj, COLOR_A(180));
247         err = wait_traj_end(END_INTR|END_TRAJ);
248
249         /* cob ejection */
250         trajectory_d_rel(&mainboard.traj, -100);
251         err = wait_traj_end(END_INTR|END_TRAJ);
252         i2c_cobboard_set_mode(I2C_COBBOARD_MODE_EJECT);
253         time_wait_ms(2000);
254
255         return 0;
256 }
257
258 static uint8_t strat_beginning(void)
259 {
260         uint8_t err;
261
262         strat_set_acc(ACC_DIST, ACC_ANGLE);
263 #ifdef HOST_VERSION
264         strat_set_speed(600, SPEED_ANGLE_FAST);
265 #else
266         /* 250 */
267         strat_set_speed(250, SPEED_ANGLE_FAST);
268 #endif
269
270         // strat_set_speed(600, 60); /* OK */
271         strat_set_speed(250, 28); /* OK */
272
273         trajectory_d_a_rel(&mainboard.traj, 1000, COLOR_A(20));
274         err = WAIT_COND_OR_TRAJ_END(trajectory_angle_finished(&mainboard.traj),
275                                     TRAJ_FLAGS_STD);
276
277         strat_set_acc(ACC_DIST, ACC_ANGLE);
278         strat_set_speed(250, SPEED_ANGLE_SLOW);
279
280 #if 1
281  l1:
282         DEBUG(E_USER_STRAT, "%s():%d", __FUNCTION__, __LINE__);
283         if (get_cob_count() >= 5)
284                 strat_set_speed(600, SPEED_ANGLE_FAST);
285
286         err = line2line(LINE_UP, 0, LINE_R_DOWN, 2);
287         if (!TRAJ_SUCCESS(err)) {
288                 strat_hardstop();
289                 time_wait_ms(2000);
290                 goto l1;
291         }
292
293  l2:
294         DEBUG(E_USER_STRAT, "%s():%d", __FUNCTION__, __LINE__);
295         if (get_cob_count() >= 5)
296                 strat_set_speed(600, SPEED_ANGLE_FAST);
297
298         err = line2line(LINE_R_DOWN, 2, LINE_R_UP, 2);
299         if (!TRAJ_SUCCESS(err)) {
300                 strat_hardstop();
301                 time_wait_ms(2000);
302                 goto l2;
303         }
304
305 #else
306         strat_set_speed(600, SPEED_ANGLE_FAST);
307         err = line2line(LINE_UP, 0, LINE_R_DOWN, 3);
308         err = line2line(LINE_R_DOWN, 3, LINE_R_UP, 2);
309         err = line2line(LINE_R_UP, 2, LINE_R_DOWN, 2);
310         err = line2line(LINE_R_DOWN, 2, LINE_R_UP, 3);
311         err = line2line(LINE_R_UP, 3, LINE_UP, 5);
312         err = line2line(LINE_UP, 5, LINE_L_DOWN, 2);
313         err = line2line(LINE_L_DOWN, 2, LINE_L_UP, 1);
314         err = line2line(LINE_L_UP, 1, LINE_L_DOWN, 1);
315         err = line2line(LINE_L_DOWN, 1, LINE_DOWN, 0);
316         wait_ms(500);
317         strat_hardstop();
318         return END_TRAJ;
319 #endif
320
321         strat_eject();
322
323         strat_set_speed(250, SPEED_ANGLE_FAST);
324
325  l4:
326         DEBUG(E_USER_STRAT, "%s():%d", __FUNCTION__, __LINE__);
327         if (get_cob_count() >= 5)
328                 strat_set_speed(600, SPEED_ANGLE_FAST);
329
330         err = line2line(LINE_DOWN, 5, LINE_L_UP, 2);
331         if (!TRAJ_SUCCESS(err)) {
332                 strat_hardstop();
333                 time_wait_ms(2000);
334                 goto l4;
335         }
336
337  l5:
338         DEBUG(E_USER_STRAT, "%s():%d", __FUNCTION__, __LINE__);
339         if (get_cob_count() >= 5)
340                 strat_set_speed(600, SPEED_ANGLE_FAST);
341
342         err = line2line(LINE_L_UP, 2, LINE_R_UP, 2);
343         if (!TRAJ_SUCCESS(err)) {
344                 strat_hardstop();
345                 time_wait_ms(2000);
346                 goto l5;
347         }
348
349         DEBUG(E_USER_STRAT, "%s():%d", __FUNCTION__, __LINE__);
350         if (get_cob_count() >= 5)
351                 strat_set_speed(600, SPEED_ANGLE_FAST);
352
353         WAIT_COND_OR_TRAJ_END(distance_from_robot(2625, COLOR_Y(1847)) < 100,
354                               TRAJ_FLAGS_STD);
355         strat_eject();
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 uint8_t strat_main(void)
394 {
395         uint8_t err;
396
397         /* harvest the first cobs + balls */
398         err = strat_beginning();
399
400         while (1) {
401                 /* end of time exit ! */
402                 if (err == END_TIMER) {
403                         DEBUG(E_USER_STRAT, "End of time");
404                         strat_exit();
405                         break;
406                 }
407
408                 if (need_more_elements() == 0) {
409                         /* we have enough elements, go to eject */
410                         err = strat_eject();
411                         if (!TRAJ_SUCCESS(err))
412                                 continue;
413                 }
414                 else {
415                         /* harvest */
416                         err = strat_harvest();
417                         if (!TRAJ_SUCCESS(err))
418                                 continue;
419                 }
420         }
421
422         return err;
423 }