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 $
27 #include <aversive/error.h>
31 #include <encoders_spi.h>
34 #include <scheduler.h>
40 #include <control_system_manager.h>
41 #include <trajectory_manager.h>
42 #include <vect_base.h>
45 #include <obstacle_avoidance.h>
46 #include <blocking_detection_manager.h>
47 #include <robot_system.h>
48 #include <position_manager.h>
57 /* called every 5 ms */
58 static void do_cs(void *dummy)
60 static uint16_t cpt = 0;
61 static int32_t old_a = 0, old_d = 0;
64 if (mainboard.flags & DO_ENCODERS) {
65 encoders_spi_manage(NULL);
68 /* XXX there is an issue which is probably related to avr-libc
69 * 1.6.2 (debian): this code using fixed_point lib does not
71 /* robot system, conversion to angle,distance */
72 if (mainboard.flags & DO_RS) {
74 rs_update(&mainboard.rs); /* takes about 0.5 ms */
75 /* process and store current speed */
76 a = rs_get_angle(&mainboard.rs);
77 d = rs_get_distance(&mainboard.rs);
78 mainboard.speed_a = a - old_a;
79 mainboard.speed_d = d - old_d;
85 if (mainboard.flags & DO_CS) {
86 if (mainboard.angle.on)
87 cs_manage(&mainboard.angle.cs);
88 if (mainboard.distance.on)
89 cs_manage(&mainboard.distance.cs);
91 if ((cpt & 1) && (mainboard.flags & DO_POS)) {
92 /* about 1.5ms (worst case without centrifugal force
94 position_manage(&mainboard.pos);
96 if (mainboard.flags & DO_BD) {
97 bd_manage_from_cs(&mainboard.angle.bd, &mainboard.angle.cs);
98 bd_manage_from_cs(&mainboard.distance.bd, &mainboard.distance.cs);
100 if (mainboard.flags & DO_TIMER) {
102 /* the robot should stop correctly in the strat, but
103 * in some cases, we must force the stop from an
105 second = time_get_s();
106 if (second >= MATCH_TIME + 2) {
107 pwm_ng_set(LEFT_PWM, 0);
108 pwm_ng_set(RIGHT_PWM, 0);
109 printf_P(PSTR("END OF TIME\r\n"));
114 if (mainboard.flags & DO_POWER)
121 void dump_cs_debug(const char *name, struct cs *cs)
123 DEBUG(E_USER_CS, "%s cons=% .5ld fcons=% .5ld err=% .5ld "
124 "in=% .5ld out=% .5ld",
125 name, cs_get_consign(cs), cs_get_filtered_consign(cs),
126 cs_get_error(cs), cs_get_filtered_feedback(cs),
130 void dump_cs(const char *name, struct cs *cs)
132 printf_P(PSTR("%s cons=% .5ld fcons=% .5ld err=% .5ld "
133 "in=% .5ld out=% .5ld\r\n"),
134 name, cs_get_consign(cs), cs_get_filtered_consign(cs),
135 cs_get_error(cs), cs_get_filtered_feedback(cs),
139 uint32_t encmask = 0xFFFFFFFF;
140 int32_t my_encoders_get_value(void *enc)
142 int32_t tmp = encoders_spi_get_value(enc);
143 return tmp & encmask;
146 void dump_pid(const char *name, struct pid_filter *pid)
148 printf_P(PSTR("%s P=% .8ld I=% .8ld D=% .8ld out=% .8ld\r\n"),
150 pid_get_value_in(pid) * pid_get_gain_P(pid),
151 pid_get_value_I(pid) * pid_get_gain_I(pid),
152 pid_get_value_D(pid) * pid_get_gain_D(pid),
153 pid_get_value_out(pid));
156 void microb_cs_init(void)
159 rs_init(&mainboard.rs);
160 rs_set_left_pwm(&mainboard.rs, pwm_set_and_save, LEFT_PWM);
161 rs_set_right_pwm(&mainboard.rs, pwm_set_and_save, RIGHT_PWM);
162 /* increase gain to decrease dist, increase left and it will turn more left */
163 rs_set_left_ext_encoder(&mainboard.rs, my_encoders_get_value,
164 LEFT_ENCODER, IMP_COEF * 1.0015);
165 rs_set_right_ext_encoder(&mainboard.rs, my_encoders_get_value,
166 RIGHT_ENCODER, IMP_COEF * -1.006);
167 /* rs will use external encoders */
168 rs_set_flags(&mainboard.rs, RS_USE_EXT);
170 /* POSITION MANAGER */
171 position_init(&mainboard.pos);
172 position_set_physical_params(&mainboard.pos, VIRTUAL_TRACK_MM, DIST_IMP_MM);
173 position_set_related_robot_system(&mainboard.pos, &mainboard.rs);
174 position_set_centrifugal_coef(&mainboard.pos, 0.000016);
175 position_use_ext(&mainboard.pos);
177 /* TRAJECTORY MANAGER */
178 trajectory_init(&mainboard.traj);
179 trajectory_set_cs(&mainboard.traj, &mainboard.distance.cs,
180 &mainboard.angle.cs);
181 trajectory_set_robot_params(&mainboard.traj, &mainboard.rs, &mainboard.pos);
182 trajectory_set_speed(&mainboard.traj, SPEED_DIST_FAST, SPEED_ANGLE_FAST); /* d, a */
183 /* distance window, angle window, angle start */
184 trajectory_set_windows(&mainboard.traj, 200., 5.0, 30.);
188 pid_init(&mainboard.angle.pid);
189 pid_set_gains(&mainboard.angle.pid, 500, 10, 7000);
190 pid_set_maximums(&mainboard.angle.pid, 0, 20000, 4095);
191 pid_set_out_shift(&mainboard.angle.pid, 10);
192 pid_set_derivate_filter(&mainboard.angle.pid, 4);
195 quadramp_init(&mainboard.angle.qr);
196 quadramp_set_1st_order_vars(&mainboard.angle.qr, 2000, 2000); /* set speed */
197 quadramp_set_2nd_order_vars(&mainboard.angle.qr, 13, 13); /* set accel */
200 cs_init(&mainboard.angle.cs);
201 cs_set_consign_filter(&mainboard.angle.cs, quadramp_do_filter, &mainboard.angle.qr);
202 cs_set_correct_filter(&mainboard.angle.cs, pid_do_filter, &mainboard.angle.pid);
203 cs_set_process_in(&mainboard.angle.cs, rs_set_angle, &mainboard.rs);
204 cs_set_process_out(&mainboard.angle.cs, rs_get_angle, &mainboard.rs);
205 cs_set_consign(&mainboard.angle.cs, 0);
207 /* Blocking detection */
208 bd_init(&mainboard.angle.bd);
209 bd_set_speed_threshold(&mainboard.angle.bd, 80);
210 bd_set_current_thresholds(&mainboard.angle.bd, 500, 8000, 1000000, 50);
212 /* ---- CS distance */
214 pid_init(&mainboard.distance.pid);
215 pid_set_gains(&mainboard.distance.pid, 500, 10, 7000);
216 pid_set_maximums(&mainboard.distance.pid, 0, 2000, 4095);
217 pid_set_out_shift(&mainboard.distance.pid, 10);
218 pid_set_derivate_filter(&mainboard.distance.pid, 6);
221 quadramp_init(&mainboard.distance.qr);
222 quadramp_set_1st_order_vars(&mainboard.distance.qr, 2000, 2000); /* set speed */
223 quadramp_set_2nd_order_vars(&mainboard.distance.qr, 17, 17); /* set accel */
226 cs_init(&mainboard.distance.cs);
227 cs_set_consign_filter(&mainboard.distance.cs, quadramp_do_filter, &mainboard.distance.qr);
228 cs_set_correct_filter(&mainboard.distance.cs, pid_do_filter, &mainboard.distance.pid);
229 cs_set_process_in(&mainboard.distance.cs, rs_set_distance, &mainboard.rs);
230 cs_set_process_out(&mainboard.distance.cs, rs_get_distance, &mainboard.rs);
231 cs_set_consign(&mainboard.distance.cs, 0);
233 /* Blocking detection */
234 bd_init(&mainboard.distance.bd);
235 bd_set_speed_threshold(&mainboard.distance.bd, 60);
236 bd_set_current_thresholds(&mainboard.distance.bd, 500, 8000, 1000000, 50);
239 mainboard.angle.on = 1;
240 mainboard.distance.on = 1;
243 scheduler_add_periodical_event_priority(do_cs, NULL,
244 5000L / SCHEDULER_UNIT,