add ballboard commands on mainboard
[aversive.git] / modules / devices / robot / trajectory_manager / trajectory_manager.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: trajectory_manager.h,v 1.4.4.10 2009-05-02 10:03:04 zer0 Exp $
19  *
20  */
21
22 #ifndef TRAJECTORY_MANAGER
23 #define TRAJECTORY_MANAGER
24
25 #include <aversive.h>
26 #include <vect2.h>
27 #include <robot_system.h>
28
29 enum trajectory_state {
30         READY,
31
32         /* simple trajectories */
33         RUNNING_A,
34         RUNNING_D,
35         RUNNING_AD,
36
37         /* trajectories using events */
38         RUNNING_XY_START,
39         RUNNING_XY_ANGLE,
40         RUNNING_XY_ANGLE_OK,
41         RUNNING_XY_F_START,
42         RUNNING_XY_F_ANGLE,
43         RUNNING_XY_F_ANGLE_OK,
44         RUNNING_XY_B_START,
45         RUNNING_XY_B_ANGLE,
46         RUNNING_XY_B_ANGLE_OK,
47         RUNNING_CIRCLE,
48 };
49
50 struct circle_target {
51         vect2_cart center;   /**< center of the circle */
52         double radius;       /**< radius of the circle */
53         int32_t dest_angle;  /**< dst angle in inc */
54
55 #define TRIGO   1 /* rotation is counterclockwise */
56 #define FORWARD 2 /* go forward or backward */
57         uint8_t flags;   /**< flags for this trajectory */
58 };
59
60 struct trajectory {
61         enum trajectory_state state; /*<< describe the type of target, and if we reached the target */
62
63         union {
64                 vect2_cart cart;     /**<< target, if it is a x,y vector */
65                 struct rs_polar pol; /**<< target, if it is a d,a vector */
66                 struct circle_target circle; /**<< target, if it is a circle */
67         } target;
68
69         double d_win;      /**<< distance window (for END_NEAR) */
70         double a_win_rad;  /**<< angle window (for END_NEAR) */
71         double a_start_rad;/**<< in xy consigns, start to move in distance
72                             *    when a_target < a_start */
73         double circle_coef;/**<< corrective circle coef */
74
75         uint16_t d_speed;  /**<< distance speed consign */
76         uint16_t a_speed;  /**<< angle speed consign */
77
78         struct robot_position *position; /**<< associated robot_position */
79         struct robot_system *robot;      /**<< associated robot_system */
80         struct cs *csm_angle;     /**<< associated control system (angle) */
81         struct cs *csm_distance;  /**<< associated control system (distance) */
82
83         int8_t scheduler_task;    /**<< id of current task (-1 if no running task) */
84 };
85
86 /** structure initialization */
87 void trajectory_init(struct trajectory *traj);
88
89 /** structure initialization */
90 void trajectory_set_cs(struct trajectory *traj, struct cs *cs_d, 
91                        struct cs * cs_a);
92
93 /** structure initialization */
94 void trajectory_set_robot_params(struct trajectory *traj, 
95                                  struct robot_system *rs, 
96                                  struct robot_position *pos) ;
97
98 /** set speed consign */
99 void trajectory_set_speed(struct trajectory *traj, int16_t d_speed, int16_t a_speed);
100
101 /** 
102  * set windows for trajectory. 
103  * params: distance window, angle window: we the robot enters this
104  * position window, we deletes the event and the last consign is 
105  * used.
106  * a_start_deg used in xy consigns (start to move in distance when 
107  * a_target < a_start)
108  */
109 void trajectory_set_windows(struct trajectory *traj, double d_win, 
110                             double a_win_deg, double a_start_deg);
111
112 /**
113  * Set coef for circle trajectory. The objective of this value is to
114  * fix the radius of the circle which is not correctly what we asked.
115  */
116 void trajectory_set_circle_coef(struct trajectory *traj, double coef);
117
118 /** return true if the position consign is equal to the filtered
119  * position consign (after quadramp filter), for angle and
120  * distance. */
121 uint8_t trajectory_finished(struct trajectory *traj);
122
123 /** return true if traj is nearly finished depending on specified
124  *  parameters */
125 uint8_t trajectory_in_window(struct trajectory *traj, double d_win, double a_win_rad);
126
127 /* simple commands */
128
129 /** set relative angle and distance consign to 0 */
130 void trajectory_stop(struct trajectory *traj);
131
132 /** set relative angle and distance consign to 0, and break any
133  * deceleration ramp in quadramp filter */
134 void trajectory_hardstop(struct trajectory *traj);
135
136 /** go straight forward (d is in mm) */
137 void trajectory_d_rel(struct trajectory *traj, double d_mm);
138
139 /** update distance consign without changing angle consign */
140 void trajectory_only_d_rel(struct trajectory *traj, double d_mm);
141
142 /** turn by 'a' degrees */
143 void trajectory_a_rel(struct trajectory *traj, double a_deg);
144
145 /** go to angle 'a' in degrees */
146 void trajectory_a_abs(struct trajectory *traj, double a_deg);
147
148 /** turn the robot until the point x,y is in front of us */ 
149 void trajectory_turnto_xy(struct trajectory*traj, double x_abs_mm, double y_abs_mm);
150
151 /** turn the robot until the point x,y is behind us */ 
152 void trajectory_turnto_xy_behind(struct trajectory*traj, double x_abs_mm, double y_abs_mm);
153
154 /** update angle consign without changing distance consign */
155 void trajectory_only_a_rel(struct trajectory *traj, double a_deg);
156
157 /** update angle consign without changing distance consign */
158 void trajectory_only_a_abs(struct trajectory *traj, double a_deg);
159
160 /** turn by 'a' degrees and go by 'd' mm */
161 void trajectory_d_a_rel(struct trajectory *traj, double d_mm, double a_deg);
162
163 /* commands using events */
164
165 /** goto a x,y point, using a trajectory event */
166 void trajectory_goto_xy_abs(struct trajectory *traj, double x_abs_mm, double y_abs_mm);
167
168 /** go forward to a x,y point, using a trajectory event */
169 void trajectory_goto_forward_xy_abs(struct trajectory *traj, double x_abs_mm, double y_abs_mm);
170
171 /** go backward to a x,y point, using a trajectory event */
172 void trajectory_goto_backward_xy_abs(struct trajectory *traj, double x_abs_mm, double y_abs_mm);
173
174 /** go forward to a d,a point, using a trajectory event */
175 void trajectory_goto_d_a_rel(struct trajectory *traj, double d, double a);
176
177 /** go forward to a x,y relative point, using a trajectory event */
178 void trajectory_goto_xy_rel(struct trajectory *traj, double x_rel_mm, double y_rel_mm);
179
180 /** make the robot orbiting around (x,y) on a circle whose radius is
181  * radius_mm, and exit when relative destination angle is reached. The
182  * flags set if we go forward or backwards, and CW/CCW. */
183 void trajectory_circle_rel(struct trajectory *traj, double x, double y,
184                            double radius_mm, double rel_a_deg, uint8_t flags);
185
186 #endif //TRAJECTORY_MANAGER