6ef8fe934024333b2029c14f93d3f6d649446bdd
[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 #ifndef HOST_VERSION
84 #define DEBUG_CPLD
85 #endif
86
87 #ifdef DEBUG_CPLD
88 extern int16_t g_encoders_spi_previous[4];
89 static int32_t ll_prev, rr_prev;
90 #endif
91
92 /* called every 5 ms */
93 static void do_cs(void *dummy)
94 {
95         static uint16_t cpt = 0;
96         static int32_t old_a = 0, old_d = 0;
97
98 #ifdef HOST_VERSION
99         robotsim_update();
100 #else
101 #ifdef DEBUG_CPLD
102         int32_t ll, rr;
103 #endif
104         /* read encoders */
105         if (mainboard.flags & DO_ENCODERS) {
106                 encoders_spi_manage(NULL);
107         }
108 #ifdef DEBUG_CPLD
109         ll = encoders_spi_get_value(LEFT_ENCODER);
110         rr = encoders_spi_get_value(RIGHT_ENCODER);
111         if ((ll - ll_prev > 3000) || (ll - ll_prev < -3000) ||
112             (rr - rr_prev > 3000) || (rr - rr_prev < -3000)) {
113                 printf_P(PSTR("/ %d %d %d %d\r\n"),
114                          g_encoders_spi_previous[0],
115                          g_encoders_spi_previous[1],
116                          g_encoders_spi_previous[2],
117                          g_encoders_spi_previous[3]);
118                 BRAKE_ON();
119                 while (1);
120         }
121         ll_prev = ll;
122         rr_prev = rr;
123 #endif
124 #endif
125
126         /* robot system, conversion to angle,distance */
127         if (mainboard.flags & DO_RS) {
128                 int16_t a,d;
129                 rs_update(&mainboard.rs); /* takes about 0.5 ms */
130                 /* process and store current speed */
131                 a = rs_get_angle(&mainboard.rs);
132                 d = rs_get_distance(&mainboard.rs);
133                 mainboard.speed_a = a - old_a;
134                 mainboard.speed_d = d - old_d;
135                 old_a = a;
136                 old_d = d;
137         }
138
139         /* control system */
140         if (mainboard.flags & DO_CS) {
141                 if (mainboard.angle.on)
142                         cs_manage(&mainboard.angle.cs);
143                 if (mainboard.distance.on)
144                         cs_manage(&mainboard.distance.cs);
145 #ifndef HOST_VERSION
146                 if (mainboard.left_cobroller.on)
147                         cs_manage(&mainboard.left_cobroller.cs);
148                 if (mainboard.right_cobroller.on)
149                         cs_manage(&mainboard.right_cobroller.cs);
150 #endif
151         }
152         if ((cpt & 1) && (mainboard.flags & DO_POS)) {
153                 /* about 1.5ms (worst case without centrifugal force
154                  * compensation) */
155                 position_manage(&mainboard.pos);
156         }
157         if ((mainboard.flags & DO_BD) && (mainboard.flags & DO_POWER)) {
158                 bd_manage_from_cs(&mainboard.angle.bd, &mainboard.angle.cs);
159                 bd_manage_from_cs(&mainboard.distance.bd, &mainboard.distance.cs);
160 #ifndef HOST_VERSION
161                 bd_manage_from_cs(&mainboard.left_cobroller.bd, &mainboard.left_cobroller.cs);
162                 bd_manage_from_cs(&mainboard.right_cobroller.bd, &mainboard.right_cobroller.cs);
163                 if (mainboard.flags & DO_ERRBLOCKING) {
164                         if (bd_get(&mainboard.left_cobroller.bd) ||
165                             bd_get(&mainboard.left_cobroller.bd)) {
166                                 printf_P(PSTR("MOTOR BLOCKED STOP ALL\r\n"));
167                                 mainboard.flags &= ~(DO_POWER | DO_ERRBLOCKING);
168                         }
169                 }
170 #endif
171         }
172 #ifndef HOST_VERSION
173         if (mainboard.flags & DO_TIMER) {
174                 uint8_t second;
175                 /* the robot should stop correctly in the strat, but
176                  * in some cases, we must force the stop from an
177                  * interrupt */
178                 second = time_get_s();
179                 if (second >= MATCH_TIME + 2) {
180                         pwm_ng_set(LEFT_PWM, 0);
181                         pwm_ng_set(RIGHT_PWM, 0);
182                         printf_P(PSTR("END OF TIME\r\n"));
183                         while(1);
184                 }
185         }
186 #endif
187         /* brakes */
188         if (mainboard.flags & DO_POWER)
189                 BRAKE_OFF();
190         else
191                 BRAKE_ON();
192         cpt++;
193
194 #ifdef HOST_VERSION
195         if ((cpt & 7) == 0) {
196                 //              dump_cs("dist", &mainboard.distance.cs);
197                 robotsim_dump();
198         }
199 #endif
200 }
201
202 void dump_cs_debug(const char *name, struct cs *cs)
203 {
204         DEBUG(E_USER_CS, "%s cons=% .5"PRIi32" fcons=% .5"PRIi32" err=% .5"PRIi32" "
205               "in=% .5"PRIi32" out=% .5"PRIi32"",
206               name, cs_get_consign(cs), cs_get_filtered_consign(cs),
207               cs_get_error(cs), cs_get_filtered_feedback(cs),
208               cs_get_out(cs));
209 }
210
211 void dump_cs(const char *name, struct cs *cs)
212 {
213         printf_P(PSTR("%s cons=% .5"PRIi32" fcons=% .5"PRIi32" err=% .5"PRIi32" "
214                       "in=% .5"PRIi32" out=% .5"PRIi32"\r\n"),
215                  name, cs_get_consign(cs), cs_get_filtered_consign(cs),
216                  cs_get_error(cs), cs_get_filtered_feedback(cs),
217                  cs_get_out(cs));
218 }
219
220 void dump_pid(const char *name, struct pid_filter *pid)
221 {
222         printf_P(PSTR("%s P=% .8"PRIi32" I=% .8"PRIi32" D=% .8"PRIi32" out=% .8"PRIi32"\r\n"),
223                  name,
224                  pid_get_value_in(pid) * pid_get_gain_P(pid),
225                  pid_get_value_I(pid) * pid_get_gain_I(pid),
226                  pid_get_value_D(pid) * pid_get_gain_D(pid),
227                  pid_get_value_out(pid));
228 }
229
230 void microb_cs_init(void)
231 {
232         /* ROBOT_SYSTEM */
233         rs_init(&mainboard.rs);
234         rs_set_left_pwm(&mainboard.rs, pwm_set_and_save, LEFT_PWM);
235         rs_set_right_pwm(&mainboard.rs,  pwm_set_and_save, RIGHT_PWM);
236         /* increase gain to decrease dist, increase left and it will turn more left */
237 #ifdef HOST_VERSION
238         rs_set_left_ext_encoder(&mainboard.rs, robotsim_encoder_get,
239                                 LEFT_ENCODER, IMP_COEF * 1.);
240         rs_set_right_ext_encoder(&mainboard.rs, robotsim_encoder_get,
241                                  RIGHT_ENCODER, IMP_COEF * 1.);
242 #else
243         rs_set_left_ext_encoder(&mainboard.rs, encoders_spi_get_value,
244                                 LEFT_ENCODER, IMP_COEF * -1.011718);
245         rs_set_right_ext_encoder(&mainboard.rs, encoders_spi_get_value,
246                                  RIGHT_ENCODER, IMP_COEF * 1.012695);
247 #endif
248         /* rs will use external encoders */
249         rs_set_flags(&mainboard.rs, RS_USE_EXT);
250
251         /* POSITION MANAGER */
252         position_init(&mainboard.pos);
253         position_set_physical_params(&mainboard.pos, VIRTUAL_TRACK_MM, DIST_IMP_MM);
254         position_set_related_robot_system(&mainboard.pos, &mainboard.rs);
255         //      position_set_centrifugal_coef(&mainboard.pos, 0.000016);
256         position_use_ext(&mainboard.pos);
257
258         /* TRAJECTORY MANAGER */
259         trajectory_init(&mainboard.traj, CS_HZ);
260         trajectory_set_cs(&mainboard.traj, &mainboard.distance.cs,
261                           &mainboard.angle.cs);
262         trajectory_set_robot_params(&mainboard.traj, &mainboard.rs, &mainboard.pos);
263         trajectory_set_speed(&mainboard.traj, SPEED_DIST_FAST, SPEED_ANGLE_FAST); /* d, a */
264         trajectory_set_acc(&mainboard.traj, ACC_DIST, ACC_ANGLE); /* d, a */
265         /* distance window, angle window, angle start */
266         trajectory_set_windows(&mainboard.traj, 200., 5.0, 30.);
267
268         /* ---- CS angle */
269         /* PID */
270         pid_init(&mainboard.angle.pid);
271         pid_set_gains(&mainboard.angle.pid, 850, 30, 15000);
272         pid_set_maximums(&mainboard.angle.pid, 0, 10000, 4095);
273         pid_set_out_shift(&mainboard.angle.pid, 10);
274         pid_set_derivate_filter(&mainboard.angle.pid, 4);
275
276         /* QUADRAMP */
277         quadramp_init(&mainboard.angle.qr);
278         quadramp_set_1st_order_vars(&mainboard.angle.qr, 500, 500); /* set speed */
279         quadramp_set_2nd_order_vars(&mainboard.angle.qr, 5, 5); /* set accel */
280
281         /* CS */
282         cs_init(&mainboard.angle.cs);
283         cs_set_consign_filter(&mainboard.angle.cs, quadramp_do_filter, &mainboard.angle.qr);
284         cs_set_correct_filter(&mainboard.angle.cs, pid_do_filter, &mainboard.angle.pid);
285         cs_set_process_in(&mainboard.angle.cs, rs_set_angle, &mainboard.rs);
286         cs_set_process_out(&mainboard.angle.cs, rs_get_angle, &mainboard.rs);
287         cs_set_consign(&mainboard.angle.cs, 0);
288
289         /* Blocking detection */
290         bd_init(&mainboard.angle.bd);
291         bd_set_speed_threshold(&mainboard.angle.bd, 80);
292         bd_set_current_thresholds(&mainboard.angle.bd, 500, 8000, 1000000, 20);
293
294         /* ---- CS distance */
295         /* PID */
296         pid_init(&mainboard.distance.pid);
297         pid_set_gains(&mainboard.distance.pid, 850, 30, 15000);
298         pid_set_maximums(&mainboard.distance.pid, 0, 10000, 4095);
299         pid_set_out_shift(&mainboard.distance.pid, 10);
300         pid_set_derivate_filter(&mainboard.distance.pid, 6);
301
302         /* QUADRAMP */
303         quadramp_init(&mainboard.distance.qr);
304         quadramp_set_1st_order_vars(&mainboard.distance.qr, 500, 500); /* set speed */
305         quadramp_set_2nd_order_vars(&mainboard.distance.qr, 5., 5.); /* set accel */
306
307         /* CS */
308         cs_init(&mainboard.distance.cs);
309         cs_set_consign_filter(&mainboard.distance.cs, quadramp_do_filter, &mainboard.distance.qr);
310         cs_set_correct_filter(&mainboard.distance.cs, pid_do_filter, &mainboard.distance.pid);
311         cs_set_process_in(&mainboard.distance.cs, rs_set_distance, &mainboard.rs);
312         cs_set_process_out(&mainboard.distance.cs, rs_get_distance, &mainboard.rs);
313         cs_set_consign(&mainboard.distance.cs, 0);
314
315         /* Blocking detection */
316         bd_init(&mainboard.distance.bd);
317         bd_set_speed_threshold(&mainboard.distance.bd, 60);
318         bd_set_current_thresholds(&mainboard.distance.bd, 500, 8000, 1000000, 20);
319
320 #ifndef HOST_VERSION
321         /* ---- CS left_cobroller */
322         /* PID */
323         pid_init(&mainboard.left_cobroller.pid);
324         pid_set_gains(&mainboard.left_cobroller.pid, 80, 10, 10);
325         pid_set_maximums(&mainboard.left_cobroller.pid, 0, 30000, 4095);
326         pid_set_out_shift(&mainboard.left_cobroller.pid, 5);
327         pid_set_derivate_filter(&mainboard.left_cobroller.pid, 6);
328
329         /* CS */
330         cs_init(&mainboard.left_cobroller.cs);
331         cs_set_correct_filter(&mainboard.left_cobroller.cs, pid_do_filter, &mainboard.left_cobroller.pid);
332         cs_set_process_in(&mainboard.left_cobroller.cs, pwm_ng_set, LEFT_COBROLLER_PWM);
333         cs_set_process_out(&mainboard.left_cobroller.cs, encoders_left_cobroller_speed, LEFT_COBROLLER_ENCODER);
334         cs_set_consign(&mainboard.left_cobroller.cs, 0);
335
336         /* Blocking detection */
337         bd_init(&mainboard.left_cobroller.bd);
338         bd_set_speed_threshold(&mainboard.left_cobroller.bd, 60);
339         bd_set_current_thresholds(&mainboard.left_cobroller.bd, 500, 8000, 1000000, 50);
340
341         /* ---- CS right_cobroller */
342         /* PID */
343         pid_init(&mainboard.right_cobroller.pid);
344         pid_set_gains(&mainboard.right_cobroller.pid, 80, 10, 10);
345         pid_set_maximums(&mainboard.right_cobroller.pid, 0, 30000, 4095);
346         pid_set_out_shift(&mainboard.right_cobroller.pid, 5);
347         pid_set_derivate_filter(&mainboard.right_cobroller.pid, 6);
348
349         /* CS */
350         cs_init(&mainboard.right_cobroller.cs);
351         cs_set_correct_filter(&mainboard.right_cobroller.cs, pid_do_filter, &mainboard.right_cobroller.pid);
352         cs_set_process_in(&mainboard.right_cobroller.cs, pwm_ng_set, RIGHT_COBROLLER_PWM);
353         cs_set_process_out(&mainboard.right_cobroller.cs, encoders_right_cobroller_speed, RIGHT_COBROLLER_ENCODER);
354         cs_set_consign(&mainboard.right_cobroller.cs, 0);
355
356         /* Blocking detection */
357         bd_init(&mainboard.right_cobroller.bd);
358         bd_set_speed_threshold(&mainboard.right_cobroller.bd, 60);
359         bd_set_current_thresholds(&mainboard.right_cobroller.bd, 500, 8000, 1000000, 50);
360 #endif /* !HOST_VERSION */
361
362         /* set them on !! */
363         mainboard.angle.on = 1;
364         mainboard.distance.on = 1;
365         mainboard.left_cobroller.on = 1;
366         mainboard.right_cobroller.on = 1;
367
368 #ifdef DEBUG_CPLD
369         ll_prev = encoders_spi_get_value(LEFT_ENCODER);
370         rr_prev = encoders_spi_get_value(RIGHT_ENCODER);
371 #endif
372
373         scheduler_add_periodical_event_priority(do_cs, NULL,
374                                                 5000L / SCHEDULER_UNIT,
375                                                 CS_PRIO);
376
377 }