20100416
[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
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));
75         hostsim_lock();
76         write(fdw, buf, len);
77         hostsim_unlock();
78 }
79
80 static int8_t
81 robotsim_i2c_ballboard_set_mode(struct i2c_cmd_ballboard_set_mode *cmd)
82 {
83         char buf[BUFSIZ];
84         int len;
85
86         ballboard.mode = cmd->mode;
87         len = snprintf(buf, sizeof(buf), "ballboard=%d\n", cmd->mode);
88         hostsim_lock();
89         write(fdw, buf, len);
90         hostsim_unlock();
91         return 0;
92 }
93
94 int8_t
95 robotsim_i2c_cobboard_set_mode(uint8_t mode)
96 {
97         char buf[BUFSIZ];
98         int len;
99
100         if (cobboard.mode == mode)
101                 return 0;
102
103         cobboard.mode = mode;
104         len = snprintf(buf, sizeof(buf), "cobboard=%d\n", mode);
105         hostsim_lock();
106         write(fdw, buf, len);
107         hostsim_unlock();
108         return 0;
109 }
110
111 static int8_t
112 robotsim_i2c_ballboard(uint8_t addr, uint8_t *buf, uint8_t size)
113 {
114         void *void_cmd = buf;
115
116         switch (buf[0]) {
117         case I2C_CMD_BALLBOARD_SET_MODE:
118                 {
119                         struct i2c_cmd_ballboard_set_mode *cmd = void_cmd;
120                         robotsim_i2c_ballboard_set_mode(cmd);
121                         break;
122                 }
123
124         default:
125                 break;
126         }
127         return 0;
128 }
129
130 static int8_t
131 robotsim_i2c_cobboard(uint8_t addr, uint8_t *buf, uint8_t size)
132 {
133         //      void *void_cmd = buf;
134
135         switch (buf[0]) {
136 #if 0 /* deleted */
137         case I2C_CMD_COBBOARD_SET_MODE:
138                 {
139                         struct i2c_cmd_cobboard_set_mode *cmd = void_cmd;
140                         robotsim_i2c_cobboard_set_mode(cmd);
141                         break;
142                 }
143 #endif
144         default:
145                 break;
146         }
147         return 0;
148 }
149
150 int8_t
151 robotsim_i2c(uint8_t addr, uint8_t *buf, uint8_t size)
152 {
153         if (addr == I2C_BALLBOARD_ADDR)
154                 return robotsim_i2c_ballboard(addr, buf, size);
155         else if (addr == I2C_COBBOARD_ADDR)
156                 return robotsim_i2c_cobboard(addr, buf, size);
157         return 0;
158 }
159
160 /* must be called periodically */
161 void robotsim_update(void)
162 {
163         static int32_t l_pwm_shift[SHIFT];
164         static int32_t r_pwm_shift[SHIFT];
165         static int32_t l_speed, r_speed;
166         static unsigned i = 0;
167         static unsigned cpt = 0;
168         int32_t local_l_pwm, local_r_pwm;
169         double x, y, a, a2, d;
170         char cmd = 0;
171
172         /* corners of the robot */
173         double xfl, yfl; /* front left */
174         double xrl, yrl; /* rear left */
175         double xrr, yrr; /* rear right */
176         double xfr, yfr; /* front right */
177
178         /* time shift the command */
179         l_pwm_shift[i] = l_pwm;
180         r_pwm_shift[i] = r_pwm;
181         i ++;
182         i %= SHIFT;
183         local_l_pwm = l_pwm_shift[i];
184         local_r_pwm = r_pwm_shift[i];
185
186         /* read command */
187         if (((cpt ++) & 0x7) == 0) {
188                 if (read(fdr, &cmd, 1) != 1)
189                         cmd = 0;
190         }
191
192         x = position_get_x_double(&mainboard.pos);
193         y = position_get_y_double(&mainboard.pos);
194         a = position_get_a_rad_double(&mainboard.pos);
195
196         l_speed = ((l_speed * FILTER) / 100) +
197                 ((local_l_pwm * 1000 * FILTER2)/1000);
198         r_speed = ((r_speed * FILTER) / 100) +
199                 ((local_r_pwm * 1000 * FILTER2)/1000);
200
201         /* basic collision detection */
202         a2 = atan2(ROBOT_WIDTH/2, ROBOT_HALF_LENGTH_REAR);
203         d = norm(ROBOT_WIDTH/2, ROBOT_HALF_LENGTH_REAR);
204
205         xfl = x + cos(a+a2) * d;
206         yfl = y + sin(a+a2) * d;
207         if (!is_in_area(xfl, yfl, 0) && l_speed > 0)
208                 l_speed = 0;
209
210         xrl = x + cos(a+M_PI-a2) * d;
211         yrl = y + sin(a+M_PI-a2) * d;
212         if (!is_in_area(xrl, yrl, 0) && l_speed < 0)
213                 l_speed = 0;
214
215         xrr = x + cos(a+M_PI+a2) * d;
216         yrr = y + sin(a+M_PI+a2) * d;
217         if (!is_in_area(xrr, yrr, 0) && r_speed < 0)
218                 r_speed = 0;
219
220         xfr = x + cos(a-a2) * d;
221         yfr = y + sin(a-a2) * d;
222         if (!is_in_area(xfr, yfr, 0) && r_speed > 0)
223                 r_speed = 0;
224
225         /* perturbation */
226         if (cmd == 'l')
227                 l_enc += 5000; /* push 1 cm */
228         if (cmd == 'r')
229                 r_enc += 5000; /* push 1 cm */
230
231         /* XXX should lock */
232         l_enc += (l_speed / 1000);
233         r_enc += (r_speed / 1000);
234 }
235
236 void robotsim_pwm(void *arg, int32_t val)
237 {
238         //      printf("%p, %d\n", arg, val);
239         if (arg == LEFT_PWM)
240                 l_pwm = (val / 1.55);
241         else if (arg == RIGHT_PWM)
242                 r_pwm = (val / 1.55);
243 }
244
245 int32_t robotsim_encoder_get(void *arg)
246 {
247         if (arg == LEFT_ENCODER)
248                 return l_enc;
249         else if (arg == RIGHT_ENCODER)
250                 return r_enc;
251         return 0;
252 }
253
254 int robotsim_init(void)
255 {
256         mkfifo("/tmp/.robot_sim2dis", 0600);
257         mkfifo("/tmp/.robot_dis2sim", 0600);
258         fdw = open("/tmp/.robot_sim2dis", O_WRONLY, 0);
259         if (fdw < 0)
260                 return -1;
261         fdr = open("/tmp/.robot_dis2sim", O_RDONLY | O_NONBLOCK, 0);
262         if (fdr < 0) {
263                 close(fdw);
264                 return -1;
265         }
266         return 0;
267 }