avant la coupe de belgique
[aversive.git] / projects / microb2010 / mainboard / robotsim.c
1 /*  
2  *  Copyright Droids Corporation, Microb Technology, Eirbot (2005)
3  * 
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.
8  *
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.
13  *
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
17  *
18  *  Revision : $Id: main.c,v 1.9.4.5 2007-06-01 09:37:22 zer0 Exp $
19  *
20  */
21
22 #include <stdio.h>
23 #include <string.h>
24 #include <stdint.h>
25 #include <unistd.h>
26 #include <sys/stat.h>
27 #include <sys/types.h>
28 #include <fcntl.h>
29 #include <unistd.h>
30
31 #include <aversive.h>
32 #include <aversive/error.h>
33
34 #include <timer.h>
35 #include <scheduler.h>
36 #include <time.h>
37
38 #include <ax12.h>
39 #include <pwm_ng.h>
40 #include <pid.h>
41 #include <quadramp.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>
47
48 #include <parse.h>
49 #include <rdline.h>
50
51 #include "../common/i2c_commands.h"
52 #include "strat.h"
53 #include "strat_utils.h"
54 #include "main.h"
55
56 static int32_t l_pwm, r_pwm;
57 static int32_t l_enc, r_enc;
58
59 static int fdr, fdw;
60
61 /* */
62 #define FILTER  97
63 #define FILTER2 (100-FILTER)
64 #define SHIFT   4
65
66 void robotsim_dump(void)
67 {
68         char buf[BUFSIZ];
69         int len;
70         int16_t x, y, a;
71
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);
75 /*      y = COLOR_Y(y); */
76 /*      a = COLOR_A(a); */
77
78         len = snprintf(buf, sizeof(buf), "pos=%d,%d,%d\n",
79                        x, y, a);
80         hostsim_lock();
81         write(fdw, buf, len);
82         hostsim_unlock();
83 }
84
85 static int8_t
86 robotsim_i2c_ballboard_set_mode(struct i2c_cmd_ballboard_set_mode *cmd)
87 {
88         char buf[BUFSIZ];
89         int len;
90
91         ballboard.mode = cmd->mode;
92         len = snprintf(buf, sizeof(buf), "ballboard=%d\n", cmd->mode);
93         hostsim_lock();
94         write(fdw, buf, len);
95         hostsim_unlock();
96         return 0;
97 }
98
99 int8_t
100 robotsim_i2c_cobboard_set_mode(uint8_t mode)
101 {
102         char buf[BUFSIZ];
103         int len;
104
105         if (cobboard.mode == mode)
106                 return 0;
107
108         cobboard.mode = mode;
109         len = snprintf(buf, sizeof(buf), "cobboard=%d\n", mode);
110         hostsim_lock();
111         write(fdw, buf, len);
112         hostsim_unlock();
113         return 0;
114 }
115
116 static int8_t
117 robotsim_i2c_ballboard(uint8_t addr, uint8_t *buf, uint8_t size)
118 {
119         void *void_cmd = buf;
120
121         switch (buf[0]) {
122         case I2C_CMD_BALLBOARD_SET_MODE:
123                 {
124                         struct i2c_cmd_ballboard_set_mode *cmd = void_cmd;
125                         robotsim_i2c_ballboard_set_mode(cmd);
126                         break;
127                 }
128
129         default:
130                 break;
131         }
132         return 0;
133 }
134
135 static int8_t
136 robotsim_i2c_cobboard(uint8_t addr, uint8_t *buf, uint8_t size)
137 {
138         //      void *void_cmd = buf;
139
140         switch (buf[0]) {
141 #if 0 /* deleted */
142         case I2C_CMD_COBBOARD_SET_MODE:
143                 {
144                         struct i2c_cmd_cobboard_set_mode *cmd = void_cmd;
145                         robotsim_i2c_cobboard_set_mode(cmd);
146                         break;
147                 }
148 #endif
149         default:
150                 break;
151         }
152         return 0;
153 }
154
155 int8_t
156 robotsim_i2c(uint8_t addr, uint8_t *buf, uint8_t size)
157 {
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);
162         return 0;
163 }
164
165 /* must be called periodically */
166 void robotsim_update(void)
167 {
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;
175         char cmd = 0;
176
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 */
182
183         /* time shift the command */
184         l_pwm_shift[i] = l_pwm;
185         r_pwm_shift[i] = r_pwm;
186         i ++;
187         i %= SHIFT;
188         local_l_pwm = l_pwm_shift[i];
189         local_r_pwm = r_pwm_shift[i];
190
191         /* read command */
192         if (((cpt ++) & 0x7) == 0) {
193                 if (read(fdr, &cmd, 1) != 1)
194                         cmd = 0;
195         }
196
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);
200
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);
205
206         /* basic collision detection */
207         a2 = atan2(ROBOT_WIDTH/2, ROBOT_HALF_LENGTH_REAR);
208         d = norm(ROBOT_WIDTH/2, ROBOT_HALF_LENGTH_REAR);
209
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)
213                 l_speed = 0;
214
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)
218                 l_speed = 0;
219
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)
223                 r_speed = 0;
224
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)
228                 r_speed = 0;
229
230         /* perturbation */
231         if (cmd == 'l')
232                 l_enc += 5000; /* push 1 cm */
233         if (cmd == 'r')
234                 r_enc += 5000; /* push 1 cm */
235
236         /* XXX should lock */
237         l_enc += (l_speed / 1000);
238         r_enc += (r_speed / 1000);
239 }
240
241 void robotsim_pwm(void *arg, int32_t val)
242 {
243         //      printf("%p, %d\n", arg, val);
244         if (arg == LEFT_PWM)
245                 l_pwm = (val / 1.55);
246         else if (arg == RIGHT_PWM)
247                 r_pwm = (val / 1.55);
248 }
249
250 int32_t robotsim_encoder_get(void *arg)
251 {
252         if (arg == LEFT_ENCODER)
253                 return l_enc;
254         else if (arg == RIGHT_ENCODER)
255                 return r_enc;
256         return 0;
257 }
258
259 int robotsim_init(void)
260 {
261         mkfifo("/tmp/.robot_sim2dis", 0600);
262         mkfifo("/tmp/.robot_dis2sim", 0600);
263         fdw = open("/tmp/.robot_sim2dis", O_WRONLY, 0);
264         if (fdw < 0)
265                 return -1;
266         fdr = open("/tmp/.robot_dis2sim", O_RDONLY | O_NONBLOCK, 0);
267         if (fdr < 0) {
268                 close(fdw);
269                 return -1;
270         }
271         return 0;
272 }