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 cob is present */
90 static uint8_t state_cob_present(uint8_t side)
92 if (side == I2C_LEFT_SIDE)
93 return sensor_get(S_LCOB);
95 return sensor_get(S_RCOB);
98 /* return the detected color of the cob (only valid if present) */
99 static uint8_t state_cob_color(uint8_t side)
101 /* XXX no color sensor for now */
102 if (side == I2C_LEFT_SIDE)
103 return I2C_COB_WHITE;
105 return I2C_COB_WHITE;
108 /* return true if the cob is correctly inside */
109 static uint8_t state_cob_inside(void)
111 return sensor_get(S_COB_INSIDE_L) &&
112 sensor_get(S_COB_INSIDE_R);
115 /* return true if there is no cob correctly inside */
116 static uint8_t state_no_cob_inside(void)
118 return !sensor_get(S_COB_INSIDE_L) &&
119 !sensor_get(S_COB_INSIDE_R);
122 /* pack/deploy spickles depending on mode */
123 static void spickle_prepare(uint8_t side)
127 else if (DEPLOY(side, state_mode) && !HARVEST(side, state_mode))
129 else if (DEPLOY(side, state_mode) && HARVEST(side, state_mode))
130 spickle_deploy(side);
135 /* set a new state, return 0 on success */
136 int8_t state_set_mode(uint8_t mode)
140 /* preempt current action if not busy */
141 if (state_status != I2C_COBBOARD_STATUS_LBUSY)
142 spickle_prepare(I2C_LEFT_SIDE);
143 if (state_status != I2C_COBBOARD_STATUS_RBUSY)
144 spickle_prepare(I2C_RIGHT_SIDE);
146 /* STMCH_DEBUG("%s(): l_deploy=%d l_harvest=%d " */
147 /* "r_deploy=%d r_harvest=%d eject=%d", */
148 /* __FUNCTION__, L_DEPLOY(mode), L_HARVEST(mode), */
149 /* R_DEPLOY(mode), R_HARVEST(mode), EJECT(mode)); */
154 /* check that state is the one in parameter and that state did not
156 static uint8_t state_want_exit(void)
160 /* force quit when CTRL-C is typed */
161 c = cmdline_getchar();
164 if (vt100_parser(&local_vt100, c) == KEY_CTRL_C)
166 printf_P(PSTR("CTRL-C\r\n"));
170 uint8_t state_get_mode(void)
175 uint8_t state_get_status(void)
180 /* harvest cobs from area */
181 static void state_do_harvest(uint8_t side)
183 if (side == I2C_LEFT_SIDE)
184 state_status = I2C_COBBOARD_STATUS_LBUSY;
186 state_status = I2C_COBBOARD_STATUS_RBUSY;
188 /* if there is no cob, return */
189 if (state_cob_present(side))
192 /* if it is black, nothing to do */
193 if (state_cob_color(side) == I2C_COB_BLACK)
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(), 400) == 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 (L_DEPLOY(state_mode) && L_HARVEST(state_mode))
322 state_do_harvest(I2C_LEFT_SIDE);
323 if (R_DEPLOY(state_mode) && R_HARVEST(state_mode))
324 state_do_harvest(I2C_RIGHT_SIDE);
328 if (EJECT(state_mode)) {
329 state_mode &= (~I2C_COBBOARD_MODE_EJECT);
335 void state_init(void)
337 vt100_init(&local_vt100);
341 spickle_pack(I2C_LEFT_SIDE);
342 spickle_pack(I2C_RIGHT_SIDE);
345 state_status = I2C_COBBOARD_STATUS_READY;