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