merge hostsim in main
[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 "strat.h"
52 #include "strat_utils.h"
53 #include "main.h"
54
55 static int32_t l_pwm, r_pwm;
56 static int32_t l_enc, r_enc;
57
58 static int fdr, fdw;
59
60 /* */
61 #define FILTER  97
62 #define FILTER2 (100-FILTER)
63 #define SHIFT   4
64
65 void robotsim_dump(void)
66 {
67         char buf[BUFSIZ];
68         int len;
69
70         len = snprintf(buf, sizeof(buf), "%d %d %d\n",
71                       position_get_x_s16(&mainboard.pos),
72                       position_get_y_s16(&mainboard.pos),
73                       position_get_a_deg_s16(&mainboard.pos));
74         hostsim_lock();
75         write(fdw, buf, len);
76         hostsim_unlock();
77 }
78
79 /* must be called periodically */
80 void robotsim_update(void)
81 {
82         static int32_t l_pwm_shift[SHIFT];
83         static int32_t r_pwm_shift[SHIFT];
84         static int32_t l_speed, r_speed;
85         static unsigned i = 0;
86         static unsigned cpt = 0;
87         int32_t local_l_pwm, local_r_pwm;
88         double x, y, a, a2, d;
89         char cmd = 0;
90
91         /* corners of the robot */
92         double xfl, yfl; /* front left */
93         double xrl, yrl; /* rear left */
94         double xrr, yrr; /* rear right */
95         double xfr, yfr; /* front right */
96
97         /* time shift the command */
98         l_pwm_shift[i] = l_pwm;
99         r_pwm_shift[i] = r_pwm;
100         i ++;
101         i %= SHIFT;
102         local_l_pwm = l_pwm_shift[i];
103         local_r_pwm = r_pwm_shift[i];
104
105         /* read command */
106         if (((cpt ++) & 0x7) == 0) {
107                 if (read(fdr, &cmd, 1) != 1)
108                         cmd = 0;
109         }
110
111         x = position_get_x_double(&mainboard.pos);
112         y = position_get_y_double(&mainboard.pos);
113         a = position_get_a_rad_double(&mainboard.pos);
114
115         l_speed = ((l_speed * FILTER) / 100) +
116                 ((local_l_pwm * 1000 * FILTER2)/1000);
117         r_speed = ((r_speed * FILTER) / 100) +
118                 ((local_r_pwm * 1000 * FILTER2)/1000);
119
120         /* basic collision detection */
121         a2 = atan2(ROBOT_WIDTH/2, ROBOT_LENGTH/2);
122         d = norm(ROBOT_WIDTH/2, ROBOT_LENGTH/2);
123
124         xfl = x + cos(a+a2) * d;
125         yfl = y + sin(a+a2) * d;
126         if (!is_in_area(xfl, yfl, 0) && l_speed > 0)
127                 l_speed = 0;
128
129         xrl = x + cos(a+M_PI-a2) * d;
130         yrl = y + sin(a+M_PI-a2) * d;
131         if (!is_in_area(xrl, yrl, 0) && l_speed < 0)
132                 l_speed = 0;
133
134         xrr = x + cos(a+M_PI+a2) * d;
135         yrr = y + sin(a+M_PI+a2) * d;
136         if (!is_in_area(xrr, yrr, 0) && r_speed < 0)
137                 r_speed = 0;
138
139         xfr = x + cos(a-a2) * d;
140         yfr = y + sin(a-a2) * d;
141         if (!is_in_area(xfr, yfr, 0) && r_speed > 0)
142                 r_speed = 0;
143
144         /* perturbation */
145         if (cmd == 'l')
146                 l_enc += 5000; /* push 1 cm */
147         if (cmd == 'r')
148                 r_enc += 5000; /* push 1 cm */
149
150         /* XXX should lock */
151         l_enc += (l_speed / 1000);
152         r_enc += (r_speed / 1000);
153 }
154
155 void robotsim_pwm(void *arg, int32_t val)
156 {
157         //      printf("%p, %d\n", arg, val);
158         if (arg == LEFT_PWM)
159                 l_pwm = val;
160         else if (arg == RIGHT_PWM)
161                 r_pwm = val;
162 }
163
164 int32_t robotsim_encoder_get(void *arg)
165 {
166         if (arg == LEFT_ENCODER)
167                 return l_enc;
168         else if (arg == RIGHT_ENCODER)
169                 return r_enc;
170         return 0;
171 }
172
173 int robotsim_init(void)
174 {
175         mkfifo("/tmp/.robot_sim2dis", 0600);
176         mkfifo("/tmp/.robot_dis2sim", 0600);
177         fdw = open("/tmp/.robot_sim2dis", O_WRONLY, 0);
178         if (fdw < 0)
179                 return -1;
180         fdr = open("/tmp/.robot_dis2sim", O_RDONLY | O_NONBLOCK, 0);
181         if (fdr < 0) {
182                 close(fdw);
183                 return -1;
184         }
185         return 0;
186 }