summaryrefslogtreecommitdiff
path: root/digital/ai/src
diff options
context:
space:
mode:
Diffstat (limited to 'digital/ai/src')
-rw-r--r--digital/ai/src/common/defs.h11
-rw-r--r--digital/ai/src/fsm/fsm.host.c56
-rw-r--r--digital/ai/src/fsm/init.c185
-rw-r--r--digital/ai/src/move/radar.c168
-rw-r--r--digital/ai/src/move/radar.h96
-rw-r--r--digital/ai/src/twi_master/asserv.c28
-rw-r--r--digital/ai/src/twi_master/asserv.h14
-rw-r--r--digital/ai/src/twi_master/mimot.c82
-rw-r--r--digital/ai/src/twi_master/mimot.h30
-rw-r--r--digital/ai/src/utils/chrono.c7
-rw-r--r--digital/ai/src/utils/chrono.h8
11 files changed, 655 insertions, 30 deletions
diff --git a/digital/ai/src/common/defs.h b/digital/ai/src/common/defs.h
index f16e9e52..12c47584 100644
--- a/digital/ai/src/common/defs.h
+++ b/digital/ai/src/common/defs.h
@@ -43,11 +43,18 @@ typedef struct position_t position_t;
/** Convert degrees to an angle usable in position_t. */
#define POSITION_A_DEG(a) G_ANGLE_UF016_DEG (a)
+/** No particular direction. */
+#define DIRECTION_NONE 0
+/** Forward direction, along the robot X axis. */
+#define DIRECTION_FORWARD 1
+/** Backward, opposite the robot X axis. */
+#define DIRECTION_BACKWARD 2
+
/** Team color, determine the start zone side. */
enum team_color_e
{
- TEAM_COLOR_LEFT = 0,
- TEAM_COLOR_RIGHT = 1
+ TEAM_COLOR_RIGHT = 0,
+ TEAM_COLOR_LEFT = 1
};
/** Current team color, to be read at start up. */
diff --git a/digital/ai/src/fsm/fsm.host.c b/digital/ai/src/fsm/fsm.host.c
index bf8b761d..50336722 100644
--- a/digital/ai/src/fsm/fsm.host.c
+++ b/digital/ai/src/fsm/fsm.host.c
@@ -1056,11 +1056,18 @@ fsm_build_gen_avr_h (fsm_build_t *fsm, uint embedded_strings)
/* Gen function table. */
fprintf (f, "typedef fsm_%s_branch_t (*fsm_%s_func_t)(void);\n", fsm->name,
fsm->name);
- fprintf (f, "extern const fsm_%s_func_t PROGMEM fsm_%s_trans_table[%u][%u];\n\n",
+ fprintf (f, "extern const fsm_%s_func_t PROGMEM fsm_%s_trans_table[%u][%u];\n",
fsm->name,
fsm->name,
fsm->event_nb,
fsm->state_nb);
+ /* Gen read function for trans table. */
+ fprintf (f, "fsm_%s_func_t fsm_%s_read_trans (fsm_%s_event_t event, "
+ "fsm_%s_state_t state);\n\n",
+ fsm->name,
+ fsm->name,
+ fsm->name,
+ fsm->name);
/* Gen active states array. */
fprintf (f, "extern fsm_%s_state_t fsm_%s_active_states[%u];\n\n",
@@ -1137,6 +1144,7 @@ fsm_build_gen_avr_c (fsm_build_t *fsm, uint embedded_strings)
/* Introduction. */
fprintf (f, "/* This file has been generated, do not edit. */\n\n");
fprintf (f, "#include \"fsm_%s_gen.h\"\n\n", fsm->name);
+ fprintf (f, "#include \"modules/proto/proto.h\"\n\n");
/* Gen state strings. */
if (embedded_strings)
@@ -1280,6 +1288,19 @@ fsm_build_gen_avr_c (fsm_build_t *fsm, uint embedded_strings)
}
fprintf (f, "};\n\n");
+ /* Gen read function for trans table. */
+ fprintf (f, "fsm_%s_func_t fsm_%s_read_trans (fsm_%s_event_t event, "
+ "fsm_%s_state_t state)\n{\n",
+ fsm->name,
+ fsm->name,
+ fsm->name,
+ fsm->name);
+ fprintf (f, "\treturn (fsm_%s_func_t) pgm_read_word "
+ "(&fsm_%s_trans_table[event][state]);\n",
+ fsm->name,
+ fsm->name);
+ fprintf (f, "}\n\n");
+
/* Gen active states array. */
fprintf (f, "fsm_%s_state_t fsm_%s_active_states[%u];\n\n",
fsm->name,
@@ -1324,19 +1345,23 @@ fsm_build_gen_avr_c (fsm_build_t *fsm, uint embedded_strings)
fprintf (f, "\tint handled = 0;\n");
fprintf (f, "\tfor (i = 0; i < fsm_%s_max_active_states; i++)\n\t{\n",
fsm->name);
- fprintf (f, "\t\tif (fsm_%s_trans_table[e][fsm_%s_active_states[i]])\n",
+ fprintf (f, "\t\tif (fsm_%s_read_trans (e, fsm_%s_active_states[i]))\n",
fsm->name,
fsm->name);
fprintf (f, "\t\t{\n");
- fprintf (f, "\t\t\tfsm_%s_active_states[i] = \
- fsm_%s_trans_table[e][fsm_%s_active_states[i]]();\n",
- fsm->name,
- fsm->name,
+ fprintf (f, "\t\t\tproto_send2b ('F', fsm_%s_active_states[i], e);\n",
fsm->name);
+ fprintf (f, "\t\t\tfsm_%s_active_states[i] = fsm_%s_read_trans (e, "
+ "fsm_%s_active_states[i])();\n",
+ fsm->name,
+ fsm->name,
+ fsm->name);
fprintf (f, "\t\t\thandled = 1;\n");
if (fsm->timeouts != NULL)
{
- fprintf (f, "\t\t\tfsm_%s_timeout_counters[i] = fsm_%s_timeout_values[e];\n",
+ fprintf (f, "\t\t\tfsm_%s_timeout_counters[i] = "
+ "fsm_%s_timeout_values[fsm_%s_active_states[i]];\n",
+ fsm->name,
fsm->name,
fsm->name);
}
@@ -1352,9 +1377,9 @@ fsm_build_gen_avr_c (fsm_build_t *fsm, uint embedded_strings)
fprintf (f, "\tuint16_t i;\n");
fprintf (f, "\tfor (i = 0; i < fsm_%s_max_active_states; i++)\n",
fsm->name);
- fprintf (f, "\t\tif (fsm_%s_trans_table[e][fsm_%s_active_states[i]])\n",
- fsm->name,
- fsm->name);
+ fprintf (f, "\t\tif (fsm_%s_read_trans (e, fsm_%s_active_states[i]))\n",
+ fsm->name,
+ fsm->name);
fprintf (f, "\t\t\treturn 1;\n");
fprintf (f, "\treturn 0;\n");
fprintf (f, "}\n\n");
@@ -1431,18 +1456,19 @@ fsm_build_gen_avr_c (fsm_build_t *fsm, uint embedded_strings)
fprintf (f, "\tint out = 0;\n");
fprintf (f, "\tfor (i = 0; i < fsm_%s_max_active_states; i++)\n\t{\n",
fsm->name);
- fprintf (f, "\t\tif (fsm_%s_timeout_counters[i] > 0)\n",
+ fprintf (f, "\t\tif (fsm_%s_timeout_counters[i] > 0)\n\t\t{\n",
fsm->name);
fprintf (f, "\t\t\tfsm_%s_timeout_counters[i]--;\n",
fsm->name);
- fprintf (f, "\t\tif (fsm_%s_timeout_counters[i] == 0)\n\t\t{\n",
+ fprintf (f, "\t\t\tif (fsm_%s_timeout_counters[i] == 0)\n\t\t\t{\n",
fsm->name);
- fprintf (f, "\t\t\tfsm_%s_handle (fsm_%s_timeout_events[fsm_%s_active_states[i]]);\n",
+ fprintf (f, "\t\t\t\tfsm_%s_handle (fsm_%s_timeout_events[fsm_%s_active_states[i]]);\n",
fsm->name,
fsm->name,
fsm->name);
- fprintf (f, "\t\t\tout = 1;\n");
- fprintf (f, "\t\t}\n\n");
+ fprintf (f, "\t\t\t\tout = 1;\n");
+ fprintf (f, "\t\t\t}\n");
+ fprintf (f, "\t\t}\n");
fprintf (f, "\t}\n");
fprintf (f, "\treturn out;\n");
fprintf (f, "}\n\n");
diff --git a/digital/ai/src/fsm/init.c b/digital/ai/src/fsm/init.c
new file mode 100644
index 00000000..51ba31ab
--- /dev/null
+++ b/digital/ai/src/fsm/init.c
@@ -0,0 +1,185 @@
+/* init.c */
+/* robospierre - Eurobot 2011 AI. {{{
+ *
+ * Copyright (C) 2011 Nicolas Schodet
+ *
+ * APBTeam:
+ * Web: http://apbteam.org/
+ * Email: team AT apbteam DOT org
+ *
+ * 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
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ *
+ * }}} */
+#include "common.h"
+
+#include "asserv.h"
+#include "contact.h"
+#include "chrono.h"
+
+#define FSM_NAME AI
+#include "fsm.h"
+#include "fsm_queue.h"
+
+#include "bot.h"
+#include "init_defs.h"
+
+/*
+ * Initialise robot position with a calibration procedure.
+ */
+
+FSM_STATES (
+ /* Initial state, waiting for jack. */
+ INIT_START,
+ /* After first jack insertion, waiting for removal to initialise actuators. */
+ INIT_WAITING_FIRST_JACK_OUT,
+ /* Initialising actuators, then waiting for second jack insertion. */
+ INIT_INITIALISING_ACTUATORS,
+ /* After second jack insertion, waiting the operator remove its
+ * hand. */
+ INIT_WAITING_HANDS_OUT,
+ /* Finding the first wall. */
+ INIT_FINDING_FIRST_WALL,
+ /* Going away from the wall. */
+ INIT_GOING_AWAY_FIRST_WALL,
+ /* Turning to face the other wall. */
+ INIT_FACING_SECOND_WALL,
+ /* Waiting after rotation for robot to stabilize. */
+ INIT_WAITING_AFTER_FACING_SECOND_WALL,
+ /* Finding the second wall. */
+ INIT_FINDING_SECOND_WALL,
+ /* Going away from the wall. */
+ INIT_GOING_AWAY_SECOND_WALL,
+#ifdef INIT_START_POSITION_ANGLE
+ /* Facing the start position. */
+ INIT_FACING_START_POSITION,
+#endif
+ /* Going to start position. */
+ INIT_GOING_TO_START_POSITION,
+ /* Waiting for the round start (waiting for the jack). */
+ INIT_WAITING_SECOND_JACK_OUT,
+ /* Initialisation finished, nothing else to do. */
+ INIT_FINISHED)
+
+FSM_EVENTS (
+ /* Jack is inserted. */
+ jack_inserted,
+ /* Jack is removed. */
+ jack_removed,
+ /* Sent to initialise actuators. */
+ init_actuators,
+ /* Sent to start round. */
+ init_start_round)
+
+FSM_START_WITH (INIT_START)
+
+FSM_TRANS (INIT_START, jack_inserted, INIT_WAITING_FIRST_JACK_OUT)
+{
+ return FSM_NEXT (INIT_START, jack_inserted);
+}
+
+FSM_TRANS (INIT_WAITING_FIRST_JACK_OUT, jack_removed,
+ INIT_INITIALISING_ACTUATORS)
+{
+ fsm_queue_post_event (FSM_EVENT (AI, init_actuators));
+ return FSM_NEXT (INIT_WAITING_FIRST_JACK_OUT, jack_removed);
+}
+
+FSM_TRANS (INIT_INITIALISING_ACTUATORS, jack_inserted, INIT_WAITING_HANDS_OUT)
+{
+ team_color = contact_get_color ();
+ return FSM_NEXT (INIT_INITIALISING_ACTUATORS, jack_inserted);
+}
+
+FSM_TRANS_TIMEOUT (INIT_WAITING_HANDS_OUT, 225, INIT_FINDING_FIRST_WALL)
+{
+ asserv_set_speed (BOT_SPEED_INIT);
+ asserv_push_the_wall (INIT_FIRST_WALL_PUSH);
+ return FSM_NEXT_TIMEOUT (INIT_WAITING_HANDS_OUT);
+}
+
+FSM_TRANS (INIT_FINDING_FIRST_WALL, robot_move_success,
+ INIT_GOING_AWAY_FIRST_WALL)
+{
+ asserv_move_linearly (INIT_FIRST_WALL_AWAY);
+ return FSM_NEXT (INIT_FINDING_FIRST_WALL, robot_move_success);
+}
+
+FSM_TRANS (INIT_GOING_AWAY_FIRST_WALL, robot_move_success,
+ INIT_FACING_SECOND_WALL)
+{
+ asserv_goto_angle (INIT_SECOND_WALL_ANGLE);
+ return FSM_NEXT (INIT_GOING_AWAY_FIRST_WALL, robot_move_success);
+}
+
+FSM_TRANS (INIT_FACING_SECOND_WALL, robot_move_success,
+ INIT_WAITING_AFTER_FACING_SECOND_WALL)
+{
+ return FSM_NEXT (INIT_FACING_SECOND_WALL, robot_move_success);
+}
+
+FSM_TRANS_TIMEOUT (INIT_WAITING_AFTER_FACING_SECOND_WALL, 225 / 2,
+ INIT_FINDING_SECOND_WALL)
+{
+ asserv_push_the_wall (INIT_SECOND_WALL_PUSH);
+ return FSM_NEXT_TIMEOUT (INIT_WAITING_AFTER_FACING_SECOND_WALL);
+}
+
+FSM_TRANS (INIT_FINDING_SECOND_WALL, robot_move_success,
+ INIT_GOING_AWAY_SECOND_WALL)
+{
+ asserv_move_linearly (INIT_SECOND_WALL_AWAY);
+ return FSM_NEXT (INIT_FINDING_SECOND_WALL, robot_move_success);
+}
+
+#ifdef INIT_START_POSITION_ANGLE
+FSM_TRANS (INIT_GOING_AWAY_SECOND_WALL, robot_move_success,
+ INIT_FACING_START_POSITION)
+{
+ asserv_goto_angle (INIT_START_POSITION_ANGLE);
+ return FSM_NEXT (INIT_GOING_AWAY_SECOND_WALL, robot_move_success);
+}
+
+FSM_TRANS (INIT_FACING_START_POSITION, robot_move_success,
+ INIT_GOING_TO_START_POSITION)
+{
+ asserv_goto_xya (INIT_START_POSITION);
+ return FSM_NEXT (INIT_FACING_START_POSITION, robot_move_success);
+}
+
+#else
+
+FSM_TRANS (INIT_GOING_AWAY_SECOND_WALL, robot_move_success,
+ INIT_GOING_TO_START_POSITION)
+{
+ asserv_goto_xya (INIT_START_POSITION);
+ return FSM_NEXT (INIT_GOING_AWAY_SECOND_WALL, robot_move_success);
+}
+
+#endif
+
+FSM_TRANS (INIT_GOING_TO_START_POSITION, robot_move_success,
+ INIT_WAITING_SECOND_JACK_OUT)
+{
+ asserv_set_speed (BOT_SPEED_NORMAL);
+ return FSM_NEXT (INIT_GOING_TO_START_POSITION, robot_move_success);
+}
+
+FSM_TRANS (INIT_WAITING_SECOND_JACK_OUT, jack_removed, INIT_FINISHED)
+{
+ chrono_init ();
+ fsm_queue_post_event (FSM_EVENT (AI, init_start_round));
+ return FSM_NEXT (INIT_WAITING_SECOND_JACK_OUT, jack_removed);
+}
+
diff --git a/digital/ai/src/move/radar.c b/digital/ai/src/move/radar.c
new file mode 100644
index 00000000..a572afba
--- /dev/null
+++ b/digital/ai/src/move/radar.c
@@ -0,0 +1,168 @@
+/* radar.c */
+/* io - Input & Output with Artificial Intelligence (ai) support on AVR. {{{
+ *
+ * Copyright (C) 2010 Nicolas Schodet
+ *
+ * APBTeam:
+ * Web: http://apbteam.org/
+ * Email: team AT apbteam DOT org
+ *
+ * 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
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ *
+ * }}} */
+#include "common.h"
+#include "radar.h"
+
+#include "bot.h"
+
+#include "modules/math/geometry/geometry.h"
+#include "modules/math/geometry/distance.h"
+#include "modules/utils/utils.h"
+
+/** Maximum distance for a sensor reading to be ignored if another sensor is
+ * nearer. */
+#define RADAR_FAR_MM 250
+
+/** Define radar configuration. */
+extern struct radar_sensor_t radar_sensors[RADAR_SENSOR_NB];
+
+uint8_t
+radar_valid (vect_t p);
+
+/** Compute the center position from several radars sensors, return 1 if
+ * any. */
+static uint8_t
+radar_hit_center (uint8_t valid[], vect_t hit[], uint8_t sensor_nb,
+ vect_t *obs_pos)
+{
+ uint8_t i, hit_nb = 0;
+ vect_t hit_center = { 0, 0 };
+ for (i = 0; i < sensor_nb; i++)
+ {
+ if (valid[i])
+ {
+ vect_add (&hit_center, &hit[i]);
+ hit_nb++;
+ }
+ }
+ if (hit_nb > 1)
+ vect_scale_f824 (&hit_center, 0x1000000l / hit_nb);
+ if (hit_nb)
+ {
+ *obs_pos = hit_center;
+ return 1;
+ }
+ else
+ return 0;
+}
+
+uint8_t
+radar_update (const position_t *robot_pos, vect_t *obs_pos)
+{
+ uint8_t i, j;
+ vect_t ray;
+ uint8_t obs_nb = 0;
+ /* Compute hit points for each sensor and eliminate invalid ones. */
+ vect_t hit[UTILS_COUNT (radar_sensors)];
+ uint8_t valid[UTILS_COUNT (radar_sensors)];
+ uint16_t dist_mm[UTILS_COUNT (radar_sensors)];
+ for (i = 0; i < UTILS_COUNT (radar_sensors); i++)
+ {
+ dist_mm[i] = *radar_sensors[i].dist_mm;
+ if (dist_mm[i] != 0xffff)
+ {
+ hit[i] = radar_sensors[i].pos;
+ vect_rotate_uf016 (&hit[i], robot_pos->a);
+ vect_translate (&hit[i], &robot_pos->v);
+ vect_from_polar_uf016 (&ray, dist_mm[i],
+ robot_pos->a + radar_sensors[i].a);
+ vect_translate (&hit[i], &ray);
+ valid[i] = radar_valid (hit[i]);
+ vect_from_polar_uf016 (&ray, RADAR_OBSTACLE_EDGE_RADIUS_MM,
+ robot_pos->a + radar_sensors[i].a);
+ vect_translate (&hit[i], &ray);
+ }
+ else
+ valid[i] = 0;
+ }
+ /* Ignore sensor results too far from other sensors. */
+ for (i = 0; i < UTILS_COUNT (radar_sensors) - 1; i++)
+ {
+ for (j = i + 1; valid[i] && j < UTILS_COUNT (radar_sensors); j++)
+ {
+ if (valid[j])
+ {
+ if (dist_mm[i] + RADAR_FAR_MM < dist_mm[j])
+ valid[j] = 0;
+ else if (dist_mm[j] + RADAR_FAR_MM < dist_mm[i])
+ valid[i] = 0;
+ }
+ }
+ }
+ /* Specific treatment about sensor topology. */
+ obs_nb += radar_hit_center (valid + RADAR_SENSOR_FRONT_FIRST,
+ hit + RADAR_SENSOR_FRONT_FIRST,
+ RADAR_SENSOR_FRONT_NB, &obs_pos[obs_nb]);
+ obs_nb += radar_hit_center (valid + RADAR_SENSOR_BACK_FIRST,
+ hit + RADAR_SENSOR_BACK_FIRST,
+ RADAR_SENSOR_BACK_NB, &obs_pos[obs_nb]);
+ /* Done. */
+ return obs_nb;
+}
+
+uint8_t
+radar_blocking (const vect_t *robot_pos, const vect_t *dest_pos,
+ const vect_t *obs_pos, uint8_t obs_nb)
+{
+ uint8_t i;
+ /* Stop here if no obstacle. */
+ if (!obs_nb)
+ return 0;
+ vect_t vd = *dest_pos; vect_sub (&vd, robot_pos);
+ uint16_t d = vect_norm (&vd);
+ /* If destination is realy near, stop here. */
+ if (d < RADAR_EPSILON_MM)
+ return 0;
+ /* If destination is near, use clearance to destination point instead of
+ * stop length. */
+ vect_t t;
+ if (d < RADAR_STOP_MM)
+ t = *dest_pos;
+ else
+ {
+ vect_scale_f824 (&vd, (1ll << 24) / d * RADAR_STOP_MM);
+ t = *robot_pos;
+ vect_translate (&t, &vd);
+ }
+ /* Now, look at obstacles. */
+ for (i = 0; i < obs_nb; i++)
+ {
+ /* Vector from robot to obstacle. */
+ vect_t vo = obs_pos[i]; vect_sub (&vo, robot_pos);
+ /* Ignore if in our back. */
+ int32_t dp = vect_dot_product (&vd, &vo);
+ if (dp < 0)
+ continue;
+ /* Check distance. */
+ int16_t od = distance_segment_point (robot_pos, &t, &obs_pos[i]);
+ if (od > BOT_SIZE_SIDE + RADAR_CLEARANCE_MM / 2
+ + RADAR_OBSTACLE_RADIUS_MM)
+ continue;
+ /* Else, obstacle is blocking. */
+ return 1;
+ }
+ return 0;
+}
+
diff --git a/digital/ai/src/move/radar.h b/digital/ai/src/move/radar.h
new file mode 100644
index 00000000..2e37d11d
--- /dev/null
+++ b/digital/ai/src/move/radar.h
@@ -0,0 +1,96 @@
+#ifndef radar_h
+#define radar_h
+/* radar.h */
+/* io - Input & Output with Artificial Intelligence (ai) support on AVR. {{{
+ *
+ * Copyright (C) 2010 Nicolas Schodet
+ *
+ * APBTeam:
+ * Web: http://apbteam.org/
+ * Email: team AT apbteam DOT org
+ *
+ * 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
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ *
+ * }}} */
+#include "defs.h"
+
+/**
+ * Handle any distance sensors information to extract useful data. This
+ * includes:
+ * - combining several sensors information for a more precise obstacle
+ * position,
+ * - ignoring obstacles not in the playground,
+ * - determining if an obstacle should make the robot stop.
+ */
+
+/** Estimated obstacle edge radius. As the sensors detect obstacle edge, this is
+ * added to position obstacle center. */
+#undef RADAR_OBSTACLE_EDGE_RADIUS_MM
+
+/** Estimated obstacle radius. The obstacle may be larger than at the
+ * detected edge. */
+#undef RADAR_OBSTACLE_RADIUS_MM
+
+/** Stop distance. Distance under which an obstacle is considered harmful when
+ * moving. */
+#undef RADAR_STOP_MM
+
+/** Clearance distance. Distance over which an obstacle should be to the side
+ * when moving.
+ *
+ * OK, more explanations: when moving, a rectangle is placed in front of the
+ * robot, of length RADAR_STOP_MM and width 2 * (RADAR_CLEARANCE_MM +
+ * BOT_SIZE_SIDE). If an obstacle is inside this rectangle, it is considered
+ * in the way.
+ *
+ * If the destination point is near (< RADAR_STOP_MM - RADAR_CLEARANCE_MM),
+ * this reduce the rectangle length.
+ *
+ * If the destination is really near (< RADAR_EPSILON_MM), ignore all this. */
+#undef RADAR_CLEARANCE_MM
+
+/** Destination distance near enough so that obstacles could be ignored. */
+#undef RADAR_EPSILON_MM
+
+/* All this in another file. */
+#include "radar_defs.h"
+
+/** Margin to be considered inside the playground. An obstacle can not be
+ * exactly at the playground edge. */
+#define RADAR_MARGIN_MM 150
+
+/** Describe a radar sensor. */
+struct radar_sensor_t
+{
+ /** Distance updated by another module. */
+ uint16_t *dist_mm;
+ /** Position relative to the robot center. */
+ vect_t pos;
+ /** Angle relative to the robot X axis. */
+ uint16_t a;
+};
+
+/** Update radar view. Return the number of obstacles found. Obstacles
+ * positions are returned in obs_pos. */
+uint8_t
+radar_update (const position_t *robot_pos, vect_t *obs_pos);
+
+/** Return non zero if there is a blocking obstacle near the robot while going
+ * to a destination point. */
+uint8_t
+radar_blocking (const vect_t *robot_pos, const vect_t *dest_pos,
+ const vect_t *obs_pos, uint8_t obs_nb);
+
+#endif /* radar_h */
diff --git a/digital/ai/src/twi_master/asserv.c b/digital/ai/src/twi_master/asserv.c
index d066737b..839b1d23 100644
--- a/digital/ai/src/twi_master/asserv.c
+++ b/digital/ai/src/twi_master/asserv.c
@@ -202,12 +202,12 @@ asserv_get_moving_direction (void)
{
/* Foward move? */
if (asserv_status.status & _BV (asserv_status_flag_move_forward))
- return 1;
+ return DIRECTION_FORWARD;
/* Backward move? */
if (asserv_status.status & _BV (asserv_status_flag_move_backward))
- return 2;
+ return DIRECTION_BACKWARD;
/* Not moving */
- return 0;
+ return DIRECTION_NONE;
}
uint8_t
@@ -301,6 +301,28 @@ asserv_go_to_the_wall (uint8_t backward)
}
void
+asserv_push_the_wall (uint8_t backward, uint32_t init_x, uint32_t init_y,
+ uint16_t init_a)
+{
+ if (init_x != (uint32_t) -1)
+ init_x = fixed_mul_f824 (init_x, asserv_scale_inv);
+ if (init_y != (uint32_t) -1)
+ init_y = fixed_mul_f824 (init_y, asserv_scale_inv);
+ uint8_t *buffer = twi_master_get_buffer (ASSERV_SLAVE);
+ buffer[0] = 'G';
+ buffer[1] = backward;
+ buffer[2] = v32_to_v8 (init_x, 2);
+ buffer[3] = v32_to_v8 (init_x, 1);
+ buffer[4] = v32_to_v8 (init_x, 0);
+ buffer[5] = v32_to_v8 (init_y, 2);
+ buffer[6] = v32_to_v8 (init_y, 1);
+ buffer[7] = v32_to_v8 (init_y, 0);
+ buffer[8] = v16_to_v8 (init_a, 1);
+ buffer[9] = v16_to_v8 (init_a, 0);
+ twi_master_send_buffer (10);
+}
+
+void
asserv_move_motor0_absolute (uint16_t position, uint8_t speed)
{
uint8_t *buffer = twi_master_get_buffer (ASSERV_SLAVE);
diff --git a/digital/ai/src/twi_master/asserv.h b/digital/ai/src/twi_master/asserv.h
index 0d29ba96..c9a52a11 100644
--- a/digital/ai/src/twi_master/asserv.h
+++ b/digital/ai/src/twi_master/asserv.h
@@ -121,16 +121,16 @@ asserv_get_motor1_position (void);
/**
* Are we moving forward/backward?
* @return
- * - 0 we are not moving;
- * - 1 we are moving forward;
- * - 2 we are moving backward.
+ * - DIRECTION_NONE we are not moving;
+ * - DIRECTION_FORWARD we are moving forward;
+ * - DIRECTION_BACKWARD we are moving backward.
*/
uint8_t
asserv_get_moving_direction (void);
/**
* Get the last moving direction of the bot.
- * @return 1 is forward, 2 is backward.
+ * @return DIRECTION_FORWARD or DIRECTION_BACKWARD.
*/
uint8_t
asserv_get_last_moving_direction (void);
@@ -198,6 +198,12 @@ asserv_goto_xya (uint32_t x, uint32_t y, int16_t a, uint8_t backward);
void
asserv_go_to_the_wall (uint8_t backward);
+/** Push the wall and initialise position. Use -1 for coordinates to keep
+ * unchanged. */
+void
+asserv_push_the_wall (uint8_t backward, uint32_t init_x, uint32_t init_y,
+ uint16_t init_a);
+
/**
* Move the motor0.
* Motor0 class command.
diff --git a/digital/ai/src/twi_master/mimot.c b/digital/ai/src/twi_master/mimot.c
index af6ff5f9..8df84538 100644
--- a/digital/ai/src/twi_master/mimot.c
+++ b/digital/ai/src/twi_master/mimot.c
@@ -97,6 +97,12 @@ mimot_motor1_cmd_status (void)
return none;
}
+uint8_t
+mimot_get_input (void)
+{
+ return mimot_status.input_port;
+}
+
uint16_t
mimot_get_motor0_position (void)
{
@@ -118,6 +124,30 @@ mimot_reset (void)
}
void
+mimot_set_motor0_position (uint16_t position)
+{
+ uint8_t *buffer = twi_master_get_buffer (MIMOT_SLAVE);
+ buffer[0] = 'p';
+ buffer[1] = 'Y';
+ buffer[2] = 0;
+ buffer[3] = v16_to_v8 (position, 1);
+ buffer[4] = v16_to_v8 (position, 0);
+ twi_master_send_buffer (5);
+}
+
+void
+mimot_set_motor1_position (uint16_t position)
+{
+ uint8_t *buffer = twi_master_get_buffer (MIMOT_SLAVE);
+ buffer[0] = 'p';
+ buffer[1] = 'Y';
+ buffer[2] = 1;
+ buffer[3] = v16_to_v8 (position, 1);
+ buffer[4] = v16_to_v8 (position, 0);
+ twi_master_send_buffer (5);
+}
+
+void
mimot_move_motor0_absolute (uint16_t position, uint8_t speed)
{
uint8_t *buffer = twi_master_get_buffer (MIMOT_SLAVE);
@@ -142,19 +172,41 @@ mimot_move_motor1_absolute (uint16_t position, uint8_t speed)
void
mimot_motor0_zero_position (int8_t speed)
{
+ mimot_motor0_find_zero (speed, 0, 0);
+}
+
+void
+mimot_motor1_zero_position (int8_t speed)
+{
+ mimot_motor1_find_zero (speed, 0, 0);
+}
+
+void
+mimot_motor0_find_zero (int8_t speed, uint8_t use_switch,
+ uint16_t reset_position)
+{
uint8_t *buffer = twi_master_get_buffer (MIMOT_SLAVE);
buffer[0] = 'B';
- buffer[1] = speed;
- twi_master_send_buffer (2);
+ buffer[1] = 0;
+ buffer[2] = speed;
+ buffer[3] = use_switch;
+ buffer[4] = v16_to_v8 (reset_position, 1);
+ buffer[5] = v16_to_v8 (reset_position, 0);
+ twi_master_send_buffer (6);
}
void
-mimot_motor1_zero_position (int8_t speed)
+mimot_motor1_find_zero (int8_t speed, uint8_t use_switch,
+ uint16_t reset_position)
{
uint8_t *buffer = twi_master_get_buffer (MIMOT_SLAVE);
- buffer[0] = 'C';
- buffer[1] = speed;
- twi_master_send_buffer (2);
+ buffer[0] = 'B';
+ buffer[1] = 1;
+ buffer[2] = speed;
+ buffer[3] = use_switch;
+ buffer[4] = v16_to_v8 (reset_position, 1);
+ buffer[5] = v16_to_v8 (reset_position, 0);
+ twi_master_send_buffer (6);
}
void
@@ -181,3 +233,21 @@ mimot_motor1_clamp (int8_t speed, int16_t pwm)
twi_master_send_buffer (5);
}
+void
+mimot_motor0_free (void)
+{
+ uint8_t *buffer = twi_master_get_buffer (MIMOT_SLAVE);
+ buffer[0] = 'w';
+ buffer[1] = 0;
+ twi_master_send_buffer (2);
+}
+
+void
+mimot_motor1_free (void)
+{
+ uint8_t *buffer = twi_master_get_buffer (MIMOT_SLAVE);
+ buffer[0] = 'w';
+ buffer[1] = 1;
+ twi_master_send_buffer (2);
+}
+
diff --git a/digital/ai/src/twi_master/mimot.h b/digital/ai/src/twi_master/mimot.h
index 60811c10..c9de7090 100644
--- a/digital/ai/src/twi_master/mimot.h
+++ b/digital/ai/src/twi_master/mimot.h
@@ -56,6 +56,10 @@ mimot_motor0_cmd_status (void);
asserv_status_e
mimot_motor1_cmd_status (void);
+/** Return input port state. */
+uint8_t
+mimot_get_input (void);
+
/** Get motor0 position in steps. */
uint16_t
mimot_get_motor0_position (void);
@@ -68,6 +72,14 @@ mimot_get_motor1_position (void);
void
mimot_reset (void);
+/** Set motor0 position in steps. */
+void
+mimot_set_motor0_position (uint16_t position);
+
+/** Set motor1 position in steps. */
+void
+mimot_set_motor1_position (uint16_t position);
+
/** Move motor0 to absolute position in steps. */
void
mimot_move_motor0_absolute (uint16_t position, uint8_t speed);
@@ -84,6 +96,16 @@ mimot_motor0_zero_position (int8_t speed);
void
mimot_motor1_zero_position (int8_t speed);
+/** Find zero position. */
+void
+mimot_motor0_find_zero (int8_t speed, uint8_t use_switch,
+ uint16_t reset_position);
+
+/** Find zero position. */
+void
+mimot_motor1_find_zero (int8_t speed, uint8_t use_switch,
+ uint16_t reset_position);
+
/** Clamp motor0. */
void
mimot_motor0_clamp (int8_t speed, int16_t pwm);
@@ -92,4 +114,12 @@ mimot_motor0_clamp (int8_t speed, int16_t pwm);
void
mimot_motor1_clamp (int8_t speed, int16_t pwm);
+/** Free motor0. */
+void
+mimot_motor0_free (void);
+
+/** Free motor1. */
+void
+mimot_motor1_free (void);
+
#endif /* mimot_h */
diff --git a/digital/ai/src/utils/chrono.c b/digital/ai/src/utils/chrono.c
index a1212e19..85851547 100644
--- a/digital/ai/src/utils/chrono.c
+++ b/digital/ai/src/utils/chrono.c
@@ -148,3 +148,10 @@ chrono_end_match (uint8_t block)
;
#endif
}
+
+void
+chrono_set_timer (uint32_t elapsed_time)
+{
+ if (chrono_enabled_)
+ chrono_ov_count_ = elapsed_time / TIMER_PERIOD_MS;
+}
diff --git a/digital/ai/src/utils/chrono.h b/digital/ai/src/utils/chrono.h
index 38bf3d61..580529cf 100644
--- a/digital/ai/src/utils/chrono.h
+++ b/digital/ai/src/utils/chrono.h
@@ -96,4 +96,12 @@ chrono_remaining_time (void);
void
chrono_end_match (uint8_t block);
+/**
+ * Set timer at desired value.
+ * This function should be used for tests purpose only.
+ * @param elapsed_time elapsed time since beginning.
+ */
+void
+chrono_set_timer (uint32_t elapsed_time);
+
#endif /* chrono_h */