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>
40 #include <control_system_manager.h>
41 #include <trajectory_manager.h>
42 #include <blocking_detection_manager.h>
43 #include <robot_system.h>
44 #include <position_manager.h>
50 #include "strat_utils.h"
53 static int32_t l_pwm, r_pwm;
54 static int32_t l_enc, r_enc;
60 #define FILTER2 (100-FILTER)
62 /* must be called periodically */
63 void robotsim_update(void)
65 static int32_t l_speed, r_speed;
66 double x, y, a, a2, d;
68 /* corners of the robot */
69 double xfl, yfl; /* front left */
70 double xrl, yrl; /* rear left */
71 double xrr, yrr; /* rear right */
72 double xfr, yfr; /* front right */
74 x = position_get_x_double(&mainboard.pos);
75 y = position_get_y_double(&mainboard.pos);
76 a = position_get_a_rad_double(&mainboard.pos);
78 l_speed = ((l_speed * FILTER) / 100) + ((l_pwm * 1000 * FILTER2)/1000);
79 r_speed = ((r_speed * FILTER) / 100) + ((r_pwm * 1000 * FILTER2)/1000);
81 /* basic collision detection */
82 a2 = atan2(ROBOT_WIDTH/2, ROBOT_LENGTH/2);
83 d = norm(ROBOT_WIDTH/2, ROBOT_LENGTH/2);
85 xfl = x + cos(a+a2) * d;
86 yfl = y + sin(a+a2) * d;
87 if (!is_in_area(xfl, yfl, 0) && l_speed > 0)
90 xrl = x + cos(a+M_PI-a2) * d;
91 yrl = y + sin(a+M_PI-a2) * d;
92 if (!is_in_area(xrl, yrl, 0) && l_speed < 0)
95 xrr = x + cos(a+M_PI+a2) * d;
96 yrr = y + sin(a+M_PI+a2) * d;
97 if (!is_in_area(xrr, yrr, 0) && r_speed < 0)
100 xfr = x + cos(a-a2) * d;
101 yfr = y + sin(a-a2) * d;
102 if (!is_in_area(xfr, yfr, 0) && r_speed > 0)
105 /* XXX should lock */
106 l_enc += (l_speed / 1000);
107 r_enc += (r_speed / 1000);
110 void robotsim_dump(void)
115 len = snprintf(buf, sizeof(buf), "%d %d %d\n",
116 position_get_x_s16(&mainboard.pos),
117 position_get_y_s16(&mainboard.pos),
118 position_get_a_deg_s16(&mainboard.pos));
124 void robotsim_pwm(void *arg, int32_t val)
126 // printf("%p, %d\n", arg, val);
129 else if (arg == RIGHT_PWM)
133 int32_t robotsim_encoder_get(void *arg)
135 if (arg == LEFT_ENCODER)
137 else if (arg == RIGHT_ENCODER)
142 int robotsim_init(void)
144 mkfifo("/tmp/.robot", 0600);
145 fd = open("/tmp/.robot", O_WRONLY, 0);