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 static int32_t l_pwm, r_pwm;
58 static int32_t l_enc, r_enc;
64 * (gdb) handle SIGUSR1 pass
65 * Signal Stop Print Pass to program Description
66 * SIGUSR1 Yes Yes Yes User defined signal 1
67 * (gdb) handle SIGUSR2 pass
68 * Signal Stop Print Pass to program Description
69 * SIGUSR2 Yes Yes Yes User defined signal 2
70 * (gdb) handle SIGUSR1 noprint
71 * Signal Stop Print Pass to program Description
72 * SIGUSR1 No No Yes User defined signal 1
73 * (gdb) handle SIGUSR2 noprint
78 #define FILTER2 (100-FILTER)
81 void robotsim_dump(void)
87 x = position_get_x_s16(&mainboard.pos);
88 y = position_get_y_s16(&mainboard.pos);
89 a = position_get_a_deg_s16(&mainboard.pos);
93 len = snprintf(buf, sizeof(buf), "pos=%d,%d,%d\n",
101 robotsim_i2c_ballboard_set_mode(struct i2c_cmd_ballboard_set_mode *cmd)
106 ballboard.mode = cmd->mode;
107 len = snprintf(buf, sizeof(buf), "ballboard=%d\n", cmd->mode);
108 if (cmd->mode == I2C_BALLBOARD_MODE_EJECT)
109 ballboard.ball_count = 0;
111 write(fdw, buf, len);
117 robotsim_i2c_cobboard_set_mode(struct i2c_cmd_cobboard_set_mode *cmd)
119 if (cmd->mode == I2C_COBBOARD_MODE_EJECT)
120 cobboard.cob_count = 0;
125 robotsim_i2c_cobboard_set_spickles(uint8_t side, uint8_t flags)
130 if (side == I2C_LEFT_SIDE) {
131 if (cobboard.lspickle == flags)
134 cobboard.lspickle = flags;
136 if (side == I2C_RIGHT_SIDE) {
137 if (cobboard.rspickle == flags)
140 cobboard.rspickle = flags;
143 len = snprintf(buf, sizeof(buf), "cobboard=%d,%d\n", side, flags);
145 write(fdw, buf, len);
151 robotsim_i2c_ballboard(uint8_t addr, uint8_t *buf, uint8_t size)
153 void *void_cmd = buf;
156 case I2C_CMD_BALLBOARD_SET_MODE:
158 struct i2c_cmd_ballboard_set_mode *cmd = void_cmd;
159 robotsim_i2c_ballboard_set_mode(cmd);
170 robotsim_i2c_cobboard(uint8_t addr, uint8_t *buf, uint8_t size)
172 void *void_cmd = buf;
176 case I2C_CMD_COBBOARD_SET_MODE:
178 struct i2c_cmd_cobboard_set_mode *cmd = void_cmd;
179 robotsim_i2c_cobboard_set_mode(cmd);
190 robotsim_i2c(uint8_t addr, uint8_t *buf, uint8_t size)
192 if (addr == I2C_BALLBOARD_ADDR)
193 return robotsim_i2c_ballboard(addr, buf, size);
194 else if (addr == I2C_COBBOARD_ADDR)
195 return robotsim_i2c_cobboard(addr, buf, size);
199 /* must be called periodically */
200 void robotsim_update(void)
202 static int32_t l_pwm_shift[SHIFT];
203 static int32_t r_pwm_shift[SHIFT];
204 static int32_t l_speed, r_speed;
205 static unsigned i = 0;
206 static unsigned cpt = 0;
209 int32_t local_l_pwm, local_r_pwm;
210 double x, y, a, a2, d;
212 int n, pertl = 0, pertr = 0;
214 /* corners of the robot */
215 double xfl, yfl; /* front left */
216 double xrl, yrl; /* rear left */
217 double xrr, yrr; /* rear right */
218 double xfr, yfr; /* front right */
223 /* time shift the command */
224 l_pwm_shift[i] = l_pwm;
225 r_pwm_shift[i] = r_pwm;
228 local_l_pwm = l_pwm_shift[i];
229 local_r_pwm = r_pwm_shift[i];
232 if (((cpt ++) & 0x7) == 0) {
233 n = read(fdr, &cmd, BUFSIZ - 1);
242 else if (cmd[0] == 'r')
245 if (sscanf(cmd, "opp %d %d", &oppx, &oppy) == 2) {
246 abs_xy_to_rel_da(oppx, oppy, &oppd, &oppa);
248 beaconboard.oppx = oppx;
249 beaconboard.oppy = oppy;
250 beaconboard.oppa = DEG(oppa);
251 if (beaconboard.oppa < 0)
252 beaconboard.oppa += 360;
253 beaconboard.oppd = oppd;
258 x = position_get_x_double(&mainboard.pos);
259 y = position_get_y_double(&mainboard.pos);
260 a = position_get_a_rad_double(&mainboard.pos);
262 l_speed = ((l_speed * FILTER) / 100) +
263 ((local_l_pwm * 1000 * FILTER2)/1000);
264 r_speed = ((r_speed * FILTER) / 100) +
265 ((local_r_pwm * 1000 * FILTER2)/1000);
267 /* basic collision detection */
268 a2 = atan2(ROBOT_WIDTH/2, ROBOT_HALF_LENGTH_REAR);
269 d = norm(ROBOT_WIDTH/2, ROBOT_HALF_LENGTH_REAR);
271 xfl = x + cos(a+a2) * d;
272 yfl = y + sin(a+a2) * d;
273 if (!is_in_area(xfl, yfl, 0) && l_speed > 0)
276 xrl = x + cos(a+M_PI-a2) * d;
277 yrl = y + sin(a+M_PI-a2) * d;
278 if (!is_in_area(xrl, yrl, 0) && l_speed < 0)
281 xrr = x + cos(a+M_PI+a2) * d;
282 yrr = y + sin(a+M_PI+a2) * d;
283 if (!is_in_area(xrr, yrr, 0) && r_speed < 0)
286 xfr = x + cos(a-a2) * d;
287 yfr = y + sin(a-a2) * d;
288 if (!is_in_area(xfr, yfr, 0) && r_speed > 0)
292 l_enc += 5000; /* push 1 cm */
294 r_enc += 5000; /* push 1 cm */
296 /* XXX should lock */
297 l_enc += (l_speed / 1000);
298 r_enc += (r_speed / 1000);
301 void robotsim_pwm(void *arg, int32_t val)
303 // printf("%p, %d\n", arg, val);
305 l_pwm = (val / 1.55);
306 else if (arg == RIGHT_PWM)
307 r_pwm = (val / 1.55);
310 int32_t robotsim_encoder_get(void *arg)
312 if (arg == LEFT_ENCODER)
314 else if (arg == RIGHT_ENCODER)
319 int robotsim_init(void)
321 mkfifo("/tmp/.robot_sim2dis", 0600);
322 mkfifo("/tmp/.robot_dis2sim", 0600);
323 fdw = open("/tmp/.robot_sim2dis", O_WRONLY, 0);
326 fdr = open("/tmp/.robot_dis2sim", O_RDONLY | O_NONBLOCK, 0);