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