cup1
[aversive.git] / projects / microb2010 / cobboard / state.c
1 /*
2  *  Copyright Droids Corporation (2009)
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: state.c,v 1.5 2009-11-08 17:25:00 zer0 Exp $
19  *
20  */
21
22 #include <math.h>
23 #include <string.h>
24
25 #include <aversive.h>
26 #include <aversive/wait.h>
27 #include <aversive/error.h>
28
29 #include <ax12.h>
30 #include <uart.h>
31 #include <spi.h>
32 #include <encoders_spi.h>
33 #include <pwm_ng.h>
34 #include <timer.h>
35 #include <scheduler.h>
36 #include <clock_time.h>
37
38 #include <pid.h>
39 #include <quadramp.h>
40 #include <control_system_manager.h>
41 #include <blocking_detection_manager.h>
42
43 #include <rdline.h>
44 #include <vt100.h>
45
46 #include "../common/i2c_commands.h"
47 #include "main.h"
48 #include "cmdline.h"
49 #include "sensor.h"
50 #include "actuator.h"
51 #include "spickle.h"
52 #include "shovel.h"
53 #include "state.h"
54
55 #define STMCH_DEBUG(args...) DEBUG(E_USER_ST_MACH, args)
56 #define STMCH_NOTICE(args...) NOTICE(E_USER_ST_MACH, args)
57 #define STMCH_ERROR(args...) ERROR(E_USER_ST_MACH, args)
58
59 static struct vt100 local_vt100;
60 static volatile uint8_t state_mode, lspickle, rspickle;
61 static volatile uint8_t state_status;
62 static volatile uint8_t state_i2c_ignore = 0;
63 static uint8_t cob_count;
64
65 /* short aliases */
66 #define INIT(mode)       ((mode) == I2C_COBBOARD_MODE_INIT)
67 #define HARVEST(mode)    ((mode) == I2C_COBBOARD_MODE_HARVEST)
68 #define EJECT(mode)      ((mode) == I2C_COBBOARD_MODE_EJECT)
69 #define KICKSTAND_UP(mode)    ((mode) == I2C_COBBOARD_MODE_KICKSTAND_UP)
70 #define KICKSTAND_DOWN(mode)  ((mode) == I2C_COBBOARD_MODE_KICKSTAND_DOWN)
71
72 uint8_t state_debug = 0;
73
74 uint8_t state_get_cob_count(void)
75 {
76         return cob_count;
77 }
78
79 static void state_debug_wait_key_pressed(void)
80 {
81         if (!state_debug)
82                 return;
83         printf_P(PSTR("press a key\r\n"));
84         while(!cmdline_keypressed());
85 }
86
87 /* return true if the cob is correctly inside */
88 static uint8_t state_cob_inside(void)
89 {
90         return sensor_get(S_COB_INSIDE_L) &&
91                 sensor_get(S_COB_INSIDE_R);
92 }
93
94 /* return true if there is no cob correctly inside */
95 static uint8_t state_no_cob_inside(void)
96 {
97         return !sensor_get(S_COB_INSIDE_L) &&
98                 !sensor_get(S_COB_INSIDE_R);
99 }
100
101 static uint8_t state_spicklemode_deployed(uint8_t side)
102 {
103         if (side == I2C_LEFT_SIDE)
104                 return lspickle & I2C_COBBOARD_SPK_DEPLOY;
105         else
106                 return rspickle & I2C_COBBOARD_SPK_DEPLOY;
107 }
108
109 static uint8_t state_spicklemode_autoharvest(uint8_t side)
110 {
111         if (side == I2C_LEFT_SIDE)
112                 return lspickle & I2C_COBBOARD_SPK_AUTOHARVEST;
113         else
114                 return rspickle & I2C_COBBOARD_SPK_AUTOHARVEST;
115 }
116
117 static uint8_t state_spicklemode_nomove(uint8_t side)
118 {
119         if (side == I2C_LEFT_SIDE)
120                 return lspickle & I2C_COBBOARD_SPK_NO_MOVE;
121         else
122                 return rspickle & I2C_COBBOARD_SPK_NO_MOVE;
123 }
124
125 uint8_t state_spicklemode_weak(uint8_t side)
126 {
127         if (side == I2C_LEFT_SIDE)
128                 return lspickle & I2C_COBBOARD_SPK_WEAK;
129         else
130                 return rspickle & I2C_COBBOARD_SPK_WEAK;
131 }
132
133 /* pack/deploy spickles depending on mode */
134 static void spickle_prepare(uint8_t side)
135 {
136         /* pack spickle if we are not in harvest mode */
137         if (!HARVEST(state_mode))
138                 spickle_pack(side);
139
140         /* we do nothing in mode no out */
141         if (state_spicklemode_nomove(side))
142                 return;
143
144         if (state_spicklemode_deployed(side))
145                 spickle_deploy(side);
146         else
147                 spickle_pack(side);
148 }
149
150 void state_set_spickle(uint8_t side, uint8_t flags)
151 {
152         if (side == I2C_LEFT_SIDE) {
153                 /* the PACK command preempts the current action if the
154                  * arm is not busy */
155                 if (lspickle != 0 && flags == 0 &&
156                     state_status != I2C_COBBOARD_STATUS_LBUSY)
157                         spickle_prepare(I2C_LEFT_SIDE);
158                 lspickle = flags;
159         }
160         else {
161                 /* the PACK command preempts the current action if the
162                  * arm is not busy */
163                 if (rspickle != 0 && flags == 0 &&
164                     state_status != I2C_COBBOARD_STATUS_RBUSY)
165                         spickle_prepare(I2C_RIGHT_SIDE);
166                 rspickle = flags;
167         }
168 }
169
170 /* set a new state, return 0 on success */
171 int8_t state_set_mode(uint8_t mode)
172 {
173         state_mode = mode;
174         STMCH_DEBUG("%s(): mode=%x", __FUNCTION__, mode);
175         return 0;
176 }
177
178 void state_set_i2c_ignore(uint8_t val)
179 {
180         state_i2c_ignore = val;
181 }
182
183 uint8_t state_get_i2c_ignore(void)
184 {
185         return state_i2c_ignore;
186 }
187
188 /* check that state is the one in parameter and that state did not
189  * changed */
190 static uint8_t state_want_exit(void)
191 {
192         int16_t c;
193
194         /* force quit when CTRL-C is typed */
195         c = cmdline_getchar();
196         if (c == -1)
197                 return 0;
198         if (vt100_parser(&local_vt100, c) == KEY_CTRL_C)
199                 return 1;
200         printf_P(PSTR("CTRL-C\r\n"));
201         return 0;
202 }
203
204 uint8_t state_get_mode(void)
205 {
206         return state_mode;
207 }
208
209 uint8_t state_get_status(void)
210 {
211         return state_status;
212 }
213
214 /* harvest cobs from area */
215 static void state_do_harvest(uint8_t side)
216 {
217         /* if there is no cob, return */
218         if (cob_falling_edge(side) == 0)
219                 return;
220
221         if (side == I2C_LEFT_SIDE)
222                 state_status = I2C_COBBOARD_STATUS_LBUSY;
223         else
224                 state_status = I2C_COBBOARD_STATUS_RBUSY;
225
226         STMCH_DEBUG("start");
227
228         /* eat the cob */
229         spickle_pack(side);
230
231         time_wait_ms(200);
232         cobroller_on(side);
233
234         /* check that cob is correctly in place */
235         if (WAIT_COND_OR_TIMEOUT(state_cob_inside(), 750) == 0) {
236                 if (state_no_cob_inside()) {
237                         STMCH_DEBUG("no cob");
238                         return;
239                 }
240                 STMCH_DEBUG("bad cob state");
241
242                 /* while cob is not correctly placed try to extract
243                  * it as much as we can */
244                 while (state_cob_inside() == 0 &&
245                        state_no_cob_inside() == 0) {
246                         uint8_t cpt = 0;
247                         if (cpt == 0)
248                                 cobroller_reverse(side);
249                         else if (cpt == 1)
250                                 cobroller_off(side);
251                         else if (cpt == 2)
252                                 cobroller_on(side);
253                         cpt ++;
254                         cpt %= 3;
255                         shovel_mid();
256                         time_wait_ms(250);
257                         shovel_down();
258                         time_wait_ms(250);
259                 }
260
261                 STMCH_DEBUG("cob removed");
262                 if (state_no_cob_inside()) {
263                         STMCH_DEBUG("no cob");
264                         return;
265                 }
266         }
267
268         /* cob is inside, switch off roller */
269         cobroller_off(side);
270         cob_count ++;
271
272         state_debug_wait_key_pressed();
273
274         /* last cob, nothing to do */
275         if (cob_count == 5)
276                 return;
277
278         /* redeploy the spickle if there is a black cob */
279         if (!state_spicklemode_nomove(side))
280                 spickle_deploy(side);
281         state_debug_wait_key_pressed();
282
283         /* let the loaded cobs go */
284         servo_door_block();
285         servo_carry_open();
286         time_wait_ms(100);
287         state_debug_wait_key_pressed();
288
289         /* store it */
290         shovel_up();
291
292         while (WAIT_COND_OR_TIMEOUT(shovel_is_up(), 600) == 0) {
293                 STMCH_DEBUG("shovel blocked");
294                 shovel_down();
295                 time_wait_ms(250);
296                 shovel_up();
297                 /* if eject command is received, force exit */
298                 if (EJECT(state_mode))
299                         return;
300         }
301
302         state_debug_wait_key_pressed();
303
304         /* close the carry servos */
305         servo_carry_close();
306         servo_door_close();
307         time_wait_ms(200);
308         state_debug_wait_key_pressed();
309
310         shovel_down();
311
312         while (WAIT_COND_OR_TIMEOUT(shovel_is_down(), 400) == 0) {
313                 STMCH_DEBUG("shovel blocked");
314                 shovel_up();
315                 time_wait_ms(250);
316                 shovel_down();
317         }
318
319         STMCH_DEBUG("end");
320 }
321
322 /* eject cobs */
323 static void state_do_eject(void)
324 {
325         state_status = I2C_COBBOARD_STATUS_EJECT;
326         cob_count = 0;
327         shovel_mid();
328         servo_carry_open();
329         servo_door_open();
330         time_wait_ms(2000);
331         shovel_down();
332         servo_door_close();
333         servo_carry_close();
334 }
335
336 /* main state machine */
337 void state_machine(void)
338 {
339         while (state_want_exit() == 0) {
340
341                 state_status = I2C_COBBOARD_STATUS_READY;
342
343                 /* init */
344                 if (INIT(state_mode)) {
345                         state_init();
346                         state_mode = I2C_COBBOARD_MODE_HARVEST;
347                 }
348
349                 if (HARVEST(state_mode)) {
350                         /* init for each loop */
351                         shovel_down();
352                         servo_carry_close();
353                         servo_door_close();
354
355                         /* pack/deply spickles, enable/disable roller */
356                         cobroller_off(I2C_LEFT_SIDE);
357                         cobroller_off(I2C_RIGHT_SIDE);
358                         spickle_prepare(I2C_LEFT_SIDE);
359                         spickle_prepare(I2C_RIGHT_SIDE);
360
361                         /* harvest if not many cobs */
362                         if (cob_count < 5) {
363                                 if (state_spicklemode_deployed(I2C_LEFT_SIDE) &&
364                                     state_spicklemode_autoharvest(I2C_LEFT_SIDE))
365                                         state_do_harvest(I2C_LEFT_SIDE);
366                                 if (state_spicklemode_deployed(I2C_RIGHT_SIDE) &&
367                                     state_spicklemode_autoharvest(I2C_RIGHT_SIDE))
368                                         state_do_harvest(I2C_RIGHT_SIDE);
369                         }
370                 }
371
372                 /* help to climb the hill */
373                 if (KICKSTAND_UP(state_mode)) {
374                         state_status = I2C_COBBOARD_STATUS_KICKSTAND_UP;
375                         servo_carry_open();
376                         servo_door_open();
377                         shovel_kickstand_up();
378                 }
379
380                 if (KICKSTAND_DOWN(state_mode)) {
381                         state_status = I2C_COBBOARD_STATUS_KICKSTAND_DOWN;
382                         servo_carry_open();
383                         servo_door_open();
384                         shovel_kickstand_down();
385                 }
386
387                 /* eject */
388                 if (EJECT(state_mode)) {
389                         state_mode = I2C_COBBOARD_MODE_HARVEST;
390                         state_do_eject();
391                 }
392         }
393         state_status = I2C_COBBOARD_STATUS_OFF;
394 }
395
396 void state_init(void)
397 {
398         vt100_init(&local_vt100);
399         shovel_down();
400         servo_door_close();
401         servo_carry_close();
402         spickle_pack(I2C_LEFT_SIDE);
403         spickle_pack(I2C_RIGHT_SIDE);
404         state_mode = 0;
405         cob_count = 0;
406         state_status = I2C_COBBOARD_STATUS_OFF;
407 }