df0e4c66c69a68d2407dc58b46482d50970ea946
[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
70 uint8_t state_debug = 0;
71
72 uint8_t state_get_cob_count(void)
73 {
74         return cob_count;
75 }
76
77 static void state_debug_wait_key_pressed(void)
78 {
79         if (!state_debug)
80                 return;
81         printf_P(PSTR("press a key\r\n"));
82         while(!cmdline_keypressed());
83 }
84
85 /* return true if the cob is correctly inside */
86 static uint8_t state_cob_inside(void)
87 {
88         return sensor_get(S_COB_INSIDE_L) &&
89                 sensor_get(S_COB_INSIDE_R);
90 }
91
92 /* return true if there is no cob correctly inside */
93 static uint8_t state_no_cob_inside(void)
94 {
95         return !sensor_get(S_COB_INSIDE_L) &&
96                 !sensor_get(S_COB_INSIDE_R);
97 }
98
99 static uint8_t is_deployed(uint8_t side)
100 {
101         if (side == I2C_LEFT_SIDE)
102                 return lspickle & I2C_COBBOARD_SPK_DEPLOY;
103         else
104                 return rspickle & I2C_COBBOARD_SPK_DEPLOY;
105 }
106
107 static uint8_t is_autoharvest(uint8_t side)
108 {
109         if (side == I2C_LEFT_SIDE)
110                 return lspickle & I2C_COBBOARD_SPK_AUTOHARVEST;
111         else
112                 return rspickle & I2C_COBBOARD_SPK_AUTOHARVEST;
113 }
114
115 /* pack/deploy spickles depending on mode */
116 static void spickle_prepare(uint8_t side)
117 {
118         if (cob_count >= 5)
119                 spickle_pack(side);
120         else if (is_deployed(side) && !is_autoharvest(side))
121                 spickle_mid(side);
122         else if (is_deployed(side) && is_autoharvest(side))
123                 spickle_deploy(side);
124         else
125                 spickle_pack(side);
126 }
127
128 void state_set_spickle(uint8_t side, uint8_t flags)
129 {
130         if (side == I2C_LEFT_SIDE) {
131                 /* the PACK command preempts the current action if the
132                  * arm is not busy */
133                 if (lspickle != 0 && flags == 0 &&
134                     state_status != I2C_COBBOARD_STATUS_LBUSY)
135                         spickle_prepare(I2C_LEFT_SIDE);
136                 lspickle = flags;
137         }
138         else {
139                 /* the PACK command preempts the current action if the
140                  * arm is not busy */
141                 if (rspickle != 0 && flags == 0 &&
142                     state_status != I2C_COBBOARD_STATUS_RBUSY)
143                         spickle_prepare(I2C_RIGHT_SIDE);
144                 rspickle = flags;
145         }
146 }
147
148 /* set a new state, return 0 on success */
149 int8_t state_set_mode(uint8_t mode)
150 {
151         state_mode = mode;
152         STMCH_DEBUG("%s(): mode=%x", __FUNCTION__, mode);
153         return 0;
154 }
155
156 void state_set_i2c_ignore(uint8_t val)
157 {
158         state_i2c_ignore = val;
159 }
160
161 uint8_t state_get_i2c_ignore(void)
162 {
163         return state_i2c_ignore;
164 }
165
166 /* check that state is the one in parameter and that state did not
167  * changed */
168 static uint8_t state_want_exit(void)
169 {
170         int16_t c;
171
172         /* force quit when CTRL-C is typed */
173         c = cmdline_getchar();
174         if (c == -1)
175                 return 0;
176         if (vt100_parser(&local_vt100, c) == KEY_CTRL_C)
177                 return 1;
178         printf_P(PSTR("CTRL-C\r\n"));
179         return 0;
180 }
181
182 uint8_t state_get_mode(void)
183 {
184         return state_mode;
185 }
186
187 uint8_t state_get_status(void)
188 {
189         return state_status;
190 }
191
192 /* harvest cobs from area */
193 static void state_do_harvest(uint8_t side)
194 {
195         /* if there is no cob, return */
196         if (cob_falling_edge(side) == 0)
197                 return;
198
199         if (side == I2C_LEFT_SIDE)
200                 state_status = I2C_COBBOARD_STATUS_LBUSY;
201         else
202                 state_status = I2C_COBBOARD_STATUS_RBUSY;
203
204         STMCH_DEBUG("start");
205
206         /* eat the cob */
207         spickle_pack(side);
208
209         time_wait_ms(250);
210         cobroller_on(side);
211
212         /* check that cob is correctly in place */
213         if (WAIT_COND_OR_TIMEOUT(state_cob_inside(), 750) == 0) {
214                 if (state_no_cob_inside()) {
215                         STMCH_DEBUG("no cob");
216                         return;
217                 }
218                 STMCH_DEBUG("bad cob state");
219
220                 /* while cob is not correctly placed try to extract
221                  * it as much as we can */
222                 while (state_cob_inside() == 0 &&
223                        state_no_cob_inside() == 0) {
224                         uint8_t cpt = 0;
225                         if (cpt == 0)
226                                 cobroller_reverse(side);
227                         else if (cpt == 1)
228                                 cobroller_off(side);
229                         else if (cpt == 2)
230                                 cobroller_on(side);
231                         cpt ++;
232                         cpt %= 3;
233                         shovel_mid();
234                         time_wait_ms(250);
235                         shovel_down();
236                         time_wait_ms(250);
237                 }
238
239                 STMCH_DEBUG("cob removed");
240                 if (state_no_cob_inside()) {
241                         STMCH_DEBUG("no cob");
242                         return;
243                 }
244         }
245
246         /* cob is inside, switch off roller */
247         cobroller_off(side);
248         cob_count ++;
249
250         /* last cob, nothing to do */
251         if (cob_count == 5)
252                 return;
253
254         /* redeploy the spickle */
255         spickle_deploy(side);
256         state_debug_wait_key_pressed();
257
258         /* let the loaded cobs go */
259         servo_door_block();
260         servo_carry_open();
261         time_wait_ms(100);
262         state_debug_wait_key_pressed();
263
264         /* store it */
265         shovel_up();
266
267         while (WAIT_COND_OR_TIMEOUT(shovel_is_up(), 600) == 0) {
268                 STMCH_DEBUG("shovel blocked");
269                 shovel_down();
270                 time_wait_ms(250);
271                 shovel_up();
272         }
273
274         state_debug_wait_key_pressed();
275
276         /* close the carry servos */
277         servo_carry_close();
278         servo_door_close();
279         time_wait_ms(200);
280         state_debug_wait_key_pressed();
281
282         shovel_down();
283
284         while (WAIT_COND_OR_TIMEOUT(shovel_is_down(), 400) == 0) {
285                 STMCH_DEBUG("shovel blocked");
286                 shovel_up();
287                 time_wait_ms(250);
288                 shovel_down();
289         }
290
291         STMCH_DEBUG("end");
292 }
293
294 /* eject cobs */
295 static void state_do_eject(void)
296 {
297         state_status = I2C_COBBOARD_STATUS_EJECT;
298         cob_count = 0;
299         shovel_mid();
300         servo_carry_open();
301         servo_door_open();
302         time_wait_ms(2000);
303         shovel_down();
304         servo_door_close();
305         servo_carry_close();
306 }
307
308 /* main state machine */
309 void state_machine(void)
310 {
311         while (state_want_exit() == 0) {
312
313                 state_status = I2C_COBBOARD_STATUS_READY;
314
315                 /* init */
316                 if (INIT(state_mode)) {
317                         state_mode = I2C_COBBOARD_MODE_HARVEST;
318                         state_init();
319                 }
320
321                 /* pack/deply spickles, enable/disable roller */
322                 cobroller_off(I2C_LEFT_SIDE);
323                 cobroller_off(I2C_RIGHT_SIDE);
324                 spickle_prepare(I2C_LEFT_SIDE);
325                 spickle_prepare(I2C_RIGHT_SIDE);
326
327                 /* harvest */
328                 if (cob_count < 5) {
329                         if ((lspickle & I2C_COBBOARD_SPK_DEPLOY) &&
330                             (lspickle & I2C_COBBOARD_SPK_AUTOHARVEST))
331                                 state_do_harvest(I2C_LEFT_SIDE);
332                         if ((rspickle & I2C_COBBOARD_SPK_DEPLOY) &&
333                             (rspickle & I2C_COBBOARD_SPK_AUTOHARVEST))
334                                 state_do_harvest(I2C_RIGHT_SIDE);
335                 }
336
337                 /* eject */
338                 if (EJECT(state_mode)) {
339                         state_mode = I2C_COBBOARD_MODE_HARVEST;
340                         state_do_eject();
341                 }
342         }
343         state_status = I2C_COBBOARD_STATUS_OFF;
344 }
345
346 void state_init(void)
347 {
348         vt100_init(&local_vt100);
349         shovel_down();
350         servo_door_close();
351         servo_carry_close();
352         spickle_pack(I2C_LEFT_SIDE);
353         spickle_pack(I2C_RIGHT_SIDE);
354         state_mode = 0;
355         cob_count = 0;
356         state_status = I2C_COBBOARD_STATUS_OFF;
357 }