X-Git-Url: http://git.droids-corp.org/?p=aversive.git;a=blobdiff_plain;f=projects%2Fmicrob2010%2Fcobboard%2Fstate.c;h=9c6af8cfee3a964900c890d4fd3160276eae982a;hp=7a1f047997df50deab9c3f20ca05a343165b163f;hb=HEAD;hpb=19cd41f7920628a962928580cd1618a1ee5750aa diff --git a/projects/microb2010/cobboard/state.c b/projects/microb2010/cobboard/state.c index 7a1f047..9c6af8c 100644 --- a/projects/microb2010/cobboard/state.c +++ b/projects/microb2010/cobboard/state.c @@ -59,14 +59,18 @@ static struct vt100 local_vt100; static volatile uint8_t state_mode, lspickle, rspickle; static volatile uint8_t state_status; +static volatile uint8_t state_i2c_ignore = 0; static uint8_t cob_count; /* short aliases */ -#define HARVEST(mode) (!!((mode) & I2C_COBBOARD_MODE_HARVEST)) -#define EJECT(mode) (!!((mode) & I2C_COBBOARD_MODE_EJECT)) -#define INIT(mode) (!!((mode) & I2C_COBBOARD_MODE_INIT)) +#define INIT(mode) ((mode) == I2C_COBBOARD_MODE_INIT) +#define HARVEST(mode) ((mode) == I2C_COBBOARD_MODE_HARVEST) +#define EJECT(mode) ((mode) == I2C_COBBOARD_MODE_EJECT) +#define KICKSTAND_UP(mode) ((mode) == I2C_COBBOARD_MODE_KICKSTAND_UP) +#define KICKSTAND_DOWN(mode) ((mode) == I2C_COBBOARD_MODE_KICKSTAND_DOWN) uint8_t state_debug = 0; +static uint8_t state_cob_partial = 0; uint8_t state_get_cob_count(void) { @@ -95,7 +99,16 @@ static uint8_t state_no_cob_inside(void) !sensor_get(S_COB_INSIDE_R); } -static uint8_t is_deployed(uint8_t side) +static uint8_t state_cob_inside_enhanced(void) +{ + if (sensor_get(S_COB_INSIDE_L)) + state_cob_partial = 1; + if (sensor_get(S_COB_INSIDE_R)) + state_cob_partial = 1; + return state_cob_inside(); +} + +static uint8_t state_spicklemode_deployed(uint8_t side) { if (side == I2C_LEFT_SIDE) return lspickle & I2C_COBBOARD_SPK_DEPLOY; @@ -103,7 +116,7 @@ static uint8_t is_deployed(uint8_t side) return rspickle & I2C_COBBOARD_SPK_DEPLOY; } -static uint8_t is_autoharvest(uint8_t side) +static uint8_t state_spicklemode_autoharvest(uint8_t side) { if (side == I2C_LEFT_SIDE) return lspickle & I2C_COBBOARD_SPK_AUTOHARVEST; @@ -111,14 +124,34 @@ static uint8_t is_autoharvest(uint8_t side) return rspickle & I2C_COBBOARD_SPK_AUTOHARVEST; } +static uint8_t state_spicklemode_nomove(uint8_t side) +{ + if (side == I2C_LEFT_SIDE) + return lspickle & I2C_COBBOARD_SPK_NO_MOVE; + else + return rspickle & I2C_COBBOARD_SPK_NO_MOVE; +} + +uint8_t state_spicklemode_weak(uint8_t side) +{ + if (side == I2C_LEFT_SIDE) + return lspickle & I2C_COBBOARD_SPK_WEAK; + else + return rspickle & I2C_COBBOARD_SPK_WEAK; +} + /* pack/deploy spickles depending on mode */ static void spickle_prepare(uint8_t side) { - if (cob_count >= 5) + /* pack spickle if we are not in harvest mode */ + if (!HARVEST(state_mode)) spickle_pack(side); - else if (is_deployed(side) && !is_autoharvest(side)) - spickle_mid(side); - else if (is_deployed(side) && is_autoharvest(side)) + + /* we do nothing in mode no out */ + if (state_spicklemode_nomove(side)) + return; + + if (state_spicklemode_deployed(side)) spickle_deploy(side); else spickle_pack(side); @@ -127,14 +160,16 @@ static void spickle_prepare(uint8_t side) void state_set_spickle(uint8_t side, uint8_t flags) { if (side == I2C_LEFT_SIDE) { - /* preempt current action if not busy */ + /* the PACK command preempts the current action if the + * arm is not busy */ if (lspickle != 0 && flags == 0 && state_status != I2C_COBBOARD_STATUS_LBUSY) spickle_prepare(I2C_LEFT_SIDE); lspickle = flags; } else { - /* preempt current action if not busy */ + /* the PACK command preempts the current action if the + * arm is not busy */ if (rspickle != 0 && flags == 0 && state_status != I2C_COBBOARD_STATUS_RBUSY) spickle_prepare(I2C_RIGHT_SIDE); @@ -146,13 +181,18 @@ void state_set_spickle(uint8_t side, uint8_t flags) int8_t state_set_mode(uint8_t mode) { state_mode = mode; + STMCH_DEBUG("%s(): mode=%x", __FUNCTION__, mode); + return 0; +} -/* STMCH_DEBUG("%s(): l_deploy=%d l_harvest=%d " */ -/* "r_deploy=%d r_harvest=%d eject=%d", */ -/* __FUNCTION__, L_DEPLOY(mode), L_HARVEST(mode), */ -/* R_DEPLOY(mode), R_HARVEST(mode), EJECT(mode)); */ +void state_set_i2c_ignore(uint8_t val) +{ + state_i2c_ignore = val; +} - return 0; +uint8_t state_get_i2c_ignore(void) +{ + return state_i2c_ignore; } /* check that state is the one in parameter and that state did not @@ -184,29 +224,35 @@ uint8_t state_get_status(void) /* harvest cobs from area */ static void state_do_harvest(uint8_t side) { - if (side == I2C_LEFT_SIDE) - state_status = I2C_COBBOARD_STATUS_LBUSY; - else - state_status = I2C_COBBOARD_STATUS_RBUSY; + uint8_t i = 0; /* if there is no cob, return */ if (cob_falling_edge(side) == 0) return; + if (side == I2C_LEFT_SIDE) + state_status = I2C_COBBOARD_STATUS_LBUSY; + else + state_status = I2C_COBBOARD_STATUS_RBUSY; + STMCH_DEBUG("start"); /* eat the cob */ spickle_pack(side); - time_wait_ms(250); + time_wait_ms(200); cobroller_on(side); /* check that cob is correctly in place */ - if (WAIT_COND_OR_TIMEOUT(state_cob_inside(), 750) == 0) { - if (state_no_cob_inside()) { + state_cob_partial = 0; + if (WAIT_COND_OR_TIMEOUT(state_cob_inside_enhanced(), 750) == 0) { + if (state_no_cob_inside() && state_cob_partial == 0) { STMCH_DEBUG("no cob"); return; } + else if (state_no_cob_inside() && state_cob_partial == 1) + goto cont; + STMCH_DEBUG("bad cob state"); /* while cob is not correctly placed try to extract @@ -226,6 +272,9 @@ static void state_do_harvest(uint8_t side) time_wait_ms(250); shovel_down(); time_wait_ms(250); + + if (EJECT(state_mode)) + return; } STMCH_DEBUG("cob removed"); @@ -235,16 +284,20 @@ static void state_do_harvest(uint8_t side) } } + cont: /* cob is inside, switch off roller */ cobroller_off(side); cob_count ++; + state_debug_wait_key_pressed(); + /* last cob, nothing to do */ if (cob_count == 5) return; - /* redeploy the spickle */ - spickle_deploy(side); + /* redeploy the spickle if there is a black cob */ + if (!state_spicklemode_nomove(side)) + spickle_deploy(side); state_debug_wait_key_pressed(); /* let the loaded cobs go */ @@ -256,11 +309,40 @@ static void state_do_harvest(uint8_t side) /* store it */ shovel_up(); + i = 0; while (WAIT_COND_OR_TIMEOUT(shovel_is_up(), 600) == 0) { STMCH_DEBUG("shovel blocked"); shovel_down(); + + if (i == 4) + break; + + /* if eject command is received, force exit */ + if (EJECT(state_mode)) + return; + time_wait_ms(250); shovel_up(); + i ++; + } + + /* bad state, try to eject to cobs */ + if (i == 4) { + servo_door_open(); + shovel_mid(); + + while (WAIT_COND_OR_TIMEOUT(shovel_is_mid(), 600) == 0) { + STMCH_DEBUG("ejecting cobs"); + + shovel_down(); + time_wait_ms(250); + if (state_no_cob_inside()) { + servo_door_close(); + cob_count = 0; + return; + } + shovel_mid(); + } } state_debug_wait_key_pressed(); @@ -306,29 +388,51 @@ void state_machine(void) /* init */ if (INIT(state_mode)) { - state_mode &= (~I2C_COBBOARD_MODE_INIT); state_init(); + state_mode = I2C_COBBOARD_MODE_HARVEST; + } + + if (HARVEST(state_mode)) { + /* init for each loop */ + shovel_down(); + servo_carry_close(); + servo_door_close(); + + /* pack/deply spickles, enable/disable roller */ + cobroller_off(I2C_LEFT_SIDE); + cobroller_off(I2C_RIGHT_SIDE); + spickle_prepare(I2C_LEFT_SIDE); + spickle_prepare(I2C_RIGHT_SIDE); + + /* harvest if not many cobs */ + if (cob_count < 5) { + if (state_spicklemode_deployed(I2C_LEFT_SIDE) && + state_spicklemode_autoharvest(I2C_LEFT_SIDE)) + state_do_harvest(I2C_LEFT_SIDE); + if (state_spicklemode_deployed(I2C_RIGHT_SIDE) && + state_spicklemode_autoharvest(I2C_RIGHT_SIDE)) + state_do_harvest(I2C_RIGHT_SIDE); + } + } + + /* help to climb the hill */ + if (KICKSTAND_UP(state_mode)) { + state_status = I2C_COBBOARD_STATUS_KICKSTAND_UP; + servo_carry_open(); + servo_door_open(); + shovel_kickstand_up(); } - /* pack/deply spickles, enable/disable roller */ - cobroller_off(I2C_LEFT_SIDE); - cobroller_off(I2C_RIGHT_SIDE); - spickle_prepare(I2C_LEFT_SIDE); - spickle_prepare(I2C_RIGHT_SIDE); - - /* harvest */ - if (cob_count < 5) { - if ((lspickle & I2C_COBBOARD_SPK_DEPLOY) && - (lspickle & I2C_COBBOARD_SPK_DEPLOY)) - state_do_harvest(I2C_LEFT_SIDE); - if ((rspickle & I2C_COBBOARD_SPK_DEPLOY) && - (rspickle & I2C_COBBOARD_SPK_DEPLOY)) - state_do_harvest(I2C_RIGHT_SIDE); + if (KICKSTAND_DOWN(state_mode)) { + state_status = I2C_COBBOARD_STATUS_KICKSTAND_DOWN; + servo_carry_open(); + servo_door_open(); + shovel_kickstand_down(); } /* eject */ if (EJECT(state_mode)) { - state_mode &= (~I2C_COBBOARD_MODE_EJECT); + state_mode = I2C_COBBOARD_MODE_HARVEST; state_do_eject(); } }