i2c rework
[aversive.git] / projects / microb2010 / cobboard / state.c
index f9aefa6..7a1f047 100644 (file)
@@ -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
 #include "cmdline.h"
 #include "sensor.h"
 #include "actuator.h"
+#include "spickle.h"
+#include "shovel.h"
 #include "state.h"
 
 #define STMCH_DEBUG(args...) DEBUG(E_USER_ST_MACH, args)
 #define STMCH_NOTICE(args...) NOTICE(E_USER_ST_MACH, args)
 #define STMCH_ERROR(args...) ERROR(E_USER_ST_MACH, args)
 
-/* shorter aliases for this file */
-#define INIT               I2C_COBBOARD_MODE_INIT
-#define MANUAL             I2C_COBBOARD_MODE_MANUAL
-#define HARVEST            I2C_COBBOARD_MODE_HARVEST
-#define EXIT               I2C_COBBOARD_MODE_EXIT
-
-static struct i2c_cmd_cobboard_set_mode mainboard_command;
 static struct vt100 local_vt100;
-static volatile uint8_t prev_state;
-static volatile uint8_t changed = 0;
+static volatile uint8_t state_mode, lspickle, rspickle;
+static volatile uint8_t state_status;
+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))
 
 uint8_t state_debug = 0;
 
-void state_dump_sensors(void)
+uint8_t state_get_cob_count(void)
 {
-       STMCH_DEBUG("TODO\n");
+       return cob_count;
 }
 
-void state_debug_wait_key_pressed(void)
+static void state_debug_wait_key_pressed(void)
 {
        if (!state_debug)
                return;
@@ -80,84 +81,269 @@ void state_debug_wait_key_pressed(void)
        while(!cmdline_keypressed());
 }
 
+/* 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 is_deployed(uint8_t side)
+{
+       if (side == I2C_LEFT_SIDE)
+               return lspickle & I2C_COBBOARD_SPK_DEPLOY;
+       else
+               return rspickle & I2C_COBBOARD_SPK_DEPLOY;
+}
+
+static uint8_t is_autoharvest(uint8_t side)
+{
+       if (side == I2C_LEFT_SIDE)
+               return lspickle & I2C_COBBOARD_SPK_AUTOHARVEST;
+       else
+               return rspickle & I2C_COBBOARD_SPK_AUTOHARVEST;
+}
+
+/* pack/deploy spickles depending on mode */
+static void spickle_prepare(uint8_t side)
+{
+       if (cob_count >= 5)
+               spickle_pack(side);
+       else if (is_deployed(side) && !is_autoharvest(side))
+               spickle_mid(side);
+       else if (is_deployed(side) && is_autoharvest(side))
+               spickle_deploy(side);
+       else
+               spickle_pack(side);
+}
+
+void state_set_spickle(uint8_t side, uint8_t flags)
+{
+       if (side == I2C_LEFT_SIDE) {
+               /* preempt current action if 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 */
+               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(struct i2c_cmd_cobboard_set_mode *cmd)
+int8_t state_set_mode(uint8_t mode)
 {
-       changed = 1;
-       prev_state = mainboard_command.mode;
-       memcpy(&mainboard_command, cmd, sizeof(mainboard_command));
-       STMCH_DEBUG("%s mode=%d", __FUNCTION__, mainboard_command.mode);
+       state_mode = 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;
 }
 
 /* check that state is the one in parameter and that state did not
  * changed */
-uint8_t state_check(uint8_t mode)
+static uint8_t state_want_exit(void)
 {
        int16_t c;
-       if (mode != mainboard_command.mode)
-               return 0;
-
-       if (changed)
-               return 0;
 
        /* force quit when CTRL-C is typed */
        c = cmdline_getchar();
        if (c == -1)
-               return 1;
-       if (vt100_parser(&local_vt100, c) == KEY_CTRL_C) {
-               mainboard_command.mode = EXIT;
                return 0;
-       }
-       return 1;
+       if (vt100_parser(&local_vt100, c) == KEY_CTRL_C)
+               return 1;
+       printf_P(PSTR("CTRL-C\r\n"));
+       return 0;
 }
 
 uint8_t state_get_mode(void)
 {
-       return mainboard_command.mode;
+       return state_mode;
 }
 
-/* manual mode, arm position is sent from mainboard */
-static void state_do_manual(void)
+uint8_t state_get_status(void)
 {
-       if (!state_check(MANUAL))
-               return;
-       STMCH_DEBUG("%s mode=%d", __FUNCTION__, state_get_mode());
-       while (state_check(MANUAL));
+       return state_status;
 }
 
-/* init mode */
-static void state_do_init(void)
+/* harvest cobs from area */
+static void state_do_harvest(uint8_t side)
 {
-       if (!state_check(INIT))
+       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 (cob_falling_edge(side) == 0)
+               return;
+
+       STMCH_DEBUG("start");
+
+       /* eat the cob */
+       spickle_pack(side);
+
+       time_wait_ms(250);
+       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()) {
+                       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;
-       state_init();
-       STMCH_DEBUG("%s mode=%d", __FUNCTION__, state_get_mode());
-       while (state_check(INIT));
+
+       /* redeploy the spickle */
+       spickle_deploy(side);
+       state_debug_wait_key_pressed();
+
+       /* let the loaded cobs go */
+       servo_door_block();
+       servo_carry_open();
+       time_wait_ms(100);
+       state_debug_wait_key_pressed();
+
+       /* store it */
+       shovel_up();
+
+       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();
+       servo_door_close();
+       time_wait_ms(200);
+       state_debug_wait_key_pressed();
+
+       shovel_down();
+
+       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");
 }
 
-/* harvest columns elts from area */
-static void state_do_harvest(void)
+/* eject cobs */
+static void state_do_eject(void)
 {
-       if (!state_check(HARVEST))
-               return;
-       STMCH_DEBUG("%s mode=%d", __FUNCTION__, state_get_mode());
-       while (state_check(HARVEST));
+       state_status = I2C_COBBOARD_STATUS_EJECT;
+       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 */
 void state_machine(void)
 {
-       while (state_get_mode() != EXIT) {
-               changed = 0;
-               state_do_init();
-               state_do_manual();
-               state_do_harvest();
+       while (state_want_exit() == 0) {
+
+               state_status = I2C_COBBOARD_STATUS_READY;
+
+               /* init */
+               if (INIT(state_mode)) {
+                       state_mode &= (~I2C_COBBOARD_MODE_INIT);
+                       state_init();
+               }
+
+               /* 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);
+               }
+
+               /* eject */
+               if (EJECT(state_mode)) {
+                       state_mode &= (~I2C_COBBOARD_MODE_EJECT);
+                       state_do_eject();
+               }
        }
+       state_status = I2C_COBBOARD_STATUS_OFF;
 }
 
 void state_init(void)
 {
        vt100_init(&local_vt100);
-       mainboard_command.mode = HARVEST;
-       cobboard.cob_count = 0;
+       shovel_down();
+       servo_door_close();
+       servo_carry_close();
+       spickle_pack(I2C_LEFT_SIDE);
+       spickle_pack(I2C_RIGHT_SIDE);
+       state_mode = 0;
+       cob_count = 0;
+       state_status = I2C_COBBOARD_STATUS_OFF;
 }