vt100: include pgmspace.h as we use PROGMEM macro
[aversive.git] / projects / microb2010 / cobboard / state.c
index 4eef55f..9c6af8c 100644 (file)
@@ -66,8 +66,11 @@ static uint8_t cob_count;
 #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)
 {
@@ -96,6 +99,15 @@ static uint8_t state_no_cob_inside(void)
                !sensor_get(S_COB_INSIDE_R);
 }
 
+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)
@@ -131,6 +143,10 @@ uint8_t state_spicklemode_weak(uint8_t side)
 /* pack/deploy spickles depending on mode */
 static void spickle_prepare(uint8_t side)
 {
+       /* pack spickle if we are not in harvest mode */
+       if (!HARVEST(state_mode))
+               spickle_pack(side);
+
        /* we do nothing in mode no out */
        if (state_spicklemode_nomove(side))
                return;
@@ -208,6 +224,8 @@ uint8_t state_get_status(void)
 /* harvest cobs from area */
 static void state_do_harvest(uint8_t side)
 {
+       uint8_t i = 0;
+
        /* if there is no cob, return */
        if (cob_falling_edge(side) == 0)
                return;
@@ -226,11 +244,15 @@ static void state_do_harvest(uint8_t side)
        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
@@ -250,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");
@@ -259,6 +284,7 @@ static void state_do_harvest(uint8_t side)
                }
        }
 
+ cont:
        /* cob is inside, switch off roller */
        cobroller_off(side);
        cob_count ++;
@@ -283,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();
@@ -337,20 +392,42 @@ void state_machine(void)
                        state_mode = I2C_COBBOARD_MODE_HARVEST;
                }
 
-               /* 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 (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);
+               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();
+               }
+
+               if (KICKSTAND_DOWN(state_mode)) {
+                       state_status = I2C_COBBOARD_STATUS_KICKSTAND_DOWN;
+                       servo_carry_open();
+                       servo_door_open();
+                       shovel_kickstand_down();
                }
 
                /* eject */