ini
[aversive.git] / projects / microb2009 / common / i2c_commands.h
1 /*
2  *  Copyright Droids Corporation (2007)
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: i2c_commands.h,v 1.9 2009-05-27 20:04:06 zer0 Exp $
19  *
20  */
21
22 #ifndef _I2C_COMMANDS_H_
23 #define _I2C_COMMANDS_H_
24
25 #define I2C_MAINBOARD_ADDR   1
26 #define I2C_MECHBOARD_ADDR   2
27 #define I2C_SENSORBOARD_ADDR 3
28
29 #define I2C_LEFT_SIDE   0
30 #define I2C_RIGHT_SIDE  1
31 #define I2C_AUTO_SIDE   2 /* for prepare_pickup */
32 #define I2C_CENTER_SIDE 3 /* for prepare_pickup */
33
34 #define I2C_COLOR_RED   0
35 #define I2C_COLOR_GREEN 1
36
37 #define I2C_AUTOBUILD_DEFAULT_DIST 210
38
39 struct i2c_cmd_hdr {
40         uint8_t cmd;
41 };
42
43 /****/
44 /* commands that do not need and answer */
45 /****/
46
47 #define I2C_CMD_GENERIC_LED_CONTROL 0x00
48
49 struct i2c_cmd_led_control {
50         struct i2c_cmd_hdr hdr;
51         uint8_t led_num:7;
52         uint8_t state:1;        
53 };
54
55 /****/
56
57 #define I2C_CMD_GENERIC_SET_COLOR 0x01
58
59 struct i2c_cmd_generic_color {
60         struct i2c_cmd_hdr hdr;
61         uint8_t color;
62 };
63
64 /****/
65
66 #define I2C_CMD_MECHBOARD_SET_MODE 0x02
67
68 struct i2c_cmd_mechboard_set_mode {
69         struct i2c_cmd_hdr hdr;
70 #define I2C_MECHBOARD_MODE_MANUAL             0x00
71 #define I2C_MECHBOARD_MODE_HARVEST            0x01
72 #define I2C_MECHBOARD_MODE_PREPARE_PICKUP     0x02
73 #define I2C_MECHBOARD_MODE_PICKUP             0x03
74 #define I2C_MECHBOARD_MODE_PREPARE_BUILD      0x04
75 #define I2C_MECHBOARD_MODE_AUTOBUILD          0x05
76 #define I2C_MECHBOARD_MODE_WAIT               0x06
77 #define I2C_MECHBOARD_MODE_INIT               0x07
78 #define I2C_MECHBOARD_MODE_PREPARE_GET_LINTEL 0x08
79 #define I2C_MECHBOARD_MODE_GET_LINTEL         0x09
80 #define I2C_MECHBOARD_MODE_PUT_LINTEL         0x0A
81 #define I2C_MECHBOARD_MODE_PREPARE_EJECT      0x0B
82 #define I2C_MECHBOARD_MODE_EJECT              0x0C
83 #define I2C_MECHBOARD_MODE_CLEAR              0x0D
84 #define I2C_MECHBOARD_MODE_LAZY_HARVEST       0x0E
85 #define I2C_MECHBOARD_MODE_LOADED             0x0F
86 #define I2C_MECHBOARD_MODE_PREPARE_INSIDE     0x10
87 #define I2C_MECHBOARD_MODE_STORE              0x11
88 #define I2C_MECHBOARD_MODE_LAZY_PICKUP        0x12
89 #define I2C_MECHBOARD_MODE_MANIVELLE          0x13
90 #define I2C_MECHBOARD_MODE_PUSH_TEMPLE        0x14
91 #define I2C_MECHBOARD_MODE_PUSH_TEMPLE_DISC   0x15
92 #define I2C_MECHBOARD_MODE_EXIT               0xFF
93         uint8_t mode;
94         union {
95                 struct {
96
97                 } manual;
98
99                 struct {
100                         uint8_t side;
101                         uint8_t next_mode;
102                 } prep_pickup;
103
104                 struct {
105                         uint8_t level_l;
106                         uint8_t level_r;
107                 } prep_build;
108
109                 struct {
110                         uint8_t side;
111                 } push_temple_disc;
112
113                 struct {
114                         uint8_t level_left;
115                         uint8_t level_right;
116                         uint8_t count_left;
117                         uint8_t count_right;
118                         uint8_t distance_left;
119                         uint8_t distance_right;
120                         uint8_t do_lintel;
121                 } autobuild;
122
123                 struct {
124                         uint8_t level_l;
125                         uint8_t level_r;
126                 } prep_inside;
127         };
128 };
129
130 /****/
131
132 /* only valid in manual mode */
133 #define I2C_CMD_MECHBOARD_ARM_GOTO 0x03
134
135 struct i2c_cmd_mechboard_arm_goto {
136         struct i2c_cmd_hdr hdr;
137 #define I2C_MECHBOARD_ARM_LEFT     0
138 #define I2C_MECHBOARD_ARM_RIGHT    1
139 #define I2C_MECHBOARD_ARM_BOTH     2
140         uint8_t which;
141
142         uint8_t height; /* in cm */
143         uint8_t distance; /* in cm */
144 };
145
146 /****/
147
148 #define I2C_CMD_SENSORBOARD_SET_BEACON 0x04
149
150 struct i2c_cmd_sensorboard_start_beacon {
151         struct i2c_cmd_hdr hdr;
152         uint8_t enable;
153 };
154
155
156 /****/
157
158 #define I2C_CMD_SENSORBOARD_SET_SCANNER 0x05
159
160 struct i2c_cmd_sensorboard_scanner {
161         struct i2c_cmd_hdr hdr;
162
163 #define I2C_SENSORBOARD_SCANNER_STOP    0x00
164 #define I2C_SENSORBOARD_SCANNER_PREPARE 0x01
165 #define I2C_SENSORBOARD_SCANNER_START   0x02
166         uint8_t mode;
167 };
168
169 /*****/
170
171 #define I2C_CMD_SENSORBOARD_CALIB_SCANNER 0x06
172 struct i2c_cmd_sensorboard_calib_scanner {
173         struct i2c_cmd_hdr hdr;
174 };
175
176 /*****/
177
178 #define I2C_CMD_SENSORBOARD_SCANNER_ALGO 0x07
179 struct i2c_cmd_sensorboard_scanner_algo {
180         struct i2c_cmd_hdr hdr;
181
182 #define I2C_SCANNER_ALGO_COLUMN_DROPZONE 1
183 #define I2C_SCANNER_ALGO_CHECK_TEMPLE    2
184 #define I2C_SCANNER_ALGO_TEMPLE_DROPZONE 3
185         uint8_t algo;
186
187         union {
188                 struct {
189 #define I2C_SCANNER_ZONE_0     0
190 #define I2C_SCANNER_ZONE_1     1
191 #define I2C_SCANNER_ZONE_DISC  2
192                         uint8_t working_zone;
193                         int16_t center_x;
194                         int16_t center_y;
195                 } drop_zone;
196                 
197                 struct {
198                         uint8_t level;
199                         int16_t temple_x;
200                         int16_t temple_y;
201                 } check_temple;
202         };
203 };
204
205 /****/
206 /* requests and their answers */
207 /****/
208
209
210 #define I2C_REQ_MECHBOARD_STATUS 0x80
211
212 struct i2c_req_mechboard_status {
213         struct i2c_cmd_hdr hdr;
214
215         int16_t pump_left1_current;
216         int16_t pump_left2_current;
217 };
218
219 #define I2C_ANS_MECHBOARD_STATUS 0x81
220
221 struct i2c_ans_mechboard_status {
222         struct i2c_cmd_hdr hdr;
223         /* mode type are defined above: I2C_MECHBOARD_MODE_xxx */
224         uint8_t mode;
225
226 #define I2C_MECHBOARD_STATUS_F_READY         0x00
227 #define I2C_MECHBOARD_STATUS_F_BUSY          0x01
228 #define I2C_MECHBOARD_STATUS_F_EXCPT         0x02
229         uint8_t status;
230
231         uint8_t lintel_count;
232
233         /* flag is there if column was taken by this pump. Note that
234          * we should also check ADC (current) to see if the column is
235          * still there. */
236 #define I2C_MECHBOARD_COLUMN_L1              0x01
237 #define I2C_MECHBOARD_COLUMN_L2              0x02
238 #define I2C_MECHBOARD_COLUMN_R1              0x04
239 #define I2C_MECHBOARD_COLUMN_R2              0x08
240         uint8_t column_flags;
241         
242         int16_t pump_left1;
243         int16_t pump_right1;
244         int16_t pump_left2;
245         int16_t pump_right2;
246
247 #define I2C_MECHBOARD_CURRENT_COLUMN 85
248         int16_t pump_right1_current;
249         int16_t pump_right2_current;
250
251         uint16_t servo_lintel_left;
252         uint16_t servo_lintel_right;
253 };
254
255 #define I2C_REQ_SENSORBOARD_STATUS 0x82
256
257 struct i2c_req_sensorboard_status {
258         struct i2c_cmd_hdr hdr;
259
260         /* position sent by mainboard */
261         int16_t x;
262         int16_t y;
263         int16_t a;
264
265         /* PWM for pickup */
266         uint8_t enable_pickup_wheels;
267 };
268
269 #define I2C_ANS_SENSORBOARD_STATUS 0x83
270
271 struct i2c_ans_sensorboard_status {
272         struct i2c_cmd_hdr hdr;
273
274         uint8_t status;
275 #define I2C_OPPONENT_NOT_THERE -1000
276         int16_t opponent_x;
277         int16_t opponent_y;
278         int16_t opponent_a;
279         int16_t opponent_d;
280
281 #define I2C_SCAN_DONE 1
282 #define I2C_SCAN_MAX_COLUMN 2
283         uint8_t scan_status;
284
285 #define I2C_COLUMN_NO_DROPZONE -1
286         int8_t dropzone_h;
287         int16_t dropzone_x;
288         int16_t dropzone_y;
289 };
290
291 #endif /* _I2C_PROTOCOL_H_ */