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.7 2009-05-02 10:08:09 zer0 Exp $
27 #include <aversive/error.h>
28 #include <aversive/wait.h>
30 #include <encoders_microb.h>
33 #include <scheduler.h>
34 #include <clock_time.h>
39 #include <control_system_manager.h>
40 #include <trajectory_manager.h>
41 #include <blocking_detection_manager.h>
42 #include <robot_system.h>
43 #include <position_manager.h>
51 static int32_t wheel_speed = 0;
53 static int32_t wheel_get_value(void * dummy)
58 static void wheel_set(void *dummy, int32_t val)
62 pwm_ng_set(WHEEL_PWM, val);
65 static void wheel_update_value(void)
67 static int32_t prev = 0;
70 val = encoders_microb_get_value(WHEEL_ENC);
71 wheel_speed = val - prev;
75 /* called every 5 ms */
76 static void do_cs(void *dummy)
78 static uint16_t cpt = 0;
79 static int32_t old_a = 0, old_d = 0;
83 /* robot system, conversion to angle,distance */
84 if (mainboard.flags & DO_RS) {
86 rs_update(&mainboard.rs); /* takes about 0.5 ms */
87 /* process and store current speed */
88 a = rs_get_angle(&mainboard.rs);
89 d = rs_get_distance(&mainboard.rs);
90 mainboard.speed_a = a - old_a;
91 mainboard.speed_d = d - old_d;
97 if (mainboard.flags & DO_CS) {
98 if (mainboard.angle.on)
99 cs_manage(&mainboard.angle.cs);
100 if (mainboard.distance.on)
101 cs_manage(&mainboard.distance.cs);
102 if (mainboard.fessor.on) {
103 cs_manage(&mainboard.fessor.cs);
106 if (mainboard.wheel.on)
107 cs_manage(&mainboard.wheel.cs);
108 if (mainboard.elevator.on) {
109 cs_manage(&mainboard.elevator.cs);
113 if ((cpt & 1) && (mainboard.flags & DO_POS)) {
114 /* about 1.5ms (worst case without centrifugal force
116 position_manage(&mainboard.pos);
118 if (mainboard.flags & DO_BD) {
119 bd_manage_from_cs(&mainboard.angle.bd, &mainboard.angle.cs);
120 bd_manage_from_cs(&mainboard.distance.bd, &mainboard.distance.cs);
122 if (mainboard.flags & DO_TIMER) {
124 /* the robot should stop correctly in the strat, but
125 * in some cases, we must force the stop from an
127 second = time_get_s();
128 if (second >= MATCH_TIME + 2) {
129 pwm_ng_set(LEFT_PWM, 0);
130 pwm_ng_set(RIGHT_PWM, 0);
131 printf_P(PSTR("END OF TIME\r\n"));
136 if (mainboard.flags & DO_POWER)
143 void dump_cs_debug(const char *name, struct cs *cs)
145 DEBUG(E_USER_CS, "%s cons=% .5ld fcons=% .5ld err=% .5ld "
146 "in=% .5ld out=% .5ld",
147 name, cs_get_consign(cs), cs_get_filtered_consign(cs),
148 cs_get_error(cs), cs_get_filtered_feedback(cs),
152 void dump_cs(const char *name, struct cs *cs)
154 printf_P(PSTR("%s cons=% .5ld fcons=% .5ld err=% .5ld "
155 "in=% .5ld out=% .5ld\r\n"),
156 name, cs_get_consign(cs), cs_get_filtered_consign(cs),
157 cs_get_error(cs), cs_get_filtered_feedback(cs),
161 void dump_pid(const char *name, struct pid_filter *pid)
163 printf_P(PSTR("%s P=% .8ld I=% .8ld D=% .8ld out=% .8ld\r\n"),
165 pid_get_value_in(pid) * pid_get_gain_P(pid),
166 pid_get_value_I(pid) * pid_get_gain_I(pid),
167 pid_get_value_D(pid) * pid_get_gain_D(pid),
168 pid_get_value_out(pid));
171 void microb_cs_init(void)
174 rs_init(&mainboard.rs);
175 rs_set_left_pwm(&mainboard.rs, pwm_set_and_save, LEFT_PWM);
176 rs_set_right_pwm(&mainboard.rs, pwm_set_and_save, RIGHT_PWM);
177 /* increase gain to decrease dist, increase left and it will turn more left */
178 rs_set_left_ext_encoder(&mainboard.rs, encoders_microb_get_value,
179 LEFT_ENCODER, IMP_COEF * -1.0000);
180 rs_set_right_ext_encoder(&mainboard.rs, encoders_microb_get_value,
181 RIGHT_ENCODER, IMP_COEF * 1.0000);
182 /* rs will use external encoders */
183 rs_set_flags(&mainboard.rs, RS_USE_EXT);
185 /* POSITION MANAGER */
186 position_init(&mainboard.pos);
187 position_set_physical_params(&mainboard.pos, VIRTUAL_TRACK_MM, DIST_IMP_MM);
188 position_set_related_robot_system(&mainboard.pos, &mainboard.rs);
189 //position_set_centrifugal_coef(&mainboard.pos, 0.000016);
190 position_use_ext(&mainboard.pos);
192 /* TRAJECTORY MANAGER */
193 trajectory_init(&mainboard.traj);
194 trajectory_set_cs(&mainboard.traj, &mainboard.distance.cs,
195 &mainboard.angle.cs);
196 trajectory_set_robot_params(&mainboard.traj, &mainboard.rs, &mainboard.pos);
197 trajectory_set_speed(&mainboard.traj, 1500, 1500); /* d, a */
198 /* distance window, angle window, angle start */
199 trajectory_set_windows(&mainboard.traj, 200., 5.0, 30.);
203 pid_init(&mainboard.angle.pid);
204 pid_set_gains(&mainboard.angle.pid, 500, 10, 7000);
205 pid_set_maximums(&mainboard.angle.pid, 0, 20000, 4095);
206 pid_set_out_shift(&mainboard.angle.pid, 10);
207 pid_set_derivate_filter(&mainboard.angle.pid, 4);
210 quadramp_init(&mainboard.angle.qr);
211 quadramp_set_1st_order_vars(&mainboard.angle.qr, 2000, 2000); /* set speed */
212 quadramp_set_2nd_order_vars(&mainboard.angle.qr, 13, 13); /* set accel */
215 cs_init(&mainboard.angle.cs);
216 cs_set_consign_filter(&mainboard.angle.cs, quadramp_do_filter, &mainboard.angle.qr);
217 cs_set_correct_filter(&mainboard.angle.cs, pid_do_filter, &mainboard.angle.pid);
218 cs_set_process_in(&mainboard.angle.cs, rs_set_angle, &mainboard.rs);
219 cs_set_process_out(&mainboard.angle.cs, rs_get_angle, &mainboard.rs);
220 cs_set_consign(&mainboard.angle.cs, 0);
222 /* Blocking detection */
223 bd_init(&mainboard.angle.bd);
224 bd_set_speed_threshold(&mainboard.angle.bd, 80);
225 bd_set_current_thresholds(&mainboard.angle.bd, 500, 8000, 1000000, 50);
227 /* ---- CS distance */
229 pid_init(&mainboard.distance.pid);
230 pid_set_gains(&mainboard.distance.pid, 500, 10, 7000);
231 pid_set_maximums(&mainboard.distance.pid, 0, 2000, 4095);
232 pid_set_out_shift(&mainboard.distance.pid, 10);
233 pid_set_derivate_filter(&mainboard.distance.pid, 6);
236 quadramp_init(&mainboard.distance.qr);
237 quadramp_set_1st_order_vars(&mainboard.distance.qr, 2000, 2000); /* set speed */
238 quadramp_set_2nd_order_vars(&mainboard.distance.qr, 17, 17); /* set accel */
241 cs_init(&mainboard.distance.cs);
242 cs_set_consign_filter(&mainboard.distance.cs, quadramp_do_filter, &mainboard.distance.qr);
243 cs_set_correct_filter(&mainboard.distance.cs, pid_do_filter, &mainboard.distance.pid);
244 cs_set_process_in(&mainboard.distance.cs, rs_set_distance, &mainboard.rs);
245 cs_set_process_out(&mainboard.distance.cs, rs_get_distance, &mainboard.rs);
246 cs_set_consign(&mainboard.distance.cs, 0);
248 /* Blocking detection */
249 bd_init(&mainboard.distance.bd);
250 bd_set_speed_threshold(&mainboard.distance.bd, 60);
251 bd_set_current_thresholds(&mainboard.distance.bd, 500, 8000, 1000000, 50);
257 pid_init(&mainboard.fessor.pid);
258 pid_set_gains(&mainboard.fessor.pid, 300, 10, 150);
259 pid_set_maximums(&mainboard.fessor.pid, 0, 10000, 4095);
260 pid_set_out_shift(&mainboard.fessor.pid, 10);
261 pid_set_derivate_filter(&mainboard.fessor.pid, 4);
264 cs_init(&mainboard.fessor.cs);
265 cs_set_correct_filter(&mainboard.fessor.cs, pid_do_filter, &mainboard.fessor.pid);
266 cs_set_process_in(&mainboard.fessor.cs, fessor_set, NULL);
267 cs_set_process_out(&mainboard.fessor.cs, encoders_microb_get_value, FESSOR_ENC);
272 /* ---- CS elevator */
276 pid_init(&mainboard.elevator.pid);
277 pid_set_gains(&mainboard.elevator.pid, 300, 10, 150);
278 pid_set_maximums(&mainboard.elevator.pid, 0, 10000, 4095);
279 pid_set_out_shift(&mainboard.elevator.pid, 10);
280 pid_set_derivate_filter(&mainboard.elevator.pid, 4);
283 cs_init(&mainboard.elevator.cs);
284 cs_set_correct_filter(&mainboard.elevator.cs, pid_do_filter, &mainboard.elevator.pid);
285 cs_set_process_in(&mainboard.elevator.cs, elevator_set, NULL);
286 cs_set_process_out(&mainboard.elevator.cs, encoders_microb_get_value, ELEVATOR_ENC);
292 pid_init(&mainboard.wheel.pid);
293 pid_set_gains(&mainboard.wheel.pid, 100, 100, 0);
294 pid_set_maximums(&mainboard.wheel.pid, 0, 30000, 4095);
295 pid_set_out_shift(&mainboard.wheel.pid, 5);
296 pid_set_derivate_filter(&mainboard.wheel.pid, 4);
299 cs_init(&mainboard.wheel.cs);
300 cs_set_correct_filter(&mainboard.wheel.cs, pid_do_filter, &mainboard.wheel.pid);
301 cs_set_process_in(&mainboard.wheel.cs, wheel_set, NULL);
302 cs_set_process_out(&mainboard.wheel.cs, wheel_get_value, NULL);
303 cs_set_consign(&mainboard.wheel.cs, 1000);
306 mainboard.angle.on = 0;
307 mainboard.distance.on = 0;
308 mainboard.fessor.on = 1;
309 mainboard.elevator.on = 0;
310 mainboard.wheel.on = 1;
311 mainboard.flags |= DO_CS;
313 scheduler_add_periodical_event_priority(do_cs, NULL,
314 5000L / SCHEDULER_UNIT,