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