split graph in several dirs
[aversive.git] / projects / microb2010 / tests / test_board2008 / cs.c
1 /*  
2  *  Copyright Droids Corporation
3  *  Olivier Matz <zer0@droids-corp.org>
4  * 
5  *  This program is free software; you can redistribute it and/or modify
6  *  it under the terms of the GNU General Public License as published by
7  *  the Free Software Foundation; either version 2 of the License, or
8  *  (at your option) any later version.
9  *
10  *  This program is distributed in the hope that it will be useful,
11  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  *  GNU General Public License for more details.
14  *
15  *  You should have received a copy of the GNU General Public License
16  *  along with this program; if not, write to the Free Software
17  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18  *
19  *  Revision : $Id: cs.c,v 1.7 2009-05-02 10:08:09 zer0 Exp $
20  *
21  */
22
23 #include <stdio.h>
24 #include <string.h>
25
26 #include <aversive.h>
27 #include <aversive/error.h>
28 #include <aversive/wait.h>
29
30 #include <encoders_microb.h>
31 #include <pwm_ng.h>
32 #include <timer.h>
33 #include <scheduler.h>
34 #include <time.h>
35 #include <adc.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 "main.h"
49 #include "actuator.h"
50
51 static int32_t wheel_speed = 0;
52
53 static int32_t wheel_get_value(void * dummy)
54 {
55         return wheel_speed;
56 }
57
58 static void wheel_set(void *dummy, int32_t val)
59 {
60         if (val < 0)
61                 val = 0;
62         pwm_ng_set(WHEEL_PWM, val);
63 }
64
65 static void wheel_update_value(void)
66 {
67         static int32_t prev = 0;
68         int32_t val;
69
70         val = encoders_microb_get_value(WHEEL_ENC);
71         wheel_speed = val - prev;
72         prev = val;
73 }
74
75 /* called every 5 ms */
76 static void do_cs(void *dummy) 
77 {
78         static uint16_t cpt = 0;
79         static int32_t old_a = 0, old_d = 0;
80
81         wheel_update_value();
82
83         /* robot system, conversion to angle,distance */
84         if (mainboard.flags & DO_RS) {
85                 int16_t a,d;
86                 rs_update(&mainboard.rs); /* takes about 0.5 ms */
87                 /* process and store current speed */
88                 a = rs_get_angle(&mainboard.rs);
89                 d = rs_get_distance(&mainboard.rs);
90                 mainboard.speed_a = a - old_a;
91                 mainboard.speed_d = d - old_d;
92                 old_a = a;
93                 old_d = d;
94         }
95
96         /* control system */
97         if (mainboard.flags & DO_CS) {
98                 if (mainboard.angle.on)
99                         cs_manage(&mainboard.angle.cs);
100                 if (mainboard.distance.on)
101                         cs_manage(&mainboard.distance.cs);
102                 if (mainboard.fessor.on)
103                         cs_manage(&mainboard.fessor.cs);
104                 if (mainboard.wheel.on)
105                         cs_manage(&mainboard.wheel.cs);
106                 if (mainboard.elevator.on)
107                         cs_manage(&mainboard.elevator.cs);
108
109                 fessor_manage();
110                 elevator_manage();
111         }
112         if ((cpt & 1) && (mainboard.flags & DO_POS)) {
113                 /* about 1.5ms (worst case without centrifugal force
114                  * compensation) */
115                 position_manage(&mainboard.pos);
116         }
117         if (mainboard.flags & DO_BD) {
118                 bd_manage_from_cs(&mainboard.angle.bd, &mainboard.angle.cs);
119                 bd_manage_from_cs(&mainboard.distance.bd, &mainboard.distance.cs);
120         }
121         if (mainboard.flags & DO_TIMER) {
122                 uint8_t second;
123                 /* the robot should stop correctly in the strat, but
124                  * in some cases, we must force the stop from an
125                  * interrupt */
126                 second = time_get_s();
127                 if (second >= MATCH_TIME + 2) {
128                         pwm_ng_set(LEFT_PWM, 0);
129                         pwm_ng_set(RIGHT_PWM, 0);
130                         printf_P(PSTR("END OF TIME\r\n"));
131                         while(1);
132                 }
133         }
134         /* brakes */
135         if (mainboard.flags & DO_POWER)
136                 BRAKE_OFF();
137         else
138                 BRAKE_ON();
139         cpt++;
140 }
141
142 void dump_cs_debug(const char *name, struct cs *cs)
143 {
144         DEBUG(E_USER_CS, "%s cons=% .5ld fcons=% .5ld err=% .5ld "
145               "in=% .5ld out=% .5ld", 
146               name, cs_get_consign(cs), cs_get_filtered_consign(cs),
147               cs_get_error(cs), cs_get_filtered_feedback(cs),
148               cs_get_out(cs));
149 }
150
151 void dump_cs(const char *name, struct cs *cs)
152 {
153         printf_P(PSTR("%s cons=% .5ld fcons=% .5ld err=% .5ld "
154                       "in=% .5ld out=% .5ld\r\n"), 
155                  name, cs_get_consign(cs), cs_get_filtered_consign(cs),
156                  cs_get_error(cs), cs_get_filtered_feedback(cs),
157                  cs_get_out(cs));
158 }
159
160 void dump_pid(const char *name, struct pid_filter *pid)
161 {
162         printf_P(PSTR("%s P=% .8ld I=% .8ld D=% .8ld out=% .8ld\r\n"),
163                  name,
164                  pid_get_value_in(pid) * pid_get_gain_P(pid),
165                  pid_get_value_I(pid) * pid_get_gain_I(pid),
166                  pid_get_value_D(pid) * pid_get_gain_D(pid),
167                  pid_get_value_out(pid));
168 }
169
170 void microb_cs_init(void)
171 {
172         /* ROBOT_SYSTEM */
173         rs_init(&mainboard.rs);
174         rs_set_left_pwm(&mainboard.rs, pwm_set_and_save, LEFT_PWM);
175         rs_set_right_pwm(&mainboard.rs,  pwm_set_and_save, RIGHT_PWM);
176         /* increase gain to decrease dist, increase left and it will turn more left */
177         rs_set_left_ext_encoder(&mainboard.rs, encoders_microb_get_value, 
178                                 LEFT_ENCODER, IMP_COEF * -1.0000);
179         rs_set_right_ext_encoder(&mainboard.rs, encoders_microb_get_value, 
180                                  RIGHT_ENCODER, IMP_COEF * 1.0000);
181         /* rs will use external encoders */
182         rs_set_flags(&mainboard.rs, RS_USE_EXT);
183
184         /* POSITION MANAGER */
185         position_init(&mainboard.pos);
186         position_set_physical_params(&mainboard.pos, VIRTUAL_TRACK_MM, DIST_IMP_MM);
187         position_set_related_robot_system(&mainboard.pos, &mainboard.rs);
188         //position_set_centrifugal_coef(&mainboard.pos, 0.000016);
189         position_use_ext(&mainboard.pos);
190
191         /* TRAJECTORY MANAGER */
192         trajectory_init(&mainboard.traj);
193         trajectory_set_cs(&mainboard.traj, &mainboard.distance.cs,
194                           &mainboard.angle.cs);
195         trajectory_set_robot_params(&mainboard.traj, &mainboard.rs, &mainboard.pos);
196         trajectory_set_speed(&mainboard.traj, 1500, 1500); /* d, a */
197         /* distance window, angle window, angle start */
198         trajectory_set_windows(&mainboard.traj, 200., 5.0, 30.);
199
200         /* ---- CS angle */
201         /* PID */
202         pid_init(&mainboard.angle.pid);
203         pid_set_gains(&mainboard.angle.pid, 500, 10, 7000);
204         pid_set_maximums(&mainboard.angle.pid, 0, 20000, 4095);
205         pid_set_out_shift(&mainboard.angle.pid, 10);
206         pid_set_derivate_filter(&mainboard.angle.pid, 4);
207
208         /* QUADRAMP */
209         quadramp_init(&mainboard.angle.qr);
210         quadramp_set_1st_order_vars(&mainboard.angle.qr, 2000, 2000); /* set speed */
211         quadramp_set_2nd_order_vars(&mainboard.angle.qr, 13, 13); /* set accel */
212
213         /* CS */
214         cs_init(&mainboard.angle.cs);
215         cs_set_consign_filter(&mainboard.angle.cs, quadramp_do_filter, &mainboard.angle.qr);
216         cs_set_correct_filter(&mainboard.angle.cs, pid_do_filter, &mainboard.angle.pid);
217         cs_set_process_in(&mainboard.angle.cs, rs_set_angle, &mainboard.rs);
218         cs_set_process_out(&mainboard.angle.cs, rs_get_angle, &mainboard.rs);
219         cs_set_consign(&mainboard.angle.cs, 0);
220
221         /* Blocking detection */
222         bd_init(&mainboard.angle.bd);
223         bd_set_speed_threshold(&mainboard.angle.bd, 80);
224         bd_set_current_thresholds(&mainboard.angle.bd, 500, 8000, 1000000, 50);
225
226         /* ---- CS distance */
227         /* PID */
228         pid_init(&mainboard.distance.pid);
229         pid_set_gains(&mainboard.distance.pid, 500, 10, 7000);
230         pid_set_maximums(&mainboard.distance.pid, 0, 2000, 4095);
231         pid_set_out_shift(&mainboard.distance.pid, 10);
232         pid_set_derivate_filter(&mainboard.distance.pid, 6);
233
234         /* QUADRAMP */
235         quadramp_init(&mainboard.distance.qr);
236         quadramp_set_1st_order_vars(&mainboard.distance.qr, 2000, 2000); /* set speed */
237         quadramp_set_2nd_order_vars(&mainboard.distance.qr, 17, 17); /* set accel */
238
239         /* CS */
240         cs_init(&mainboard.distance.cs);
241         cs_set_consign_filter(&mainboard.distance.cs, quadramp_do_filter, &mainboard.distance.qr);
242         cs_set_correct_filter(&mainboard.distance.cs, pid_do_filter, &mainboard.distance.pid);
243         cs_set_process_in(&mainboard.distance.cs, rs_set_distance, &mainboard.rs);
244         cs_set_process_out(&mainboard.distance.cs, rs_get_distance, &mainboard.rs);
245         cs_set_consign(&mainboard.distance.cs, 0);
246
247         /* Blocking detection */
248         bd_init(&mainboard.distance.bd);
249         bd_set_speed_threshold(&mainboard.distance.bd, 60);
250         bd_set_current_thresholds(&mainboard.distance.bd, 500, 8000, 1000000, 50);
251
252         /* ---- CS fessor */
253         
254         fessor_autopos();
255         /* PID */
256         pid_init(&mainboard.fessor.pid);
257         pid_set_gains(&mainboard.fessor.pid, 300, 10, 150);
258         pid_set_maximums(&mainboard.fessor.pid, 0, 10000, 4095);
259         pid_set_out_shift(&mainboard.fessor.pid, 10);
260         pid_set_derivate_filter(&mainboard.fessor.pid, 4);
261
262         /* CS */
263         cs_init(&mainboard.fessor.cs);
264         cs_set_correct_filter(&mainboard.fessor.cs, pid_do_filter, &mainboard.fessor.pid);
265         cs_set_process_in(&mainboard.fessor.cs, fessor_set, NULL);
266         cs_set_process_out(&mainboard.fessor.cs, encoders_microb_get_value, FESSOR_ENC);
267         fessor_up();
268
269
270
271         /* ---- CS elevator */
272         
273         elevator_autopos();
274         /* PID */
275         pid_init(&mainboard.elevator.pid);
276         pid_set_gains(&mainboard.elevator.pid, 300, 10, 150);
277         pid_set_maximums(&mainboard.elevator.pid, 0, 10000, 4095);
278         pid_set_out_shift(&mainboard.elevator.pid, 10);
279         pid_set_derivate_filter(&mainboard.elevator.pid, 4);
280
281         /* CS */
282         cs_init(&mainboard.elevator.cs);
283         cs_set_correct_filter(&mainboard.elevator.cs, pid_do_filter, &mainboard.elevator.pid);
284         cs_set_process_in(&mainboard.elevator.cs, elevator_set, NULL);
285         cs_set_process_out(&mainboard.elevator.cs, encoders_microb_get_value, ELEVATOR_ENC);
286         elevator_down();
287
288         /* ---- CS wheel */
289         
290         /* PID */
291         pid_init(&mainboard.wheel.pid);
292         pid_set_gains(&mainboard.wheel.pid, 100, 100, 0);
293         pid_set_maximums(&mainboard.wheel.pid, 0, 30000, 4095);
294         pid_set_out_shift(&mainboard.wheel.pid, 5);
295         pid_set_derivate_filter(&mainboard.wheel.pid, 4);
296
297         /* CS */
298         cs_init(&mainboard.wheel.cs);
299         cs_set_correct_filter(&mainboard.wheel.cs, pid_do_filter, &mainboard.wheel.pid);
300         cs_set_process_in(&mainboard.wheel.cs, wheel_set, NULL);
301         cs_set_process_out(&mainboard.wheel.cs, wheel_get_value, NULL);
302         cs_set_consign(&mainboard.wheel.cs, 1000);
303
304         /* set them on !! */
305         mainboard.angle.on = 0;
306         mainboard.distance.on = 0;
307         mainboard.fessor.on = 1;
308         mainboard.elevator.on = 1;
309         mainboard.wheel.on = 1;
310         mainboard.flags |= DO_CS;
311
312         scheduler_add_periodical_event_priority(do_cs, NULL,
313                                                 5000L / SCHEDULER_UNIT,
314                                                 CS_PRIO);
315 }