afaa35222e1c7d3a4e24081b6f3a48a4a2600a66
[aversive.git] / modules / devices / robot / robot_system / robot_system.h
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: robot_system.h,v 1.5.4.3 2008-04-06 17:33:57 zer0 Exp $
19  *
20  */
21
22 /**
23  * The goal of this module is to provide an interface to motor and
24  * encoders of the robot. This module provide a function that returns
25  * the value of virtual encoders containing distance and angle. It
26  * also allow the user to command virtual angle and distance PWMs.
27  */
28
29 #include <aversive.h>
30 #ifdef CONFIG_MODULE_ROBOT_SYSTEM_USE_F64
31 #include <f64.h>
32 #endif
33 #include "angle_distance.h"
34
35 #ifndef _ROBOT_SYSTEM_H_
36 #define _ROBOT_SYSTEM_H_
37
38 /* flags */
39 #define RS_USE_EXT 1
40 #define RS_IGNORE_EXT_GAIN 2
41 #define RS_IGNORE_MOT_GAIN 4
42
43
44 struct robot_system
45 {
46         uint8_t flags;
47
48 #ifdef CONFIG_MODULE_ROBOT_SYSTEM_MOT_AND_EXT
49         struct rs_polar pmot_prev;
50         struct rs_wheels wmot_prev;
51
52         f64 ratio_mot_ext;
53
54         /* Motor encoders */
55         int32_t (*left_mot_encoder)(void *);
56         void* left_mot_encoder_param;
57         f64 left_mot_gain;
58         
59         int32_t (*right_mot_encoder)(void *);
60         void* right_mot_encoder_param;
61         f64 right_mot_gain;
62 #endif
63
64         struct rs_polar virtual_pwm;
65         struct rs_polar virtual_encoders;
66
67         struct rs_polar pext_prev;
68         struct rs_wheels wext_prev;
69
70         /* External encoders */
71         int32_t (*left_ext_encoder)(void *);
72         void* left_ext_encoder_param;
73 #ifdef CONFIG_MODULE_ROBOT_SYSTEM_USE_F64
74         f64 left_ext_gain;
75 #else
76         double left_ext_gain;
77 #endif
78         
79         int32_t (*right_ext_encoder)(void *);
80         void* right_ext_encoder_param;
81 #ifdef CONFIG_MODULE_ROBOT_SYSTEM_USE_F64
82         f64 right_ext_gain;
83 #else
84         double right_ext_gain;
85 #endif
86
87         /* PWM */
88         void (*left_pwm)(void *, int32_t);
89         void *left_pwm_param;
90         void (*right_pwm)(void *, int32_t);
91         void *right_pwm_param;
92 };
93
94 /** Set the structure to 0 */
95 void rs_init( struct robot_system * );
96
97 /**** ACCESSORS */
98
99 #ifdef CONFIG_MODULE_ROBOT_SYSTEM_MOT_AND_EXT
100 /** define ratio between mot and ext track. (track_mot / track_ext) */
101 void rs_set_ratio(struct robot_system * rs, double ratio);
102 #endif
103
104 /** define left PWM function and param */
105 void rs_set_left_pwm(struct robot_system * rs, void (*left_pwm)(void *, int32_t), void *left_pwm_param);
106
107 /** define right PWM function and param */
108 void rs_set_right_pwm(struct robot_system * rs, void (*right_pwm)(void *, int32_t), void *right_pwm_param);
109
110 #ifdef CONFIG_MODULE_ROBOT_SYSTEM_MOT_AND_EXT
111 /** define left motor encoder function and param */
112 void rs_set_left_mot_encoder(struct robot_system * rs, int32_t (*left_mot_encoder)(void *), 
113                              void *left_mot_encoder_param, double gain);
114
115 /** define right motor encoder function and param */
116 void rs_set_right_mot_encoder(struct robot_system * rs, int32_t (*right_mot_encoder)(void *), 
117                               void *right_mot_encoder_param, double gain);
118 #endif
119
120 /** define left external encoder function and param */
121 void rs_set_left_ext_encoder(struct robot_system * rs, int32_t (*left_ext_encoder)(void *), 
122                              void *left_ext_encoder_param, double gain);
123
124 /** define right external encoder function and param */
125 void rs_set_right_ext_encoder(struct robot_system * rs, int32_t (*right_ext_encoder)(void *), 
126                               void *right_ext_encoder_param, double gain);
127
128 /**** Virtual encoders and PWM */
129
130 /** 
131  * set the real pwms according to the specified angle (it also
132  * depends on the last distance command sent) 
133  */
134 void rs_set_angle(void * rs, int32_t angle);
135
136 /** 
137  * set the real pwms according to the specified distance (it also
138  * depends on the last angle command sent) 
139  */
140 void rs_set_distance(void * rs, int32_t distance);
141
142 /** 
143  * get the virtual angle according to real encoders value. 
144  */
145 int32_t rs_get_angle(void * rs);
146
147 /** 
148  * get the virtual distance according to real encoders value. 
149  */
150 int32_t rs_get_distance(void * rs);
151
152 /** 
153  * get the angle according to ext encoders value. 
154  */
155 int32_t rs_get_ext_angle(void * rs);
156
157 /** 
158  * get the distance according to ext encoders value. 
159  */
160 int32_t rs_get_ext_distance(void * rs);
161
162 #ifdef CONFIG_MODULE_ROBOT_SYSTEM_MOT_AND_EXT
163 /** 
164  * get the angle according to mot encoders value. 
165  */
166 int32_t rs_get_mot_angle(void * rs);
167
168 /** 
169  * get the distance according to mot encoders value. 
170  */
171 int32_t rs_get_mot_distance(void * rs);
172 #endif
173
174 /* same for left/right */
175 int32_t rs_get_ext_left(void * rs);
176 int32_t rs_get_ext_right(void * rs);
177 #ifdef CONFIG_MODULE_ROBOT_SYSTEM_MOT_AND_EXT
178 int32_t rs_get_mot_left(void * rs);
179 int32_t rs_get_mot_right(void * rs);
180 #endif
181
182
183 /** 
184  * Read the encoders, and update internal virtual counters. Call this
185  * function is needed before reading the virtual encoders. The program
186  * will decide if it the external encoders or the motor encoders are
187  * taken in account (depending on flags, but not yet)
188  */
189 void rs_update(void * rs);
190
191 void rs_set_flags(struct robot_system * rs, uint8_t flags);
192
193
194 #endif /* #ifndef _ROBOT_SYSTEM */