2 * Copyright Droids, Microb Technology (2010)
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.
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.
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
18 * Revision : $Id: strat.c,v 1.6 2009-11-08 17:24:33 zer0 Exp $
20 * Olivier MATZ <zer0@droids-corp.org>
31 #include <aversive/pgmspace.h>
36 #include <clock_time.h>
41 #include <control_system_manager.h>
42 #include <trajectory_manager.h>
43 #include <trajectory_manager_utils.h>
44 #include <trajectory_manager_core.h>
45 #include <vect_base.h>
48 #include <obstacle_avoidance.h>
49 #include <blocking_detection_manager.h>
50 #include <robot_system.h>
51 #include <position_manager.h>
53 #include <diagnostic.h>
58 #include "../common/i2c_commands.h"
59 #include "i2c_protocol.h"
62 #include "strat_base.h"
63 #include "strat_corn.h"
65 #include "strat_utils.h"
69 /* status of objects on area */
70 struct strat_db strat_db;
72 /* given an index, give the i coord */
73 static const uint8_t corn_coord_i[CORN_NB] = {
74 0, 0, 0, 2, 2, 2, 4, 4, 6,
75 6, 8, 8, 10, 10, 10, 12, 12, 12,
78 /* given an index, give the j coord */
79 static const uint8_t corn_coord_j[CORN_NB] = {
80 2, 4, 6, 3, 5, 7, 4, 6, 5,
81 7, 4, 6, 3, 5, 7, 2, 4, 6,
84 /* table to find the symetric idx */
85 static const uint8_t corn_sym[] = {
86 15, 16, 17, 12, 13, 14, 10, 11,
87 8, 9, 6, 7, 3, 4, 5, 0, 1, 2
90 #if 0 /* XXX maybe useless */
91 /* the 10 possible configurations for corn on the side */
92 static const uint8_t corn_side_confs[9][2] = {
104 /* the 4 possible configurations for corn on center */
105 static const uint8_t corn_center_confs[4][2] = {
113 /* in these groups, only one black cob */
114 static const int8_t corn_group1[] = { 0, 1, 2, -1, };
115 static const int8_t corn_group2[] = { 3, 4, 6, -1, };
116 static const int8_t corn_group3[] = { 5, 7, -1, };
117 static const int8_t corn_group4[] = { 8, 9, -1, };
118 static const int8_t corn_group5[] = { 11, 14, -1, };
119 static const int8_t corn_group6[] = { 10, 12, 13, -1, };
120 static const int8_t corn_group7[] = { 15, 16, 17, -1, };
122 static const int8_t *corn_groups[] = {
133 /* given an index, give the i coord */
134 static const uint8_t tomato_coord_i[TOMATO_NB] = {
135 0, 0, 2, 2, 4, 4, 6, 6,
136 8, 8, 10, 10, 12, 12,
139 /* given an index, give the j coord */
140 static const uint8_t tomato_coord_j[TOMATO_NB] = {
141 3, 5, 4, 6, 5, 7, 4, 6, 5, 7, 4, 6, 3, 5,
144 /******** Generic waypoint */
146 /* return the xy coords of a waypoint given its ij coords. */
147 int8_t ijcoord_to_xycoord(uint8_t i, uint8_t j, int16_t *x, int16_t *y)
149 if (i >= WAYPOINTS_NBX && j >= WAYPOINTS_NBY)
151 *x = (OFFSET_CORN_X + i*STEP_CORN_X);
152 *y = (OFFSET_CORN_Y + j*STEP_CORN_Y);
158 /* return the index of a corn given its i,j coords. */
159 int8_t ijcoord_to_corn_idx(uint8_t i, uint8_t j)
162 for (n = 0; n < CORN_NB; n ++) {
163 if (i == corn_coord_i[n] &&
164 j == corn_coord_j[n])
170 /* return the i,j coords of a corn given its index */
171 int8_t corn_idx_to_ijcoord(uint8_t idx, uint8_t *i, uint8_t *j)
175 *i = corn_coord_i[idx];
176 *j = corn_coord_j[idx];
180 /* return the index of a corn given its x,y coords. */
181 int8_t corn_idx_to_xycoord(uint8_t idx, int16_t *x, int16_t *y)
184 if (corn_idx_to_ijcoord(idx, &i, &j) < 0)
186 if (ijcoord_to_xycoord(i, j, x, y) < 0)
191 /* return the index of the closest corn at these coordinates. If the
192 * corn is really too far (~20cm), return NULL. The x and y pointer are
193 * updated with the real position of the corn */
194 struct waypoint_db *xycoord_to_corn_idx(int16_t *x, int16_t *y)
197 int16_t d, x_corn, y_corn;
198 int16_t x_corn_min = 0, y_corn_min = 0;
201 /* XXX does it work when we are blue ? */
202 for (n = 0; n < CORN_NB; n ++) {
203 corn_idx_to_xycoord(n, &x_corn, &y_corn);
204 d = xy_norm(x_corn, y_corn, *x, *y);
205 /* XXX 200 -> constant */
206 if (d < 200 && (d_min == 0 || d < d_min)) {
219 return strat_db.corn_table[idx];
222 /* return true if 'idx' is in group */
223 static uint8_t is_in_group(const int8_t *group, uint8_t idx)
226 for (pidx = group; *pidx != -1; pidx++) {
234 /* return the number of cob of that color in the group */
235 static uint8_t count_in_group(const int8_t *group, uint8_t color)
238 struct waypoint_db *wp;
241 for (pidx = &group[0]; *pidx != -1; pidx++) {
242 wp = strat_db.corn_table[*pidx];
243 if (wp->corn.color == color)
249 /* set all unkown cobs to specified color */
250 static void set_unknown_in_group(const int8_t *group, uint8_t color)
253 struct waypoint_db *wp;
255 for (pidx = &group[0]; *pidx != -1; pidx++) {
256 wp = strat_db.corn_table[*pidx];
257 if (wp->corn.color == I2C_COB_UNKNOWN)
258 wp->corn.color = color;
262 /* depending on which cob is set (and its color), set the color of
264 static void corn_deduct_other(uint8_t idx, uint8_t color)
266 const int8_t **pgroup;
268 for (pgroup = &corn_groups[0]; *pgroup; pgroup++) {
269 if (!is_in_group(*pgroup, idx))
271 if (color == I2C_COB_BLACK) {
272 set_unknown_in_group(*pgroup, I2C_COB_WHITE);
274 else if (color == I2C_COB_WHITE) {
275 if (count_in_group(*pgroup, I2C_COB_UNKNOWN) == 1)
276 set_unknown_in_group(*pgroup, I2C_COB_BLACK);
281 /* set color of a corn
282 * type is I2C_COB_BLACK, I2C_COB_WHITE, I2C_COB_UNKNOWN
283 * it will update the symetric corn if != UNKOWN
284 * it will also deduct color of some other cobs */
285 void corn_set_color(struct waypoint_db *wp, uint8_t color)
289 wp->corn.color = color;
290 if (color == I2C_COB_UNKNOWN)
292 corn_deduct_other(wp->corn.idx, color);
293 symidx = corn_get_sym_idx(wp->corn.idx);
294 strat_db.corn_table[symidx]->corn.color = color;
295 corn_deduct_other(symidx, color);
299 /* return the idx of the symetric corn */
300 int8_t corn_get_sym_idx(int8_t i)
307 /*********** TOMATO */
309 /* return the index of a tomato given its i,j coords. */
310 int8_t ijcoord_to_tomato_idx(uint8_t i, uint8_t j)
313 for (n = 0; n < TOMATO_NB; n ++) {
314 if (i == tomato_coord_i[n] &&
315 j == tomato_coord_j[n])
321 /* return the i,j coords of a tomato given its index */
322 int8_t tomato_idx_to_ijcoord(uint8_t idx, uint8_t *i, uint8_t *j)
324 if (idx >= TOMATO_NB)
326 *i = tomato_coord_i[idx];
327 *j = tomato_coord_j[idx];
331 /* return the index of a tomato given its x,y coords. */
332 int8_t tomato_idx_to_xycoord(uint8_t idx, int16_t *x, int16_t *y)
335 if (tomato_idx_to_ijcoord(idx, &i, &j) < 0)
337 if (ijcoord_to_xycoord(i, j, x, y) < 0)
342 /* return the index of the closest tomato at these coordinates. If the
343 * tomato is really too far (~20cm), return NULL. The x and y pointer are
344 * updated with the real position of the tomato */
345 struct waypoint_db *xycoord_to_tomato_idx(int16_t *x, int16_t *y)
348 int16_t d, x_tomato, y_tomato;
349 int16_t x_tomato_min = 0, y_tomato_min = 0;
352 /* XXX does it work when we are blue ? */
353 for (n = 0; n < TOMATO_NB; n ++) {
354 tomato_idx_to_xycoord(n, &x_tomato, &y_tomato);
355 d = xy_norm(x_tomato, y_tomato, *x, *y);
356 /* XXX 200 -> constant */
357 if (d < 200 && (d_min == 0 || d < d_min)) {
360 x_tomato_min = x_tomato;
361 y_tomato_min = y_tomato;
370 return strat_db.tomato_table[idx];
374 * Init internal database. The initialization is done with UNKNOWN
375 * corn with all objects present
377 void strat_db_init(void)
379 struct waypoint_db *wp;
383 memset(&strat_db, 0, sizeof(strat_db));
386 for (i=0; i<CORN_NB; i++) {
387 strat_db.corn_table[i] =
388 &strat_db.wp_table[corn_coord_i[i]][corn_coord_j[i]];
391 for (i=0; i<TOMATO_NB; i++) {
392 strat_db.tomato_table[i] =
393 &strat_db.wp_table[tomato_coord_i[i]][tomato_coord_j[i]];
396 strat_db.our_oranges_count = 6;
397 strat_db.opp_oranges_count = 6;
399 for (i=0; i<WAYPOINTS_NBX; i++) {
401 for (j=0; j<WAYPOINTS_NBY; j++) {
402 wp = &strat_db.wp_table[i][j];
405 wp->type = WP_TYPE_WAYPOINT;
407 /* mark dangerous points */
408 if (i == 0 || i == (WAYPOINTS_NBX-1))
410 if ((i&1) == 0 && j == (WAYPOINTS_NBY-1))
413 /* too close of border, unreachable wp */
414 if ((i & 1) == 1 && j == (WAYPOINTS_NBY-1)) {
415 wp->type = WP_TYPE_OBSTACLE;
420 if (i >= 2 && i < (WAYPOINTS_NBX-2) && j < 2) {
421 wp->type = WP_TYPE_OBSTACLE;
426 idx = ijcoord_to_corn_idx(i, j);
428 wp->type = WP_TYPE_CORN;
431 wp->corn.color = I2C_COB_UNKNOWN;
436 idx = ijcoord_to_tomato_idx(i, j);
438 wp->type = WP_TYPE_TOMATO;
446 /* dump infos about area and objects */
447 void strat_db_dump(const char *caller)
450 struct waypoint_db *wp;
452 if (strat_db.dump_enabled == 0)
455 printf_P(PSTR("DB dump from <%s>\r\n"), caller);
456 for (i=0; i<CORN_NB; i++) {
457 wp = strat_db.corn_table[i];
458 printf_P(PSTR("corn%d: present=%d opp=%d "),
459 i, wp->present, wp->opp_visited);
460 if (wp->corn.color == I2C_COB_UNKNOWN)
461 printf_P(PSTR("unknown"));
462 else if (wp->corn.color == I2C_COB_BLACK)
463 printf_P(PSTR("black"));
464 else if (wp->corn.color == I2C_COB_WHITE)
465 printf_P(PSTR("white"));
466 printf_P(PSTR("\r\n"));
469 for (i=0; i<TOMATO_NB; i++) {
470 wp = strat_db.tomato_table[i];
471 printf_P(PSTR("tomato%d: present=%d opp=%d\r\n"),
472 i, wp->present, wp->opp_visited);