905d533ed4e3369b27ea417c4b705871a5841d46
[aversive.git] / projects / microb2010 / mainboard / 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.9 2009-11-08 17:24:33 zer0 Exp $
20  *
21  */
22
23 #include <stdio.h>
24 #include <string.h>
25 #include <stdint.h>
26
27 #include <aversive.h>
28 #include <aversive/error.h>
29
30 #include <ax12.h>
31 #include <spi.h>
32 #include <encoders_spi.h>
33 #include <pwm_ng.h>
34 #include <timer.h>
35 #include <scheduler.h>
36 #include <clock_time.h>
37 #include <adc.h>
38
39 #include <pid.h>
40 #include <quadramp.h>
41 #include <control_system_manager.h>
42 #include <trajectory_manager.h>
43 #include <vect_base.h>
44 #include <lines.h>
45 #include <polygon.h>
46 #include <obstacle_avoidance.h>
47 #include <blocking_detection_manager.h>
48 #include <robot_system.h>
49 #include <position_manager.h>
50
51 #include <parse.h>
52 #include <rdline.h>
53
54 #include "main.h"
55 #include "robotsim.h"
56 #include "strat.h"
57 #include "actuator.h"
58
59 void dump_cs(const char *name, struct cs *cs);
60
61 #ifndef HOST_VERSION
62 int32_t encoders_left_cobroller_speed(void *number)
63 {
64         static volatile int32_t roller_pos;
65         int32_t tmp, speed;
66         tmp = encoders_spi_get_value(number);
67         speed = tmp - roller_pos;
68         roller_pos = tmp;
69         return speed;
70 }
71
72 int32_t encoders_right_cobroller_speed(void *number)
73 {
74         static volatile int32_t roller_pos;
75         int32_t tmp, speed;
76         tmp = encoders_spi_get_value(number);
77         speed = tmp - roller_pos;
78         roller_pos = tmp;
79         return speed;
80 }
81 #endif
82
83 /* called every 5 ms */
84 static void do_cs(void *dummy)
85 {
86         static uint16_t cpt = 0;
87         static int32_t old_a = 0, old_d = 0;
88
89 #ifdef HOST_VERSION
90         robotsim_update();
91 #else
92         /* read encoders */
93         if (mainboard.flags & DO_ENCODERS) {
94                 encoders_spi_manage(NULL);
95         }
96 #endif
97
98         /* robot system, conversion to angle,distance */
99         if (mainboard.flags & DO_RS) {
100                 int16_t a,d;
101                 rs_update(&mainboard.rs); /* takes about 0.5 ms */
102                 /* process and store current speed */
103                 a = rs_get_angle(&mainboard.rs);
104                 d = rs_get_distance(&mainboard.rs);
105                 mainboard.speed_a = a - old_a;
106                 mainboard.speed_d = d - old_d;
107                 old_a = a;
108                 old_d = d;
109         }
110
111         /* control system */
112         if (mainboard.flags & DO_CS) {
113                 if (mainboard.angle.on)
114                         cs_manage(&mainboard.angle.cs);
115                 if (mainboard.distance.on)
116                         cs_manage(&mainboard.distance.cs);
117 #ifndef HOST_VERSION
118                 if (mainboard.left_cobroller.on)
119                         cs_manage(&mainboard.left_cobroller.cs);
120                 if (mainboard.right_cobroller.on)
121                         cs_manage(&mainboard.right_cobroller.cs);
122 #endif
123         }
124         if ((cpt & 1) && (mainboard.flags & DO_POS)) {
125                 /* about 1.5ms (worst case without centrifugal force
126                  * compensation) */
127                 position_manage(&mainboard.pos);
128         }
129         if (mainboard.flags & DO_BD) {
130                 bd_manage_from_cs(&mainboard.angle.bd, &mainboard.angle.cs);
131                 bd_manage_from_cs(&mainboard.distance.bd, &mainboard.distance.cs);
132 #ifndef HOST_VERSION
133                 bd_manage_from_cs(&mainboard.left_cobroller.bd, &mainboard.left_cobroller.cs);
134                 bd_manage_from_cs(&mainboard.right_cobroller.bd, &mainboard.right_cobroller.cs);
135 #endif
136         }
137 #ifndef HOST_VERSION
138         if (mainboard.flags & DO_TIMER) {
139                 uint8_t second;
140                 /* the robot should stop correctly in the strat, but
141                  * in some cases, we must force the stop from an
142                  * interrupt */
143                 second = time_get_s();
144                 if (second >= MATCH_TIME + 2) {
145                         pwm_ng_set(LEFT_PWM, 0);
146                         pwm_ng_set(RIGHT_PWM, 0);
147                         printf_P(PSTR("END OF TIME\r\n"));
148                         while(1);
149                 }
150         }
151 #endif
152         /* brakes */
153         if (mainboard.flags & DO_POWER)
154                 BRAKE_OFF();
155         else
156                 BRAKE_ON();
157         cpt++;
158
159 #ifdef HOST_VERSION
160         if ((cpt & 7) == 0) {
161                 //              dump_cs("dist", &mainboard.distance.cs);
162                 robotsim_dump();
163         }
164 #endif
165 }
166
167 void dump_cs_debug(const char *name, struct cs *cs)
168 {
169         DEBUG(E_USER_CS, "%s cons=% .5"PRIi32" fcons=% .5"PRIi32" err=% .5"PRIi32" "
170               "in=% .5"PRIi32" out=% .5"PRIi32"",
171               name, cs_get_consign(cs), cs_get_filtered_consign(cs),
172               cs_get_error(cs), cs_get_filtered_feedback(cs),
173               cs_get_out(cs));
174 }
175
176 void dump_cs(const char *name, struct cs *cs)
177 {
178         printf_P(PSTR("%s cons=% .5"PRIi32" fcons=% .5"PRIi32" err=% .5"PRIi32" "
179                       "in=% .5"PRIi32" out=% .5"PRIi32"\r\n"),
180                  name, cs_get_consign(cs), cs_get_filtered_consign(cs),
181                  cs_get_error(cs), cs_get_filtered_feedback(cs),
182                  cs_get_out(cs));
183 }
184
185 void dump_pid(const char *name, struct pid_filter *pid)
186 {
187         printf_P(PSTR("%s P=% .8"PRIi32" I=% .8"PRIi32" D=% .8"PRIi32" out=% .8"PRIi32"\r\n"),
188                  name,
189                  pid_get_value_in(pid) * pid_get_gain_P(pid),
190                  pid_get_value_I(pid) * pid_get_gain_I(pid),
191                  pid_get_value_D(pid) * pid_get_gain_D(pid),
192                  pid_get_value_out(pid));
193 }
194
195 void microb_cs_init(void)
196 {
197         /* ROBOT_SYSTEM */
198         rs_init(&mainboard.rs);
199         rs_set_left_pwm(&mainboard.rs, pwm_set_and_save, LEFT_PWM);
200         rs_set_right_pwm(&mainboard.rs,  pwm_set_and_save, RIGHT_PWM);
201         /* increase gain to decrease dist, increase left and it will turn more left */
202 #ifdef HOST_VERSION
203         rs_set_left_ext_encoder(&mainboard.rs, robotsim_encoder_get,
204                                 LEFT_ENCODER, IMP_COEF * 1.);
205         rs_set_right_ext_encoder(&mainboard.rs, robotsim_encoder_get,
206                                  RIGHT_ENCODER, IMP_COEF * 1.);
207 #else
208         rs_set_left_ext_encoder(&mainboard.rs, encoders_spi_get_value,
209                                 LEFT_ENCODER, IMP_COEF * -1.036);
210         rs_set_right_ext_encoder(&mainboard.rs, encoders_spi_get_value,
211                                  RIGHT_ENCODER, IMP_COEF * 1.037);
212 #endif
213         /* rs will use external encoders */
214         rs_set_flags(&mainboard.rs, RS_USE_EXT);
215
216         /* POSITION MANAGER */
217         position_init(&mainboard.pos);
218         position_set_physical_params(&mainboard.pos, VIRTUAL_TRACK_MM, DIST_IMP_MM);
219         position_set_related_robot_system(&mainboard.pos, &mainboard.rs);
220         position_set_centrifugal_coef(&mainboard.pos, 0.000016);
221         position_use_ext(&mainboard.pos);
222
223         /* TRAJECTORY MANAGER */
224         trajectory_init(&mainboard.traj, CS_HZ);
225         trajectory_set_cs(&mainboard.traj, &mainboard.distance.cs,
226                           &mainboard.angle.cs);
227         trajectory_set_robot_params(&mainboard.traj, &mainboard.rs, &mainboard.pos);
228         trajectory_set_speed(&mainboard.traj, SPEED_DIST_FAST, SPEED_ANGLE_FAST); /* d, a */
229         trajectory_set_speed(&mainboard.traj, ACC_DIST, ACC_ANGLE); /* d, a */
230         /* distance window, angle window, angle start */
231         trajectory_set_windows(&mainboard.traj, 200., 5.0, 30.);
232
233         /* ---- CS angle */
234         /* PID */
235         pid_init(&mainboard.angle.pid);
236         pid_set_gains(&mainboard.angle.pid, 500, 10, 7000);
237         pid_set_maximums(&mainboard.angle.pid, 0, 20000, 4095);
238         pid_set_out_shift(&mainboard.angle.pid, 10);
239         pid_set_derivate_filter(&mainboard.angle.pid, 4);
240
241         /* QUADRAMP */
242         quadramp_init(&mainboard.angle.qr);
243         quadramp_set_1st_order_vars(&mainboard.angle.qr, 500, 500); /* set speed */
244         quadramp_set_2nd_order_vars(&mainboard.angle.qr, 5, 5); /* set accel */
245
246         /* CS */
247         cs_init(&mainboard.angle.cs);
248         cs_set_consign_filter(&mainboard.angle.cs, quadramp_do_filter, &mainboard.angle.qr);
249         cs_set_correct_filter(&mainboard.angle.cs, pid_do_filter, &mainboard.angle.pid);
250         cs_set_process_in(&mainboard.angle.cs, rs_set_angle, &mainboard.rs);
251         cs_set_process_out(&mainboard.angle.cs, rs_get_angle, &mainboard.rs);
252         cs_set_consign(&mainboard.angle.cs, 0);
253
254         /* Blocking detection */
255         bd_init(&mainboard.angle.bd);
256         bd_set_speed_threshold(&mainboard.angle.bd, 80);
257         bd_set_current_thresholds(&mainboard.angle.bd, 500, 8000, 1000000, 50);
258
259         /* ---- CS distance */
260         /* PID */
261         pid_init(&mainboard.distance.pid);
262         pid_set_gains(&mainboard.distance.pid, 500, 10, 7000);
263         pid_set_maximums(&mainboard.distance.pid, 0, 2000, 4095);
264         pid_set_out_shift(&mainboard.distance.pid, 10);
265         pid_set_derivate_filter(&mainboard.distance.pid, 6);
266
267         /* QUADRAMP */
268         quadramp_init(&mainboard.distance.qr);
269         quadramp_set_1st_order_vars(&mainboard.distance.qr, 500, 500); /* set speed */
270         quadramp_set_2nd_order_vars(&mainboard.distance.qr, 5., 5.); /* set accel */
271
272         /* CS */
273         cs_init(&mainboard.distance.cs);
274         cs_set_consign_filter(&mainboard.distance.cs, quadramp_do_filter, &mainboard.distance.qr);
275         cs_set_correct_filter(&mainboard.distance.cs, pid_do_filter, &mainboard.distance.pid);
276         cs_set_process_in(&mainboard.distance.cs, rs_set_distance, &mainboard.rs);
277         cs_set_process_out(&mainboard.distance.cs, rs_get_distance, &mainboard.rs);
278         cs_set_consign(&mainboard.distance.cs, 0);
279
280         /* Blocking detection */
281         bd_init(&mainboard.distance.bd);
282         bd_set_speed_threshold(&mainboard.distance.bd, 60);
283         bd_set_current_thresholds(&mainboard.distance.bd, 500, 8000, 1000000, 50);
284
285 #ifndef HOST_VERSION
286         /* ---- CS left_cobroller */
287         /* PID */
288         pid_init(&mainboard.left_cobroller.pid);
289         pid_set_gains(&mainboard.left_cobroller.pid, 80, 10, 10);
290         pid_set_maximums(&mainboard.left_cobroller.pid, 0, 30000, 4095);
291         pid_set_out_shift(&mainboard.left_cobroller.pid, 5);
292         pid_set_derivate_filter(&mainboard.left_cobroller.pid, 6);
293
294         /* CS */
295         cs_init(&mainboard.left_cobroller.cs);
296         cs_set_correct_filter(&mainboard.left_cobroller.cs, pid_do_filter, &mainboard.left_cobroller.pid);
297         cs_set_process_in(&mainboard.left_cobroller.cs, pwm_ng_set, LEFT_COBROLLER_PWM);
298         cs_set_process_out(&mainboard.left_cobroller.cs, encoders_left_cobroller_speed, LEFT_COBROLLER_ENCODER);
299         cs_set_consign(&mainboard.left_cobroller.cs, 0);
300
301         /* Blocking detection */
302         bd_init(&mainboard.left_cobroller.bd);
303         bd_set_speed_threshold(&mainboard.left_cobroller.bd, 60);
304         bd_set_current_thresholds(&mainboard.left_cobroller.bd, 500, 8000, 1000000, 50);
305
306         /* ---- CS right_cobroller */
307         /* PID */
308         pid_init(&mainboard.right_cobroller.pid);
309         pid_set_gains(&mainboard.right_cobroller.pid, 80, 10, 10);
310         pid_set_maximums(&mainboard.right_cobroller.pid, 0, 30000, 4095);
311         pid_set_out_shift(&mainboard.right_cobroller.pid, 5);
312         pid_set_derivate_filter(&mainboard.right_cobroller.pid, 6);
313
314         /* CS */
315         cs_init(&mainboard.right_cobroller.cs);
316         cs_set_correct_filter(&mainboard.right_cobroller.cs, pid_do_filter, &mainboard.right_cobroller.pid);
317         cs_set_process_in(&mainboard.right_cobroller.cs, pwm_ng_set, RIGHT_COBROLLER_PWM);
318         cs_set_process_out(&mainboard.right_cobroller.cs, encoders_right_cobroller_speed, RIGHT_COBROLLER_ENCODER);
319         cs_set_consign(&mainboard.right_cobroller.cs, 0);
320
321         /* Blocking detection */
322         bd_init(&mainboard.right_cobroller.bd);
323         bd_set_speed_threshold(&mainboard.right_cobroller.bd, 60);
324         bd_set_current_thresholds(&mainboard.right_cobroller.bd, 500, 8000, 1000000, 50);
325 #endif /* !HOST_VERSION */
326
327         /* set them on !! */
328         mainboard.angle.on = 0;
329         mainboard.distance.on = 0;
330         mainboard.left_cobroller.on = 1;
331         mainboard.right_cobroller.on = 1;
332
333
334         scheduler_add_periodical_event_priority(do_cs, NULL,
335                                                 5000L / SCHEDULER_UNIT,
336                                                 CS_PRIO);
337 }