2 * Copyright Droids Corporation, Microb Technology (2009)
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 * Revision : $Id: strat_utils.c,v 1.7 2009-11-08 17:24:33 zer0 Exp $
26 #include <aversive/pgmspace.h>
27 #include <aversive/wait.h>
28 #include <aversive/error.h>
38 #include <control_system_manager.h>
39 #include <trajectory_manager.h>
40 #include <vect_base.h>
43 #include <obstacle_avoidance.h>
44 #include <blocking_detection_manager.h>
45 #include <robot_system.h>
46 #include <position_manager.h>
51 #include "../common/i2c_commands.h"
54 #include "strat_utils.h"
57 #include "i2c_protocol.h"
59 /* return the distance between two points */
60 int16_t distance_between(int16_t x1, int16_t y1, int16_t x2, int16_t y2)
70 /* return the distance to a point in the area */
71 int16_t distance_from_robot(int16_t x, int16_t y)
73 return distance_between(position_get_x_s16(&mainboard.pos),
74 position_get_y_s16(&mainboard.pos), x, y);
77 /** do a modulo 360 -> [-180,+180], knowing that 'a' is in [-3*180,+3*180] */
78 int16_t simple_modulo_360(int16_t a)
89 /* return the distance to a point in the area */
90 int16_t angle_abs_to_rel(int16_t a_abs)
92 return simple_modulo_360(a_abs - position_get_a_deg_s16(&mainboard.pos));
95 void rel_da_to_abs_xy(double d_rel, double a_rel_rad,
96 double *x_abs, double *y_abs)
98 double x = position_get_x_double(&mainboard.pos);
99 double y = position_get_y_double(&mainboard.pos);
100 double a = position_get_a_rad_double(&mainboard.pos);
102 *x_abs = x + d_rel*cos(a+a_rel_rad);
103 *y_abs = y + d_rel*sin(a+a_rel_rad);
106 double norm(double x, double y)
108 return sqrt(x*x + y*y);
111 void rel_xy_to_abs_xy(double x_rel, double y_rel,
112 double *x_abs, double *y_abs)
115 d_rel = norm(x_rel, y_rel);
116 a_rel = atan2(y_rel, x_rel);
117 rel_da_to_abs_xy(d_rel, a_rel, x_abs, y_abs);
120 /* return an angle between -pi and pi */
121 void abs_xy_to_rel_da(double x_abs, double y_abs,
122 double *d_rel, double *a_rel_rad)
124 double x = position_get_x_double(&mainboard.pos);
125 double y = position_get_y_double(&mainboard.pos);
126 double a = position_get_a_rad_double(&mainboard.pos);
128 *a_rel_rad = atan2(y_abs - y, x_abs - x) - a;
129 if (*a_rel_rad < -M_PI) {
132 else if (*a_rel_rad > M_PI) {
135 *d_rel = norm(x_abs-x, y_abs-y);
138 void rotate(double *x, double *y, double rot)
150 /* return true if the point is in area */
151 uint8_t is_in_area(int16_t x, int16_t y, int16_t margin)
155 if (x > (AREA_X - margin))
159 if (y > (AREA_Y - margin))
165 /* return true if the point is in area */
166 uint8_t robot_is_in_area(int16_t margin)
168 return is_in_area(position_get_x_s16(&mainboard.pos),
169 position_get_y_s16(&mainboard.pos),
173 /* return true if we are near the disc */
174 uint8_t robot_is_near_disc(void)
176 if (distance_from_robot(CENTER_X, CENTER_Y) < DISC_PENTA_DIAG)
181 /* return 1 or 0 depending on which side of a line (y=cste) is the
182 * robot. works in red or green color. */
183 uint8_t y_is_more_than(int16_t y)
187 posy = position_get_y_s16(&mainboard.pos);
188 if (mainboard.our_color == I2C_COLOR_RED) {
195 if (posy < (AREA_Y-y))
202 /* return 1 or 0 depending on which side of a line (x=cste) is the
203 * robot. works in red or green color. */
204 uint8_t x_is_more_than(int16_t x)
208 posx = position_get_x_s16(&mainboard.pos);
215 int16_t sin_table[] = {
235 int16_t fast_sin(int16_t deg)
243 return sin_table[(deg*16)/90];
245 return sin_table[((180-deg)*16)/90];
247 return -sin_table[((deg-180)*16)/90];
249 return -sin_table[((360-deg)*16)/90];
252 int16_t fast_cos(int16_t deg)
254 return fast_sin(90+deg);
258 /* get the color of our robot */
259 uint8_t get_color(void)
261 return mainboard.our_color;
264 /* get the color of the opponent robot */
265 uint8_t get_opponent_color(void)
267 if (mainboard.our_color == I2C_COLOR_RED)
268 return I2C_COLOR_GREEN;
270 return I2C_COLOR_RED;
273 /* get the xy pos of the opponent robot */
274 int8_t get_opponent_xy(int16_t *x, int16_t *y)
278 *x = sensorboard.opponent_x;
279 *y = sensorboard.opponent_y;
281 if (*x == I2C_OPPONENT_NOT_THERE)
286 /* get the da pos of the opponent robot */
287 int8_t get_opponent_da(int16_t *d, int16_t *a)
292 x_tmp = sensorboard.opponent_x;
293 *d = sensorboard.opponent_d;
294 *a = sensorboard.opponent_a;
296 if (x_tmp == I2C_OPPONENT_NOT_THERE)
301 /* get the da pos of the opponent robot */
302 int8_t get_opponent_xyda(int16_t *x, int16_t *y, int16_t *d, int16_t *a)
306 *x = sensorboard.opponent_x;
307 *y = sensorboard.opponent_y;
308 *d = sensorboard.opponent_d;
309 *a = sensorboard.opponent_a;
311 if (*x == I2C_OPPONENT_NOT_THERE)
316 uint8_t pump_left1_is_full(void)
318 return !!( (mechboard.column_flags & I2C_MECHBOARD_COLUMN_L1) &&
319 (sensor_get_adc(ADC_CSENSE3) > I2C_MECHBOARD_CURRENT_COLUMN));
322 uint8_t pump_left2_is_full(void)
324 return !!( (mechboard.column_flags & I2C_MECHBOARD_COLUMN_L2) &&
325 (sensor_get_adc(ADC_CSENSE4) > I2C_MECHBOARD_CURRENT_COLUMN));
328 uint8_t pump_right1_is_full(void)
330 return !!( (mechboard.column_flags & I2C_MECHBOARD_COLUMN_R1) &&
331 (mechboard.pump_right1_current > I2C_MECHBOARD_CURRENT_COLUMN));
334 uint8_t pump_right2_is_full(void)
336 return !!( (mechboard.column_flags & I2C_MECHBOARD_COLUMN_R2) &&
337 (mechboard.pump_right2_current > I2C_MECHBOARD_CURRENT_COLUMN));
340 /* number of column owned by the robot */
341 uint8_t get_column_count_left(void)
344 ret += pump_left1_is_full();
345 ret += pump_left2_is_full();
349 /* number of column owned by the robot */
350 uint8_t get_column_count_right(void)
353 ret += pump_right1_is_full();
354 ret += pump_right2_is_full();
358 /* number of column owned by the robot */
359 uint8_t get_column_count(void)
362 ret += pump_left1_is_full();
363 ret += pump_left2_is_full();
364 ret += pump_right1_is_full();
365 ret += pump_right2_is_full();
369 uint8_t get_lintel_count(void)
371 return mechboard.lintel_count;
374 uint8_t get_mechboard_mode(void)
376 return mechboard.mode;
379 uint8_t get_scanner_status(void)
381 return sensorboard.scan_status;
384 /* return 0 if timeout, or 1 if cond is true */
385 uint8_t wait_scan_done(uint16_t timeout)
388 err = WAIT_COND_OR_TIMEOUT(get_scanner_status() & I2C_SCAN_DONE, timeout);
392 uint8_t opponent_is_behind(void)
395 int16_t opp_d, opp_a;
397 opp_there = get_opponent_da(&opp_d, &opp_a);
398 if (opp_there && (opp_a < 215 && opp_a > 145) && opp_d < 600)