2c6042828a6b74e1593388d8e8b7acdfc5acc315
[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 struct strat_conf strat_conf;
71
72 /*************************************************************/
73
74 /*                  INIT                                     */
75
76 /*************************************************************/
77
78 /* called before each strat, and before the start switch */
79 void strat_preinit(void)
80 {
81         time_reset();
82         interrupt_traj_reset();
83         mainboard.flags =  DO_ENCODERS | DO_CS | DO_RS |
84                 DO_POS | DO_BD | DO_POWER;
85
86         //i2c_cobboard_mode_init();
87         strat_conf_dump(__FUNCTION__);
88         strat_db_dump(__FUNCTION__);
89 }
90
91 void strat_conf_dump(const char *caller)
92 {
93         if (!strat_conf.dump_enabled)
94                 return;
95
96         printf_P(PSTR("-- conf --\r\n"));
97
98 }
99
100 /* call it just before launching the strat */
101 void strat_init(void)
102 {
103         position_set(&mainboard.pos, 298.16,
104                      COLOR_Y(308.78), COLOR_A(70.00));
105
106         /* XXX init rollers, .. */
107
108         strat_db_init();
109
110         /* we consider that the color is correctly set */
111
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_cobboard_harvest(I2C_LEFT_SIDE);
118         i2c_cobboard_harvest(I2C_RIGHT_SIDE);
119         i2c_ballboard_set_mode(I2C_BALLBOARD_MODE_HARVEST);
120
121         /* used in strat_base for END_TIMER */
122         mainboard.flags = DO_ENCODERS | DO_CS | DO_RS |
123                 DO_POS | DO_BD | DO_TIMER | DO_POWER;
124 }
125
126
127 /* call it after each strat */
128 void strat_exit(void)
129 {
130 #ifndef HOST_VERSION
131         uint8_t flags;
132 #endif
133
134         mainboard.flags &= ~(DO_TIMER);
135         strat_hardstop();
136         time_reset();
137         wait_ms(100);
138 #ifndef HOST_VERSION
139         IRQ_LOCK(flags);
140         mainboard.flags &= ~(DO_CS);
141         IRQ_UNLOCK(flags);
142         pwm_ng_set(LEFT_PWM, 0);
143         pwm_ng_set(RIGHT_PWM, 0);
144 #endif
145 }
146
147 /* called periodically */
148 void strat_event(void *dummy)
149 {
150         uint8_t flags;
151         uint8_t lcob, rcob;
152
153         IRQ_LOCK(flags);
154         lcob = ballboard.lcob;
155         ballboard.lcob = I2C_COB_NONE;
156         rcob = ballboard.rcob;
157         ballboard.rcob = I2C_COB_NONE;
158         IRQ_UNLOCK(flags);
159
160         if (lcob == I2C_COB_WHITE)
161                 DEBUG(E_USER_STRAT, "lcob white");
162         if (lcob == I2C_COB_BLACK)
163                 DEBUG(E_USER_STRAT, "lcob black");
164         if (rcob == I2C_COB_WHITE)
165                 DEBUG(E_USER_STRAT, "rcob white");
166         if (rcob == I2C_COB_BLACK)
167                 DEBUG(E_USER_STRAT, "rcob black");
168
169         /* limit speed when opponent is close */
170         strat_limit_speed();
171 }
172
173 static uint8_t strat_beginning(void)
174 {
175         uint8_t err;
176
177         strat_set_acc(ACC_DIST, ACC_ANGLE);
178 #ifdef HOST_VERSION
179         strat_set_speed(600, SPEED_ANGLE_FAST);
180 #else
181         /* 250 */
182         strat_set_speed(250, SPEED_ANGLE_FAST);
183 #endif
184
185
186         // strat_set_speed(600, 60); /* OK */
187         strat_set_speed(250, 28); /* OK */
188
189         trajectory_d_a_rel(&mainboard.traj, 1000, COLOR_A(20));
190         err = WAIT_COND_OR_TRAJ_END(trajectory_angle_finished(&mainboard.traj),
191                                     TRAJ_FLAGS_STD);
192
193         strat_set_acc(ACC_DIST, ACC_ANGLE);
194
195 #if 1
196  l1:
197         if (get_cob_count() >= 5)
198                 strat_set_speed(600, SPEED_ANGLE_FAST);
199
200         err = line2line(LINE_UP, 0, LINE_R_DOWN, 2);
201         if (!TRAJ_SUCCESS(err)) {
202                 trajectory_hardstop(&mainboard.traj);
203                 time_wait_ms(2000);
204                 goto l1;
205         }
206
207  l2:
208         if (get_cob_count() >= 5)
209                 strat_set_speed(600, SPEED_ANGLE_FAST);
210
211         err = line2line(LINE_R_DOWN, 2, LINE_R_UP, 2);
212         if (!TRAJ_SUCCESS(err)) {
213                 trajectory_hardstop(&mainboard.traj);
214                 time_wait_ms(2000);
215                 goto l2;
216         }
217
218  l3:
219         if (get_cob_count() >= 5)
220                 strat_set_speed(600, SPEED_ANGLE_FAST);
221
222         err = line2line(LINE_R_UP, 2, LINE_UP, 5);
223         if (!TRAJ_SUCCESS(err)) {
224                 trajectory_hardstop(&mainboard.traj);
225                 time_wait_ms(2000);
226                 goto l3;
227         }
228 #else
229         strat_set_speed(600, SPEED_ANGLE_FAST);
230         err = line2line(LINE_UP, 0, LINE_R_DOWN, 3);
231         err = line2line(LINE_R_DOWN, 3, LINE_R_UP, 2);
232         err = line2line(LINE_R_UP, 2, LINE_R_DOWN, 2);
233         err = line2line(LINE_R_DOWN, 2, LINE_R_UP, 3);
234         err = line2line(LINE_R_UP, 3, LINE_UP, 5);
235         err = line2line(LINE_UP, 5, LINE_L_DOWN, 2);
236         err = line2line(LINE_L_DOWN, 2, LINE_L_UP, 1);
237         err = line2line(LINE_L_UP, 1, LINE_L_DOWN, 1);
238         err = line2line(LINE_L_DOWN, 1, LINE_DOWN, 0);
239         wait_ms(500);
240         trajectory_hardstop(&mainboard.traj);
241         return END_TRAJ;
242 #endif
243
244         trajectory_hardstop(&mainboard.traj);
245
246         /* ball ejection */
247         trajectory_a_abs(&mainboard.traj, COLOR_A(90));
248         i2c_ballboard_set_mode(I2C_BALLBOARD_MODE_EJECT);
249         time_wait_ms(2000);
250
251         /* half turn */
252         trajectory_goto_xy_abs(&mainboard.traj, 2625, COLOR_Y(1847));
253         err = wait_traj_end(END_INTR|END_TRAJ);
254         i2c_cobboard_pack(I2C_LEFT_SIDE);
255         i2c_cobboard_pack(I2C_RIGHT_SIDE);
256         trajectory_a_rel(&mainboard.traj, COLOR_A(180));
257         err = wait_traj_end(END_INTR|END_TRAJ);
258
259         /* cob ejection */
260         trajectory_d_rel(&mainboard.traj, -100);
261         err = wait_traj_end(END_INTR|END_TRAJ);
262         i2c_cobboard_set_mode(I2C_COBBOARD_MODE_EJECT);
263         time_wait_ms(2000);
264
265         trajectory_hardstop(&mainboard.traj);
266         return END_TRAJ;
267 }
268
269 /* dump state (every 5 s max) */
270 #define DUMP_RATE_LIMIT(dump, last_print)               \
271         do {                                            \
272                 if (time_get_s() - last_print > 5) {    \
273                         dump();                         \
274                         last_print = time_get_s();      \
275                 }                                       \
276         } while (0)
277
278
279 /* return true if we need to grab some more elements */
280 static uint8_t need_more_elements(void)
281 {
282         if (time_get_s() <= 75) {
283                 /* we have enough time left */
284                 if (get_ball_count() >= 4)
285                         return 0;
286                 if (get_cob_count() >= 4)
287                         return 0;
288                 if ((get_ball_count() >= 2) &&
289                     (get_cob_count() >= 2))
290                         return 0;
291                 return 1;
292         }
293         else {
294                 /* not much time remaining */
295                 if ((get_ball_count() >= 1) &&
296                     (get_cob_count() >= 1))
297                         return 0;
298                 return 1;
299         }
300 }
301
302 static uint8_t strat_harvest(void)
303 {
304         return 0;
305 }
306
307 static uint8_t strat_eject(void)
308 {
309         return 0;
310 }
311
312 uint8_t strat_main(void)
313 {
314         uint8_t err;
315
316         /* harvest the first cobs + balls */
317         err = strat_beginning();
318
319         while (1) {
320                 /* end of time exit ! */
321                 if (err == END_TIMER) {
322                         DEBUG(E_USER_STRAT, "End of time");
323                         strat_exit();
324                         break;
325                 }
326
327                 if (need_more_elements() == 0) {
328                         /* we have enough elements, go to eject */
329                         err = strat_eject();
330                         if (!TRAJ_SUCCESS(err))
331                                 continue;
332                 }
333                 else {
334                         /* harvest */
335                         err = strat_harvest();
336                         if (!TRAJ_SUCCESS(err))
337                                 continue;
338                 }
339         }
340
341         return err;
342 }