vt100: include pgmspace.h as we use PROGMEM macro
[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 static uint8_t state_cob_partial = 0;
74
75 uint8_t state_get_cob_count(void)
76 {
77         return cob_count;
78 }
79
80 static void state_debug_wait_key_pressed(void)
81 {
82         if (!state_debug)
83                 return;
84         printf_P(PSTR("press a key\r\n"));
85         while(!cmdline_keypressed());
86 }
87
88 /* return true if the cob is correctly inside */
89 static uint8_t state_cob_inside(void)
90 {
91         return sensor_get(S_COB_INSIDE_L) &&
92                 sensor_get(S_COB_INSIDE_R);
93 }
94
95 /* return true if there is no cob correctly inside */
96 static uint8_t state_no_cob_inside(void)
97 {
98         return !sensor_get(S_COB_INSIDE_L) &&
99                 !sensor_get(S_COB_INSIDE_R);
100 }
101
102 static uint8_t state_cob_inside_enhanced(void)
103 {
104         if (sensor_get(S_COB_INSIDE_L))
105                 state_cob_partial = 1;
106         if (sensor_get(S_COB_INSIDE_R))
107                 state_cob_partial = 1;
108         return state_cob_inside();
109 }
110
111 static uint8_t state_spicklemode_deployed(uint8_t side)
112 {
113         if (side == I2C_LEFT_SIDE)
114                 return lspickle & I2C_COBBOARD_SPK_DEPLOY;
115         else
116                 return rspickle & I2C_COBBOARD_SPK_DEPLOY;
117 }
118
119 static uint8_t state_spicklemode_autoharvest(uint8_t side)
120 {
121         if (side == I2C_LEFT_SIDE)
122                 return lspickle & I2C_COBBOARD_SPK_AUTOHARVEST;
123         else
124                 return rspickle & I2C_COBBOARD_SPK_AUTOHARVEST;
125 }
126
127 static uint8_t state_spicklemode_nomove(uint8_t side)
128 {
129         if (side == I2C_LEFT_SIDE)
130                 return lspickle & I2C_COBBOARD_SPK_NO_MOVE;
131         else
132                 return rspickle & I2C_COBBOARD_SPK_NO_MOVE;
133 }
134
135 uint8_t state_spicklemode_weak(uint8_t side)
136 {
137         if (side == I2C_LEFT_SIDE)
138                 return lspickle & I2C_COBBOARD_SPK_WEAK;
139         else
140                 return rspickle & I2C_COBBOARD_SPK_WEAK;
141 }
142
143 /* pack/deploy spickles depending on mode */
144 static void spickle_prepare(uint8_t side)
145 {
146         /* pack spickle if we are not in harvest mode */
147         if (!HARVEST(state_mode))
148                 spickle_pack(side);
149
150         /* we do nothing in mode no out */
151         if (state_spicklemode_nomove(side))
152                 return;
153
154         if (state_spicklemode_deployed(side))
155                 spickle_deploy(side);
156         else
157                 spickle_pack(side);
158 }
159
160 void state_set_spickle(uint8_t side, uint8_t flags)
161 {
162         if (side == I2C_LEFT_SIDE) {
163                 /* the PACK command preempts the current action if the
164                  * arm is not busy */
165                 if (lspickle != 0 && flags == 0 &&
166                     state_status != I2C_COBBOARD_STATUS_LBUSY)
167                         spickle_prepare(I2C_LEFT_SIDE);
168                 lspickle = flags;
169         }
170         else {
171                 /* the PACK command preempts the current action if the
172                  * arm is not busy */
173                 if (rspickle != 0 && flags == 0 &&
174                     state_status != I2C_COBBOARD_STATUS_RBUSY)
175                         spickle_prepare(I2C_RIGHT_SIDE);
176                 rspickle = flags;
177         }
178 }
179
180 /* set a new state, return 0 on success */
181 int8_t state_set_mode(uint8_t mode)
182 {
183         state_mode = mode;
184         STMCH_DEBUG("%s(): mode=%x", __FUNCTION__, mode);
185         return 0;
186 }
187
188 void state_set_i2c_ignore(uint8_t val)
189 {
190         state_i2c_ignore = val;
191 }
192
193 uint8_t state_get_i2c_ignore(void)
194 {
195         return state_i2c_ignore;
196 }
197
198 /* check that state is the one in parameter and that state did not
199  * changed */
200 static uint8_t state_want_exit(void)
201 {
202         int16_t c;
203
204         /* force quit when CTRL-C is typed */
205         c = cmdline_getchar();
206         if (c == -1)
207                 return 0;
208         if (vt100_parser(&local_vt100, c) == KEY_CTRL_C)
209                 return 1;
210         printf_P(PSTR("CTRL-C\r\n"));
211         return 0;
212 }
213
214 uint8_t state_get_mode(void)
215 {
216         return state_mode;
217 }
218
219 uint8_t state_get_status(void)
220 {
221         return state_status;
222 }
223
224 /* harvest cobs from area */
225 static void state_do_harvest(uint8_t side)
226 {
227         uint8_t i = 0;
228
229         /* if there is no cob, return */
230         if (cob_falling_edge(side) == 0)
231                 return;
232
233         if (side == I2C_LEFT_SIDE)
234                 state_status = I2C_COBBOARD_STATUS_LBUSY;
235         else
236                 state_status = I2C_COBBOARD_STATUS_RBUSY;
237
238         STMCH_DEBUG("start");
239
240         /* eat the cob */
241         spickle_pack(side);
242
243         time_wait_ms(200);
244         cobroller_on(side);
245
246         /* check that cob is correctly in place */
247         state_cob_partial = 0;
248         if (WAIT_COND_OR_TIMEOUT(state_cob_inside_enhanced(), 750) == 0) {
249                 if (state_no_cob_inside() && state_cob_partial == 0) {
250                         STMCH_DEBUG("no cob");
251                         return;
252                 }
253                 else if (state_no_cob_inside() && state_cob_partial == 1)
254                         goto cont;
255
256                 STMCH_DEBUG("bad cob state");
257
258                 /* while cob is not correctly placed try to extract
259                  * it as much as we can */
260                 while (state_cob_inside() == 0 &&
261                        state_no_cob_inside() == 0) {
262                         uint8_t cpt = 0;
263                         if (cpt == 0)
264                                 cobroller_reverse(side);
265                         else if (cpt == 1)
266                                 cobroller_off(side);
267                         else if (cpt == 2)
268                                 cobroller_on(side);
269                         cpt ++;
270                         cpt %= 3;
271                         shovel_mid();
272                         time_wait_ms(250);
273                         shovel_down();
274                         time_wait_ms(250);
275
276                         if (EJECT(state_mode))
277                                 return;
278                 }
279
280                 STMCH_DEBUG("cob removed");
281                 if (state_no_cob_inside()) {
282                         STMCH_DEBUG("no cob");
283                         return;
284                 }
285         }
286
287  cont:
288         /* cob is inside, switch off roller */
289         cobroller_off(side);
290         cob_count ++;
291
292         state_debug_wait_key_pressed();
293
294         /* last cob, nothing to do */
295         if (cob_count == 5)
296                 return;
297
298         /* redeploy the spickle if there is a black cob */
299         if (!state_spicklemode_nomove(side))
300                 spickle_deploy(side);
301         state_debug_wait_key_pressed();
302
303         /* let the loaded cobs go */
304         servo_door_block();
305         servo_carry_open();
306         time_wait_ms(100);
307         state_debug_wait_key_pressed();
308
309         /* store it */
310         shovel_up();
311
312         i = 0;
313         while (WAIT_COND_OR_TIMEOUT(shovel_is_up(), 600) == 0) {
314                 STMCH_DEBUG("shovel blocked");
315                 shovel_down();
316
317                 if (i == 4)
318                         break;
319
320                 /* if eject command is received, force exit */
321                 if (EJECT(state_mode))
322                         return;
323
324                 time_wait_ms(250);
325                 shovel_up();
326                 i ++;
327         }
328
329         /* bad state, try to eject to cobs */
330         if (i == 4) {
331                 servo_door_open();
332                 shovel_mid();
333
334                 while (WAIT_COND_OR_TIMEOUT(shovel_is_mid(), 600) == 0) {
335                         STMCH_DEBUG("ejecting cobs");
336
337                         shovel_down();
338                         time_wait_ms(250);
339                         if (state_no_cob_inside()) {
340                                 servo_door_close();
341                                 cob_count = 0;
342                                 return;
343                         }
344                         shovel_mid();
345                 }
346         }
347
348         state_debug_wait_key_pressed();
349
350         /* close the carry servos */
351         servo_carry_close();
352         servo_door_close();
353         time_wait_ms(200);
354         state_debug_wait_key_pressed();
355
356         shovel_down();
357
358         while (WAIT_COND_OR_TIMEOUT(shovel_is_down(), 400) == 0) {
359                 STMCH_DEBUG("shovel blocked");
360                 shovel_up();
361                 time_wait_ms(250);
362                 shovel_down();
363         }
364
365         STMCH_DEBUG("end");
366 }
367
368 /* eject cobs */
369 static void state_do_eject(void)
370 {
371         state_status = I2C_COBBOARD_STATUS_EJECT;
372         cob_count = 0;
373         shovel_mid();
374         servo_carry_open();
375         servo_door_open();
376         time_wait_ms(2000);
377         shovel_down();
378         servo_door_close();
379         servo_carry_close();
380 }
381
382 /* main state machine */
383 void state_machine(void)
384 {
385         while (state_want_exit() == 0) {
386
387                 state_status = I2C_COBBOARD_STATUS_READY;
388
389                 /* init */
390                 if (INIT(state_mode)) {
391                         state_init();
392                         state_mode = I2C_COBBOARD_MODE_HARVEST;
393                 }
394
395                 if (HARVEST(state_mode)) {
396                         /* init for each loop */
397                         shovel_down();
398                         servo_carry_close();
399                         servo_door_close();
400
401                         /* pack/deply spickles, enable/disable roller */
402                         cobroller_off(I2C_LEFT_SIDE);
403                         cobroller_off(I2C_RIGHT_SIDE);
404                         spickle_prepare(I2C_LEFT_SIDE);
405                         spickle_prepare(I2C_RIGHT_SIDE);
406
407                         /* harvest if not many cobs */
408                         if (cob_count < 5) {
409                                 if (state_spicklemode_deployed(I2C_LEFT_SIDE) &&
410                                     state_spicklemode_autoharvest(I2C_LEFT_SIDE))
411                                         state_do_harvest(I2C_LEFT_SIDE);
412                                 if (state_spicklemode_deployed(I2C_RIGHT_SIDE) &&
413                                     state_spicklemode_autoharvest(I2C_RIGHT_SIDE))
414                                         state_do_harvest(I2C_RIGHT_SIDE);
415                         }
416                 }
417
418                 /* help to climb the hill */
419                 if (KICKSTAND_UP(state_mode)) {
420                         state_status = I2C_COBBOARD_STATUS_KICKSTAND_UP;
421                         servo_carry_open();
422                         servo_door_open();
423                         shovel_kickstand_up();
424                 }
425
426                 if (KICKSTAND_DOWN(state_mode)) {
427                         state_status = I2C_COBBOARD_STATUS_KICKSTAND_DOWN;
428                         servo_carry_open();
429                         servo_door_open();
430                         shovel_kickstand_down();
431                 }
432
433                 /* eject */
434                 if (EJECT(state_mode)) {
435                         state_mode = I2C_COBBOARD_MODE_HARVEST;
436                         state_do_eject();
437                 }
438         }
439         state_status = I2C_COBBOARD_STATUS_OFF;
440 }
441
442 void state_init(void)
443 {
444         vt100_init(&local_vt100);
445         shovel_down();
446         servo_door_close();
447         servo_carry_close();
448         spickle_pack(I2C_LEFT_SIDE);
449         spickle_pack(I2C_RIGHT_SIDE);
450         state_mode = 0;
451         cob_count = 0;
452         state_status = I2C_COBBOARD_STATUS_OFF;
453 }