robotsim first test
[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
29 #include <aversive.h>
30 #include <aversive/error.h>
31
32 #include <timer.h>
33 #include <scheduler.h>
34 #include <time.h>
35
36 #include <pid.h>
37 #include <quadramp.h>
38 #include <control_system_manager.h>
39 #include <trajectory_manager.h>
40 #include <blocking_detection_manager.h>
41 #include <robot_system.h>
42 #include <position_manager.h>
43
44 #include <parse.h>
45 #include <rdline.h>
46
47 #include "main.h"
48
49 static int32_t l_pwm, r_pwm;
50 static int32_t l_enc, r_enc;
51
52 static int fd;
53
54 /* */
55 #define FILTER  97
56 #define FILTER2 (100-FILTER)
57
58 /* must be called periodically */
59 void robotsim_update(void)
60 {
61         static int32_t l_speed, r_speed;
62
63         /* XXX should lock */
64         l_speed = ((l_speed * FILTER) / 100) + ((l_pwm * 1000 * FILTER2)/1000);
65         l_enc += (l_speed / 1000);
66         r_speed = ((r_speed * FILTER) / 100) + ((r_pwm * 1000 * FILTER2)/1000);
67         r_enc += (r_speed / 1000);
68 }
69
70 void robotsim_dump(void)
71 {
72         char buf[BUFSIZ];
73         int len;
74
75         len =snprintf(buf, sizeof(buf), "%d %d %d\n",
76                       position_get_x_s16(&mainboard.pos),
77                       position_get_y_s16(&mainboard.pos),
78                       position_get_a_deg_s16(&mainboard.pos));
79         write(fd, buf, len);
80 }
81
82 void robotsim_pwm(void *arg, int32_t val)
83 {
84         //      printf("%p, %d\n", arg, val);
85         if (arg == LEFT_PWM)
86                 l_pwm = val;
87         else if (arg == RIGHT_PWM)
88                 r_pwm = val;
89 }
90
91 int32_t robotsim_encoder_get(void *arg)
92 {
93         if (arg == LEFT_ENCODER)
94                 return l_enc;
95         else if (arg == RIGHT_ENCODER)
96                 return r_enc;
97         return 0;
98 }
99
100 int robotsim_init(void)
101 {
102         mkfifo("/tmp/.robot", 0600);
103         fd = open("/tmp/.robot", O_WRONLY, 0);
104         if (fd < 0)
105                 return -1;
106         return 0;
107 }