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>
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);
104 if (mainboard.wheel.on)
105 cs_manage(&mainboard.wheel.cs);
106 if (mainboard.elevator.on)
107 cs_manage(&mainboard.elevator.cs);
112 if ((cpt & 1) && (mainboard.flags & DO_POS)) {
113 /* about 1.5ms (worst case without centrifugal force
115 position_manage(&mainboard.pos);
117 if (mainboard.flags & DO_BD) {
118 bd_manage_from_cs(&mainboard.angle.bd, &mainboard.angle.cs);
119 bd_manage_from_cs(&mainboard.distance.bd, &mainboard.distance.cs);
121 if (mainboard.flags & DO_TIMER) {
123 /* the robot should stop correctly in the strat, but
124 * in some cases, we must force the stop from an
126 second = time_get_s();
127 if (second >= MATCH_TIME + 2) {
128 pwm_ng_set(LEFT_PWM, 0);
129 pwm_ng_set(RIGHT_PWM, 0);
130 printf_P(PSTR("END OF TIME\r\n"));
135 if (mainboard.flags & DO_POWER)
142 void dump_cs_debug(const char *name, struct cs *cs)
144 DEBUG(E_USER_CS, "%s cons=% .5ld fcons=% .5ld err=% .5ld "
145 "in=% .5ld out=% .5ld",
146 name, cs_get_consign(cs), cs_get_filtered_consign(cs),
147 cs_get_error(cs), cs_get_filtered_feedback(cs),
151 void dump_cs(const char *name, struct cs *cs)
153 printf_P(PSTR("%s cons=% .5ld fcons=% .5ld err=% .5ld "
154 "in=% .5ld out=% .5ld\r\n"),
155 name, cs_get_consign(cs), cs_get_filtered_consign(cs),
156 cs_get_error(cs), cs_get_filtered_feedback(cs),
160 void dump_pid(const char *name, struct pid_filter *pid)
162 printf_P(PSTR("%s P=% .8ld I=% .8ld D=% .8ld out=% .8ld\r\n"),
164 pid_get_value_in(pid) * pid_get_gain_P(pid),
165 pid_get_value_I(pid) * pid_get_gain_I(pid),
166 pid_get_value_D(pid) * pid_get_gain_D(pid),
167 pid_get_value_out(pid));
170 void microb_cs_init(void)
173 rs_init(&mainboard.rs);
174 rs_set_left_pwm(&mainboard.rs, pwm_set_and_save, LEFT_PWM);
175 rs_set_right_pwm(&mainboard.rs, pwm_set_and_save, RIGHT_PWM);
176 /* increase gain to decrease dist, increase left and it will turn more left */
177 rs_set_left_ext_encoder(&mainboard.rs, encoders_microb_get_value,
178 LEFT_ENCODER, IMP_COEF * -1.0000);
179 rs_set_right_ext_encoder(&mainboard.rs, encoders_microb_get_value,
180 RIGHT_ENCODER, IMP_COEF * 1.0000);
181 /* rs will use external encoders */
182 rs_set_flags(&mainboard.rs, RS_USE_EXT);
184 /* POSITION MANAGER */
185 position_init(&mainboard.pos);
186 position_set_physical_params(&mainboard.pos, VIRTUAL_TRACK_MM, DIST_IMP_MM);
187 position_set_related_robot_system(&mainboard.pos, &mainboard.rs);
188 //position_set_centrifugal_coef(&mainboard.pos, 0.000016);
189 position_use_ext(&mainboard.pos);
191 /* TRAJECTORY MANAGER */
192 trajectory_init(&mainboard.traj);
193 trajectory_set_cs(&mainboard.traj, &mainboard.distance.cs,
194 &mainboard.angle.cs);
195 trajectory_set_robot_params(&mainboard.traj, &mainboard.rs, &mainboard.pos);
196 trajectory_set_speed(&mainboard.traj, 1500, 1500); /* d, a */
197 /* distance window, angle window, angle start */
198 trajectory_set_windows(&mainboard.traj, 200., 5.0, 30.);
202 pid_init(&mainboard.angle.pid);
203 pid_set_gains(&mainboard.angle.pid, 500, 10, 7000);
204 pid_set_maximums(&mainboard.angle.pid, 0, 20000, 4095);
205 pid_set_out_shift(&mainboard.angle.pid, 10);
206 pid_set_derivate_filter(&mainboard.angle.pid, 4);
209 quadramp_init(&mainboard.angle.qr);
210 quadramp_set_1st_order_vars(&mainboard.angle.qr, 2000, 2000); /* set speed */
211 quadramp_set_2nd_order_vars(&mainboard.angle.qr, 13, 13); /* set accel */
214 cs_init(&mainboard.angle.cs);
215 cs_set_consign_filter(&mainboard.angle.cs, quadramp_do_filter, &mainboard.angle.qr);
216 cs_set_correct_filter(&mainboard.angle.cs, pid_do_filter, &mainboard.angle.pid);
217 cs_set_process_in(&mainboard.angle.cs, rs_set_angle, &mainboard.rs);
218 cs_set_process_out(&mainboard.angle.cs, rs_get_angle, &mainboard.rs);
219 cs_set_consign(&mainboard.angle.cs, 0);
221 /* Blocking detection */
222 bd_init(&mainboard.angle.bd);
223 bd_set_speed_threshold(&mainboard.angle.bd, 80);
224 bd_set_current_thresholds(&mainboard.angle.bd, 500, 8000, 1000000, 50);
226 /* ---- CS distance */
228 pid_init(&mainboard.distance.pid);
229 pid_set_gains(&mainboard.distance.pid, 500, 10, 7000);
230 pid_set_maximums(&mainboard.distance.pid, 0, 2000, 4095);
231 pid_set_out_shift(&mainboard.distance.pid, 10);
232 pid_set_derivate_filter(&mainboard.distance.pid, 6);
235 quadramp_init(&mainboard.distance.qr);
236 quadramp_set_1st_order_vars(&mainboard.distance.qr, 2000, 2000); /* set speed */
237 quadramp_set_2nd_order_vars(&mainboard.distance.qr, 17, 17); /* set accel */
240 cs_init(&mainboard.distance.cs);
241 cs_set_consign_filter(&mainboard.distance.cs, quadramp_do_filter, &mainboard.distance.qr);
242 cs_set_correct_filter(&mainboard.distance.cs, pid_do_filter, &mainboard.distance.pid);
243 cs_set_process_in(&mainboard.distance.cs, rs_set_distance, &mainboard.rs);
244 cs_set_process_out(&mainboard.distance.cs, rs_get_distance, &mainboard.rs);
245 cs_set_consign(&mainboard.distance.cs, 0);
247 /* Blocking detection */
248 bd_init(&mainboard.distance.bd);
249 bd_set_speed_threshold(&mainboard.distance.bd, 60);
250 bd_set_current_thresholds(&mainboard.distance.bd, 500, 8000, 1000000, 50);
256 pid_init(&mainboard.fessor.pid);
257 pid_set_gains(&mainboard.fessor.pid, 300, 10, 150);
258 pid_set_maximums(&mainboard.fessor.pid, 0, 10000, 4095);
259 pid_set_out_shift(&mainboard.fessor.pid, 10);
260 pid_set_derivate_filter(&mainboard.fessor.pid, 4);
263 cs_init(&mainboard.fessor.cs);
264 cs_set_correct_filter(&mainboard.fessor.cs, pid_do_filter, &mainboard.fessor.pid);
265 cs_set_process_in(&mainboard.fessor.cs, fessor_set, NULL);
266 cs_set_process_out(&mainboard.fessor.cs, encoders_microb_get_value, FESSOR_ENC);
271 /* ---- CS elevator */
275 pid_init(&mainboard.elevator.pid);
276 pid_set_gains(&mainboard.elevator.pid, 300, 10, 150);
277 pid_set_maximums(&mainboard.elevator.pid, 0, 10000, 4095);
278 pid_set_out_shift(&mainboard.elevator.pid, 10);
279 pid_set_derivate_filter(&mainboard.elevator.pid, 4);
282 cs_init(&mainboard.elevator.cs);
283 cs_set_correct_filter(&mainboard.elevator.cs, pid_do_filter, &mainboard.elevator.pid);
284 cs_set_process_in(&mainboard.elevator.cs, elevator_set, NULL);
285 cs_set_process_out(&mainboard.elevator.cs, encoders_microb_get_value, ELEVATOR_ENC);
291 pid_init(&mainboard.wheel.pid);
292 pid_set_gains(&mainboard.wheel.pid, 100, 100, 0);
293 pid_set_maximums(&mainboard.wheel.pid, 0, 30000, 4095);
294 pid_set_out_shift(&mainboard.wheel.pid, 5);
295 pid_set_derivate_filter(&mainboard.wheel.pid, 4);
298 cs_init(&mainboard.wheel.cs);
299 cs_set_correct_filter(&mainboard.wheel.cs, pid_do_filter, &mainboard.wheel.pid);
300 cs_set_process_in(&mainboard.wheel.cs, wheel_set, NULL);
301 cs_set_process_out(&mainboard.wheel.cs, wheel_get_value, NULL);
302 cs_set_consign(&mainboard.wheel.cs, 1000);
305 mainboard.angle.on = 0;
306 mainboard.distance.on = 0;
307 mainboard.fessor.on = 1;
308 mainboard.elevator.on = 1;
309 mainboard.wheel.on = 1;
310 mainboard.flags |= DO_CS;
312 scheduler_add_periodical_event_priority(do_cs, NULL,
313 5000L / SCHEDULER_UNIT,