2 * Copyright Droids Corporation (2009)
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.
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.
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
18 * Revision : $Id: state.c,v 1.5 2009-11-08 17:25:00 zer0 Exp $
26 #include <aversive/wait.h>
27 #include <aversive/error.h>
32 #include <encoders_spi.h>
35 #include <scheduler.h>
36 #include <clock_time.h>
40 #include <control_system_manager.h>
41 #include <blocking_detection_manager.h>
46 #include "../common/i2c_commands.h"
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)
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;
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)
72 uint8_t state_debug = 0;
74 uint8_t state_get_cob_count(void)
79 static void state_debug_wait_key_pressed(void)
83 printf_P(PSTR("press a key\r\n"));
84 while(!cmdline_keypressed());
87 /* return true if the cob is correctly inside */
88 static uint8_t state_cob_inside(void)
90 return sensor_get(S_COB_INSIDE_L) &&
91 sensor_get(S_COB_INSIDE_R);
94 /* return true if there is no cob correctly inside */
95 static uint8_t state_no_cob_inside(void)
97 return !sensor_get(S_COB_INSIDE_L) &&
98 !sensor_get(S_COB_INSIDE_R);
101 static uint8_t state_spicklemode_deployed(uint8_t side)
103 if (side == I2C_LEFT_SIDE)
104 return lspickle & I2C_COBBOARD_SPK_DEPLOY;
106 return rspickle & I2C_COBBOARD_SPK_DEPLOY;
109 static uint8_t state_spicklemode_autoharvest(uint8_t side)
111 if (side == I2C_LEFT_SIDE)
112 return lspickle & I2C_COBBOARD_SPK_AUTOHARVEST;
114 return rspickle & I2C_COBBOARD_SPK_AUTOHARVEST;
117 static uint8_t state_spicklemode_nomove(uint8_t side)
119 if (side == I2C_LEFT_SIDE)
120 return lspickle & I2C_COBBOARD_SPK_NO_MOVE;
122 return rspickle & I2C_COBBOARD_SPK_NO_MOVE;
125 uint8_t state_spicklemode_weak(uint8_t side)
127 if (side == I2C_LEFT_SIDE)
128 return lspickle & I2C_COBBOARD_SPK_WEAK;
130 return rspickle & I2C_COBBOARD_SPK_WEAK;
133 /* pack/deploy spickles depending on mode */
134 static void spickle_prepare(uint8_t side)
136 /* pack spickle if we are not in harvest mode */
137 if (!HARVEST(state_mode))
140 /* we do nothing in mode no out */
141 if (state_spicklemode_nomove(side))
144 if (state_spicklemode_deployed(side))
145 spickle_deploy(side);
150 void state_set_spickle(uint8_t side, uint8_t flags)
152 if (side == I2C_LEFT_SIDE) {
153 /* the PACK command preempts the current action if the
155 if (lspickle != 0 && flags == 0 &&
156 state_status != I2C_COBBOARD_STATUS_LBUSY)
157 spickle_prepare(I2C_LEFT_SIDE);
161 /* the PACK command preempts the current action if the
163 if (rspickle != 0 && flags == 0 &&
164 state_status != I2C_COBBOARD_STATUS_RBUSY)
165 spickle_prepare(I2C_RIGHT_SIDE);
170 /* set a new state, return 0 on success */
171 int8_t state_set_mode(uint8_t mode)
174 STMCH_DEBUG("%s(): mode=%x", __FUNCTION__, mode);
178 void state_set_i2c_ignore(uint8_t val)
180 state_i2c_ignore = val;
183 uint8_t state_get_i2c_ignore(void)
185 return state_i2c_ignore;
188 /* check that state is the one in parameter and that state did not
190 static uint8_t state_want_exit(void)
194 /* force quit when CTRL-C is typed */
195 c = cmdline_getchar();
198 if (vt100_parser(&local_vt100, c) == KEY_CTRL_C)
200 printf_P(PSTR("CTRL-C\r\n"));
204 uint8_t state_get_mode(void)
209 uint8_t state_get_status(void)
214 /* harvest cobs from area */
215 static void state_do_harvest(uint8_t side)
217 /* if there is no cob, return */
218 if (cob_falling_edge(side) == 0)
221 if (side == I2C_LEFT_SIDE)
222 state_status = I2C_COBBOARD_STATUS_LBUSY;
224 state_status = I2C_COBBOARD_STATUS_RBUSY;
226 STMCH_DEBUG("start");
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");
240 STMCH_DEBUG("bad cob state");
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) {
248 cobroller_reverse(side);
261 STMCH_DEBUG("cob removed");
262 if (state_no_cob_inside()) {
263 STMCH_DEBUG("no cob");
268 /* cob is inside, switch off roller */
272 state_debug_wait_key_pressed();
274 /* last cob, nothing to do */
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();
283 /* let the loaded cobs go */
287 state_debug_wait_key_pressed();
292 while (WAIT_COND_OR_TIMEOUT(shovel_is_up(), 600) == 0) {
293 STMCH_DEBUG("shovel blocked");
297 /* if eject command is received, force exit */
298 if (EJECT(state_mode))
302 state_debug_wait_key_pressed();
304 /* close the carry servos */
308 state_debug_wait_key_pressed();
312 while (WAIT_COND_OR_TIMEOUT(shovel_is_down(), 400) == 0) {
313 STMCH_DEBUG("shovel blocked");
323 static void state_do_eject(void)
325 state_status = I2C_COBBOARD_STATUS_EJECT;
336 /* main state machine */
337 void state_machine(void)
339 while (state_want_exit() == 0) {
341 state_status = I2C_COBBOARD_STATUS_READY;
344 if (INIT(state_mode)) {
346 state_mode = I2C_COBBOARD_MODE_HARVEST;
349 if (HARVEST(state_mode)) {
350 /* init for each loop */
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);
361 /* harvest if not many cobs */
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);
372 /* help to climb the hill */
373 if (KICKSTAND_UP(state_mode)) {
374 state_status = I2C_COBBOARD_STATUS_KICKSTAND_UP;
377 shovel_kickstand_up();
380 if (KICKSTAND_DOWN(state_mode)) {
381 state_status = I2C_COBBOARD_STATUS_KICKSTAND_DOWN;
384 shovel_kickstand_down();
388 if (EJECT(state_mode)) {
389 state_mode = I2C_COBBOARD_MODE_HARVEST;
393 state_status = I2C_COBBOARD_STATUS_OFF;
396 void state_init(void)
398 vt100_init(&local_vt100);
402 spickle_pack(I2C_LEFT_SIDE);
403 spickle_pack(I2C_RIGHT_SIDE);
406 state_status = I2C_COBBOARD_STATUS_OFF;