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)
71 len = snprintf(buf, sizeof(buf), "pos=%d,%d,%d\n",
72 position_get_x_s16(&mainboard.pos),
73 position_get_y_s16(&mainboard.pos),
74 position_get_a_deg_s16(&mainboard.pos));
81 robotsim_i2c_ballboard_set_mode(struct i2c_cmd_ballboard_set_mode *cmd)
86 ballboard.mode = cmd->mode;
87 len = snprintf(buf, sizeof(buf), "ballboard=%d\n", cmd->mode);
95 robotsim_i2c_cobboard_set_mode(struct i2c_cmd_cobboard_set_mode *cmd)
100 cobboard.mode = cmd->mode;
101 len = snprintf(buf, sizeof(buf), "cobboard=%d\n", cmd->mode);
103 write(fdw, buf, len);
109 robotsim_i2c_ballboard(uint8_t addr, uint8_t *buf, uint8_t size)
111 void *void_cmd = buf;
114 case I2C_CMD_BALLBOARD_SET_MODE:
116 struct i2c_cmd_ballboard_set_mode *cmd = void_cmd;
117 robotsim_i2c_ballboard_set_mode(cmd);
128 robotsim_i2c_cobboard(uint8_t addr, uint8_t *buf, uint8_t size)
130 void *void_cmd = buf;
133 case I2C_CMD_COBBOARD_SET_MODE:
135 struct i2c_cmd_cobboard_set_mode *cmd = void_cmd;
136 robotsim_i2c_cobboard_set_mode(cmd);
146 robotsim_i2c(uint8_t addr, uint8_t *buf, uint8_t size)
148 if (addr == I2C_BALLBOARD_ADDR)
149 return robotsim_i2c_ballboard(addr, buf, size);
150 else if (addr == I2C_COBBOARD_ADDR)
151 return robotsim_i2c_cobboard(addr, buf, size);
155 /* must be called periodically */
156 void robotsim_update(void)
158 static int32_t l_pwm_shift[SHIFT];
159 static int32_t r_pwm_shift[SHIFT];
160 static int32_t l_speed, r_speed;
161 static unsigned i = 0;
162 static unsigned cpt = 0;
163 int32_t local_l_pwm, local_r_pwm;
164 double x, y, a, a2, d;
167 /* corners of the robot */
168 double xfl, yfl; /* front left */
169 double xrl, yrl; /* rear left */
170 double xrr, yrr; /* rear right */
171 double xfr, yfr; /* front right */
173 /* time shift the command */
174 l_pwm_shift[i] = l_pwm;
175 r_pwm_shift[i] = r_pwm;
178 local_l_pwm = l_pwm_shift[i];
179 local_r_pwm = r_pwm_shift[i];
182 if (((cpt ++) & 0x7) == 0) {
183 if (read(fdr, &cmd, 1) != 1)
187 x = position_get_x_double(&mainboard.pos);
188 y = position_get_y_double(&mainboard.pos);
189 a = position_get_a_rad_double(&mainboard.pos);
191 l_speed = ((l_speed * FILTER) / 100) +
192 ((local_l_pwm * 1000 * FILTER2)/1000);
193 r_speed = ((r_speed * FILTER) / 100) +
194 ((local_r_pwm * 1000 * FILTER2)/1000);
196 /* basic collision detection */
197 a2 = atan2(ROBOT_WIDTH/2, ROBOT_HALF_LENGTH_REAR);
198 d = norm(ROBOT_WIDTH/2, ROBOT_HALF_LENGTH_REAR);
200 xfl = x + cos(a+a2) * d;
201 yfl = y + sin(a+a2) * d;
202 if (!is_in_area(xfl, yfl, 0) && l_speed > 0)
205 xrl = x + cos(a+M_PI-a2) * d;
206 yrl = y + sin(a+M_PI-a2) * d;
207 if (!is_in_area(xrl, yrl, 0) && l_speed < 0)
210 xrr = x + cos(a+M_PI+a2) * d;
211 yrr = y + sin(a+M_PI+a2) * d;
212 if (!is_in_area(xrr, yrr, 0) && r_speed < 0)
215 xfr = x + cos(a-a2) * d;
216 yfr = y + sin(a-a2) * d;
217 if (!is_in_area(xfr, yfr, 0) && r_speed > 0)
222 l_enc += 5000; /* push 1 cm */
224 r_enc += 5000; /* push 1 cm */
226 /* XXX should lock */
227 l_enc += (l_speed / 1000);
228 r_enc += (r_speed / 1000);
231 void robotsim_pwm(void *arg, int32_t val)
233 // printf("%p, %d\n", arg, val);
235 l_pwm = (val / 1.55);
236 else if (arg == RIGHT_PWM)
237 r_pwm = (val / 1.55);
240 int32_t robotsim_encoder_get(void *arg)
242 if (arg == LEFT_ENCODER)
244 else if (arg == RIGHT_ENCODER)
249 int robotsim_init(void)
251 mkfifo("/tmp/.robot_sim2dis", 0600);
252 mkfifo("/tmp/.robot_dis2sim", 0600);
253 fdw = open("/tmp/.robot_sim2dis", O_WRONLY, 0);
256 fdr = open("/tmp/.robot_dis2sim", O_RDONLY | O_NONBLOCK, 0);