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"
53 #define STMCH_DEBUG(args...) DEBUG(E_USER_ST_MACH, args)
54 #define STMCH_NOTICE(args...) NOTICE(E_USER_ST_MACH, args)
55 #define STMCH_ERROR(args...) ERROR(E_USER_ST_MACH, args)
57 static struct vt100 local_vt100;
58 static volatile uint8_t state_mode;
59 static volatile uint8_t state_status;
60 static volatile uint8_t ball_count;
63 #define INIT I2C_BALLBOARD_MODE_INIT
64 #define OFF I2C_BALLBOARD_MODE_OFF
65 #define HARVEST I2C_BALLBOARD_MODE_HARVEST
66 #define EJECT I2C_BALLBOARD_MODE_EJECT
67 #define PREP_FORK I2C_BALLBOARD_MODE_PREP_FORK
68 #define TAKE_FORK I2C_BALLBOARD_MODE_TAKE_FORK
70 uint8_t state_debug = 0;
73 static void state_dump_sensors(void)
75 STMCH_DEBUG("TODO\n");
79 uint8_t state_get_ball_count(void)
85 static void state_debug_wait_key_pressed(void)
89 printf_P(PSTR("press a key\r\n"));
90 while (!cmdline_keypressed());
94 /* set a new state, return 0 on success */
95 int8_t state_set_mode(uint8_t mode)
98 STMCH_DEBUG("%s(): mode=%x ", __FUNCTION__, mode);
100 /* STMCH_DEBUG("%s(): l_deploy=%d l_harvest=%d " */
101 /* "r_deploy=%d r_harvest=%d eject=%d", */
102 /* __FUNCTION__, L_DEPLOY(mode), L_HARVEST(mode), */
103 /* R_DEPLOY(mode), R_HARVEST(mode), EJECT(mode)); */
108 /* check that state is the one in parameter and that state did not
110 static uint8_t state_want_exit(void)
114 /* force quit when CTRL-C is typed */
115 c = cmdline_getchar();
118 if (vt100_parser(&local_vt100, c) == KEY_CTRL_C)
120 printf_P(PSTR("CTRL-C\r\n"));
124 uint8_t state_get_status(void)
129 /* harvest balls from area */
130 static void state_do_harvest(void)
132 //state_debug_wait_key_pressed();
134 if (bd_get(&ballboard.roller.bd)) {
135 STMCH_DEBUG("%s(): roller blocked", __FUNCTION__);
138 bd_reset(&ballboard.roller.bd);
139 STMCH_DEBUG("%s(): roller restart", __FUNCTION__);
143 /* deduct ball count */
144 if (sensor_get(S_LOW_BARRIER) && !sensor_get(S_HIGH_BARRIER))
146 else if (sensor_get(S_LOW_BARRIER) && sensor_get(S_HIGH_BARRIER))
149 if (sensor_get(S_HIGH_BARRIER))
156 static void state_do_eject(void)
161 for (i = 0; i < 3; i ++) {
170 /* no more balls (sensor is heavily filtered) */
171 if (!sensor_get(S_LOW_BARRIER)) {
172 STMCH_DEBUG("%s(): no more balls", __FUNCTION__);
177 if ((time_get_us2() - us) > 1000UL * 1000UL) {
178 STMCH_DEBUG("%s(): eject timeout", __FUNCTION__);
184 if (bd_get(&ballboard.roller.bd)) {
193 STMCH_DEBUG("%s(): roller blocked", __FUNCTION__);
196 bd_reset(&ballboard.roller.bd);
197 STMCH_DEBUG("%s(): roller restart", __FUNCTION__);
201 /* main state machine */
202 void state_machine(void)
206 while (state_want_exit() == 0) {
208 if (state_mode != mode) {
210 STMCH_DEBUG("%s(): mode=%x ", __FUNCTION__, mode);
213 switch (state_mode) {
219 state_status = I2C_BALLBOARD_STATUS_F_READY;
223 state_status = I2C_BALLBOARD_STATUS_F_READY;
229 state_status = I2C_BALLBOARD_STATUS_F_READY;
235 state_status = I2C_BALLBOARD_STATUS_F_BUSY;
238 state_status = I2C_BALLBOARD_STATUS_F_READY;
239 state_mode = HARVEST;
260 void state_init(void)
262 vt100_init(&local_vt100);
264 state_status = I2C_BALLBOARD_STATUS_F_READY;