test
[aversive.git] / projects / microb2010 / mainboard / strat.h
1 /*  
2  *  Copyright Droids Corporation, Microb Technology, Eirbot (2008)
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.h,v 1.7 2009-11-08 17:24:33 zer0 Exp $
19  *
20  */
21
22 #ifndef _STRAT_H_
23 #define _STRAT_H_
24
25 /* convert coords according to our color */
26 #define COLOR_Y(y)     ((mainboard.our_color==I2C_COLOR_RED)? (y) : (AREA_Y-(y)))
27 #define COLOR_A(a)     ((mainboard.our_color==I2C_COLOR_RED)? (a) : (-a))
28 #define COLOR_SIGN(x)  ((mainboard.our_color==I2C_COLOR_RED)? (x) : (-x))
29 #define COLOR_INVERT(x) ((mainboard.our_color==I2C_COLOR_RED)? (x) : (!x))
30
31 /* area */
32 #define AREA_X 3000
33 #define AREA_Y 2100
34
35 #define START_X 200
36 #define START_Y COLOR_Y(200)
37 #define START_A COLOR_A(45)
38
39 #define CENTER_X 1500
40 #define CENTER_Y 1050
41
42 #define CORNER_X 3000
43 #define CORNER_Y COLOR_Y(2100)
44
45 /*
46  *              /- line 0
47  *             |   /- line 1
48  *             |  |   /- line 2
49  *             |  |  |
50  *     +---C1--------------------------C1---+
51  *     |          z0a   z1    z0b           |
52  *     |                 .                  |
53  *     |                 .                  |
54  *    C3       0  1  2   ^                  C3
55  *  y  |       3  4  5 <   >                |
56  *    C2       6  7  8   v                  C2 
57  *     |------ 9 10 11   .            ------| 
58  *     |     |           .            |     | 
59  *     | red |           .            |green| 
60  *     +-----+--L1--L2-------L2--L1---+-----+
61  *                       x
62  */
63
64 /* static columns */
65 #define LINE0_X 600
66 #define LINE1_X 850
67 #define LINE2_X 1100
68
69 #define COL0_X 600
70 #define COL0_Y COLOR_Y(1175)
71 #define COL1_X 850
72 #define COL1_Y COLOR_Y(1175)
73 #define COL2_X 1100
74 #define COL2_Y COLOR_Y(1175)
75
76 #define COL3_X 600
77 #define COL3_Y COLOR_Y(975)
78 #define COL4_X 850
79 #define COL4_Y COLOR_Y(975)
80 #define COL5_X 1100
81 #define COL5_Y COLOR_Y(975)
82
83 #define COL6_X 600
84 #define COL6_Y COLOR_Y(775)
85 #define COL7_X 850
86 #define COL7_Y COLOR_Y(775)
87 #define COL8_X 1100
88 #define COL8_Y COLOR_Y(775)
89
90 #define COL9_X 600
91 #define COL9_Y COLOR_Y(575)
92 #define COL10_X 850
93 #define COL10_Y COLOR_Y(575)
94 #define COL11_X 1100
95 #define COL11_Y COLOR_Y(575)
96
97 /* distance to go backward before pickup in dispenser */
98 #define DIST_BACK_DISPENSER 35
99
100 /* diag of the pentagon (pentacle ?) */
101 #define DISC_PENTA_DIAG 530
102
103 #define COL_DISP_MAX_TRIES 5
104 #define LIN_DISP_MAX_TRIES 3
105
106 /* useful traj flags */
107 #define TRAJ_SUCCESS(f) (f & (END_TRAJ|END_NEAR))
108 #define TRAJ_FLAGS_STD (END_TRAJ|END_BLOCKING|END_NEAR|END_OBSTACLE|END_INTR|END_TIMER)
109 #define TRAJ_FLAGS_NO_TIMER (END_TRAJ|END_BLOCKING|END_NEAR|END_OBSTACLE|END_INTR)
110 #define TRAJ_FLAGS_NO_NEAR (END_TRAJ|END_BLOCKING|END_OBSTACLE|END_INTR|END_TIMER)
111 #define TRAJ_FLAGS_NO_NEAR_NO_TIMER (END_TRAJ|END_BLOCKING|END_OBSTACLE|END_INTR)
112 #define TRAJ_FLAGS_SMALL_DIST (END_TRAJ|END_BLOCKING|END_INTR)
113
114 /* default speeds */
115 #define SPEED_DIST_FAST 2500
116 #define SPEED_ANGLE_FAST 2000
117 #define SPEED_DIST_SLOW 1000
118 #define SPEED_ANGLE_SLOW 1000
119 #define SPEED_DIST_VERY_SLOW 400
120 #define SPEED_ANGLE_VERY_SLOW 400
121
122 /* strat infos structures */
123
124 struct bbox {
125         int32_t x1;
126         int32_t y1;
127         int32_t x2;
128         int32_t y2;
129 };
130
131 struct conf {
132 #define STRAT_CONF_ONLY_ONE_ON_DISC      0x01
133 #define STRAT_CONF_BYPASS_STATIC2        0x02
134 #define STRAT_CONF_TAKE_ONE_LINTEL       0x04
135 #define STRAT_CONF_SKIP_WHEN_CHECK_FAILS 0x08
136 #define STRAT_CONF_STORE_STATIC2         0x10
137 #define STRAT_CONF_BIG_3_TEMPLE          0x20
138 #define STRAT_CONF_EARLY_SCAN            0x40
139 #define STRAT_CONF_PUSH_OPP_COLS         0x80
140         uint8_t flags;
141         uint8_t scan_opp_min_time;
142         uint8_t delay_between_opp_scan;
143         uint8_t scan_our_min_time;
144         uint8_t delay_between_our_scan;
145         uint8_t wait_opponent;
146         uint8_t lintel_min_time;
147         int16_t scan_opp_angle;
148 };
149
150 struct static_columns {
151 #define STATIC_COL_LINE0_DONE 0x01
152 #define STATIC_COL_LINE1_DONE 0x02
153 #define STATIC_COL_LINE2_DONE 0x04
154         uint8_t flags;
155         uint8_t configuration;
156 };
157
158 struct column_dispenser {
159         int8_t count;
160         uint8_t last_try_time;
161         uint8_t scan_left;
162         int16_t checkpoint_x;
163         int16_t checkpoint_y;
164         int16_t scan_a;
165         int16_t eject_a;
166         int16_t pickup_a;
167         int16_t recalib_x;
168         int16_t recalib_y;
169         char *name;
170 };
171
172 struct lintel_dispenser {
173         int8_t count;
174         uint8_t last_try_time;
175         int16_t x;
176         char *name;
177 };
178
179 struct temple {
180 #define TEMPLE_F_VALID    0x01 /* structure is valid */
181 #define TEMPLE_F_MONOCOL  0x02 /* temple has only one col */
182 #define TEMPLE_F_ON_DISC  0x04 /* temple is on disc (else it's on other zone) */
183 #define TEMPLE_F_OPPONENT 0x08 /* temple was originally built by opponent */
184 #define TEMPLE_F_LINTEL   0x10 /* lintel on top (don't put another lintel) */
185
186         uint8_t flags;
187         /* position of the robot when we built it */
188         int16_t x;
189         int16_t y;
190         int16_t a;
191
192         /* position of the robot checkpoint */
193         int16_t checkpoint_x;
194         int16_t checkpoint_y;
195
196         /* position and level of each col */
197         uint8_t level_l;
198         uint8_t dist_l;
199         uint8_t angle_l;
200
201         uint8_t level_r;
202         uint8_t dist_r;
203         uint8_t angle_r;
204
205 #define TEMPLE_DISABLE_TIME 5
206         uint8_t last_try_time;
207
208         struct build_zone *zone;
209 };
210
211 struct build_zone {
212 #define ZONE_F_VALID 0x01      /* zone is valid */
213 #define ZONE_F_DISC  0x02      /* specific disc zone */
214 #define ZONE_F_ZONE1 0x04      /* specific zone 1 */
215 #define ZONE_F_ZONE0 0x08      /* specific zone 0 */
216 #define ZONE_F_BUSY  0x10      /* this zone is busy */
217         uint8_t flags;
218         uint8_t level;
219         int16_t checkpoint_x;
220         int16_t checkpoint_y;
221
222 #define ZONE_DISABLE_TIME 5
223         uint8_t last_try_time;
224         char *name;
225 };
226
227 #define MAX_TEMPLE 5
228 #define MAX_ZONE 5
229
230 /* all infos related to strat */
231 struct strat_infos {
232         uint8_t dump_enabled;
233         struct conf conf;
234         struct bbox area_bbox;
235         uint8_t taken_lintel;
236         uint8_t col_in_boobs;
237         uint8_t lazy_pickup_done;
238         uint8_t i2c_loaded_skipped;
239         struct static_columns s_cols;
240         struct column_dispenser c1;
241         struct column_dispenser c2;
242         struct column_dispenser c3;
243         struct lintel_dispenser l1;
244         struct lintel_dispenser l2;
245         struct build_zone zone_list[MAX_ZONE];
246         struct temple temple_list[MAX_TEMPLE];
247 };
248 extern struct strat_infos strat_infos;
249
250 /* in strat.c */
251 void strat_dump_infos(const char *caller); /* show current known state
252                                               of area */
253 void strat_dump_temple(struct temple *temple);
254 void strat_dump_conf(void);
255 void strat_reset_infos(void); /* reset current known state */
256 void strat_preinit(void);
257 void strat_init(void);
258 void strat_exit(void);
259 void strat_dump_flags(void);
260 void strat_goto_near(int16_t x, int16_t y, uint16_t dist);
261 uint8_t strat_main(void);
262 void strat_event(void *dummy);
263
264 /* in strat_static_columns.c */
265 uint8_t strat_static_columns(uint8_t configuration);
266 uint8_t strat_static_columns_pass2(void);
267
268 /* in strat_lintel.c */
269 uint8_t strat_goto_lintel_disp(struct lintel_dispenser *disp);
270 uint8_t strat_pickup_lintels(void);
271
272 /* in strat_column_disp.c */
273 uint8_t strat_eject_col(int16_t eject_a, int16_t pickup_a);
274 uint8_t strat_pickup_columns(void);
275 uint8_t strat_goto_col_disp(struct column_dispenser **disp);
276
277 /* in strat_building.c */
278 uint8_t strat_goto_disc(int8_t level);
279 uint8_t strat_goto_build_zone(struct build_zone *build_zone, uint8_t level);
280 uint8_t strat_build_new_temple(struct build_zone *build_zone);
281 uint8_t strat_goto_temple(struct temple *temple);
282 uint8_t strat_grow_temple(struct temple *temple);
283 uint8_t strat_grow_temple_column(struct temple *temple);
284 struct temple *strat_get_best_temple(void);
285 struct temple *strat_get_our_temple_on_disc(uint8_t valid);
286 struct build_zone *strat_get_best_zone(void);
287 struct temple *strat_get_free_temple(void);
288
289 /* in strat_scan.c */
290 struct scan_disc_result;
291 void scanner_dump_state(void);
292 int8_t strat_scan_get_checkpoint(uint8_t mode, int16_t *ckpt_rel_x,
293                                  int16_t *ckpt_rel_y, int16_t *back_mm);
294 uint8_t strat_scan_disc(int16_t angle, uint8_t mode,
295                         struct scan_disc_result *result);
296 uint8_t strat_goto_disc_angle(int16_t a_deg, int8_t level);
297 int16_t strat_get_temple_angle(struct temple *temple);
298 int16_t strat_temple_angle_to_scan_angle(int16_t temple_angle);
299 uint8_t strat_build_on_opponent_temple(void);
300 uint8_t strat_check_temple_and_build(void);
301
302 #endif