beacon from 2009
[aversive.git] / projects / microb2010 / mainboard / strat_db.h
1 /*
2  *  Copyright Droids, Microb Technology (2010)
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 #define WAYPOINTS_NBX 13
24 #define WAYPOINTS_NBY 8
25
26 struct corn_db {
27         /* I2C_COB_UNKNOWN, I2C_COB_WHITE, I2C_COB_BLACK */
28         uint8_t color:2;
29         uint8_t reserved:6;
30
31         /* index in corn table */
32         uint8_t idx;
33 };
34
35 struct tomato_db {
36         uint8_t idx;
37 };
38
39 /* structure describing the status of a waypoint */
40 struct waypoint_db {
41         /* type of the waypoint */
42 #define WP_TYPE_WAYPOINT 0 /* no object on it */
43 #define WP_TYPE_OBSTACLE 1 /* cannot reach this point */
44 #define WP_TYPE_TOMATO   2 /* place for a tomato */
45 #define WP_TYPE_CORN     3 /* place for a corn */
46         uint8_t type:2;
47
48         /* true if point is near the border */
49         uint8_t dangerous:1;
50
51         /* true if element is present */
52         uint8_t present:1;
53
54         /* visited by opponent */
55         uint8_t opp_visited:1;
56
57         /* true if the wp is on a circuit */
58         uint8_t on_circuit:1;
59
60         uint8_t reserved:2;
61
62         union {
63                 struct corn_db corn;
64                 struct tomato_db tomato;
65         };
66
67         /* to monitor time when corn/ball was removed */
68         /* not optimal... but... we have enough ram */
69         int8_t time_removed;
70 };
71
72 /* database reflecting the status of objects on area */
73 struct strat_db {
74         uint8_t dump_enabled;
75
76         /* table of waypoints */
77         struct waypoint_db wp_table[WAYPOINTS_NBX][WAYPOINTS_NBY];
78
79         /* corn_table: pointers to waypoints */
80         struct waypoint_db *corn_table[CORN_NB];
81
82         /* tomato_table: pointers to waypoints */
83         struct waypoint_db *tomato_table[TOMATO_NB];
84
85         /* number of oranges remaining */
86         uint8_t our_oranges_count;
87         uint8_t opp_oranges_count;
88 };
89
90 /* global structure storing the database */
91 extern struct strat_db strat_db;
92
93 /* return the nearest waypoint */
94 int8_t xycoord_to_ijcoord(int16_t *xp, int16_t *yp, uint8_t *ip, uint8_t *jp);
95
96 /* return the nearest waypoint that is not a corn: xp and yp contains
97  * the input and output, and ip, jp are only outputs. return 0 on
98  * success. */
99 int8_t xycoord_to_ijcoord_not_corn(int16_t *xp, int16_t *yp, uint8_t *ip, uint8_t *jp);
100
101 /* convert i,j coords to x,y coords */
102 int8_t ijcoord_to_xycoord(uint8_t i, uint8_t j, int16_t *x, int16_t *y);
103
104 /* return the index of a corn given its i,j coords. */
105 int8_t ijcoord_to_corn_idx(uint8_t i, uint8_t j);
106
107 /* return the i,j coords of a corn given its index */
108 int8_t corn_idx_to_ijcoord(uint8_t idx, uint8_t *i, uint8_t *j);
109
110 /* return the index of a corn given its x,y coords. */
111 int8_t corn_idx_to_xycoord(uint8_t idx, int16_t *x, int16_t *y);
112
113 /* return the index of the closest corn at these coordinates. If the
114  * corn is really too far (~20cm), return NULL. The x and y pointer are
115  * updated with the real position of the corn */
116 struct waypoint_db *xycoord_to_corn_idx(int16_t *x, int16_t *y);
117
118 /* set color of a corn
119  * type is I2C_COB_BLACK, I2C_COB_WHITE, I2C_COB_UNKNOWN
120  * it will update the symetric corn if != UNKOWN
121  * it will also deduct color of some other cobs */
122 void corn_set_color(struct waypoint_db *wp, uint8_t color);
123
124 /* return the idx of the symetric corn */
125 int8_t corn_get_sym_idx(int8_t i);
126
127 /*********** TOMATO */
128
129 /* return the index of a tomato given its i,j coords. */
130 int8_t ijcoord_to_tomato_idx(uint8_t i, uint8_t j);
131
132 /* return the i,j coords of a tomato given its index */
133 int8_t tomato_idx_to_ijcoord(uint8_t idx, uint8_t *i, uint8_t *j);
134
135 /* return the index of a tomato given its x,y coords. */
136 int8_t tomato_idx_to_xycoord(uint8_t idx, int16_t *x, int16_t *y);
137
138 /* return the index of the closest tomato at these coordinates. If the
139  * tomato is really too far (~20cm), return NULL. The x and y pointer are
140  * updated with the real position of the tomato */
141 struct waypoint_db *xycoord_to_tomato_idx(int16_t *x, int16_t *y);
142
143 /*
144  * Init internal database. The initialization is done with UNKNOWN
145  * corn with all objects present
146  */
147 void strat_db_init(void);
148
149 /* dump infos about area and objects */
150 void strat_db_dump(const char *caller);