X-Git-Url: http://git.droids-corp.org/?p=aversive.git;a=blobdiff_plain;f=projects%2Fmicrob2010%2Fballboard%2Fstate.c;h=a322d17c8615eb885d5c429099f48cbbe90eb086;hp=9568be8997eadfa49efd1c89ef75a77876cb251b;hb=063af91cd38fceba6cbcf3a9b8abd4c48684d990;hpb=d913aa23d2f02c5a66b3c9930763fcbd9e963d52 diff --git a/projects/microb2010/ballboard/state.c b/projects/microb2010/ballboard/state.c index 9568be8..a322d17 100644 --- a/projects/microb2010/ballboard/state.c +++ b/projects/microb2010/ballboard/state.c @@ -1,6 +1,6 @@ -/* +/* * Copyright Droids Corporation (2009) - * + * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or @@ -56,7 +56,8 @@ static struct vt100 local_vt100; static volatile uint8_t state_mode; -static uint8_t ball_count; +static volatile uint8_t state_status; +static volatile uint8_t ball_count; /* short aliases */ #define INIT I2C_BALLBOARD_MODE_INIT @@ -82,6 +83,7 @@ uint8_t state_get_ball_count(void) return ball_count; } +#if 0 static void state_debug_wait_key_pressed(void) { if (!state_debug) @@ -89,11 +91,13 @@ static void state_debug_wait_key_pressed(void) printf_P(PSTR("press a key\r\n")); while (!cmdline_keypressed()); } +#endif /* set a new state, return 0 on success */ int8_t state_set_mode(uint8_t mode) { state_mode = mode; + STMCH_DEBUG("%s(): mode=%x ", __FUNCTION__, mode); /* STMCH_DEBUG("%s(): l_deploy=%d l_harvest=%d " */ /* "r_deploy=%d r_harvest=%d eject=%d", */ @@ -119,47 +123,118 @@ static uint8_t state_want_exit(void) return 0; } -uint8_t state_get_mode(void) +uint8_t state_get_status(void) { - return state_mode; + return state_status; } /* harvest balls from area */ static void state_do_harvest(void) { - state_debug_wait_key_pressed(); - roller_on(); + //state_debug_wait_key_pressed(); + + if (bd_get(&ballboard.roller.bd)) { + STMCH_DEBUG("%s(): roller blocked", __FUNCTION__); + roller_reverse(); + time_wait_ms(500); + bd_reset(&ballboard.roller.bd); + STMCH_DEBUG("%s(): roller restart", __FUNCTION__); + return; + } + + /* deduct ball count */ + if (sensor_get(S_LOW_BARRIER) && !sensor_get(S_HIGH_BARRIER)) + ball_count = 1; + else if (sensor_get(S_LOW_BARRIER) && sensor_get(S_HIGH_BARRIER)) + ball_count = 3; + + if (sensor_get(S_HIGH_BARRIER)) + roller_off(); + else + roller_on(); } /* eject balls */ static void state_do_eject(void) { - roller_reverse(); + uint8_t i, blocked; + microseconds us; + + for (i = 0; i < 3; i ++) { + + roller_reverse(); + + us = time_get_us2(); + blocked = 0; + + while (1) { + + /* no more balls (sensor is heavily filtered) */ + if (!sensor_get(S_LOW_BARRIER)) { + STMCH_DEBUG("%s(): no more balls", __FUNCTION__); + break; + } + + /* timeout */ + if ((time_get_us2() - us) > 1000UL * 1000UL) { + STMCH_DEBUG("%s(): eject timeout", __FUNCTION__); + blocked = 1; + break; + } + + /* blocking ! */ + if (bd_get(&ballboard.roller.bd)) { + blocked = 1; + break; + } + } + + if (!blocked) + break; + + STMCH_DEBUG("%s(): roller blocked", __FUNCTION__); + roller_on(); + time_wait_ms(500); + bd_reset(&ballboard.roller.bd); + STMCH_DEBUG("%s(): roller restart", __FUNCTION__); + } } /* main state machine */ void state_machine(void) { + uint8_t mode = 0; + while (state_want_exit() == 0) { + if (state_mode != mode) { + mode = state_mode; + STMCH_DEBUG("%s(): mode=%x ", __FUNCTION__, mode); + } + switch (state_mode) { case INIT: - state_mode = OFF; state_init(); + state_mode = OFF; + state_status = I2C_BALLBOARD_STATUS_F_READY; break; case OFF: + state_status = I2C_BALLBOARD_STATUS_F_READY; roller_off(); break; case HARVEST: + state_status = I2C_BALLBOARD_STATUS_F_READY; state_do_harvest(); break; case EJECT: - state_mode = HARVEST; + state_status = I2C_BALLBOARD_STATUS_F_BUSY; state_do_eject(); + state_status = I2C_BALLBOARD_STATUS_F_READY; + state_mode = HARVEST; break; default: @@ -172,5 +247,6 @@ void state_init(void) { vt100_init(&local_vt100); state_mode = 0; + state_status = I2C_BALLBOARD_STATUS_F_READY; ball_count = 0; }