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 uint8_t cob_count;
65 #define HARVEST(mode) (!!((mode) & I2C_COBBOARD_MODE_HARVEST))
66 #define EJECT(mode) (!!((mode) & I2C_COBBOARD_MODE_EJECT))
67 #define INIT(mode) (!!((mode) & I2C_COBBOARD_MODE_INIT))
69 uint8_t state_debug = 0;
71 uint8_t state_get_cob_count(void)
76 static void state_debug_wait_key_pressed(void)
80 printf_P(PSTR("press a key\r\n"));
81 while(!cmdline_keypressed());
84 /* return true if the cob is correctly inside */
85 static uint8_t state_cob_inside(void)
87 return sensor_get(S_COB_INSIDE_L) &&
88 sensor_get(S_COB_INSIDE_R);
91 /* return true if there is no cob correctly inside */
92 static uint8_t state_no_cob_inside(void)
94 return !sensor_get(S_COB_INSIDE_L) &&
95 !sensor_get(S_COB_INSIDE_R);
98 static uint8_t is_deployed(uint8_t side)
100 if (side == I2C_LEFT_SIDE)
101 return lspickle & I2C_COBBOARD_SPK_DEPLOY;
103 return rspickle & I2C_COBBOARD_SPK_DEPLOY;
106 static uint8_t is_autoharvest(uint8_t side)
108 if (side == I2C_LEFT_SIDE)
109 return lspickle & I2C_COBBOARD_SPK_AUTOHARVEST;
111 return rspickle & I2C_COBBOARD_SPK_AUTOHARVEST;
114 /* pack/deploy spickles depending on mode */
115 static void spickle_prepare(uint8_t side)
119 else if (is_deployed(side) && !is_autoharvest(side))
121 else if (is_deployed(side) && is_autoharvest(side))
122 spickle_deploy(side);
127 void state_set_spickle(uint8_t side, uint8_t flags)
129 if (side == I2C_LEFT_SIDE) {
130 /* preempt current action if not busy */
131 if (lspickle != 0 && flags == 0 &&
132 state_status != I2C_COBBOARD_STATUS_LBUSY)
133 spickle_prepare(I2C_LEFT_SIDE);
137 /* preempt current action if not busy */
138 if (rspickle != 0 && flags == 0 &&
139 state_status != I2C_COBBOARD_STATUS_RBUSY)
140 spickle_prepare(I2C_RIGHT_SIDE);
145 /* set a new state, return 0 on success */
146 int8_t state_set_mode(uint8_t mode)
150 /* STMCH_DEBUG("%s(): l_deploy=%d l_harvest=%d " */
151 /* "r_deploy=%d r_harvest=%d eject=%d", */
152 /* __FUNCTION__, L_DEPLOY(mode), L_HARVEST(mode), */
153 /* R_DEPLOY(mode), R_HARVEST(mode), EJECT(mode)); */
158 /* check that state is the one in parameter and that state did not
160 static uint8_t state_want_exit(void)
164 /* force quit when CTRL-C is typed */
165 c = cmdline_getchar();
168 if (vt100_parser(&local_vt100, c) == KEY_CTRL_C)
170 printf_P(PSTR("CTRL-C\r\n"));
174 uint8_t state_get_mode(void)
179 uint8_t state_get_status(void)
184 /* harvest cobs from area */
185 static void state_do_harvest(uint8_t side)
187 if (side == I2C_LEFT_SIDE)
188 state_status = I2C_COBBOARD_STATUS_LBUSY;
190 state_status = I2C_COBBOARD_STATUS_RBUSY;
192 /* if there is no cob, return */
193 if (cob_falling_edge(side) == 0)
196 STMCH_DEBUG("start");
204 /* check that cob is correctly in place */
205 if (WAIT_COND_OR_TIMEOUT(state_cob_inside(), 750) == 0) {
206 if (state_no_cob_inside()) {
207 STMCH_DEBUG("no cob");
210 STMCH_DEBUG("bad cob state");
212 /* while cob is not correctly placed try to extract
213 * it as much as we can */
214 while (state_cob_inside() == 0 &&
215 state_no_cob_inside() == 0) {
218 cobroller_reverse(side);
231 STMCH_DEBUG("cob removed");
232 if (state_no_cob_inside()) {
233 STMCH_DEBUG("no cob");
238 /* cob is inside, switch off roller */
242 /* last cob, nothing to do */
246 /* redeploy the spickle */
247 spickle_deploy(side);
248 state_debug_wait_key_pressed();
250 /* let the loaded cobs go */
254 state_debug_wait_key_pressed();
259 while (WAIT_COND_OR_TIMEOUT(shovel_is_up(), 600) == 0) {
260 STMCH_DEBUG("shovel blocked");
266 state_debug_wait_key_pressed();
268 /* close the carry servos */
272 state_debug_wait_key_pressed();
276 while (WAIT_COND_OR_TIMEOUT(shovel_is_down(), 400) == 0) {
277 STMCH_DEBUG("shovel blocked");
287 static void state_do_eject(void)
289 state_status = I2C_COBBOARD_STATUS_EJECT;
300 /* main state machine */
301 void state_machine(void)
303 while (state_want_exit() == 0) {
305 state_status = I2C_COBBOARD_STATUS_READY;
308 if (INIT(state_mode)) {
309 state_mode &= (~I2C_COBBOARD_MODE_INIT);
313 /* pack/deply spickles, enable/disable roller */
314 cobroller_off(I2C_LEFT_SIDE);
315 cobroller_off(I2C_RIGHT_SIDE);
316 spickle_prepare(I2C_LEFT_SIDE);
317 spickle_prepare(I2C_RIGHT_SIDE);
321 if ((lspickle & I2C_COBBOARD_SPK_DEPLOY) &&
322 (lspickle & I2C_COBBOARD_SPK_DEPLOY))
323 state_do_harvest(I2C_LEFT_SIDE);
324 if ((rspickle & I2C_COBBOARD_SPK_DEPLOY) &&
325 (rspickle & I2C_COBBOARD_SPK_DEPLOY))
326 state_do_harvest(I2C_RIGHT_SIDE);
330 if (EJECT(state_mode)) {
331 state_mode &= (~I2C_COBBOARD_MODE_EJECT);
335 state_status = I2C_COBBOARD_STATUS_OFF;
338 void state_init(void)
340 vt100_init(&local_vt100);
344 spickle_pack(I2C_LEFT_SIDE);
345 spickle_pack(I2C_RIGHT_SIDE);
348 state_status = I2C_COBBOARD_STATUS_OFF;