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 #define FILTER2 (100-FILTER)
66 void robotsim_dump(void)
72 x = position_get_x_s16(&mainboard.pos);
73 y = position_get_y_s16(&mainboard.pos);
74 a = position_get_a_deg_s16(&mainboard.pos);
78 len = snprintf(buf, sizeof(buf), "pos=%d,%d,%d\n",
86 robotsim_i2c_ballboard_set_mode(struct i2c_cmd_ballboard_set_mode *cmd)
91 ballboard.mode = cmd->mode;
92 len = snprintf(buf, sizeof(buf), "ballboard=%d\n", cmd->mode);
100 robotsim_i2c_cobboard_set_mode(uint8_t mode)
105 if (cobboard.mode == mode)
108 cobboard.mode = mode;
109 len = snprintf(buf, sizeof(buf), "cobboard=%d\n", mode);
111 write(fdw, buf, len);
117 robotsim_i2c_ballboard(uint8_t addr, uint8_t *buf, uint8_t size)
119 void *void_cmd = buf;
122 case I2C_CMD_BALLBOARD_SET_MODE:
124 struct i2c_cmd_ballboard_set_mode *cmd = void_cmd;
125 robotsim_i2c_ballboard_set_mode(cmd);
136 robotsim_i2c_cobboard(uint8_t addr, uint8_t *buf, uint8_t size)
138 // void *void_cmd = buf;
142 case I2C_CMD_COBBOARD_SET_MODE:
144 struct i2c_cmd_cobboard_set_mode *cmd = void_cmd;
145 robotsim_i2c_cobboard_set_mode(cmd);
156 robotsim_i2c(uint8_t addr, uint8_t *buf, uint8_t size)
158 if (addr == I2C_BALLBOARD_ADDR)
159 return robotsim_i2c_ballboard(addr, buf, size);
160 else if (addr == I2C_COBBOARD_ADDR)
161 return robotsim_i2c_cobboard(addr, buf, size);
165 /* must be called periodically */
166 void robotsim_update(void)
168 static int32_t l_pwm_shift[SHIFT];
169 static int32_t r_pwm_shift[SHIFT];
170 static int32_t l_speed, r_speed;
171 static unsigned i = 0;
172 static unsigned cpt = 0;
173 int32_t local_l_pwm, local_r_pwm;
174 double x, y, a, a2, d;
177 /* corners of the robot */
178 double xfl, yfl; /* front left */
179 double xrl, yrl; /* rear left */
180 double xrr, yrr; /* rear right */
181 double xfr, yfr; /* front right */
183 /* time shift the command */
184 l_pwm_shift[i] = l_pwm;
185 r_pwm_shift[i] = r_pwm;
188 local_l_pwm = l_pwm_shift[i];
189 local_r_pwm = r_pwm_shift[i];
192 if (((cpt ++) & 0x7) == 0) {
193 if (read(fdr, &cmd, 1) != 1)
197 x = position_get_x_double(&mainboard.pos);
198 y = position_get_y_double(&mainboard.pos);
199 a = position_get_a_rad_double(&mainboard.pos);
201 l_speed = ((l_speed * FILTER) / 100) +
202 ((local_l_pwm * 1000 * FILTER2)/1000);
203 r_speed = ((r_speed * FILTER) / 100) +
204 ((local_r_pwm * 1000 * FILTER2)/1000);
206 /* basic collision detection */
207 a2 = atan2(ROBOT_WIDTH/2, ROBOT_HALF_LENGTH_REAR);
208 d = norm(ROBOT_WIDTH/2, ROBOT_HALF_LENGTH_REAR);
210 xfl = x + cos(a+a2) * d;
211 yfl = y + sin(a+a2) * d;
212 if (!is_in_area(xfl, yfl, 0) && l_speed > 0)
215 xrl = x + cos(a+M_PI-a2) * d;
216 yrl = y + sin(a+M_PI-a2) * d;
217 if (!is_in_area(xrl, yrl, 0) && l_speed < 0)
220 xrr = x + cos(a+M_PI+a2) * d;
221 yrr = y + sin(a+M_PI+a2) * d;
222 if (!is_in_area(xrr, yrr, 0) && r_speed < 0)
225 xfr = x + cos(a-a2) * d;
226 yfr = y + sin(a-a2) * d;
227 if (!is_in_area(xfr, yfr, 0) && r_speed > 0)
232 l_enc += 5000; /* push 1 cm */
234 r_enc += 5000; /* push 1 cm */
236 /* XXX should lock */
237 l_enc += (l_speed / 1000);
238 r_enc += (r_speed / 1000);
241 void robotsim_pwm(void *arg, int32_t val)
243 // printf("%p, %d\n", arg, val);
245 l_pwm = (val / 1.55);
246 else if (arg == RIGHT_PWM)
247 r_pwm = (val / 1.55);
250 int32_t robotsim_encoder_get(void *arg)
252 if (arg == LEFT_ENCODER)
254 else if (arg == RIGHT_ENCODER)
259 int robotsim_init(void)
261 mkfifo("/tmp/.robot_sim2dis", 0600);
262 mkfifo("/tmp/.robot_dis2sim", 0600);
263 fdw = open("/tmp/.robot_sim2dis", O_WRONLY, 0);
266 fdr = open("/tmp/.robot_dis2sim", O_RDONLY | O_NONBLOCK, 0);