X-Git-Url: http://git.droids-corp.org/?p=aversive.git;a=blobdiff_plain;f=projects%2Fmicrob2010%2Fcobboard%2Fstate.c;h=d310168dab210cbf05fc7f4601785d8638a68e2d;hp=33084050186c3e0e9406288604d5c0dd3e98812a;hb=78150017ab8c5615af414df706a0525fe7c262ae;hpb=0c4732c1e255ea56aa766b772dfdb6be49620830 diff --git a/projects/microb2010/cobboard/state.c b/projects/microb2010/cobboard/state.c index 3308405..d310168 100644 --- a/projects/microb2010/cobboard/state.c +++ b/projects/microb2010/cobboard/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 @@ -58,6 +58,7 @@ static struct vt100 local_vt100; static volatile uint8_t state_mode; +static volatile uint8_t state_status; static uint8_t cob_count; /* short aliases */ @@ -72,13 +73,6 @@ static uint8_t cob_count; 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,33 +86,31 @@ 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) { - if (side == I2C_LEFT_SIDE) - return sensor_get(S_LCOB); - else - /* XXX */ - // return sensor_get(S_LCOB); - return 0; + return sensor_get(S_COB_INSIDE_L) && + sensor_get(S_COB_INSIDE_R); } -/* return the detected color of the cob (only valid if present) */ -static uint8_t state_cob_color(uint8_t side) +/* return true if there is no cob correctly inside */ +static uint8_t state_no_cob_inside(void) { - if (side == I2C_LEFT_SIDE) - return I2C_COB_WHITE; - else - /* XXX */ - // return sensor_get(S_LCOB); - return 0; + return !sensor_get(S_COB_INSIDE_L) && + !sensor_get(S_COB_INSIDE_R); } -/* return true if the cob is correctly inside */ -static uint8_t state_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); + if (cob_count >= 5) + spickle_pack(side); + else if (DEPLOY(side, state_mode) && !HARVEST(side, state_mode)) + spickle_mid(side); + else if (DEPLOY(side, state_mode) && HARVEST(side, state_mode)) + spickle_deploy(side); + else + spickle_pack(side); } /* set a new state, return 0 on success */ @@ -126,6 +118,12 @@ int8_t state_set_mode(uint8_t mode) { state_mode = mode; + /* preempt current action if not busy */ + if (state_status != I2C_COBBOARD_STATUS_LBUSY) + spickle_prepare(I2C_LEFT_SIDE); + if (state_status != I2C_COBBOARD_STATUS_RBUSY) + spickle_prepare(I2C_RIGHT_SIDE); + /* STMCH_DEBUG("%s(): l_deploy=%d l_harvest=%d " */ /* "r_deploy=%d r_harvest=%d eject=%d", */ /* __FUNCTION__, L_DEPLOY(mode), L_HARVEST(mode), */ @@ -155,58 +153,117 @@ 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) { - uint16_t delay; + if (side == I2C_LEFT_SIDE) + state_status = I2C_COBBOARD_STATUS_LBUSY; + else + state_status = I2C_COBBOARD_STATUS_RBUSY; /* 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; + STMCH_DEBUG("start"); /* eat the cob */ spickle_pack(side); - /* xxx */ + time_wait_ms(250); - left_cobroller_on(); - delay = spickle_get_pack_delay(side); + cobroller_on(side); - WAIT_COND_OR_TIMEOUT(state_cob_inside(), delay); + /* check that cob is correctly in place */ + if (WAIT_COND_OR_TIMEOUT(state_cob_inside(), 750) == 0) { + if (state_no_cob_inside()) { + 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; + } + } + + /* cob is inside, switch off roller */ + cobroller_off(side); + cob_count ++; + + /* last cob, nothing to do */ + if (cob_count == 5) + return; /* redeploy the spickle */ spickle_deploy(side); state_debug_wait_key_pressed(); - /* let the cob go */ + /* let the loaded cobs go */ + servo_door_block(); servo_carry_open(); - wait_ms(300); /* XXX */ + time_wait_ms(100); state_debug_wait_key_pressed(); - cob_count ++; - /* store it */ shovel_up(); - wait_ms(200); + + 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(); /* close the carry servos */ servo_carry_close(); - wait_ms(300); /* XXX */ + servo_door_close(); + time_wait_ms(200); state_debug_wait_key_pressed(); shovel_down(); - left_cobroller_off(); - state_debug_wait_key_pressed(); - time_wait_ms(500); + + 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"); } /* eject cobs */ static void state_do_eject(void) { + state_status = I2C_COBBOARD_STATUS_EJECT; cob_count = 0; shovel_mid(); servo_carry_open(); @@ -222,6 +279,8 @@ 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); @@ -229,29 +288,18 @@ void state_machine(void) } /* pack/deply spickles, enable/disable roller */ - if (L_DEPLOY(state_mode)) { - spickle_deploy(I2C_LEFT_SIDE); - //left_cobroller_on(); - left_cobroller_off(); - } - else { - spickle_pack(I2C_LEFT_SIDE); - left_cobroller_off(); - } - if (R_DEPLOY(state_mode)) { - spickle_deploy(I2C_RIGHT_SIDE); - right_cobroller_on(); - } - else { - spickle_pack(I2C_RIGHT_SIDE); - right_cobroller_off(); - } + cobroller_off(I2C_LEFT_SIDE); + cobroller_off(I2C_RIGHT_SIDE); + spickle_prepare(I2C_LEFT_SIDE); + spickle_prepare(I2C_RIGHT_SIDE); /* harvest */ - if (L_DEPLOY(state_mode) && L_HARVEST(state_mode)) - state_do_harvest(I2C_LEFT_SIDE); - if (R_DEPLOY(state_mode) && R_HARVEST(state_mode)) - state_do_harvest(I2C_RIGHT_SIDE); + if (cob_count < 5) { + if (L_DEPLOY(state_mode) && L_HARVEST(state_mode)) + state_do_harvest(I2C_LEFT_SIDE); + if (R_DEPLOY(state_mode) && R_HARVEST(state_mode)) + state_do_harvest(I2C_RIGHT_SIDE); + } /* eject */ if (EJECT(state_mode)) { @@ -259,6 +307,7 @@ void state_machine(void) state_do_eject(); } } + state_status = I2C_COBBOARD_STATUS_READY; } void state_init(void) @@ -271,4 +320,5 @@ void state_init(void) spickle_pack(I2C_RIGHT_SIDE); state_mode = 0; cob_count = 0; + state_status = I2C_COBBOARD_STATUS_READY; }