avant la coupe de belgique
[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_base.h"
61 #include "strat_corn.h"
62 #include "strat_utils.h"
63 #include "sensor.h"
64 #include "actuator.h"
65
66 #define COL_DISP_MARGIN 400 /* stop 40 cm in front of dispenser */
67 #define COL_SCAN_PRE_MARGIN 250
68
69 struct strat_infos strat_infos = {
70         /* conf */
71         .conf = {
72                 .flags = 0,
73         },
74         /* status */
75         .status = {
76                 .flags = 0,
77         },
78 };
79
80 /*************************************************************/
81
82 /*                  INIT                                     */
83
84 /*************************************************************/
85
86 void strat_set_bounding_box(void)
87 {
88         if (get_color() == I2C_COLOR_YELLOW) {
89                 strat_infos.area_bbox.x1 = 300;
90                 strat_infos.area_bbox.y1 = 200;
91                 strat_infos.area_bbox.x2 = 2720; /* needed for c1 */
92                 strat_infos.area_bbox.y2 = 1800;
93         }
94         else {
95                 strat_infos.area_bbox.x1 = 200;
96                 strat_infos.area_bbox.y1 = 300;
97                 strat_infos.area_bbox.x2 = 2720; /* needed for c1 */
98                 strat_infos.area_bbox.y2 = 1900;
99         }
100
101         polygon_set_boundingbox(strat_infos.area_bbox.x1,
102                                 strat_infos.area_bbox.y1,
103                                 strat_infos.area_bbox.x2,
104                                 strat_infos.area_bbox.y2);
105 }
106
107 /* called before each strat, and before the start switch */
108 void strat_preinit(void)
109 {
110         time_reset();
111         interrupt_traj_reset();
112         mainboard.flags =  DO_ENCODERS | DO_CS | DO_RS |
113                 DO_POS | DO_BD | DO_POWER;
114
115         //i2c_cobboard_mode_init();
116         strat_dump_conf();
117         strat_dump_infos(__FUNCTION__);
118 }
119
120 void strat_dump_conf(void)
121 {
122         if (!strat_infos.dump_enabled)
123                 return;
124
125         printf_P(PSTR("-- conf --\r\n"));
126
127 }
128
129 /* display current information about the state of the game */
130 void strat_dump_infos(const char *caller)
131 {
132         if (!strat_infos.dump_enabled)
133                 return;
134
135         printf_P(PSTR("%s() dump strat infos:\r\n"), caller);
136 }
137
138 /* init current area state before a match. Dump update user conf
139  * here */
140 void strat_reset_infos(void)
141 {
142         init_corn_table(-1, -1);
143 }
144
145 /* call it just before launching the strat */
146 void strat_init(void)
147 {
148         /* XXX init rollers, .. */
149
150         strat_reset_infos();
151
152         /* we consider that the color is correctly set */
153
154         strat_set_speed(SPEED_DIST_FAST, SPEED_ANGLE_FAST);
155         time_reset();
156         interrupt_traj_reset();
157
158         i2c_cobboard_mode_harvest(I2C_LEFT_SIDE);
159         i2c_cobboard_mode_harvest(I2C_RIGHT_SIDE);
160         i2c_ballboard_set_mode(I2C_BALLBOARD_MODE_HARVEST);
161
162         /* used in strat_base for END_TIMER */
163         mainboard.flags = DO_ENCODERS | DO_CS | DO_RS |
164                 DO_POS | DO_BD | DO_TIMER | DO_POWER;
165 }
166
167
168 /* call it after each strat */
169 void strat_exit(void)
170 {
171         uint8_t flags;
172
173         mainboard.flags &= ~(DO_TIMER);
174         strat_hardstop();
175         time_reset();
176         wait_ms(1000);
177         IRQ_LOCK(flags);
178         mainboard.flags &= ~(DO_CS);
179         pwm_ng_set(LEFT_PWM, 0);
180         pwm_ng_set(RIGHT_PWM, 0);
181         IRQ_UNLOCK(flags);
182 }
183
184 /* called periodically */
185 void strat_event(void *dummy)
186 {
187 #if 0
188         /* pack or deploy spickle */
189         if (strat_infos.status.flags & STRAT_STATUS_LHARVEST) {
190                 if (sensor_get(S_LCOB_PRESENT)) {
191                         if (sensor_get(S_LCOB_WHITE))
192                                 i2c_ballboard_set_mode();
193                         else
194                                 ;
195                 }
196         }
197 #endif
198         /* limit speed when opponent is close */
199         strat_limit_speed();
200 }
201
202 static uint8_t strat_beginning(void)
203 {
204         uint8_t err;
205
206         strat_set_speed(250, SPEED_ANGLE_FAST);
207         //init_corn_table(0, 0);
208
209         err = line2line(LINE_UP, 0, LINE_R_DOWN, 2);
210         err = line2line(LINE_R_DOWN, 2, LINE_R_UP, 2);
211         err = line2line(LINE_R_UP, 2, LINE_UP, 5);
212         trajectory_hardstop(&mainboard.traj);
213
214         /* ball ejection */
215         trajectory_a_abs(&mainboard.traj, COLOR_A(90));
216         i2c_ballboard_set_mode(I2C_BALLBOARD_MODE_EJECT);
217         time_wait_ms(2000);
218
219         /* half turn */
220         trajectory_goto_xy_abs(&mainboard.traj, 2625, COLOR_Y(1847));
221         err = wait_traj_end(END_INTR|END_TRAJ);
222         i2c_cobboard_mode_pack(I2C_LEFT_SIDE);
223         i2c_cobboard_mode_pack(I2C_RIGHT_SIDE);
224         trajectory_a_rel(&mainboard.traj, COLOR_A(180));
225         err = wait_traj_end(END_INTR|END_TRAJ);
226
227         /* cob ejection */
228         trajectory_d_rel(&mainboard.traj, -100);
229         err = wait_traj_end(END_INTR|END_TRAJ);
230         i2c_cobboard_mode_eject();
231         time_wait_ms(2000);
232
233         return END_TRAJ;
234 }
235
236 /* dump state (every 5 s max) */
237 #define DUMP_RATE_LIMIT(dump, last_print)               \
238         do {                                            \
239                 if (time_get_s() - last_print > 5) {    \
240                         dump();                         \
241                         last_print = time_get_s();      \
242                 }                                       \
243         } while (0)
244
245
246 uint8_t strat_main(void)
247 {
248         uint8_t err;
249
250         /* */
251         err = strat_beginning();
252
253         return err;
254 }