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