oa
[aversive.git] / projects / microb2010 / cobboard / state.c
index 4c9651e..3308405 100644 (file)
@@ -114,15 +114,23 @@ static uint8_t state_cob_color(uint8_t side)
                return 0;
 }
 
+/* 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);
+}
+
 /* set a new state, return 0 on success */
 int8_t state_set_mode(uint8_t mode)
 {
        state_mode = mode;
-       /* XXX synchrounous pack here */
-       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));
+
+/*     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)); */
+
        return 0;
 }
 
@@ -138,6 +146,7 @@ static uint8_t state_want_exit(void)
                return 0;
        if (vt100_parser(&local_vt100, c) == KEY_CTRL_C)
                return 1;
+       printf_P(PSTR("CTRL-C\r\n"));
        return 0;
 }
 
@@ -154,29 +163,45 @@ static void state_do_harvest(uint8_t side)
        /* if there is no cob, return */
        if (state_cob_present(side))
                return;
-               
+
        /* if it is black, nothing to do */
        if (state_cob_color(side) == I2C_COB_BLACK)
                return;
 
        /* eat the cob */
        spickle_pack(side);
-       state_debug_wait_key_pressed();
+       /* xxx */
+       time_wait_ms(250);
+       left_cobroller_on();
        delay = spickle_get_pack_delay(side);
-       time_wait_ms(delay);
+
+       WAIT_COND_OR_TIMEOUT(state_cob_inside(), delay);
 
        /* redeploy the spickle */
        spickle_deploy(side);
        state_debug_wait_key_pressed();
 
+       /* let the cob go */
+       servo_carry_open();
+       wait_ms(300); /* XXX */
+       state_debug_wait_key_pressed();
+
        cob_count ++;
 
        /* store it */
        shovel_up();
        wait_ms(200);
        state_debug_wait_key_pressed();
+
+       /* close the carry servos */
+       servo_carry_close();
+       wait_ms(300); /* XXX */
+       state_debug_wait_key_pressed();
+
        shovel_down();
+       left_cobroller_off();
        state_debug_wait_key_pressed();
+       time_wait_ms(500);
 }
 
 /* eject cobs */
@@ -184,8 +209,12 @@ static void state_do_eject(void)
 {
        cob_count = 0;
        shovel_mid();
+       servo_carry_open();
+       servo_door_open();
        time_wait_ms(2000);
        shovel_down();
+       servo_door_close();
+       servo_carry_close();
 }
 
 /* main state machine */
@@ -199,15 +228,24 @@ void state_machine(void)
                        state_init();
                }
 
-               /* pack spickles */
-               if (L_DEPLOY(state_mode))
+               /* pack/deply spickles, enable/disable roller */
+               if (L_DEPLOY(state_mode)) {
                        spickle_deploy(I2C_LEFT_SIDE);
-               else
+                       //left_cobroller_on();
+                       left_cobroller_off();
+               }
+               else {
                        spickle_pack(I2C_LEFT_SIDE);
-               if (R_DEPLOY(state_mode))
+                       left_cobroller_off();
+               }
+               if (R_DEPLOY(state_mode)) {
                        spickle_deploy(I2C_RIGHT_SIDE);
-               else
+                       right_cobroller_on();
+               }
+               else {
                        spickle_pack(I2C_RIGHT_SIDE);
+                       right_cobroller_off();
+               }
 
                /* harvest */
                if (L_DEPLOY(state_mode) && L_HARVEST(state_mode))
@@ -227,6 +265,8 @@ void state_init(void)
 {
        vt100_init(&local_vt100);
        shovel_down();
+       servo_door_close();
+       servo_carry_close();
        spickle_pack(I2C_LEFT_SIDE);
        spickle_pack(I2C_RIGHT_SIDE);
        state_mode = 0;