2 * Copyright Droids Corporation, Microb Technology, Eirbot (2005)
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: main.c,v 1.9.4.5 2007-06-01 09:37:22 zer0 Exp $
27 #include <sys/types.h>
32 #include <aversive/error.h>
35 #include <scheduler.h>
42 #include <control_system_manager.h>
43 #include <trajectory_manager.h>
44 #include <blocking_detection_manager.h>
45 #include <robot_system.h>
46 #include <position_manager.h>
47 #include <trajectory_manager_utils.h>
52 #include "../common/i2c_commands.h"
54 #include "strat_utils.h"
57 uint8_t robotsim_blocking = 0;
59 static int32_t l_pwm, r_pwm;
60 static int32_t l_enc, r_enc;
66 * (gdb) handle SIGUSR1 pass
67 * Signal Stop Print Pass to program Description
68 * SIGUSR1 Yes Yes Yes User defined signal 1
69 * (gdb) handle SIGUSR2 pass
70 * Signal Stop Print Pass to program Description
71 * SIGUSR2 Yes Yes Yes User defined signal 2
72 * (gdb) handle SIGUSR1 noprint
73 * Signal Stop Print Pass to program Description
74 * SIGUSR1 No No Yes User defined signal 1
75 * (gdb) handle SIGUSR2 noprint
80 #define FILTER2 (100-FILTER)
83 void robotsim_dump(void)
89 x = position_get_x_s16(&mainboard.pos);
90 y = position_get_y_s16(&mainboard.pos);
91 a = position_get_a_deg_s16(&mainboard.pos);
95 len = snprintf(buf, sizeof(buf), "pos=%d,%d,%d\n",
103 robotsim_i2c_ballboard_set_mode(struct i2c_cmd_ballboard_set_mode *cmd)
108 ballboard.mode = cmd->mode;
109 len = snprintf(buf, sizeof(buf), "ballboard=%d\n", cmd->mode);
110 if (cmd->mode == I2C_BALLBOARD_MODE_EJECT)
111 ballboard.ball_count = 0;
113 write(fdw, buf, len);
119 robotsim_i2c_cobboard_set_mode(struct i2c_cmd_cobboard_set_mode *cmd)
121 if (cmd->mode == I2C_COBBOARD_MODE_EJECT)
122 cobboard.cob_count = 0;
127 robotsim_i2c_cobboard_set_spickles(uint8_t side, uint8_t flags)
132 if (side == I2C_LEFT_SIDE) {
133 if (cobboard.lspickle == flags)
136 cobboard.lspickle = flags;
138 if (side == I2C_RIGHT_SIDE) {
139 if (cobboard.rspickle == flags)
142 cobboard.rspickle = flags;
145 len = snprintf(buf, sizeof(buf), "cobboard=%d,%d\n", side, flags);
147 write(fdw, buf, len);
153 robotsim_i2c_ballboard(uint8_t addr, uint8_t *buf, uint8_t size)
155 void *void_cmd = buf;
158 case I2C_CMD_BALLBOARD_SET_MODE:
160 struct i2c_cmd_ballboard_set_mode *cmd = void_cmd;
161 robotsim_i2c_ballboard_set_mode(cmd);
172 robotsim_i2c_cobboard(uint8_t addr, uint8_t *buf, uint8_t size)
174 void *void_cmd = buf;
178 case I2C_CMD_COBBOARD_SET_MODE:
180 struct i2c_cmd_cobboard_set_mode *cmd = void_cmd;
181 robotsim_i2c_cobboard_set_mode(cmd);
192 robotsim_i2c(uint8_t addr, uint8_t *buf, uint8_t size)
194 if (addr == I2C_BALLBOARD_ADDR)
195 return robotsim_i2c_ballboard(addr, buf, size);
196 else if (addr == I2C_COBBOARD_ADDR)
197 return robotsim_i2c_cobboard(addr, buf, size);
201 static void beacon_update(void)
208 if (ballboard.opponent_x == I2C_OPPONENT_NOT_THERE) {
212 oppx = ballboard.opponent_x;
213 oppy = ballboard.opponent_y;
214 abs_xy_to_rel_da(oppx, oppy, &oppd, &oppa);
215 ballboard.opponent_a = DEG(oppa);
216 if (ballboard.opponent_a < 0)
217 ballboard.opponent_a += 360;
218 ballboard.opponent_d = oppd;
222 /* must be called periodically */
223 void robotsim_update(void)
225 static int32_t l_pwm_shift[SHIFT];
226 static int32_t r_pwm_shift[SHIFT];
227 static int32_t l_speed, r_speed;
228 static unsigned i = 0;
229 static unsigned cpt = 0;
232 int32_t local_l_pwm, local_r_pwm;
233 double x, y, a, a2, d;
235 int n, pertl = 0, pertr = 0;
237 /* corners of the robot */
238 double xfl, yfl; /* front left */
239 double xrl, yrl; /* rear left */
240 double xrr, yrr; /* rear right */
241 double xfr, yfr; /* front right */
248 /* time shift the command */
249 l_pwm_shift[i] = l_pwm;
250 r_pwm_shift[i] = r_pwm;
253 local_l_pwm = l_pwm_shift[i];
254 local_r_pwm = r_pwm_shift[i];
257 if (((cpt ++) & 0x7) == 0) {
258 n = read(fdr, &cmd, BUFSIZ - 1);
267 else if (cmd[0] == 'r')
269 else if (cmd[0] == 'b')
270 robotsim_blocking = 1;
272 if (sscanf(cmd, "opp %d %d", &oppx, &oppy) == 2) {
273 abs_xy_to_rel_da(oppx, oppy, &oppd, &oppa);
275 ballboard.opponent_x = oppx;
276 ballboard.opponent_y = oppy;
277 ballboard.opponent_a = DEG(oppa);
278 if (ballboard.opponent_a < 0)
279 ballboard.opponent_a += 360;
280 ballboard.opponent_d = oppd;
285 x = position_get_x_double(&mainboard.pos);
286 y = position_get_y_double(&mainboard.pos);
287 a = position_get_a_rad_double(&mainboard.pos);
289 l_speed = ((l_speed * FILTER) / 100) +
290 ((local_l_pwm * 1000 * FILTER2)/1000);
291 r_speed = ((r_speed * FILTER) / 100) +
292 ((local_r_pwm * 1000 * FILTER2)/1000);
294 /* basic collision detection */
295 a2 = atan2(ROBOT_WIDTH/2, ROBOT_HALF_LENGTH_REAR);
296 d = norm(ROBOT_WIDTH/2, ROBOT_HALF_LENGTH_REAR);
298 xfl = x + cos(a+a2) * d;
299 yfl = y + sin(a+a2) * d;
300 if (!is_in_area(xfl, yfl, 0) && l_speed > 0)
303 xrl = x + cos(a+M_PI-a2) * d;
304 yrl = y + sin(a+M_PI-a2) * d;
305 if (!is_in_area(xrl, yrl, 0) && l_speed < 0)
308 xrr = x + cos(a+M_PI+a2) * d;
309 yrr = y + sin(a+M_PI+a2) * d;
310 if (!is_in_area(xrr, yrr, 0) && r_speed < 0)
313 xfr = x + cos(a-a2) * d;
314 yfr = y + sin(a-a2) * d;
315 if (!is_in_area(xfr, yfr, 0) && r_speed > 0)
319 l_enc += 5000; /* push 1 cm */
321 r_enc += 5000; /* push 1 cm */
323 /* XXX should lock */
324 l_enc += (l_speed / 1000);
325 r_enc += (r_speed / 1000);
328 void robotsim_pwm(void *arg, int32_t val)
330 // printf("%p, %d\n", arg, val);
332 l_pwm = (val / 1.55);
333 else if (arg == RIGHT_PWM)
334 r_pwm = (val / 1.55);
337 int32_t robotsim_encoder_get(void *arg)
339 if (arg == LEFT_ENCODER)
341 else if (arg == RIGHT_ENCODER)
346 int robotsim_init(void)
348 mkfifo("/tmp/.robot_sim2dis", 0600);
349 mkfifo("/tmp/.robot_dis2sim", 0600);
350 fdw = open("/tmp/.robot_sim2dis", O_WRONLY, 0);
353 fdr = open("/tmp/.robot_dis2sim", O_RDONLY | O_NONBLOCK, 0);