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