microb_cmd
[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 <clock_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                         fessor_manage();
105                 }
106                 if (mainboard.wheel.on)
107                         cs_manage(&mainboard.wheel.cs);
108                 if (mainboard.elevator.on) {
109                         cs_manage(&mainboard.elevator.cs);
110                         elevator_manage();
111                 }
112         }
113         if ((cpt & 1) && (mainboard.flags & DO_POS)) {
114                 /* about 1.5ms (worst case without centrifugal force
115                  * compensation) */
116                 position_manage(&mainboard.pos);
117         }
118         if (mainboard.flags & DO_BD) {
119                 bd_manage_from_cs(&mainboard.angle.bd, &mainboard.angle.cs);
120                 bd_manage_from_cs(&mainboard.distance.bd, &mainboard.distance.cs);
121         }
122         if (mainboard.flags & DO_TIMER) {
123                 uint8_t second;
124                 /* the robot should stop correctly in the strat, but
125                  * in some cases, we must force the stop from an
126                  * interrupt */
127                 second = time_get_s();
128                 if (second >= MATCH_TIME + 2) {
129                         pwm_ng_set(LEFT_PWM, 0);
130                         pwm_ng_set(RIGHT_PWM, 0);
131                         printf_P(PSTR("END OF TIME\r\n"));
132                         while(1);
133                 }
134         }
135         /* brakes */
136         if (mainboard.flags & DO_POWER)
137                 BRAKE_OFF();
138         else
139                 BRAKE_ON();
140         cpt++;
141 }
142
143 void dump_cs_debug(const char *name, struct cs *cs)
144 {
145         DEBUG(E_USER_CS, "%s cons=% .5ld fcons=% .5ld err=% .5ld "
146               "in=% .5ld out=% .5ld", 
147               name, cs_get_consign(cs), cs_get_filtered_consign(cs),
148               cs_get_error(cs), cs_get_filtered_feedback(cs),
149               cs_get_out(cs));
150 }
151
152 void dump_cs(const char *name, struct cs *cs)
153 {
154         printf_P(PSTR("%s cons=% .5ld fcons=% .5ld err=% .5ld "
155                       "in=% .5ld out=% .5ld\r\n"), 
156                  name, cs_get_consign(cs), cs_get_filtered_consign(cs),
157                  cs_get_error(cs), cs_get_filtered_feedback(cs),
158                  cs_get_out(cs));
159 }
160
161 void dump_pid(const char *name, struct pid_filter *pid)
162 {
163         printf_P(PSTR("%s P=% .8ld I=% .8ld D=% .8ld out=% .8ld\r\n"),
164                  name,
165                  pid_get_value_in(pid) * pid_get_gain_P(pid),
166                  pid_get_value_I(pid) * pid_get_gain_I(pid),
167                  pid_get_value_D(pid) * pid_get_gain_D(pid),
168                  pid_get_value_out(pid));
169 }
170
171 void microb_cs_init(void)
172 {
173         /* ROBOT_SYSTEM */
174         rs_init(&mainboard.rs);
175         rs_set_left_pwm(&mainboard.rs, pwm_set_and_save, LEFT_PWM);
176         rs_set_right_pwm(&mainboard.rs,  pwm_set_and_save, RIGHT_PWM);
177         /* increase gain to decrease dist, increase left and it will turn more left */
178         rs_set_left_ext_encoder(&mainboard.rs, encoders_microb_get_value, 
179                                 LEFT_ENCODER, IMP_COEF * -1.0000);
180         rs_set_right_ext_encoder(&mainboard.rs, encoders_microb_get_value, 
181                                  RIGHT_ENCODER, IMP_COEF * 1.0000);
182         /* rs will use external encoders */
183         rs_set_flags(&mainboard.rs, RS_USE_EXT);
184
185         /* POSITION MANAGER */
186         position_init(&mainboard.pos);
187         position_set_physical_params(&mainboard.pos, VIRTUAL_TRACK_MM, DIST_IMP_MM);
188         position_set_related_robot_system(&mainboard.pos, &mainboard.rs);
189         //position_set_centrifugal_coef(&mainboard.pos, 0.000016);
190         position_use_ext(&mainboard.pos);
191
192         /* TRAJECTORY MANAGER */
193         trajectory_init(&mainboard.traj);
194         trajectory_set_cs(&mainboard.traj, &mainboard.distance.cs,
195                           &mainboard.angle.cs);
196         trajectory_set_robot_params(&mainboard.traj, &mainboard.rs, &mainboard.pos);
197         trajectory_set_speed(&mainboard.traj, 1500, 1500); /* d, a */
198         /* distance window, angle window, angle start */
199         trajectory_set_windows(&mainboard.traj, 200., 5.0, 30.);
200
201         /* ---- CS angle */
202         /* PID */
203         pid_init(&mainboard.angle.pid);
204         pid_set_gains(&mainboard.angle.pid, 500, 10, 7000);
205         pid_set_maximums(&mainboard.angle.pid, 0, 20000, 4095);
206         pid_set_out_shift(&mainboard.angle.pid, 10);
207         pid_set_derivate_filter(&mainboard.angle.pid, 4);
208
209         /* QUADRAMP */
210         quadramp_init(&mainboard.angle.qr);
211         quadramp_set_1st_order_vars(&mainboard.angle.qr, 2000, 2000); /* set speed */
212         quadramp_set_2nd_order_vars(&mainboard.angle.qr, 13, 13); /* set accel */
213
214         /* CS */
215         cs_init(&mainboard.angle.cs);
216         cs_set_consign_filter(&mainboard.angle.cs, quadramp_do_filter, &mainboard.angle.qr);
217         cs_set_correct_filter(&mainboard.angle.cs, pid_do_filter, &mainboard.angle.pid);
218         cs_set_process_in(&mainboard.angle.cs, rs_set_angle, &mainboard.rs);
219         cs_set_process_out(&mainboard.angle.cs, rs_get_angle, &mainboard.rs);
220         cs_set_consign(&mainboard.angle.cs, 0);
221
222         /* Blocking detection */
223         bd_init(&mainboard.angle.bd);
224         bd_set_speed_threshold(&mainboard.angle.bd, 80);
225         bd_set_current_thresholds(&mainboard.angle.bd, 500, 8000, 1000000, 50);
226
227         /* ---- CS distance */
228         /* PID */
229         pid_init(&mainboard.distance.pid);
230         pid_set_gains(&mainboard.distance.pid, 500, 10, 7000);
231         pid_set_maximums(&mainboard.distance.pid, 0, 2000, 4095);
232         pid_set_out_shift(&mainboard.distance.pid, 10);
233         pid_set_derivate_filter(&mainboard.distance.pid, 6);
234
235         /* QUADRAMP */
236         quadramp_init(&mainboard.distance.qr);
237         quadramp_set_1st_order_vars(&mainboard.distance.qr, 2000, 2000); /* set speed */
238         quadramp_set_2nd_order_vars(&mainboard.distance.qr, 17, 17); /* set accel */
239
240         /* CS */
241         cs_init(&mainboard.distance.cs);
242         cs_set_consign_filter(&mainboard.distance.cs, quadramp_do_filter, &mainboard.distance.qr);
243         cs_set_correct_filter(&mainboard.distance.cs, pid_do_filter, &mainboard.distance.pid);
244         cs_set_process_in(&mainboard.distance.cs, rs_set_distance, &mainboard.rs);
245         cs_set_process_out(&mainboard.distance.cs, rs_get_distance, &mainboard.rs);
246         cs_set_consign(&mainboard.distance.cs, 0);
247
248         /* Blocking detection */
249         bd_init(&mainboard.distance.bd);
250         bd_set_speed_threshold(&mainboard.distance.bd, 60);
251         bd_set_current_thresholds(&mainboard.distance.bd, 500, 8000, 1000000, 50);
252
253         /* ---- CS fessor */
254         
255         fessor_autopos();
256         /* PID */
257         pid_init(&mainboard.fessor.pid);
258         pid_set_gains(&mainboard.fessor.pid, 300, 10, 150);
259         pid_set_maximums(&mainboard.fessor.pid, 0, 10000, 4095);
260         pid_set_out_shift(&mainboard.fessor.pid, 10);
261         pid_set_derivate_filter(&mainboard.fessor.pid, 4);
262
263         /* CS */
264         cs_init(&mainboard.fessor.cs);
265         cs_set_correct_filter(&mainboard.fessor.cs, pid_do_filter, &mainboard.fessor.pid);
266         cs_set_process_in(&mainboard.fessor.cs, fessor_set, NULL);
267         cs_set_process_out(&mainboard.fessor.cs, encoders_microb_get_value, FESSOR_ENC);
268         fessor_up();
269
270
271
272         /* ---- CS elevator */
273         
274         elevator_autopos();
275         /* PID */
276         pid_init(&mainboard.elevator.pid);
277         pid_set_gains(&mainboard.elevator.pid, 300, 10, 150);
278         pid_set_maximums(&mainboard.elevator.pid, 0, 10000, 4095);
279         pid_set_out_shift(&mainboard.elevator.pid, 10);
280         pid_set_derivate_filter(&mainboard.elevator.pid, 4);
281
282         /* CS */
283         cs_init(&mainboard.elevator.cs);
284         cs_set_correct_filter(&mainboard.elevator.cs, pid_do_filter, &mainboard.elevator.pid);
285         cs_set_process_in(&mainboard.elevator.cs, elevator_set, NULL);
286         cs_set_process_out(&mainboard.elevator.cs, encoders_microb_get_value, ELEVATOR_ENC);
287         elevator_down();
288
289         /* ---- CS wheel */
290         
291         /* PID */
292         pid_init(&mainboard.wheel.pid);
293         pid_set_gains(&mainboard.wheel.pid, 100, 100, 0);
294         pid_set_maximums(&mainboard.wheel.pid, 0, 30000, 4095);
295         pid_set_out_shift(&mainboard.wheel.pid, 5);
296         pid_set_derivate_filter(&mainboard.wheel.pid, 4);
297
298         /* CS */
299         cs_init(&mainboard.wheel.cs);
300         cs_set_correct_filter(&mainboard.wheel.cs, pid_do_filter, &mainboard.wheel.pid);
301         cs_set_process_in(&mainboard.wheel.cs, wheel_set, NULL);
302         cs_set_process_out(&mainboard.wheel.cs, wheel_get_value, NULL);
303         cs_set_consign(&mainboard.wheel.cs, 1000);
304
305         /* set them on !! */
306         mainboard.angle.on = 0;
307         mainboard.distance.on = 0;
308         mainboard.fessor.on = 1;
309         mainboard.elevator.on = 0;
310         mainboard.wheel.on = 1;
311         mainboard.flags |= DO_CS;
312
313         scheduler_add_periodical_event_priority(do_cs, NULL,
314                                                 5000L / SCHEDULER_UNIT,
315                                                 CS_PRIO);
316 }