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>
30 #include <aversive/pgmspace.h>
35 #include <clock_time.h>
40 #include <control_system_manager.h>
41 #include <trajectory_manager.h>
42 #include <trajectory_manager_utils.h>
43 #include <trajectory_manager_core.h>
44 #include <vect_base.h>
47 #include <obstacle_avoidance.h>
48 #include <blocking_detection_manager.h>
49 #include <robot_system.h>
50 #include <position_manager.h>
52 #include <diagnostic.h>
57 #include "../common/i2c_commands.h"
58 #include "i2c_protocol.h"
62 #include "strat_base.h"
63 #include "strat_corn.h"
64 #include "strat_utils.h"
71 change x,y -> i,j to avoid confusion with coords
72 could be optimized in mem space: it is not needed to store the x,y coord,
73 we can process it from idx. however it will be less optimized for speed
77 /* XXX enum possible ? else just rename */
89 struct djpoint *parent;
97 /* database for dijkstra */
98 static struct djpoint djpoints[WAYPOINTS_NBX][WAYPOINTS_NBY];
100 /* return index from neigh pointer */
101 #define PT2IDX(neigh) ( ((void *)(neigh)-(void *)(&djpoints)) / sizeof(*neigh) )
107 struct waypoint_db *wp;
110 for (i=0; i<WAYPOINTS_NBX; i++) {
111 printf_P(PSTR(" %2d "), i);
113 printf_P(PSTR("\r\n"));
115 for (j=WAYPOINTS_NBY*2-1; j>=0; j--) {
116 printf_P(PSTR("%3d "), j/2);
121 for (i=0; i<WAYPOINTS_NBX; i++) {
122 pt = &djpoints[i][j/2];
123 wp = &strat_db.wp_table[i][j/2];
125 if (((i+j) & 1) == 0)
128 if (wp->type == WP_TYPE_OBSTACLE)
129 printf_P(PSTR(" X "));
130 else if (wp->dangerous)
131 printf_P(PSTR(" D "));
132 else if (wp->type == WP_TYPE_CORN &&
133 wp->corn.color == I2C_COB_WHITE)
134 printf_P(PSTR(" W "));
135 else if (wp->type == WP_TYPE_CORN &&
136 wp->corn.color == I2C_COB_BLACK)
137 printf_P(PSTR(" B "));
138 else if (wp->type == WP_TYPE_CORN &&
139 wp->corn.color == I2C_COB_UNKNOWN)
140 printf_P(PSTR(" U "));
141 else if (wp->type == WP_TYPE_WAYPOINT ||
142 wp->type == WP_TYPE_TOMATO)
143 printf_P(PSTR(" %5d "), pt->weight);
145 printf_P(PSTR(" ? "));
147 printf_P(PSTR("\r\n"));
151 static inline uint8_t opposite_position(uint8_t pos)
159 /* is point reachable by the robot ? */
160 static uint8_t is_reachable(uint8_t i, uint8_t j)
162 struct waypoint_db *wp;
164 wp = &strat_db.wp_table[i][j];
165 if (wp->type == WP_TYPE_WAYPOINT)
167 if (wp->type == WP_TYPE_TOMATO)
169 if (wp->type == WP_TYPE_CORN &&
175 /* return coord of the entry in the table from the pointer */
176 static void djpoint2ij(struct djpoint *pt, uint8_t *i, uint8_t *j)
178 int8_t idx = PT2IDX(pt);
179 *i = idx / WAYPOINTS_NBY;
180 *j = idx % WAYPOINTS_NBY;
183 /* get the neighbour of the point at specified position */
184 static struct djpoint *get_neigh(struct djpoint *pt,
188 struct djpoint *neigh;
190 djpoint2ij(pt, &i, &j);
218 if (i >= WAYPOINTS_NBX || j >= WAYPOINTS_NBY)
221 if (is_reachable(i, j) == 0)
224 neigh = &djpoints[i][j];
228 static struct djpoint *get_next_neigh(struct djpoint *pt, uint8_t *position)
230 struct djpoint *neigh = NULL;
231 uint8_t pos = *position + 1;
233 for (pos = *position + 1; pos < END; pos++) {
234 neigh = get_neigh(pt, pos);
243 /* browse all points */
244 #define POINT_FOREACH(cur) \
245 for (cur = &djpoints[0][0]; \
246 cur < &djpoints[WAYPOINTS_NBX][WAYPOINTS_NBY]; \
250 #define NEIGH_FOREACH(neigh, pos, point) \
251 for (pos = START, neigh = get_next_neigh(point, &pos); \
253 neigh = get_next_neigh(point, &pos))
255 int dijkstra_init(void)
260 /* return distance between p1 and p2 */
261 static uint16_t dist(struct djpoint *p1, struct djpoint *p2)
263 int16_t x1, y1, x2, y2;
267 djpoint2ij(p1, &i, &j);
268 ijcoord_to_xycoord(i, j, &x1, &y1);
270 djpoint2ij(p2, &i, &j);
271 ijcoord_to_xycoord(i, j, &x2, &y2);
275 return sqrt(vx * vx + vy * vy);
278 void dijkstra_process_neighs(struct djpoint *pt)
280 struct djpoint *neigh;
281 uint8_t pos, parent_pos;
285 djpoint2ij(pt, &i, &j);
286 printf_P(PSTR("at %d %d:\r\n"), i, j);
288 NEIGH_FOREACH(neigh, pos, pt) {
289 weight = pt->weight + dist(pt, neigh);
290 parent_pos = opposite_position(pos);
292 /* bonus if we keep the same direction */
293 if (parent_pos == pt->parent_pos ||
294 pt->parent_pos == END)
297 printf_P(PSTR(" pos=%d: ppos=%d opos=%d nw=%d w=%d\r\n"), pos,
298 pt->parent_pos, parent_pos,
299 neigh->weight, weight);
300 if (neigh->weight == 0 || weight < neigh->weight) {
301 djpoint2ij(neigh, &i, &j);
302 //printf_P(PSTR(" %d,%d updated\r\n"), i, j);
303 neigh->weight = weight;
304 neigh->parent_pos = parent_pos;
310 int dijkstra(struct djpoint *start)
313 uint8_t todolist = 1;
318 printf_P(PSTR("\r\n"));
320 /* process all neighbours of todo list */
324 dijkstra_process_neighs(cur);
328 /* convert updated list in todo list */
341 /* init waypoints position */
342 void init_djpoints(void)
347 for (i=0; i<WAYPOINTS_NBX; i++) {
349 for (j=0; j<WAYPOINTS_NBY; j++) {
350 pt = &djpoints[i][j];
351 pt->parent_pos = END;
359 int get_path(struct djpoint *start, struct djpoint *end)
362 uint8_t prev_direction = 0;
367 cur != NULL && cur->parent_pos != END && cur != end;
368 cur = get_neigh(cur, cur->parent_pos)) {
369 if (prev_direction != cur->parent_pos) {
371 corn_idx_to_xycoord(idx, &x, &y);
372 printf_P(PSTR("%d %d (%d)\r\n"),
373 x, y, cur->parent_pos);
375 prev_direction = cur->parent_pos;
378 corn_idx_to_xycoord(idx, &x, &y);
379 printf_P(PSTR("%d %d\r\n"), x, y);