From d776dc0dab6dd3ddf92041f1ad42e521847ea9cf Mon Sep 17 00:00:00 2001 From: zer0 Date: Wed, 28 Apr 2010 00:03:28 +0200 Subject: [PATCH] optim cobboard --- .../microb2010/mainboard/commands_mainboard.c | 2 +- projects/microb2010/mainboard/i2c_protocol.c | 2 +- projects/microb2010/mainboard/i2c_protocol.h | 2 +- projects/microb2010/mainboard/strat.c | 63 ++++++++++++++----- projects/microb2010/mainboard/strat_corn.c | 7 +-- projects/microb2010/mainboard/strat_corn.h | 2 + projects/microb2010/mainboard/strat_db.c | 2 + 7 files changed, 57 insertions(+), 23 deletions(-) diff --git a/projects/microb2010/mainboard/commands_mainboard.c b/projects/microb2010/mainboard/commands_mainboard.c index 7c58bb9..ee0ffa4 100644 --- a/projects/microb2010/mainboard/commands_mainboard.c +++ b/projects/microb2010/mainboard/commands_mainboard.c @@ -779,7 +779,7 @@ static void cmd_cobboard_setmode2_parsed(void *parsed_result, void *data) } else if (!strcmp_P(res->arg1, PSTR("harvest"))) { i2c_cobboard_set_mode(I2C_COBBOARD_MODE_HARVEST); - i2c_cobboard_harvest(side); + i2c_cobboard_autoharvest(side); } else if (!strcmp_P(res->arg1, PSTR("pack"))) { i2c_cobboard_set_mode(I2C_COBBOARD_MODE_HARVEST); diff --git a/projects/microb2010/mainboard/i2c_protocol.c b/projects/microb2010/mainboard/i2c_protocol.c index d264622..9908f1a 100644 --- a/projects/microb2010/mainboard/i2c_protocol.c +++ b/projects/microb2010/mainboard/i2c_protocol.c @@ -426,7 +426,7 @@ int8_t i2c_cobboard_pack(uint8_t side) return i2c_cobboard_set_spickle(side, 0); } -int8_t i2c_cobboard_harvest(uint8_t side) +int8_t i2c_cobboard_autoharvest(uint8_t side) { return i2c_cobboard_set_spickle(side, I2C_COBBOARD_SPK_DEPLOY | diff --git a/projects/microb2010/mainboard/i2c_protocol.h b/projects/microb2010/mainboard/i2c_protocol.h index 20c8ada..b107c44 100644 --- a/projects/microb2010/mainboard/i2c_protocol.h +++ b/projects/microb2010/mainboard/i2c_protocol.h @@ -37,7 +37,7 @@ int8_t i2c_led_control(uint8_t addr, uint8_t led, uint8_t state); int8_t i2c_cobboard_set_mode(uint8_t mode); int8_t i2c_cobboard_pack(uint8_t side); -int8_t i2c_cobboard_harvest(uint8_t side); +int8_t i2c_cobboard_autoharvest(uint8_t side); int8_t i2c_cobboard_deploy(uint8_t side); int8_t i2c_ballboard_set_mode(uint8_t mode); diff --git a/projects/microb2010/mainboard/strat.c b/projects/microb2010/mainboard/strat.c index 2c60428..3839738 100644 --- a/projects/microb2010/mainboard/strat.c +++ b/projects/microb2010/mainboard/strat.c @@ -67,6 +67,7 @@ #define COL_DISP_MARGIN 400 /* stop 40 cm in front of dispenser */ #define COL_SCAN_PRE_MARGIN 250 +static uint8_t strat_running = 0; struct strat_conf strat_conf; /*************************************************************/ @@ -100,22 +101,19 @@ void strat_conf_dump(const char *caller) /* call it just before launching the strat */ void strat_init(void) { +#ifdef HOST_VERSION position_set(&mainboard.pos, 298.16, COLOR_Y(308.78), COLOR_A(70.00)); - - /* XXX init rollers, .. */ - - strat_db_init(); +#endif /* we consider that the color is correctly set */ - + strat_running = 1; + strat_db_init(); strat_set_speed(SPEED_DIST_FAST, SPEED_ANGLE_FAST); time_reset(); interrupt_traj_reset(); 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 */ @@ -131,6 +129,7 @@ void strat_exit(void) uint8_t flags; #endif + strat_running = 0; mainboard.flags &= ~(DO_TIMER); strat_hardstop(); time_reset(); @@ -144,11 +143,16 @@ void strat_exit(void) #endif } -/* called periodically */ +/* called periodically (10ms) */ void strat_event(void *dummy) { uint8_t flags; uint8_t lcob, rcob; + uint8_t lidx, ridx; + + /* ignore when strat is not running */ + if (strat_running == 0) + return; IRQ_LOCK(flags); lcob = ballboard.lcob; @@ -157,14 +161,41 @@ void strat_event(void *dummy) ballboard.rcob = I2C_COB_NONE; IRQ_UNLOCK(flags); - if (lcob == I2C_COB_WHITE) - DEBUG(E_USER_STRAT, "lcob white"); - if (lcob == I2C_COB_BLACK) - DEBUG(E_USER_STRAT, "lcob black"); - if (rcob == I2C_COB_WHITE) - DEBUG(E_USER_STRAT, "rcob white"); - if (rcob == I2C_COB_BLACK) - DEBUG(E_USER_STRAT, "rcob black"); + /* XXX take opponent position into account */ + + + /* detect cob on left side */ + if (corn_is_near(&lidx, I2C_LEFT_SIDE)) { + if (lcob != I2C_COB_NONE) { + corn_set_color(strat_db.corn_table[lidx], lcob); + DEBUG(E_USER_STRAT, "lcob %s %d", + lcob == I2C_COB_WHITE ? "white" : "black", lidx); + } + if (strat_db.corn_table[lidx]->corn.color == I2C_COB_WHITE) + i2c_cobboard_autoharvest(I2C_LEFT_SIDE); + else + i2c_cobboard_deploy(I2C_LEFT_SIDE); + } + else { + i2c_cobboard_deploy(I2C_LEFT_SIDE); + } + + /* detect cob on right side */ + if (corn_is_near(&ridx, I2C_RIGHT_SIDE)) { + if (rcob != I2C_COB_NONE) { + corn_set_color(strat_db.corn_table[ridx], rcob); + DEBUG(E_USER_STRAT, "rcob %s %d", + rcob == I2C_COB_WHITE ? "white" : "black", ridx); + } + if (strat_db.corn_table[ridx]->corn.color == I2C_COB_WHITE) + i2c_cobboard_autoharvest(I2C_RIGHT_SIDE); + else + i2c_cobboard_deploy(I2C_RIGHT_SIDE); + } + else { + i2c_cobboard_deploy(I2C_RIGHT_SIDE); + } + /* limit speed when opponent is close */ strat_limit_speed(); diff --git a/projects/microb2010/mainboard/strat_corn.c b/projects/microb2010/mainboard/strat_corn.c index 5224787..7fa64c4 100644 --- a/projects/microb2010/mainboard/strat_corn.c +++ b/projects/microb2010/mainboard/strat_corn.c @@ -66,9 +66,8 @@ #include "sensor.h" #include "actuator.h" -#if 0 /* return 1 if there is a corn near, and fill the index ptr */ -uint8_t corn_is_near(int8_t *corn_idx, uint8_t side) +int8_t corn_is_near(uint8_t *corn_idx, uint8_t side) { #define SENSOR_CORN_DIST 225 #define SENSOR_CORN_ANGLE 90 @@ -91,11 +90,11 @@ uint8_t corn_is_near(int8_t *corn_idx, uint8_t side) y_corn_int = y_corn; wp = xycoord_to_corn_idx(&x_corn_int, &y_corn_int); - if (wp->corn.idx < 0) + if (wp == NULL) return 0; + *corn_idx = wp->corn.idx; return 1; } -#endif /* * - send the correct commands to the spickles diff --git a/projects/microb2010/mainboard/strat_corn.h b/projects/microb2010/mainboard/strat_corn.h index ca4a7d6..ac806d3 100644 --- a/projects/microb2010/mainboard/strat_corn.h +++ b/projects/microb2010/mainboard/strat_corn.h @@ -32,6 +32,8 @@ struct line_2pts { point_t p2; }; +int8_t corn_is_near(uint8_t *corn_idx, uint8_t side); + void num2line(struct line_2pts *l, uint8_t dir, uint8_t num); uint8_t line2line(uint8_t dir1, uint8_t num1, diff --git a/projects/microb2010/mainboard/strat_db.c b/projects/microb2010/mainboard/strat_db.c index 57c20ba..18db6ea 100644 --- a/projects/microb2010/mainboard/strat_db.c +++ b/projects/microb2010/mainboard/strat_db.c @@ -289,6 +289,8 @@ void corn_set_color(struct waypoint_db *wp, uint8_t color) wp->corn.color = color; if (color == I2C_COB_UNKNOWN) return; + if (wp->corn.color != I2C_COB_UNKNOWN) + return; corn_deduct_other(wp->corn.idx, color); symidx = corn_get_sym_idx(wp->corn.idx); strat_db.corn_table[symidx]->corn.color = color; -- 2.39.5