X-Git-Url: http://git.droids-corp.org/?p=aversive.git;a=blobdiff_plain;f=projects%2Fmicrob2010%2Fcobboard%2Fstate.c;h=cbbc8293b5d28ffeb83d3afaf095711cd2799fab;hp=9d082bacf7aabb9b606b654d00fd635bac4e0e12;hb=ba4ad67d2bd3457a12615439a7e2f748cfb7bcde;hpb=fa8546ea39c7442ad3bf5a822a72a2b50a41045d diff --git a/projects/microb2010/cobboard/state.c b/projects/microb2010/cobboard/state.c index 9d082ba..cbbc829 100644 --- a/projects/microb2010/cobboard/state.c +++ b/projects/microb2010/cobboard/state.c @@ -57,28 +57,18 @@ #define STMCH_ERROR(args...) ERROR(E_USER_ST_MACH, args) static struct vt100 local_vt100; -static volatile uint8_t state_mode; +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 L_DEPLOY(mode) (!!((mode) & I2C_COBBOARD_MODE_L_DEPLOY)) -#define R_DEPLOY(mode) (!!((mode) & I2C_COBBOARD_MODE_R_DEPLOY)) -#define DEPLOY(side, mode) ((side) == I2C_LEFT_SIDE ? L_DEPLOY(mode) : R_DEPLOY(mode)) -#define L_HARVEST(mode) (!!((mode) & I2C_COBBOARD_MODE_L_HARVEST)) -#define R_HARVEST(mode) (!!((mode) & I2C_COBBOARD_MODE_R_HARVEST)) -#define HARVEST(side, mode) ((side) == I2C_LEFT_SIDE ? L_HARVEST(mode) : R_HARVEST(mode)) -#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) uint8_t state_debug = 0; -#if 0 -static void state_dump_sensors(void) -{ - STMCH_DEBUG("TODO\n"); -} -#endif - uint8_t state_get_cob_count(void) { return cob_count; @@ -92,50 +82,93 @@ static void state_debug_wait_key_pressed(void) while(!cmdline_keypressed()); } -/* return true if cob is present */ -static uint8_t state_cob_present(uint8_t side) +/* return true if the cob is correctly inside */ +static uint8_t state_cob_inside(void) +{ + return sensor_get(S_COB_INSIDE_L) && + sensor_get(S_COB_INSIDE_R); +} + +/* return true if there is no cob correctly inside */ +static uint8_t state_no_cob_inside(void) +{ + return !sensor_get(S_COB_INSIDE_L) && + !sensor_get(S_COB_INSIDE_R); +} + +static uint8_t state_spicklemode_deployed(uint8_t side) { if (side == I2C_LEFT_SIDE) - return sensor_get(S_LCOB); + return lspickle & I2C_COBBOARD_SPK_DEPLOY; else - return sensor_get(S_RCOB); + return rspickle & I2C_COBBOARD_SPK_DEPLOY; } -/* return the detected color of the cob (only valid if present) */ -static uint8_t state_cob_color(uint8_t side) +static uint8_t state_spicklemode_autoharvest(uint8_t side) { - /* XXX no color sensor for now */ if (side == I2C_LEFT_SIDE) - return I2C_COB_WHITE; + return lspickle & I2C_COBBOARD_SPK_AUTOHARVEST; else - return I2C_COB_WHITE; + return rspickle & I2C_COBBOARD_SPK_AUTOHARVEST; } -/* return true if the cob is correctly inside */ -static uint8_t state_cob_inside(void) +static uint8_t state_spicklemode_nomove(uint8_t side) { - return sensor_get(S_COB_INSIDE_L) && - sensor_get(S_COB_INSIDE_R); + if (side == I2C_LEFT_SIDE) + return lspickle & I2C_COBBOARD_SPK_NO_MOVE; + else + return rspickle & I2C_COBBOARD_SPK_NO_MOVE; } -/* return true if there is no cob correctly inside */ -static uint8_t state_no_cob_inside(void) +/* pack/deploy spickles depending on mode */ +static void spickle_prepare(uint8_t side) { - return !sensor_get(S_COB_INSIDE_L) && - !sensor_get(S_COB_INSIDE_R); + /* 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); +} + +void state_set_spickle(uint8_t side, uint8_t flags) +{ + if (side == I2C_LEFT_SIDE) { + /* 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 { + /* 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); + rspickle = flags; + } } /* 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); + 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 @@ -159,44 +192,78 @@ uint8_t state_get_mode(void) return state_mode; } +uint8_t state_get_status(void) +{ + return state_status; +} + /* harvest cobs from area */ static void state_do_harvest(uint8_t side) { /* if there is no cob, return */ - if (state_cob_present(side)) + if (cob_falling_edge(side) == 0) return; - /* if it is black, nothing to do */ - if (state_cob_color(side) == I2C_COB_BLACK) - 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()) { - printf_P(PSTR("NO COB\r\n")); + STMCH_DEBUG("no cob"); + return; + } + STMCH_DEBUG("bad cob state"); + + /* while cob is not correctly placed try to extract + * it as much as we can */ + while (state_cob_inside() == 0 && + state_no_cob_inside() == 0) { + uint8_t cpt = 0; + if (cpt == 0) + cobroller_reverse(side); + else if (cpt == 1) + cobroller_off(side); + else if (cpt == 2) + cobroller_on(side); + cpt ++; + cpt %= 3; + shovel_mid(); + time_wait_ms(250); + shovel_down(); + time_wait_ms(250); + } + + STMCH_DEBUG("cob removed"); + if (state_no_cob_inside()) { + STMCH_DEBUG("no cob"); return; } - printf_P(PSTR("BAD COB - press a key\r\n")); - while(!cmdline_keypressed()); } /* 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 */ @@ -208,10 +275,11 @@ static void state_do_harvest(uint8_t side) /* store it */ shovel_up(); - if (WAIT_COND_OR_TIMEOUT(shovel_is_up(), 400) == 0) { - BRAKE_ON(); - printf_P(PSTR("BLOCKED\r\n")); - while(!cmdline_keypressed()); + while (WAIT_COND_OR_TIMEOUT(shovel_is_up(), 600) == 0) { + STMCH_DEBUG("shovel blocked"); + shovel_down(); + time_wait_ms(250); + shovel_up(); } state_debug_wait_key_pressed(); @@ -224,10 +292,11 @@ static void state_do_harvest(uint8_t side) shovel_down(); - if (WAIT_COND_OR_TIMEOUT(shovel_is_down(), 400) == 0) { - BRAKE_ON(); - printf_P(PSTR("BLOCKED\r\n")); - while(!cmdline_keypressed()); + while (WAIT_COND_OR_TIMEOUT(shovel_is_down(), 400) == 0) { + STMCH_DEBUG("shovel blocked"); + shovel_up(); + time_wait_ms(250); + shovel_down(); } STMCH_DEBUG("end"); @@ -236,6 +305,7 @@ static void state_do_harvest(uint8_t side) /* eject cobs */ static void state_do_eject(void) { + state_status = I2C_COBBOARD_STATUS_EJECT; cob_count = 0; shovel_mid(); servo_carry_open(); @@ -251,48 +321,37 @@ void state_machine(void) { while (state_want_exit() == 0) { + state_status = I2C_COBBOARD_STATUS_READY; + /* init */ if (INIT(state_mode)) { - state_mode &= (~I2C_COBBOARD_MODE_INIT); state_init(); + state_mode = I2C_COBBOARD_MODE_HARVEST; } + /* pack/deply spickles, enable/disable roller */ cobroller_off(I2C_LEFT_SIDE); cobroller_off(I2C_RIGHT_SIDE); - - /* pack/deply spickles, enable/disable roller */ - if (cob_count >= 5) - spickle_pack(I2C_LEFT_SIDE); - else if (L_DEPLOY(state_mode) && !L_HARVEST(state_mode)) - spickle_mid(I2C_LEFT_SIDE); - else if (L_DEPLOY(state_mode) && L_HARVEST(state_mode)) - spickle_deploy(I2C_LEFT_SIDE); - else - spickle_pack(I2C_LEFT_SIDE); - - if (cob_count >= 5) - spickle_pack(I2C_RIGHT_SIDE); - else if (R_DEPLOY(state_mode) && !R_HARVEST(state_mode)) - spickle_mid(I2C_RIGHT_SIDE); - else if (R_DEPLOY(state_mode) && R_HARVEST(state_mode)) - spickle_deploy(I2C_RIGHT_SIDE); - else - spickle_pack(I2C_RIGHT_SIDE); + spickle_prepare(I2C_LEFT_SIDE); + spickle_prepare(I2C_RIGHT_SIDE); /* harvest */ if (cob_count < 5) { - if (L_DEPLOY(state_mode) && L_HARVEST(state_mode)) + if (state_spicklemode_deployed(I2C_LEFT_SIDE) && + state_spicklemode_autoharvest(I2C_LEFT_SIDE)) state_do_harvest(I2C_LEFT_SIDE); - if (R_DEPLOY(state_mode) && R_HARVEST(state_mode)) + if (state_spicklemode_deployed(I2C_RIGHT_SIDE) && + state_spicklemode_autoharvest(I2C_RIGHT_SIDE)) state_do_harvest(I2C_RIGHT_SIDE); } /* eject */ if (EJECT(state_mode)) { - state_mode &= (~I2C_COBBOARD_MODE_EJECT); + state_mode = I2C_COBBOARD_MODE_HARVEST; state_do_eject(); } } + state_status = I2C_COBBOARD_STATUS_OFF; } void state_init(void) @@ -305,4 +364,5 @@ void state_init(void) spickle_pack(I2C_RIGHT_SIDE); state_mode = 0; cob_count = 0; + state_status = I2C_COBBOARD_STATUS_OFF; }