7ea7b88ab068b2465b13301ce30c92af4fdb87eb
[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 struct strat_conf strat_conf;
74
75 /*************************************************************/
76
77 /*                  INIT                                     */
78
79 /*************************************************************/
80
81 /* called before each strat, and before the start switch */
82 void strat_preinit(void)
83 {
84         time_reset();
85         interrupt_traj_reset();
86         mainboard.flags =  DO_ENCODERS | DO_CS | DO_RS |
87                 DO_POS | DO_BD | DO_POWER;
88
89         strat_db_init();
90         strat_conf_dump(__FUNCTION__);
91         strat_db_dump(__FUNCTION__);
92 }
93
94 void strat_conf_dump(const char *caller)
95 {
96         if (!strat_conf.dump_enabled)
97                 return;
98
99         printf_P(PSTR("-- conf --\r\n"));
100
101 }
102
103 void strat_event_enable(void)
104 {
105         strat_running = 1;
106 }
107
108 void strat_event_disable(void)
109 {
110         strat_running = 0;
111 }
112
113 /* call it just before launching the strat */
114 void strat_init(void)
115 {
116 #ifdef HOST_VERSION
117         position_set(&mainboard.pos, 298.16,
118                      COLOR_Y(308.78), COLOR_A(70.00));
119 #endif
120
121         /* we consider that the color is correctly set */
122         strat_running = 1;
123         strat_set_speed(SPEED_DIST_FAST, SPEED_ANGLE_FAST);
124         time_reset();
125         interrupt_traj_reset();
126
127         i2c_cobboard_set_mode(I2C_COBBOARD_MODE_HARVEST);
128         i2c_ballboard_set_mode(I2C_BALLBOARD_MODE_HARVEST);
129
130         /* used in strat_base for END_TIMER */
131         mainboard.flags = DO_ENCODERS | DO_CS | DO_RS |
132                 DO_POS | DO_BD | DO_TIMER | DO_POWER;
133 }
134
135 /* call it after each strat */
136 void strat_exit(void)
137 {
138 #ifndef HOST_VERSION
139         uint8_t flags;
140 #endif
141
142         strat_running = 0;
143         mainboard.flags &= ~(DO_TIMER);
144         strat_hardstop();
145         time_reset();
146         wait_ms(100);
147 #ifndef HOST_VERSION
148         IRQ_LOCK(flags);
149         mainboard.flags &= ~(DO_CS);
150         IRQ_UNLOCK(flags);
151         pwm_ng_set(LEFT_PWM, 0);
152         pwm_ng_set(RIGHT_PWM, 0);
153 #endif
154 }
155
156 /* called periodically (10ms) */
157 void strat_event(void *dummy)
158 {
159         uint8_t flags;
160         int8_t lcob_near, rcob_near;
161         uint8_t lcob, rcob;
162         uint8_t lidx, ridx;
163
164         /* ignore when strat is not running */
165         if (strat_running == 0)
166                 return;
167
168         /* read sensors from ballboard */
169         IRQ_LOCK(flags);
170         lcob = ballboard.lcob;
171         ballboard.lcob = I2C_COB_NONE;
172         rcob = ballboard.rcob;
173         ballboard.rcob = I2C_COB_NONE;
174         IRQ_UNLOCK(flags);
175
176 /*      if (lcob != I2C_COB_NONE) */
177 /*              DEBUG(E_USER_STRAT, "XXX lcob %s", */
178 /*                    lcob == I2C_COB_WHITE ? "white" : "black"); */
179 /*      if (rcob != I2C_COB_NONE) */
180 /*              DEBUG(E_USER_STRAT, "XXX rcob %s", */
181 /*                    rcob == I2C_COB_WHITE ? "white" : "black"); */
182         /* XXX take opponent position into account */
183
184 #ifdef HOST_VERSION
185         if (time_get_s() == 15)
186                 cobboard.cob_count = 5;
187         if (time_get_s() == 16)
188                 cobboard.cob_count = 0;
189         if (time_get_s() == 25)
190                 cobboard.cob_count = 5;
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) {
213                 if (get_cob_count() >= 5 || want_pack) {
214                         if (strat_db.corn_table[lidx]->corn.color != I2C_COB_WHITE &&
215                             strat_db.corn_table[lidx]->corn.color != I2C_COB_BLACK)
216                                 i2c_cobboard_pack(I2C_LEFT_SIDE);
217                 }
218                 else {
219                         if (strat_db.corn_table[lidx]->corn.color == I2C_COB_WHITE)
220                                 i2c_cobboard_autoharvest(I2C_LEFT_SIDE);
221                         else if (strat_db.corn_table[lidx]->corn.color == I2C_COB_BLACK)
222                                 i2c_cobboard_deploy_nomove(I2C_LEFT_SIDE);
223                         else
224                                 i2c_cobboard_deploy(I2C_LEFT_SIDE);
225                 }
226         }
227         else {
228                 if (get_cob_count() >= 5 || want_pack)
229                         i2c_cobboard_pack(I2C_LEFT_SIDE);
230                 else
231                         i2c_cobboard_deploy(I2C_LEFT_SIDE);
232         }
233
234         /* control the cobboard mode for right spickle */
235         if (rcob_near) {
236                 if (get_cob_count() >= 5 || want_pack) {
237                         if (strat_db.corn_table[ridx]->corn.color != I2C_COB_WHITE &&
238                             strat_db.corn_table[ridx]->corn.color != I2C_COB_BLACK)
239                                 i2c_cobboard_pack(I2C_RIGHT_SIDE);
240                 }
241                 else {
242                         if (strat_db.corn_table[ridx]->corn.color == I2C_COB_WHITE)
243                                 i2c_cobboard_autoharvest(I2C_RIGHT_SIDE);
244                         else if (strat_db.corn_table[ridx]->corn.color == I2C_COB_BLACK)
245                                 i2c_cobboard_deploy_nomove(I2C_RIGHT_SIDE);
246                         else
247                                 i2c_cobboard_deploy(I2C_RIGHT_SIDE);
248                 }
249         }
250         else {
251                 if (get_cob_count() >= 5 || want_pack)
252                         i2c_cobboard_pack(I2C_RIGHT_SIDE);
253                 else
254                         i2c_cobboard_deploy(I2C_RIGHT_SIDE);
255         }
256
257         /* limit speed when opponent is near */
258         strat_limit_speed();
259 }
260
261
262 static uint8_t strat_harvest(void)
263 {
264         return 0;
265 }
266
267 static uint8_t strat_eject(void)
268 {
269         uint8_t err;
270
271         //XXX return vals
272         strat_set_speed(600, SPEED_ANGLE_SLOW);
273
274         want_pack = 1;
275         trajectory_goto_xy_abs(&mainboard.traj, 2625, COLOR_Y(1847));
276         err = wait_traj_end(TRAJ_FLAGS_NO_NEAR);
277
278         /* ball ejection */
279         i2c_ballboard_set_mode(I2C_BALLBOARD_MODE_EJECT);
280         trajectory_a_abs(&mainboard.traj, COLOR_A(70));
281         err = wait_traj_end(TRAJ_FLAGS_NO_NEAR);
282
283         DEBUG(E_USER_STRAT, "%s():%d", __FUNCTION__, __LINE__);
284         strat_hardstop();
285         time_wait_ms(2000);
286
287
288         /* half turn */
289         strat_event_disable();
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, -70);
297         err = wait_traj_end(END_INTR|END_TRAJ);
298
299         i2c_cobboard_set_mode(I2C_COBBOARD_MODE_EJECT);
300         strat_db_dump(__FUNCTION__);
301         time_wait_ms(2000);
302
303         strat_event_enable();
304         want_pack = 0;
305         return 0;
306 }
307
308 static uint8_t strat_beginning(void)
309 {
310         uint8_t err;
311
312         strat_set_acc(ACC_DIST, ACC_ANGLE);
313 #ifdef HOST_VERSION
314         strat_set_speed(600, SPEED_ANGLE_FAST);
315 #else
316         /* 250 */
317         strat_set_speed(250, SPEED_ANGLE_FAST);
318 #endif
319
320         // strat_set_speed(600, 60); /* OK */
321         strat_set_speed(250, 28); /* OK */
322
323         trajectory_d_a_rel(&mainboard.traj, 1000, COLOR_A(20));
324         err = WAIT_COND_OR_TRAJ_END(trajectory_angle_finished(&mainboard.traj),
325                                     TRAJ_FLAGS_STD);
326
327         strat_set_acc(ACC_DIST, ACC_ANGLE);
328         strat_set_speed(250, SPEED_ANGLE_SLOW);
329
330 #if 1
331  l1:
332         DEBUG(E_USER_STRAT, "%s():%d count=%d", __FUNCTION__, __LINE__, get_cob_count());
333         if (get_cob_count() >= 5)
334                 strat_set_speed(600, SPEED_ANGLE_FAST);
335
336         err = line2line(0, LINE_UP, 2, LINE_R_DOWN);
337         if (!TRAJ_SUCCESS(err)) {
338                 strat_hardstop();
339                 time_wait_ms(2000);
340                 goto l1;
341         }
342
343  l2:
344         DEBUG(E_USER_STRAT, "%s():%d count=%d", __FUNCTION__, __LINE__, get_cob_count());
345         if (get_cob_count() >= 5)
346                 strat_set_speed(600, SPEED_ANGLE_FAST);
347
348         err = line2line(2, LINE_R_DOWN, 2, LINE_R_UP);
349         if (!TRAJ_SUCCESS(err)) {
350                 strat_hardstop();
351                 time_wait_ms(2000);
352                 goto l2;
353         }
354
355 #else
356 /*      strat_set_speed(600, SPEED_ANGLE_FAST); */
357 /*      err = line2line(0, LINE_UP, 3, LINE_R_DOWN); */
358 /*      err = line2line(3, LINE_R_DOWN, 2, LINE_R_UP); */
359 /*      err = line2line(2, LINE_R_UP, 2, LINE_R_DOWN); */
360 /*      err = line2line(2, LINE_R_DOWN, 3, LINE_R_UP); */
361 /*      err = line2line(3, LINE_R_UP, 5, LINE_UP); */
362 /*      err = line2line(5, LINE_UP, 2, LINE_L_DOWN); */
363 /*      err = line2line(2, LINE_L_DOWN, 1, LINE_L_UP); */
364 /*      err = line2line(1, LINE_L_UP, 1, LINE_L_DOWN); */
365 /*      err = line2line(1, LINE_L_DOWN, 0, LINE_DOWN); */
366
367         strat_set_speed(300, SPEED_ANGLE_FAST);
368         err = line2line(0, LINE_UP, 0, LINE_R_UP);
369         err = line2line(0, LINE_R_UP, 1, LINE_DOWN);
370         err = line2line(1, LINE_DOWN, 1, LINE_L_DOWN);
371         err = line2line(1, LINE_L_DOWN, 0, LINE_DOWN);
372         wait_ms(500);
373         strat_hardstop();
374         return END_TRAJ;
375 #endif
376
377         strat_eject();
378
379         strat_set_speed(250, SPEED_ANGLE_FAST);
380
381 #if 0
382  l4:
383         DEBUG(E_USER_STRAT, "%s():%d count=%d", __FUNCTION__, __LINE__, get_cob_count());
384         if (get_cob_count() >= 5)
385                 strat_set_speed(600, SPEED_ANGLE_FAST);
386
387         err = line2line(5, LINE_DOWN, 2, LINE_L_UP);
388         if (!TRAJ_SUCCESS(err)) {
389                 strat_hardstop();
390                 time_wait_ms(2000);
391                 goto l4;
392         }
393
394  l5:
395         DEBUG(E_USER_STRAT, "%s():%d count=%d", __FUNCTION__, __LINE__, get_cob_count());
396         if (get_cob_count() >= 5)
397                 strat_set_speed(600, SPEED_ANGLE_FAST);
398
399         err = line2line(2, LINE_L_UP, 2, LINE_R_UP);
400         if (!TRAJ_SUCCESS(err)) {
401                 strat_hardstop();
402                 time_wait_ms(2000);
403                 goto l5;
404         }
405
406         DEBUG(E_USER_STRAT, "%s():%d count=%d", __FUNCTION__, __LINE__, get_cob_count());
407         if (get_cob_count() >= 5)
408                 strat_set_speed(600, SPEED_ANGLE_FAST);
409 #else
410         strat_harvest_circuit();
411 #endif
412
413         WAIT_COND_OR_TRAJ_END(distance_from_robot(2625, COLOR_Y(1847)) < 100,
414                               TRAJ_FLAGS_STD);
415         strat_eject();
416
417         return END_TRAJ;
418 }
419
420 /* dump state (every 5 s max) */
421 #define DUMP_RATE_LIMIT(dump, last_print)               \
422         do {                                            \
423                 if (time_get_s() - last_print > 5) {    \
424                         dump();                         \
425                         last_print = time_get_s();      \
426                 }                                       \
427         } while (0)
428
429
430 /* return true if we need to grab some more elements */
431 static uint8_t need_more_elements(void)
432 {
433         if (time_get_s() <= 75) {
434                 /* we have enough time left */
435                 if (get_ball_count() >= 4)
436                         return 0;
437                 if (get_cob_count() >= 4)
438                         return 0;
439                 if ((get_ball_count() >= 2) &&
440                     (get_cob_count() >= 2))
441                         return 0;
442                 return 1;
443         }
444         else {
445                 /* not much time remaining */
446                 if ((get_ball_count() >= 1) &&
447                     (get_cob_count() >= 1))
448                         return 0;
449                 return 1;
450         }
451 }
452
453 uint8_t strat_main(void)
454 {
455         uint8_t err;
456
457         /* harvest the first cobs + balls */
458         err = strat_beginning();
459
460         while (1) {
461                 /* end of time exit ! */
462                 if (err == END_TIMER) {
463                         DEBUG(E_USER_STRAT, "End of time");
464                         strat_exit();
465                         break;
466                 }
467
468                 if (need_more_elements() == 0) {
469                         /* we have enough elements, go to eject */
470                         err = strat_eject();
471                         if (!TRAJ_SUCCESS(err))
472                                 continue;
473                 }
474                 else {
475                         /* harvest */
476                         err = strat_harvest();
477                         if (!TRAJ_SUCCESS(err))
478                                 continue;
479                 }
480         }
481
482         return err;
483 }