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.4 2009-05-27 20:04:07 zer0 Exp $
27 #include <aversive/error.h>
31 #include <encoders_spi.h>
34 #include <scheduler.h>
40 #include <control_system_manager.h>
41 #include <blocking_detection_manager.h>
51 /* called every 5 ms */
52 static void do_cs(void *dummy)
55 if (sensorboard.flags & DO_ENCODERS) {
56 encoders_spi_manage(NULL);
59 if (sensorboard.flags & DO_CS) {
60 if (sensorboard.beacon.on)
61 cs_manage(&sensorboard.beacon.cs);
62 if (sensorboard.scanner.on)
63 cs_manage(&sensorboard.scanner.cs);
65 if (sensorboard.flags & DO_BD) {
66 bd_manage_from_cs(&sensorboard.beacon.bd, &sensorboard.beacon.cs);
67 bd_manage_from_cs(&sensorboard.scanner.bd, &sensorboard.scanner.cs);
69 if (sensorboard.flags & DO_POWER)
75 void dump_cs(const char *name, struct cs *cs)
77 printf_P(PSTR("%s cons=% .5ld fcons=% .5ld err=% .5ld "
78 "in=% .5ld out=% .5ld\r\n"),
79 name, cs_get_consign(cs), cs_get_filtered_consign(cs),
80 cs_get_error(cs), cs_get_filtered_feedback(cs),
84 void dump_pid(const char *name, struct pid_filter *pid)
86 printf_P(PSTR("%s P=% .8ld I=% .8ld D=% .8ld out=% .8ld\r\n"),
88 pid_get_value_in(pid) * pid_get_gain_P(pid),
89 pid_get_value_I(pid) * pid_get_gain_I(pid),
90 pid_get_value_D(pid) * pid_get_gain_D(pid),
91 pid_get_value_out(pid));
94 void microb_cs_init(void)
98 pid_init(&sensorboard.beacon.pid);
99 pid_set_gains(&sensorboard.beacon.pid, 80, 80, 250);
100 pid_set_maximums(&sensorboard.beacon.pid, 0, 10000, 2000);
101 pid_set_out_shift(&sensorboard.beacon.pid, 6);
102 pid_set_derivate_filter(&sensorboard.beacon.pid, 6);
105 cs_init(&sensorboard.beacon.cs);
106 cs_set_correct_filter(&sensorboard.beacon.cs, pid_do_filter, &sensorboard.beacon.pid);
107 cs_set_process_in(&sensorboard.beacon.cs, pwm_ng_set, BEACON_PWM);
108 cs_set_process_out(&sensorboard.beacon.cs, encoders_spi_update_beacon_speed, BEACON_ENCODER);
109 cs_set_consign(&sensorboard.beacon.cs, 0);
111 /* ---- CS scanner */
113 pid_init(&sensorboard.scanner.pid);
114 pid_set_gains(&sensorboard.scanner.pid, 200, 5, 250);
115 pid_set_maximums(&sensorboard.scanner.pid, 0, 10000, 2047);
116 pid_set_out_shift(&sensorboard.scanner.pid, 6);
117 pid_set_derivate_filter(&sensorboard.scanner.pid, 6);
120 quadramp_init(&sensorboard.scanner.qr);
121 quadramp_set_1st_order_vars(&sensorboard.scanner.qr, 200, 200); /* set speed */
122 quadramp_set_2nd_order_vars(&sensorboard.scanner.qr, 20, 20); /* set accel */
125 cs_init(&sensorboard.scanner.cs);
126 cs_set_consign_filter(&sensorboard.scanner.cs, quadramp_do_filter, &sensorboard.scanner.qr);
127 cs_set_correct_filter(&sensorboard.scanner.cs, pid_do_filter, &sensorboard.scanner.pid);
128 cs_set_process_in(&sensorboard.scanner.cs, pwm_ng_set, SCANNER_PWM);
129 cs_set_process_out(&sensorboard.scanner.cs, encoders_spi_update_scanner, SCANNER_ENCODER);
130 cs_set_consign(&sensorboard.scanner.cs, 0);
132 /* Blocking detection */
133 bd_init(&sensorboard.scanner.bd);
134 bd_set_speed_threshold(&sensorboard.scanner.bd, 150);
135 bd_set_current_thresholds(&sensorboard.scanner.bd, 500, 8000, 1000000, 40);
138 sensorboard.beacon.on = 0;
139 sensorboard.scanner.on = 1;
142 scheduler_add_periodical_event_priority(do_cs, NULL,
143 5000L / SCHEDULER_UNIT,