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;
61 static volatile uint8_t state_status;
62 static uint8_t cob_count;
65 #define L_DEPLOY(mode) (!!((mode) & I2C_COBBOARD_MODE_L_DEPLOY))
66 #define R_DEPLOY(mode) (!!((mode) & I2C_COBBOARD_MODE_R_DEPLOY))
67 #define DEPLOY(side, mode) ((side) == I2C_LEFT_SIDE ? L_DEPLOY(mode) : R_DEPLOY(mode))
68 #define L_HARVEST(mode) (!!((mode) & I2C_COBBOARD_MODE_L_HARVEST))
69 #define R_HARVEST(mode) (!!((mode) & I2C_COBBOARD_MODE_R_HARVEST))
70 #define HARVEST(side, mode) ((side) == I2C_LEFT_SIDE ? L_HARVEST(mode) : R_HARVEST(mode))
71 #define EJECT(mode) (!!((mode) & I2C_COBBOARD_MODE_EJECT))
72 #define INIT(mode) (!!((mode) & I2C_COBBOARD_MODE_INIT))
74 uint8_t state_debug = 0;
76 uint8_t state_get_cob_count(void)
81 static void state_debug_wait_key_pressed(void)
85 printf_P(PSTR("press a key\r\n"));
86 while(!cmdline_keypressed());
89 /* return true if the cob is correctly inside */
90 static uint8_t state_cob_inside(void)
92 return sensor_get(S_COB_INSIDE_L) &&
93 sensor_get(S_COB_INSIDE_R);
96 /* return true if there is no cob correctly inside */
97 static uint8_t state_no_cob_inside(void)
99 return !sensor_get(S_COB_INSIDE_L) &&
100 !sensor_get(S_COB_INSIDE_R);
103 /* pack/deploy spickles depending on mode */
104 static void spickle_prepare(uint8_t side)
108 else if (DEPLOY(side, state_mode) && !HARVEST(side, state_mode))
110 else if (DEPLOY(side, state_mode) && HARVEST(side, state_mode))
111 spickle_deploy(side);
116 /* set a new state, return 0 on success */
117 int8_t state_set_mode(uint8_t mode)
121 /* preempt current action if not busy */
122 if (state_status != I2C_COBBOARD_STATUS_LBUSY)
123 spickle_prepare(I2C_LEFT_SIDE);
124 if (state_status != I2C_COBBOARD_STATUS_RBUSY)
125 spickle_prepare(I2C_RIGHT_SIDE);
127 /* STMCH_DEBUG("%s(): l_deploy=%d l_harvest=%d " */
128 /* "r_deploy=%d r_harvest=%d eject=%d", */
129 /* __FUNCTION__, L_DEPLOY(mode), L_HARVEST(mode), */
130 /* R_DEPLOY(mode), R_HARVEST(mode), EJECT(mode)); */
135 /* check that state is the one in parameter and that state did not
137 static uint8_t state_want_exit(void)
141 /* force quit when CTRL-C is typed */
142 c = cmdline_getchar();
145 if (vt100_parser(&local_vt100, c) == KEY_CTRL_C)
147 printf_P(PSTR("CTRL-C\r\n"));
151 uint8_t state_get_mode(void)
156 uint8_t state_get_status(void)
161 /* harvest cobs from area */
162 static void state_do_harvest(uint8_t side)
164 if (side == I2C_LEFT_SIDE)
165 state_status = I2C_COBBOARD_STATUS_LBUSY;
167 state_status = I2C_COBBOARD_STATUS_RBUSY;
169 /* if there is no cob, return */
170 if (cob_falling_edge(side) == 0)
173 STMCH_DEBUG("start");
181 /* check that cob is correctly in place */
182 if (WAIT_COND_OR_TIMEOUT(state_cob_inside(), 750) == 0) {
183 if (state_no_cob_inside()) {
184 STMCH_DEBUG("no cob");
187 STMCH_DEBUG("bad cob state");
189 /* while cob is not correctly placed try to extract
190 * it as much as we can */
191 while (state_cob_inside() == 0 &&
192 state_no_cob_inside() == 0) {
195 cobroller_reverse(side);
208 STMCH_DEBUG("cob removed");
209 if (state_no_cob_inside()) {
210 STMCH_DEBUG("no cob");
215 /* cob is inside, switch off roller */
219 /* last cob, nothing to do */
223 /* redeploy the spickle */
224 spickle_deploy(side);
225 state_debug_wait_key_pressed();
227 /* let the loaded cobs go */
231 state_debug_wait_key_pressed();
236 while (WAIT_COND_OR_TIMEOUT(shovel_is_up(), 600) == 0) {
237 STMCH_DEBUG("shovel blocked");
243 state_debug_wait_key_pressed();
245 /* close the carry servos */
249 state_debug_wait_key_pressed();
253 while (WAIT_COND_OR_TIMEOUT(shovel_is_down(), 400) == 0) {
254 STMCH_DEBUG("shovel blocked");
264 static void state_do_eject(void)
266 state_status = I2C_COBBOARD_STATUS_EJECT;
277 /* main state machine */
278 void state_machine(void)
280 while (state_want_exit() == 0) {
282 state_status = I2C_COBBOARD_STATUS_READY;
285 if (INIT(state_mode)) {
286 state_mode &= (~I2C_COBBOARD_MODE_INIT);
290 /* pack/deply spickles, enable/disable roller */
291 cobroller_off(I2C_LEFT_SIDE);
292 cobroller_off(I2C_RIGHT_SIDE);
293 spickle_prepare(I2C_LEFT_SIDE);
294 spickle_prepare(I2C_RIGHT_SIDE);
298 if (L_DEPLOY(state_mode) && L_HARVEST(state_mode))
299 state_do_harvest(I2C_LEFT_SIDE);
300 if (R_DEPLOY(state_mode) && R_HARVEST(state_mode))
301 state_do_harvest(I2C_RIGHT_SIDE);
305 if (EJECT(state_mode)) {
306 state_mode &= (~I2C_COBBOARD_MODE_EJECT);
310 state_status = I2C_COBBOARD_STATUS_READY;
313 void state_init(void)
315 vt100_init(&local_vt100);
319 spickle_pack(I2C_LEFT_SIDE);
320 spickle_pack(I2C_RIGHT_SIDE);
323 state_status = I2C_COBBOARD_STATUS_READY;