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 uint8_t cob_count;
64 #define L_DEPLOY(mode) (!!((mode) & I2C_COBBOARD_MODE_L_DEPLOY))
65 #define R_DEPLOY(mode) (!!((mode) & I2C_COBBOARD_MODE_R_DEPLOY))
66 #define DEPLOY(side, mode) ((side) == I2C_LEFT_SIDE ? L_DEPLOY(mode) : R_DEPLOY(mode))
67 #define L_HARVEST(mode) (!!((mode) & I2C_COBBOARD_MODE_L_HARVEST))
68 #define R_HARVEST(mode) (!!((mode) & I2C_COBBOARD_MODE_R_HARVEST))
69 #define HARVEST(side, mode) ((side) == I2C_LEFT_SIDE ? L_HARVEST(mode) : R_HARVEST(mode))
70 #define EJECT(mode) (!!((mode) & I2C_COBBOARD_MODE_EJECT))
71 #define INIT(mode) (!!((mode) & I2C_COBBOARD_MODE_INIT))
73 uint8_t state_debug = 0;
76 static void state_dump_sensors(void)
78 STMCH_DEBUG("TODO\n");
82 uint8_t state_get_cob_count(void)
87 static void state_debug_wait_key_pressed(void)
91 printf_P(PSTR("press a key\r\n"));
92 while(!cmdline_keypressed());
95 /* return true if cob is present */
96 static uint8_t state_cob_present(uint8_t side)
98 if (side == I2C_LEFT_SIDE)
99 return sensor_get(S_LCOB);
102 // return sensor_get(S_LCOB);
106 /* return the detected color of the cob (only valid if present) */
107 static uint8_t state_cob_color(uint8_t side)
109 if (side == I2C_LEFT_SIDE)
110 return I2C_COB_WHITE;
113 // return sensor_get(S_LCOB);
117 /* return true if the cob is correctly inside */
118 static uint8_t state_cob_inside(void)
120 return sensor_get(S_COB_INSIDE_L) &&
121 sensor_get(S_COB_INSIDE_R);
124 /* set a new state, return 0 on success */
125 int8_t state_set_mode(uint8_t mode)
129 /* STMCH_DEBUG("%s(): l_deploy=%d l_harvest=%d " */
130 /* "r_deploy=%d r_harvest=%d eject=%d", */
131 /* __FUNCTION__, L_DEPLOY(mode), L_HARVEST(mode), */
132 /* R_DEPLOY(mode), R_HARVEST(mode), EJECT(mode)); */
137 /* check that state is the one in parameter and that state did not
139 static uint8_t state_want_exit(void)
143 /* force quit when CTRL-C is typed */
144 c = cmdline_getchar();
147 if (vt100_parser(&local_vt100, c) == KEY_CTRL_C)
149 printf_P(PSTR("CTRL-C\r\n"));
153 uint8_t state_get_mode(void)
158 /* harvest cobs from area */
159 static void state_do_harvest(uint8_t side)
163 /* if there is no cob, return */
164 if (state_cob_present(side))
167 /* if it is black, nothing to do */
168 if (state_cob_color(side) == I2C_COB_BLACK)
176 delay = spickle_get_pack_delay(side);
178 WAIT_COND_OR_TIMEOUT(state_cob_inside(), delay);
180 /* redeploy the spickle */
181 spickle_deploy(side);
182 state_debug_wait_key_pressed();
186 wait_ms(300); /* XXX */
187 state_debug_wait_key_pressed();
194 state_debug_wait_key_pressed();
196 /* close the carry servos */
198 wait_ms(300); /* XXX */
199 state_debug_wait_key_pressed();
202 left_cobroller_off();
203 state_debug_wait_key_pressed();
208 static void state_do_eject(void)
220 /* main state machine */
221 void state_machine(void)
223 while (state_want_exit() == 0) {
226 if (INIT(state_mode)) {
227 state_mode &= (~I2C_COBBOARD_MODE_INIT);
231 /* pack/deply spickles, enable/disable roller */
232 if (L_DEPLOY(state_mode)) {
233 spickle_deploy(I2C_LEFT_SIDE);
234 //left_cobroller_on();
235 left_cobroller_off();
238 spickle_pack(I2C_LEFT_SIDE);
239 left_cobroller_off();
241 if (R_DEPLOY(state_mode)) {
242 spickle_deploy(I2C_RIGHT_SIDE);
243 right_cobroller_on();
246 spickle_pack(I2C_RIGHT_SIDE);
247 right_cobroller_off();
251 if (L_DEPLOY(state_mode) && L_HARVEST(state_mode))
252 state_do_harvest(I2C_LEFT_SIDE);
253 if (R_DEPLOY(state_mode) && R_HARVEST(state_mode))
254 state_do_harvest(I2C_RIGHT_SIDE);
257 if (EJECT(state_mode)) {
258 state_mode &= (~I2C_COBBOARD_MODE_EJECT);
264 void state_init(void)
266 vt100_init(&local_vt100);
270 spickle_pack(I2C_LEFT_SIDE);
271 spickle_pack(I2C_RIGHT_SIDE);