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)
70 uint8_t state_debug = 0;
72 uint8_t state_get_cob_count(void)
77 static void state_debug_wait_key_pressed(void)
81 printf_P(PSTR("press a key\r\n"));
82 while(!cmdline_keypressed());
85 /* return true if the cob is correctly inside */
86 static uint8_t state_cob_inside(void)
88 return sensor_get(S_COB_INSIDE_L) &&
89 sensor_get(S_COB_INSIDE_R);
92 /* return true if there is no cob correctly inside */
93 static uint8_t state_no_cob_inside(void)
95 return !sensor_get(S_COB_INSIDE_L) &&
96 !sensor_get(S_COB_INSIDE_R);
99 static uint8_t state_spicklemode_deployed(uint8_t side)
101 if (side == I2C_LEFT_SIDE)
102 return lspickle & I2C_COBBOARD_SPK_DEPLOY;
104 return rspickle & I2C_COBBOARD_SPK_DEPLOY;
107 static uint8_t state_spicklemode_autoharvest(uint8_t side)
109 if (side == I2C_LEFT_SIDE)
110 return lspickle & I2C_COBBOARD_SPK_AUTOHARVEST;
112 return rspickle & I2C_COBBOARD_SPK_AUTOHARVEST;
115 static uint8_t state_spicklemode_nomove(uint8_t side)
117 if (side == I2C_LEFT_SIDE)
118 return lspickle & I2C_COBBOARD_SPK_NO_MOVE;
120 return rspickle & I2C_COBBOARD_SPK_NO_MOVE;
123 /* pack/deploy spickles depending on mode */
124 static void spickle_prepare(uint8_t side)
126 /* we do nothing in mode no out */
127 if (state_spicklemode_nomove(side))
130 if (state_spicklemode_deployed(side))
131 spickle_deploy(side);
136 void state_set_spickle(uint8_t side, uint8_t flags)
138 if (side == I2C_LEFT_SIDE) {
139 /* the PACK command preempts the current action if the
141 if (lspickle != 0 && flags == 0 &&
142 state_status != I2C_COBBOARD_STATUS_LBUSY)
143 spickle_prepare(I2C_LEFT_SIDE);
147 /* the PACK command preempts the current action if the
149 if (rspickle != 0 && flags == 0 &&
150 state_status != I2C_COBBOARD_STATUS_RBUSY)
151 spickle_prepare(I2C_RIGHT_SIDE);
156 /* set a new state, return 0 on success */
157 int8_t state_set_mode(uint8_t mode)
160 STMCH_DEBUG("%s(): mode=%x", __FUNCTION__, mode);
164 void state_set_i2c_ignore(uint8_t val)
166 state_i2c_ignore = val;
169 uint8_t state_get_i2c_ignore(void)
171 return state_i2c_ignore;
174 /* check that state is the one in parameter and that state did not
176 static uint8_t state_want_exit(void)
180 /* force quit when CTRL-C is typed */
181 c = cmdline_getchar();
184 if (vt100_parser(&local_vt100, c) == KEY_CTRL_C)
186 printf_P(PSTR("CTRL-C\r\n"));
190 uint8_t state_get_mode(void)
195 uint8_t state_get_status(void)
200 /* harvest cobs from area */
201 static void state_do_harvest(uint8_t side)
203 /* if there is no cob, return */
204 if (cob_falling_edge(side) == 0)
207 if (side == I2C_LEFT_SIDE)
208 state_status = I2C_COBBOARD_STATUS_LBUSY;
210 state_status = I2C_COBBOARD_STATUS_RBUSY;
212 STMCH_DEBUG("start");
220 /* check that cob is correctly in place */
221 if (WAIT_COND_OR_TIMEOUT(state_cob_inside(), 750) == 0) {
222 if (state_no_cob_inside()) {
223 STMCH_DEBUG("no cob");
226 STMCH_DEBUG("bad cob state");
228 /* while cob is not correctly placed try to extract
229 * it as much as we can */
230 while (state_cob_inside() == 0 &&
231 state_no_cob_inside() == 0) {
234 cobroller_reverse(side);
247 STMCH_DEBUG("cob removed");
248 if (state_no_cob_inside()) {
249 STMCH_DEBUG("no cob");
254 /* cob is inside, switch off roller */
258 state_debug_wait_key_pressed();
260 /* last cob, nothing to do */
264 /* redeploy the spickle if there is a black cob */
265 if (!state_spicklemode_nomove(side))
266 spickle_deploy(side);
267 state_debug_wait_key_pressed();
269 /* let the loaded cobs go */
273 state_debug_wait_key_pressed();
278 while (WAIT_COND_OR_TIMEOUT(shovel_is_up(), 600) == 0) {
279 STMCH_DEBUG("shovel blocked");
285 state_debug_wait_key_pressed();
287 /* close the carry servos */
291 state_debug_wait_key_pressed();
295 while (WAIT_COND_OR_TIMEOUT(shovel_is_down(), 400) == 0) {
296 STMCH_DEBUG("shovel blocked");
306 static void state_do_eject(void)
308 state_status = I2C_COBBOARD_STATUS_EJECT;
319 /* main state machine */
320 void state_machine(void)
322 while (state_want_exit() == 0) {
324 state_status = I2C_COBBOARD_STATUS_READY;
327 if (INIT(state_mode)) {
329 state_mode = I2C_COBBOARD_MODE_HARVEST;
332 /* pack/deply spickles, enable/disable roller */
333 cobroller_off(I2C_LEFT_SIDE);
334 cobroller_off(I2C_RIGHT_SIDE);
335 spickle_prepare(I2C_LEFT_SIDE);
336 spickle_prepare(I2C_RIGHT_SIDE);
340 if (state_spicklemode_deployed(I2C_LEFT_SIDE) &&
341 state_spicklemode_autoharvest(I2C_LEFT_SIDE) &&
342 !state_spicklemode_nomove(I2C_LEFT_SIDE))
343 state_do_harvest(I2C_LEFT_SIDE);
344 if (state_spicklemode_deployed(I2C_RIGHT_SIDE) &&
345 state_spicklemode_autoharvest(I2C_RIGHT_SIDE) &&
346 !state_spicklemode_nomove(I2C_RIGHT_SIDE))
347 state_do_harvest(I2C_RIGHT_SIDE);
351 if (EJECT(state_mode)) {
352 state_mode = I2C_COBBOARD_MODE_HARVEST;
356 state_status = I2C_COBBOARD_STATUS_OFF;
359 void state_init(void)
361 vt100_init(&local_vt100);
365 spickle_pack(I2C_LEFT_SIDE);
366 spickle_pack(I2C_RIGHT_SIDE);
369 state_status = I2C_COBBOARD_STATUS_OFF;