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;
83 /* called every 5 ms */
84 static void do_cs(void *dummy)
86 static uint16_t cpt = 0;
87 static int32_t old_a = 0, old_d = 0;
93 if (mainboard.flags & DO_ENCODERS) {
94 encoders_spi_manage(NULL);
98 /* robot system, conversion to angle,distance */
99 if (mainboard.flags & DO_RS) {
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;
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);
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);
124 if ((cpt & 1) && (mainboard.flags & DO_POS)) {
125 /* about 1.5ms (worst case without centrifugal force
127 position_manage(&mainboard.pos);
129 if ((mainboard.flags & DO_BD) && (mainboard.flags & DO_POWER)) {
130 bd_manage_from_cs(&mainboard.angle.bd, &mainboard.angle.cs);
131 bd_manage_from_cs(&mainboard.distance.bd, &mainboard.distance.cs);
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 if (mainboard.flags & DO_ERRBLOCKING) {
136 if (bd_get(&mainboard.left_cobroller.bd) ||
137 bd_get(&mainboard.left_cobroller.bd)) {
138 printf_P(PSTR("MOTOR BLOCKED STOP ALL\r\n"));
139 mainboard.flags &= ~(DO_POWER | DO_ERRBLOCKING);
145 if (mainboard.flags & DO_TIMER) {
147 /* the robot should stop correctly in the strat, but
148 * in some cases, we must force the stop from an
150 second = time_get_s();
151 if (second >= MATCH_TIME + 2) {
152 pwm_ng_set(LEFT_PWM, 0);
153 pwm_ng_set(RIGHT_PWM, 0);
154 printf_P(PSTR("END OF TIME\r\n"));
160 if (mainboard.flags & DO_POWER)
167 if ((cpt & 7) == 0) {
168 // dump_cs("dist", &mainboard.distance.cs);
174 void dump_cs_debug(const char *name, struct cs *cs)
176 DEBUG(E_USER_CS, "%s cons=% .5"PRIi32" fcons=% .5"PRIi32" err=% .5"PRIi32" "
177 "in=% .5"PRIi32" out=% .5"PRIi32"",
178 name, cs_get_consign(cs), cs_get_filtered_consign(cs),
179 cs_get_error(cs), cs_get_filtered_feedback(cs),
183 void dump_cs(const char *name, struct cs *cs)
185 printf_P(PSTR("%s cons=% .5"PRIi32" fcons=% .5"PRIi32" err=% .5"PRIi32" "
186 "in=% .5"PRIi32" out=% .5"PRIi32"\r\n"),
187 name, cs_get_consign(cs), cs_get_filtered_consign(cs),
188 cs_get_error(cs), cs_get_filtered_feedback(cs),
192 void dump_pid(const char *name, struct pid_filter *pid)
194 printf_P(PSTR("%s P=% .8"PRIi32" I=% .8"PRIi32" D=% .8"PRIi32" out=% .8"PRIi32"\r\n"),
196 pid_get_value_in(pid) * pid_get_gain_P(pid),
197 pid_get_value_I(pid) * pid_get_gain_I(pid),
198 pid_get_value_D(pid) * pid_get_gain_D(pid),
199 pid_get_value_out(pid));
202 void microb_cs_init(void)
205 rs_init(&mainboard.rs);
206 rs_set_left_pwm(&mainboard.rs, pwm_set_and_save, LEFT_PWM);
207 rs_set_right_pwm(&mainboard.rs, pwm_set_and_save, RIGHT_PWM);
208 /* increase gain to decrease dist, increase left and it will turn more left */
210 rs_set_left_ext_encoder(&mainboard.rs, robotsim_encoder_get,
211 LEFT_ENCODER, IMP_COEF * 1.);
212 rs_set_right_ext_encoder(&mainboard.rs, robotsim_encoder_get,
213 RIGHT_ENCODER, IMP_COEF * 1.);
215 rs_set_left_ext_encoder(&mainboard.rs, encoders_spi_get_value,
216 LEFT_ENCODER, IMP_COEF * -1.011718);
217 rs_set_right_ext_encoder(&mainboard.rs, encoders_spi_get_value,
218 RIGHT_ENCODER, IMP_COEF * 1.012695);
220 /* rs will use external encoders */
221 rs_set_flags(&mainboard.rs, RS_USE_EXT);
223 /* POSITION MANAGER */
224 position_init(&mainboard.pos);
225 position_set_physical_params(&mainboard.pos, VIRTUAL_TRACK_MM, DIST_IMP_MM);
226 position_set_related_robot_system(&mainboard.pos, &mainboard.rs);
227 // position_set_centrifugal_coef(&mainboard.pos, 0.000016);
228 position_use_ext(&mainboard.pos);
230 /* TRAJECTORY MANAGER */
231 trajectory_init(&mainboard.traj, CS_HZ);
232 trajectory_set_cs(&mainboard.traj, &mainboard.distance.cs,
233 &mainboard.angle.cs);
234 trajectory_set_robot_params(&mainboard.traj, &mainboard.rs, &mainboard.pos);
235 trajectory_set_speed(&mainboard.traj, SPEED_DIST_FAST, SPEED_ANGLE_FAST); /* d, a */
236 trajectory_set_acc(&mainboard.traj, ACC_DIST, ACC_ANGLE); /* d, a */
237 /* distance window, angle window, angle start */
238 trajectory_set_windows(&mainboard.traj, 200., 5.0, 30.);
242 pid_init(&mainboard.angle.pid);
243 pid_set_gains(&mainboard.angle.pid, 850, 30, 15000);
244 pid_set_maximums(&mainboard.angle.pid, 0, 10000, 4095);
245 pid_set_out_shift(&mainboard.angle.pid, 10);
246 pid_set_derivate_filter(&mainboard.angle.pid, 4);
249 quadramp_init(&mainboard.angle.qr);
250 quadramp_set_1st_order_vars(&mainboard.angle.qr, 500, 500); /* set speed */
251 quadramp_set_2nd_order_vars(&mainboard.angle.qr, 5, 5); /* set accel */
254 cs_init(&mainboard.angle.cs);
255 cs_set_consign_filter(&mainboard.angle.cs, quadramp_do_filter, &mainboard.angle.qr);
256 cs_set_correct_filter(&mainboard.angle.cs, pid_do_filter, &mainboard.angle.pid);
257 cs_set_process_in(&mainboard.angle.cs, rs_set_angle, &mainboard.rs);
258 cs_set_process_out(&mainboard.angle.cs, rs_get_angle, &mainboard.rs);
259 cs_set_consign(&mainboard.angle.cs, 0);
261 /* Blocking detection */
262 bd_init(&mainboard.angle.bd);
263 bd_set_speed_threshold(&mainboard.angle.bd, 80);
264 bd_set_current_thresholds(&mainboard.angle.bd, 500, 8000, 1000000, 20);
266 /* ---- CS distance */
268 pid_init(&mainboard.distance.pid);
269 pid_set_gains(&mainboard.distance.pid, 850, 30, 15000);
270 pid_set_maximums(&mainboard.distance.pid, 0, 10000, 4095);
271 pid_set_out_shift(&mainboard.distance.pid, 10);
272 pid_set_derivate_filter(&mainboard.distance.pid, 6);
275 quadramp_init(&mainboard.distance.qr);
276 quadramp_set_1st_order_vars(&mainboard.distance.qr, 500, 500); /* set speed */
277 quadramp_set_2nd_order_vars(&mainboard.distance.qr, 5., 5.); /* set accel */
280 cs_init(&mainboard.distance.cs);
281 cs_set_consign_filter(&mainboard.distance.cs, quadramp_do_filter, &mainboard.distance.qr);
282 cs_set_correct_filter(&mainboard.distance.cs, pid_do_filter, &mainboard.distance.pid);
283 cs_set_process_in(&mainboard.distance.cs, rs_set_distance, &mainboard.rs);
284 cs_set_process_out(&mainboard.distance.cs, rs_get_distance, &mainboard.rs);
285 cs_set_consign(&mainboard.distance.cs, 0);
287 /* Blocking detection */
288 bd_init(&mainboard.distance.bd);
289 bd_set_speed_threshold(&mainboard.distance.bd, 60);
290 bd_set_current_thresholds(&mainboard.distance.bd, 500, 8000, 1000000, 20);
293 /* ---- CS left_cobroller */
295 pid_init(&mainboard.left_cobroller.pid);
296 pid_set_gains(&mainboard.left_cobroller.pid, 80, 10, 10);
297 pid_set_maximums(&mainboard.left_cobroller.pid, 0, 30000, 4095);
298 pid_set_out_shift(&mainboard.left_cobroller.pid, 5);
299 pid_set_derivate_filter(&mainboard.left_cobroller.pid, 6);
302 cs_init(&mainboard.left_cobroller.cs);
303 cs_set_correct_filter(&mainboard.left_cobroller.cs, pid_do_filter, &mainboard.left_cobroller.pid);
304 cs_set_process_in(&mainboard.left_cobroller.cs, pwm_ng_set, LEFT_COBROLLER_PWM);
305 cs_set_process_out(&mainboard.left_cobroller.cs, encoders_left_cobroller_speed, LEFT_COBROLLER_ENCODER);
306 cs_set_consign(&mainboard.left_cobroller.cs, 0);
308 /* Blocking detection */
309 bd_init(&mainboard.left_cobroller.bd);
310 bd_set_speed_threshold(&mainboard.left_cobroller.bd, 60);
311 bd_set_current_thresholds(&mainboard.left_cobroller.bd, 500, 8000, 1000000, 50);
313 /* ---- CS right_cobroller */
315 pid_init(&mainboard.right_cobroller.pid);
316 pid_set_gains(&mainboard.right_cobroller.pid, 80, 10, 10);
317 pid_set_maximums(&mainboard.right_cobroller.pid, 0, 30000, 4095);
318 pid_set_out_shift(&mainboard.right_cobroller.pid, 5);
319 pid_set_derivate_filter(&mainboard.right_cobroller.pid, 6);
322 cs_init(&mainboard.right_cobroller.cs);
323 cs_set_correct_filter(&mainboard.right_cobroller.cs, pid_do_filter, &mainboard.right_cobroller.pid);
324 cs_set_process_in(&mainboard.right_cobroller.cs, pwm_ng_set, RIGHT_COBROLLER_PWM);
325 cs_set_process_out(&mainboard.right_cobroller.cs, encoders_right_cobroller_speed, RIGHT_COBROLLER_ENCODER);
326 cs_set_consign(&mainboard.right_cobroller.cs, 0);
328 /* Blocking detection */
329 bd_init(&mainboard.right_cobroller.bd);
330 bd_set_speed_threshold(&mainboard.right_cobroller.bd, 60);
331 bd_set_current_thresholds(&mainboard.right_cobroller.bd, 500, 8000, 1000000, 50);
332 #endif /* !HOST_VERSION */
335 mainboard.angle.on = 1;
336 mainboard.distance.on = 1;
337 mainboard.left_cobroller.on = 1;
338 mainboard.right_cobroller.on = 1;
341 scheduler_add_periodical_event_priority(do_cs, NULL,
342 5000L / SCHEDULER_UNIT,