+ /* ignore when strat is not running */
+ if (strat_running == 0)
+ return;
+
+ check_tomato();
+ check_corn();
+
+ /* limit speed when opponent is near */
+ /* disabled for 2010, we are already slow :) */
+ //strat_limit_speed();
+}
+
+/* check that we are on an eject line */
+static uint8_t robot_is_on_eject_line(void)
+{
+ int16_t x, y;
+ uint8_t i, j;
+
+ x = position_get_x_s16(&mainboard.pos);
+ y = position_get_y_s16(&mainboard.pos);
+
+ if (xycoord_to_ijcoord(&x, &y, &i, &j) < 0)
+ return 0;
+
+ if (!wp_belongs_to_line(i, j, 5, LINE_UP) &&
+ !wp_belongs_to_line(i, j, 2, LINE_R_UP))
+ return 0;
+
+ return 1;
+}
+
+/* 0 = fast, 1 = slow */
+static uint8_t eject_select_speed(void)
+{
+ int16_t x, y;
+ uint8_t i, j;
+
+ x = position_get_x_s16(&mainboard.pos);
+ y = position_get_y_s16(&mainboard.pos);
+
+ if (get_cob_count() >= 5) {
+ strat_want_pack = 1;
+ return 0; /* fast */
+ }
+
+ if (xycoord_to_ijcoord(&x, &y, &i, &j) < 0) {
+ DEBUG(E_USER_STRAT, "%s(): cannot find waypoint at %d,%d",
+ __FUNCTION__, x, y);
+ return 1; /* slow */
+ }
+
+ if (corn_count_neigh(i, j) == 2)
+ return 1; /* slow */
+
+ return 0; /* fast */
+}
+
+/* called multiple times while we are waiting to reach the ejection
+ * point */
+static uint8_t speedify_eject(void)
+{
+ if (eject_select_speed())
+ strat_set_speed(SPEED_CLITOID_SLOW, SPEED_ANGLE_SLOW);
+ else
+ strat_set_speed(SPEED_CLITOID_FAST, SPEED_ANGLE_SLOW);
+ return 0;
+}
+
+/* must be called from a terminal line */
+static uint8_t strat_eject(void)
+{
+ uint8_t err;
+
+ DEBUG(E_USER_STRAT, "%s()", __FUNCTION__);
+
+ /* check that we are called from an eject line */
+ if (!robot_is_on_eject_line()) {
+ DEBUG(E_USER_STRAT, "%s() not on eject line", __FUNCTION__);
+ return END_ERROR;
+ }
+
+ /* go to eject point */
+ trajectory_goto_xy_abs(&mainboard.traj, 2625, COLOR_Y(1847));
+ err = WAIT_COND_OR_TRAJ_END(speedify_eject(),
+ TRAJ_FLAGS_NO_NEAR);
+ /* err is never == 0 because speedify_eject() always return 0 */
+ if (!TRAJ_SUCCESS(err))
+ return err;
+
+ /* pack arms (force), and disable strat_event */
+ strat_event_disable();
+ i2c_cobboard_pack_weak(I2C_LEFT_SIDE);
+ i2c_cobboard_pack_weak(I2C_RIGHT_SIDE);
+
+ /* ball ejection */
+ if (get_ball_count() > 0) {
+ i2c_ballboard_set_mode(I2C_BALLBOARD_MODE_EJECT);
+ trajectory_a_abs(&mainboard.traj, COLOR_A(70));
+ err = wait_traj_end(TRAJ_FLAGS_NO_NEAR);
+ if (!TRAJ_SUCCESS(err))
+ goto fail;
+
+ DEBUG(E_USER_STRAT, "%s():%d", __FUNCTION__, __LINE__);
+ strat_hardstop();
+#ifdef HOST_VERSION
+ time_wait_ms(2000);
+#else
+ WAIT_COND_OR_TIMEOUT(ballboard.status == I2C_BALLBOARD_STATUS_F_BUSY,
+ 2000);
+ WAIT_COND_OR_TIMEOUT(ballboard.status == I2C_BALLBOARD_STATUS_F_READY,
+ 2000);
+#endif
+ }
+
+ /* half turn */
+ trajectory_a_abs(&mainboard.traj, COLOR_A(-110));
+ err = wait_traj_end(END_INTR|END_TRAJ);
+ if (!TRAJ_SUCCESS(err))
+ goto fail;
+
+ /* cob ejection */
+ trajectory_d_rel(&mainboard.traj, -70);
+ err = wait_traj_end(END_INTR|END_TRAJ);
+ if (!TRAJ_SUCCESS(err))
+ goto fail;
+
+ if (get_cob_count() > 0) {
+ i2c_cobboard_set_mode(I2C_COBBOARD_MODE_EJECT);
+ strat_db_dump(__FUNCTION__);
+ time_wait_ms(2000);
+ }
+
+ err = END_TRAJ;
+
+ fail:
+ strat_event_enable();
+ strat_want_pack = 0;
+ return err;