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>
52 #include "strat_utils.h"
55 static int32_t l_pwm, r_pwm;
56 static int32_t l_enc, r_enc;
62 #define FILTER2 (100-FILTER)
65 void robotsim_dump(void)
70 len = snprintf(buf, sizeof(buf), "%d %d %d\n",
71 position_get_x_s16(&mainboard.pos),
72 position_get_y_s16(&mainboard.pos),
73 position_get_a_deg_s16(&mainboard.pos));
79 /* must be called periodically */
80 void robotsim_update(void)
82 static int32_t l_pwm_shift[SHIFT];
83 static int32_t r_pwm_shift[SHIFT];
84 static int32_t l_speed, r_speed;
85 static unsigned i = 0;
86 static unsigned cpt = 0;
87 int32_t local_l_pwm, local_r_pwm;
88 double x, y, a, a2, d;
91 /* corners of the robot */
92 double xfl, yfl; /* front left */
93 double xrl, yrl; /* rear left */
94 double xrr, yrr; /* rear right */
95 double xfr, yfr; /* front right */
97 /* time shift the command */
98 l_pwm_shift[i] = l_pwm;
99 r_pwm_shift[i] = r_pwm;
102 local_l_pwm = l_pwm_shift[i];
103 local_r_pwm = r_pwm_shift[i];
106 if (((cpt ++) & 0x7) == 0) {
107 if (read(fdr, &cmd, 1) != 1)
111 x = position_get_x_double(&mainboard.pos);
112 y = position_get_y_double(&mainboard.pos);
113 a = position_get_a_rad_double(&mainboard.pos);
115 l_speed = ((l_speed * FILTER) / 100) +
116 ((local_l_pwm * 1000 * FILTER2)/1000);
117 r_speed = ((r_speed * FILTER) / 100) +
118 ((local_r_pwm * 1000 * FILTER2)/1000);
120 /* basic collision detection */
121 a2 = atan2(ROBOT_WIDTH/2, ROBOT_HALF_LENGTH_REAR);
122 d = norm(ROBOT_WIDTH/2, ROBOT_HALF_LENGTH_REAR);
124 xfl = x + cos(a+a2) * d;
125 yfl = y + sin(a+a2) * d;
126 if (!is_in_area(xfl, yfl, 0) && l_speed > 0)
129 xrl = x + cos(a+M_PI-a2) * d;
130 yrl = y + sin(a+M_PI-a2) * d;
131 if (!is_in_area(xrl, yrl, 0) && l_speed < 0)
134 xrr = x + cos(a+M_PI+a2) * d;
135 yrr = y + sin(a+M_PI+a2) * d;
136 if (!is_in_area(xrr, yrr, 0) && r_speed < 0)
139 xfr = x + cos(a-a2) * d;
140 yfr = y + sin(a-a2) * d;
141 if (!is_in_area(xfr, yfr, 0) && r_speed > 0)
146 l_enc += 5000; /* push 1 cm */
148 r_enc += 5000; /* push 1 cm */
150 /* XXX should lock */
151 l_enc += (l_speed / 1000);
152 r_enc += (r_speed / 1000);
155 void robotsim_pwm(void *arg, int32_t val)
157 // printf("%p, %d\n", arg, val);
159 l_pwm = (val / 1.55);
160 else if (arg == RIGHT_PWM)
161 r_pwm = (val / 1.55);
164 int32_t robotsim_encoder_get(void *arg)
166 if (arg == LEFT_ENCODER)
168 else if (arg == RIGHT_ENCODER)
173 int robotsim_init(void)
175 mkfifo("/tmp/.robot_sim2dis", 0600);
176 mkfifo("/tmp/.robot_dis2sim", 0600);
177 fdw = open("/tmp/.robot_sim2dis", O_WRONLY, 0);
180 fdr = open("/tmp/.robot_dis2sim", O_RDONLY | O_NONBLOCK, 0);