hostsim enhancements
[aversive.git] / projects / microb2010 / tests / hostsim / 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 <sys/types.h>
26 #include <sys/stat.h>
27 #include <fcntl.h>
28 #include <unistd.h>
29
30 #include <aversive.h>
31 #include <aversive/error.h>
32
33 #include <timer.h>
34 #include <scheduler.h>
35 #include <time.h>
36
37 #include <pid.h>
38 #include <quadramp.h>
39 #include <control_system_manager.h>
40 #include <trajectory_manager.h>
41 #include <blocking_detection_manager.h>
42 #include <robot_system.h>
43 #include <position_manager.h>
44
45 #include <parse.h>
46 #include <rdline.h>
47
48 #include "strat.h"
49 #include "strat_utils.h"
50 #include "main.h"
51
52 static int32_t l_pwm, r_pwm;
53 static int32_t l_enc, r_enc;
54
55 static int fd;
56
57 /* */
58 #define FILTER  97
59 #define FILTER2 (100-FILTER)
60
61 /* must be called periodically */
62 void robotsim_update(void)
63 {
64         static int32_t l_speed, r_speed;
65         double x, y, a, a2, d;
66
67         /* corners of the robot */
68         double xfl, yfl; /* front left */
69         double xrl, yrl; /* rear left */
70         double xrr, yrr; /* rear right */
71         double xfr, yfr; /* front right */
72
73         x = position_get_x_double(&mainboard.pos);
74         y = position_get_y_double(&mainboard.pos);
75         a = position_get_a_rad_double(&mainboard.pos);
76
77         l_speed = ((l_speed * FILTER) / 100) + ((l_pwm * 1000 * FILTER2)/1000);
78         r_speed = ((r_speed * FILTER) / 100) + ((r_pwm * 1000 * FILTER2)/1000);
79
80         /* basic collision detection */
81         a2 = atan2(ROBOT_WIDTH/2, ROBOT_LENGTH/2);
82         d = norm(ROBOT_WIDTH/2, ROBOT_LENGTH/2);
83
84         xfl = x + cos(a+a2) * d;
85         yfl = y + sin(a+a2) * d;
86         if (!is_in_area(xfl, yfl, 0) && l_speed > 0)
87                 l_speed = 0;
88
89         xrl = x + cos(a+M_PI-a2) * d;
90         yrl = y + sin(a+M_PI-a2) * d;
91         if (!is_in_area(xrl, yrl, 0) && l_speed < 0)
92                 l_speed = 0;
93
94         xrr = x + cos(a+M_PI+a2) * d;
95         yrr = y + sin(a+M_PI+a2) * d;
96         if (!is_in_area(xrr, yrr, 0) && r_speed < 0)
97                 r_speed = 0;
98
99         xfr = x + cos(a-a2) * d;
100         yfr = y + sin(a-a2) * d;
101         if (!is_in_area(xfr, yfr, 0) && r_speed > 0)
102                 r_speed = 0;
103
104         /* XXX should lock */
105         l_enc += (l_speed / 1000);
106         r_enc += (r_speed / 1000);
107 }
108
109 void robotsim_dump(void)
110 {
111         char buf[BUFSIZ];
112         int len;
113
114         len =snprintf(buf, sizeof(buf), "%d %d %d\n",
115                       position_get_x_s16(&mainboard.pos),
116                       position_get_y_s16(&mainboard.pos),
117                       position_get_a_deg_s16(&mainboard.pos));
118         write(fd, buf, len);
119 }
120
121 void robotsim_pwm(void *arg, int32_t val)
122 {
123         //      printf("%p, %d\n", arg, val);
124         if (arg == LEFT_PWM)
125                 l_pwm = val;
126         else if (arg == RIGHT_PWM)
127                 r_pwm = val;
128 }
129
130 int32_t robotsim_encoder_get(void *arg)
131 {
132         if (arg == LEFT_ENCODER)
133                 return l_enc;
134         else if (arg == RIGHT_ENCODER)
135                 return r_enc;
136         return 0;
137 }
138
139 int robotsim_init(void)
140 {
141         mkfifo("/tmp/.robot", 0600);
142         fd = open("/tmp/.robot", O_WRONLY, 0);
143         if (fd < 0)
144                 return -1;
145         return 0;
146 }