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>
51 #include "../common/i2c_commands.h"
53 #include "strat_utils.h"
56 static int32_t l_pwm, r_pwm;
57 static int32_t l_enc, r_enc;
63 * (gdb) handle SIGUSR1 pass
64 * Signal Stop Print Pass to program Description
65 * SIGUSR1 Yes Yes Yes User defined signal 1
66 * (gdb) handle SIGUSR2 pass
67 * Signal Stop Print Pass to program Description
68 * SIGUSR2 Yes Yes Yes User defined signal 2
69 * (gdb) handle SIGUSR1 noprint
70 * Signal Stop Print Pass to program Description
71 * SIGUSR1 No No Yes User defined signal 1
72 * (gdb) handle SIGUSR2 noprint
77 #define FILTER2 (100-FILTER)
80 void robotsim_dump(void)
86 x = position_get_x_s16(&mainboard.pos);
87 y = position_get_y_s16(&mainboard.pos);
88 a = position_get_a_deg_s16(&mainboard.pos);
92 len = snprintf(buf, sizeof(buf), "pos=%d,%d,%d\n",
100 robotsim_i2c_ballboard_set_mode(struct i2c_cmd_ballboard_set_mode *cmd)
105 ballboard.mode = cmd->mode;
106 len = snprintf(buf, sizeof(buf), "ballboard=%d\n", cmd->mode);
108 write(fdw, buf, len);
114 robotsim_i2c_cobboard_set_spickles(uint8_t side, uint8_t flags)
119 if (side == I2C_LEFT_SIDE) {
120 if (cobboard.lspickle == flags)
123 cobboard.lspickle = flags;
125 if (side == I2C_RIGHT_SIDE) {
126 if (cobboard.rspickle == flags)
129 cobboard.rspickle = flags;
132 len = snprintf(buf, sizeof(buf), "cobboard=%d,%d\n", side, flags);
134 write(fdw, buf, len);
140 robotsim_i2c_ballboard(uint8_t addr, uint8_t *buf, uint8_t size)
142 void *void_cmd = buf;
145 case I2C_CMD_BALLBOARD_SET_MODE:
147 struct i2c_cmd_ballboard_set_mode *cmd = void_cmd;
148 robotsim_i2c_ballboard_set_mode(cmd);
159 robotsim_i2c_cobboard(uint8_t addr, uint8_t *buf, uint8_t size)
161 // void *void_cmd = buf;
165 case I2C_CMD_COBBOARD_SET_MODE:
167 struct i2c_cmd_cobboard_set_mode *cmd = void_cmd;
168 robotsim_i2c_cobboard_set_mode(cmd);
179 robotsim_i2c(uint8_t addr, uint8_t *buf, uint8_t size)
181 if (addr == I2C_BALLBOARD_ADDR)
182 return robotsim_i2c_ballboard(addr, buf, size);
183 else if (addr == I2C_COBBOARD_ADDR)
184 return robotsim_i2c_cobboard(addr, buf, size);
188 /* must be called periodically */
189 void robotsim_update(void)
191 static int32_t l_pwm_shift[SHIFT];
192 static int32_t r_pwm_shift[SHIFT];
193 static int32_t l_speed, r_speed;
194 static unsigned i = 0;
195 static unsigned cpt = 0;
196 int32_t local_l_pwm, local_r_pwm;
197 double x, y, a, a2, d;
200 /* corners of the robot */
201 double xfl, yfl; /* front left */
202 double xrl, yrl; /* rear left */
203 double xrr, yrr; /* rear right */
204 double xfr, yfr; /* front right */
206 /* time shift the command */
207 l_pwm_shift[i] = l_pwm;
208 r_pwm_shift[i] = r_pwm;
211 local_l_pwm = l_pwm_shift[i];
212 local_r_pwm = r_pwm_shift[i];
215 if (((cpt ++) & 0x7) == 0) {
216 if (read(fdr, &cmd, 1) != 1)
220 x = position_get_x_double(&mainboard.pos);
221 y = position_get_y_double(&mainboard.pos);
222 a = position_get_a_rad_double(&mainboard.pos);
224 l_speed = ((l_speed * FILTER) / 100) +
225 ((local_l_pwm * 1000 * FILTER2)/1000);
226 r_speed = ((r_speed * FILTER) / 100) +
227 ((local_r_pwm * 1000 * FILTER2)/1000);
229 /* basic collision detection */
230 a2 = atan2(ROBOT_WIDTH/2, ROBOT_HALF_LENGTH_REAR);
231 d = norm(ROBOT_WIDTH/2, ROBOT_HALF_LENGTH_REAR);
233 xfl = x + cos(a+a2) * d;
234 yfl = y + sin(a+a2) * d;
235 if (!is_in_area(xfl, yfl, 0) && l_speed > 0)
238 xrl = x + cos(a+M_PI-a2) * d;
239 yrl = y + sin(a+M_PI-a2) * d;
240 if (!is_in_area(xrl, yrl, 0) && l_speed < 0)
243 xrr = x + cos(a+M_PI+a2) * d;
244 yrr = y + sin(a+M_PI+a2) * d;
245 if (!is_in_area(xrr, yrr, 0) && r_speed < 0)
248 xfr = x + cos(a-a2) * d;
249 yfr = y + sin(a-a2) * d;
250 if (!is_in_area(xfr, yfr, 0) && r_speed > 0)
255 l_enc += 5000; /* push 1 cm */
257 r_enc += 5000; /* push 1 cm */
259 /* XXX should lock */
260 l_enc += (l_speed / 1000);
261 r_enc += (r_speed / 1000);
264 void robotsim_pwm(void *arg, int32_t val)
266 // printf("%p, %d\n", arg, val);
268 l_pwm = (val / 1.55);
269 else if (arg == RIGHT_PWM)
270 r_pwm = (val / 1.55);
273 int32_t robotsim_encoder_get(void *arg)
275 if (arg == LEFT_ENCODER)
277 else if (arg == RIGHT_ENCODER)
282 int robotsim_init(void)
284 mkfifo("/tmp/.robot_sim2dis", 0600);
285 mkfifo("/tmp/.robot_dis2sim", 0600);
286 fdw = open("/tmp/.robot_sim2dis", O_WRONLY, 0);
289 fdr = open("/tmp/.robot_dis2sim", O_RDONLY | O_NONBLOCK, 0);