first hostsim project
[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
26 #include <aversive.h>
27 #include <aversive/error.h>
28
29 #include <timer.h>
30 #include <scheduler.h>
31 #include <time.h>
32
33 #include <pid.h>
34 #include <quadramp.h>
35 #include <control_system_manager.h>
36 #include <trajectory_manager.h>
37 #include <blocking_detection_manager.h>
38 #include <robot_system.h>
39 #include <position_manager.h>
40
41 #include <parse.h>
42 #include <rdline.h>
43
44 #include "main.h"
45
46 static int32_t l_pwm, r_pwm;
47 static int32_t l_enc, r_enc;
48
49 /* */
50 #define FILTER  98
51 #define FILTER2 (100-FILTER)
52
53 /* must be called periodically */
54 void robotsim_update(void)
55 {
56         static int32_t l_speed, r_speed;
57
58         /* XXX should lock */
59         l_speed = ((l_speed * FILTER) / 100) + ((l_pwm * 1000 * FILTER2)/1000);
60         l_enc += (l_speed / 1000);
61         r_speed = ((r_speed * FILTER) / 100) + ((r_pwm * 1000 * FILTER2)/1000);
62         r_enc += (r_speed / 1000);
63 }
64
65 void robotsim_pwm(void *arg, int32_t val)
66 {
67         //      printf("%p, %d\n", arg, val);
68         if (arg == LEFT_PWM)
69                 l_pwm = val;
70         else if (arg == RIGHT_PWM)
71                 r_pwm = val;
72 }
73
74 int32_t robotsim_encoder_get(void *arg)
75 {
76         if (arg == LEFT_ENCODER)
77                 return l_enc;
78         else if (arg == RIGHT_ENCODER)
79                 return r_enc;
80         return 0;
81 }