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(mode) ((mode) == I2C_COBBOARD_MODE_KICKSTAND)
71 uint8_t state_debug = 0;
73 uint8_t state_get_cob_count(void)
78 static void state_debug_wait_key_pressed(void)
82 printf_P(PSTR("press a key\r\n"));
83 while(!cmdline_keypressed());
86 /* return true if the cob is correctly inside */
87 static uint8_t state_cob_inside(void)
89 return sensor_get(S_COB_INSIDE_L) &&
90 sensor_get(S_COB_INSIDE_R);
93 /* return true if there is no cob correctly inside */
94 static uint8_t state_no_cob_inside(void)
96 return !sensor_get(S_COB_INSIDE_L) &&
97 !sensor_get(S_COB_INSIDE_R);
100 static uint8_t state_spicklemode_deployed(uint8_t side)
102 if (side == I2C_LEFT_SIDE)
103 return lspickle & I2C_COBBOARD_SPK_DEPLOY;
105 return rspickle & I2C_COBBOARD_SPK_DEPLOY;
108 static uint8_t state_spicklemode_autoharvest(uint8_t side)
110 if (side == I2C_LEFT_SIDE)
111 return lspickle & I2C_COBBOARD_SPK_AUTOHARVEST;
113 return rspickle & I2C_COBBOARD_SPK_AUTOHARVEST;
116 static uint8_t state_spicklemode_nomove(uint8_t side)
118 if (side == I2C_LEFT_SIDE)
119 return lspickle & I2C_COBBOARD_SPK_NO_MOVE;
121 return rspickle & I2C_COBBOARD_SPK_NO_MOVE;
124 uint8_t state_spicklemode_weak(uint8_t side)
126 if (side == I2C_LEFT_SIDE)
127 return lspickle & I2C_COBBOARD_SPK_WEAK;
129 return rspickle & I2C_COBBOARD_SPK_WEAK;
132 /* pack/deploy spickles depending on mode */
133 static void spickle_prepare(uint8_t side)
135 /* pack spickle if we are not in harvest mode */
136 if (!HARVEST(state_mode))
139 /* we do nothing in mode no out */
140 if (state_spicklemode_nomove(side))
143 if (state_spicklemode_deployed(side))
144 spickle_deploy(side);
149 void state_set_spickle(uint8_t side, uint8_t flags)
151 if (side == I2C_LEFT_SIDE) {
152 /* the PACK command preempts the current action if the
154 if (lspickle != 0 && flags == 0 &&
155 state_status != I2C_COBBOARD_STATUS_LBUSY)
156 spickle_prepare(I2C_LEFT_SIDE);
160 /* the PACK command preempts the current action if the
162 if (rspickle != 0 && flags == 0 &&
163 state_status != I2C_COBBOARD_STATUS_RBUSY)
164 spickle_prepare(I2C_RIGHT_SIDE);
169 /* set a new state, return 0 on success */
170 int8_t state_set_mode(uint8_t mode)
173 STMCH_DEBUG("%s(): mode=%x", __FUNCTION__, mode);
177 void state_set_i2c_ignore(uint8_t val)
179 state_i2c_ignore = val;
182 uint8_t state_get_i2c_ignore(void)
184 return state_i2c_ignore;
187 /* check that state is the one in parameter and that state did not
189 static uint8_t state_want_exit(void)
193 /* force quit when CTRL-C is typed */
194 c = cmdline_getchar();
197 if (vt100_parser(&local_vt100, c) == KEY_CTRL_C)
199 printf_P(PSTR("CTRL-C\r\n"));
203 uint8_t state_get_mode(void)
208 uint8_t state_get_status(void)
213 /* harvest cobs from area */
214 static void state_do_harvest(uint8_t side)
216 /* if there is no cob, return */
217 if (cob_falling_edge(side) == 0)
220 if (side == I2C_LEFT_SIDE)
221 state_status = I2C_COBBOARD_STATUS_LBUSY;
223 state_status = I2C_COBBOARD_STATUS_RBUSY;
225 STMCH_DEBUG("start");
233 /* check that cob is correctly in place */
234 if (WAIT_COND_OR_TIMEOUT(state_cob_inside(), 750) == 0) {
235 if (state_no_cob_inside()) {
236 STMCH_DEBUG("no cob");
239 STMCH_DEBUG("bad cob state");
241 /* while cob is not correctly placed try to extract
242 * it as much as we can */
243 while (state_cob_inside() == 0 &&
244 state_no_cob_inside() == 0) {
247 cobroller_reverse(side);
260 STMCH_DEBUG("cob removed");
261 if (state_no_cob_inside()) {
262 STMCH_DEBUG("no cob");
267 /* cob is inside, switch off roller */
271 state_debug_wait_key_pressed();
273 /* last cob, nothing to do */
277 /* redeploy the spickle if there is a black cob */
278 if (!state_spicklemode_nomove(side))
279 spickle_deploy(side);
280 state_debug_wait_key_pressed();
282 /* let the loaded cobs go */
286 state_debug_wait_key_pressed();
291 while (WAIT_COND_OR_TIMEOUT(shovel_is_up(), 600) == 0) {
292 STMCH_DEBUG("shovel blocked");
298 state_debug_wait_key_pressed();
300 /* close the carry servos */
304 state_debug_wait_key_pressed();
308 while (WAIT_COND_OR_TIMEOUT(shovel_is_down(), 400) == 0) {
309 STMCH_DEBUG("shovel blocked");
319 static void state_do_eject(void)
321 state_status = I2C_COBBOARD_STATUS_EJECT;
332 /* main state machine */
333 void state_machine(void)
335 while (state_want_exit() == 0) {
337 state_status = I2C_COBBOARD_STATUS_READY;
340 if (INIT(state_mode)) {
342 state_mode = I2C_COBBOARD_MODE_HARVEST;
345 if (HARVEST(state_mode)) {
346 /* init for each loop */
350 shovel_current_limit_enable(0);
352 /* pack/deply spickles, enable/disable roller */
353 cobroller_off(I2C_LEFT_SIDE);
354 cobroller_off(I2C_RIGHT_SIDE);
355 spickle_prepare(I2C_LEFT_SIDE);
356 spickle_prepare(I2C_RIGHT_SIDE);
358 /* harvest if not many cobs */
360 if (state_spicklemode_deployed(I2C_LEFT_SIDE) &&
361 state_spicklemode_autoharvest(I2C_LEFT_SIDE))
362 state_do_harvest(I2C_LEFT_SIDE);
363 if (state_spicklemode_deployed(I2C_RIGHT_SIDE) &&
364 state_spicklemode_autoharvest(I2C_RIGHT_SIDE))
365 state_do_harvest(I2C_RIGHT_SIDE);
369 /* help to climb the hill */
370 if (KICKSTAND(state_mode)) {
373 shovel_current_limit_enable(1);
378 if (EJECT(state_mode)) {
379 state_mode = I2C_COBBOARD_MODE_HARVEST;
383 state_status = I2C_COBBOARD_STATUS_OFF;
386 void state_init(void)
388 vt100_init(&local_vt100);
392 spickle_pack(I2C_LEFT_SIDE);
393 spickle_pack(I2C_RIGHT_SIDE);
396 state_status = I2C_COBBOARD_STATUS_OFF;