high level strat
[aversive.git] / projects / microb2010 / mainboard / strat.c
index a5a952c..e08c4af 100644 (file)
@@ -110,8 +110,9 @@ void strat_init(void)
        time_reset();
        interrupt_traj_reset();
 
-       i2c_cobboard_mode_harvest(I2C_LEFT_SIDE);
-       i2c_cobboard_mode_harvest(I2C_RIGHT_SIDE);
+       i2c_cobboard_set_mode(I2C_COBBOARD_MODE_HARVEST);
+       i2c_cobboard_harvest(I2C_LEFT_SIDE);
+       i2c_cobboard_harvest(I2C_RIGHT_SIDE);
        i2c_ballboard_set_mode(I2C_BALLBOARD_MODE_HARVEST);
 
        /* used in strat_base for END_TIMER */
@@ -198,15 +199,15 @@ static uint8_t strat_beginning(void)
        /* half turn */
        trajectory_goto_xy_abs(&mainboard.traj, 2625, COLOR_Y(1847));
        err = wait_traj_end(END_INTR|END_TRAJ);
-       i2c_cobboard_mode_pack(I2C_LEFT_SIDE);
-       i2c_cobboard_mode_pack(I2C_RIGHT_SIDE);
+       i2c_cobboard_pack(I2C_LEFT_SIDE);
+       i2c_cobboard_pack(I2C_RIGHT_SIDE);
        trajectory_a_rel(&mainboard.traj, COLOR_A(180));
        err = wait_traj_end(END_INTR|END_TRAJ);
 
        /* cob ejection */
        trajectory_d_rel(&mainboard.traj, -100);
        err = wait_traj_end(END_INTR|END_TRAJ);
-       i2c_cobboard_mode_eject();
+       i2c_cobboard_set_mode(I2C_COBBOARD_MODE_EJECT);
        time_wait_ms(2000);
 
        trajectory_hardstop(&mainboard.traj);
@@ -223,12 +224,67 @@ static uint8_t strat_beginning(void)
        } while (0)
 
 
+/* return true if we need to grab some more elements */
+static uint8_t need_more_elements(void)
+{
+       if (time_get_s() <= 75) {
+               /* we have enough time left */
+               if (get_ball_count() >= 4)
+                       return 0;
+               if (get_cob_count() >= 4)
+                       return 0;
+               if ((get_ball_count() >= 2) &&
+                   (get_cob_count() >= 2))
+                       return 0;
+               return 1;
+       }
+       else {
+               /* not much time remaining */
+               if ((get_ball_count() >= 1) &&
+                   (get_cob_count() >= 1))
+                       return 0;
+               return 1;
+       }
+}
+
+static uint8_t strat_harvest(void)
+{
+       return 0;
+}
+
+static uint8_t strat_eject(void)
+{
+       return 0;
+}
+
 uint8_t strat_main(void)
 {
        uint8_t err;
 
-       /* */
+       /* harvest the first cobs + balls */
        err = strat_beginning();
 
+       while (1) {
+               /* end of time exit ! */
+               if (err == END_TIMER) {
+                       DEBUG(E_USER_STRAT, "End of time");
+                       strat_exit();
+                       break;
+               }
+
+               if (need_more_elements() == 0) {
+                       /* we have enough elements, go to eject */
+                       err = strat_eject();
+                       if (!TRAJ_SUCCESS(err))
+                               continue;
+               }
+               else {
+                       /* harvest */
+                       err = strat_harvest();
+                       if (!TRAJ_SUCCESS(err))
+                               continue;
+               }
+       }
+
        return err;
 }