2 * Copyright Droids Corporation
3 * Olivier Matz <zer0@droids-corp.org>
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.
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.
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
19 * Revision : $Id: cs.c,v 1.9 2009-11-08 17:24:33 zer0 Exp $
28 #include <aversive/error.h>
32 #include <encoders_spi.h>
35 #include <scheduler.h>
36 #include <clock_time.h>
41 #include <control_system_manager.h>
42 #include <trajectory_manager.h>
43 #include <vect_base.h>
46 #include <obstacle_avoidance.h>
47 #include <blocking_detection_manager.h>
48 #include <robot_system.h>
49 #include <position_manager.h>
59 void dump_cs(const char *name, struct cs *cs);
62 int32_t encoders_left_cobroller_speed(void *number)
64 static volatile int32_t roller_pos;
66 tmp = encoders_spi_get_value(number);
67 speed = tmp - roller_pos;
72 int32_t encoders_right_cobroller_speed(void *number)
74 static volatile int32_t roller_pos;
76 tmp = encoders_spi_get_value(number);
77 speed = tmp - roller_pos;
88 extern int16_t g_encoders_spi_previous[4];
89 static int32_t ll_prev, rr_prev;
92 /* called every 5 ms */
93 static void do_cs(void *dummy)
95 static uint16_t cpt = 0;
96 static int32_t old_a = 0, old_d = 0;
105 if (mainboard.flags & DO_ENCODERS) {
106 encoders_spi_manage(NULL);
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]);
126 /* robot system, conversion to angle,distance */
127 if (mainboard.flags & DO_RS) {
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;
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);
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);
152 if ((cpt & 1) && (mainboard.flags & DO_POS)) {
153 /* about 1.5ms (worst case without centrifugal force
155 position_manage(&mainboard.pos);
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);
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);
173 if (mainboard.flags & DO_TIMER) {
175 /* the robot should stop correctly in the strat, but
176 * in some cases, we must force the stop from an
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"));
188 if (mainboard.flags & DO_POWER)
195 if ((cpt & 7) == 0) {
196 // dump_cs("dist", &mainboard.distance.cs);
202 void dump_cs_debug(const char *name, struct cs *cs)
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),
211 void dump_cs(const char *name, struct cs *cs)
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),
220 void dump_pid(const char *name, struct pid_filter *pid)
222 printf_P(PSTR("%s P=% .8"PRIi32" I=% .8"PRIi32" D=% .8"PRIi32" out=% .8"PRIi32"\r\n"),
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));
230 void microb_cs_init(void)
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 */
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.);
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);
248 /* rs will use external encoders */
249 rs_set_flags(&mainboard.rs, RS_USE_EXT);
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.000025);
256 position_use_ext(&mainboard.pos);
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.);
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);
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 */
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);
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);
294 /* ---- CS distance */
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);
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 */
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);
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);
321 /* ---- CS left_cobroller */
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);
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);
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);
341 /* ---- CS right_cobroller */
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);
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);
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 */
363 mainboard.angle.on = 1;
364 mainboard.distance.on = 1;
365 mainboard.left_cobroller.on = 1;
366 mainboard.right_cobroller.on = 1;
369 ll_prev = encoders_spi_get_value(LEFT_ENCODER);
370 rr_prev = encoders_spi_get_value(RIGHT_ENCODER);
373 scheduler_add_periodical_event_priority(do_cs, NULL,
374 5000L / SCHEDULER_UNIT,