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-04-24 19:30:42 zer0 Exp $
27 #include <aversive/error.h>
31 #include <encoders_spi.h>
34 #include <scheduler.h>
35 #include <clock_time.h>
40 #include <control_system_manager.h>
41 #include <blocking_detection_manager.h>
51 /* called every 5 ms */
52 static void do_cs(__attribute__((unused)) void *dummy)
55 if (cobboard.flags & DO_ENCODERS) {
56 encoders_spi_manage(NULL);
60 if (cobboard.flags & DO_CS) {
61 if (cobboard.left_spickle.on)
62 cs_manage(&cobboard.left_spickle.cs);
63 if (cobboard.right_spickle.on)
64 cs_manage(&cobboard.right_spickle.cs);
65 if (cobboard.shovel.on)
66 cs_manage(&cobboard.shovel.cs);
69 if ((cobboard.flags & DO_BD) && (cobboard.flags & DO_POWER)) {
70 bd_manage_from_cs(&cobboard.left_spickle.bd, &cobboard.left_spickle.cs);
71 bd_manage_from_cs(&cobboard.right_spickle.bd, &cobboard.right_spickle.cs);
72 bd_manage_from_cs(&cobboard.shovel.bd, &cobboard.shovel.cs);
74 /* urgent case: stop power on blocking */
75 if (cobboard.flags & DO_ERRBLOCKING) {
76 /* if (bd_get(&cobboard.left_spickle.bd) || */
77 /* bd_get(&cobboard.right_spickle.bd) || */
78 /* bd_get(&cobboard.shovel.bd)) { */
79 /* printf_P(PSTR("MOTOR BLOCKED STOP ALL\r\n")); */
80 /* cobboard.flags &= ~(DO_POWER | DO_ERRBLOCKING); */
84 if (cobboard.flags & DO_POWER)
90 void dump_cs_debug(const char *name, struct cs *cs)
92 DEBUG(E_USER_CS, "%s cons=% .5ld fcons=% .5ld err=% .5ld "
93 "in=% .5ld out=% .5ld",
94 name, cs_get_consign(cs), cs_get_filtered_consign(cs),
95 cs_get_error(cs), cs_get_filtered_feedback(cs),
99 void dump_cs(const char *name, struct cs *cs)
101 printf_P(PSTR("%s cons=% .5ld fcons=% .5ld err=% .5ld "
102 "in=% .5ld out=% .5ld\r\n"),
103 name, cs_get_consign(cs), cs_get_filtered_consign(cs),
104 cs_get_error(cs), cs_get_filtered_feedback(cs),
108 void dump_pid(const char *name, struct pid_filter *pid)
110 printf_P(PSTR("%s P=% .8ld I=% .8ld D=% .8ld out=% .8ld\r\n"),
112 pid_get_value_in(pid) * pid_get_gain_P(pid),
113 pid_get_value_I(pid) * pid_get_gain_I(pid),
114 pid_get_value_D(pid) * pid_get_gain_D(pid),
115 pid_get_value_out(pid));
118 void microb_cs_init(void)
120 /* ---- CS left_spickle */
122 pid_init(&cobboard.left_spickle.pid);
123 pid_set_gains(&cobboard.left_spickle.pid, 400, 10, 1500);
124 pid_set_maximums(&cobboard.left_spickle.pid, 0, 25000, 4095);
125 pid_set_out_shift(&cobboard.left_spickle.pid, 10);
126 pid_set_derivate_filter(&cobboard.left_spickle.pid, 4);
129 quadramp_init(&cobboard.left_spickle.qr);
130 quadramp_set_1st_order_vars(&cobboard.left_spickle.qr, 3000, 3000); /* set speed */
131 quadramp_set_2nd_order_vars(&cobboard.left_spickle.qr, 0, 0); /* set accel */
134 cs_init(&cobboard.left_spickle.cs);
135 cs_set_consign_filter(&cobboard.left_spickle.cs, quadramp_do_filter, &cobboard.left_spickle.qr);
136 cs_set_correct_filter(&cobboard.left_spickle.cs, pid_do_filter, &cobboard.left_spickle.pid);
137 cs_set_process_in(&cobboard.left_spickle.cs, spickle_set, LEFT_SPICKLE_PWM);
138 cs_set_process_out(&cobboard.left_spickle.cs, encoders_spi_get_value, LEFT_SPICKLE_ENCODER);
139 cs_set_consign(&cobboard.left_spickle.cs, 0);
141 /* Blocking detection */
142 bd_init(&cobboard.left_spickle.bd);
143 bd_set_speed_threshold(&cobboard.left_spickle.bd, 150);
144 bd_set_current_thresholds(&cobboard.left_spickle.bd, 500, 8000, 1000000, 200);
146 /* ---- CS right_spickle */
148 pid_init(&cobboard.right_spickle.pid);
149 pid_set_gains(&cobboard.right_spickle.pid, 400, 10, 1500);
150 pid_set_maximums(&cobboard.right_spickle.pid, 0, 25000, 4095);
151 pid_set_out_shift(&cobboard.right_spickle.pid, 10);
152 pid_set_derivate_filter(&cobboard.right_spickle.pid, 4);
155 quadramp_init(&cobboard.right_spickle.qr);
156 quadramp_set_1st_order_vars(&cobboard.right_spickle.qr, 0, 0); /* set speed */
157 quadramp_set_2nd_order_vars(&cobboard.right_spickle.qr, 0, 0); /* set accel */
160 cs_init(&cobboard.right_spickle.cs);
161 cs_set_consign_filter(&cobboard.right_spickle.cs, quadramp_do_filter, &cobboard.right_spickle.qr);
162 cs_set_correct_filter(&cobboard.right_spickle.cs, pid_do_filter, &cobboard.right_spickle.pid);
163 cs_set_process_in(&cobboard.right_spickle.cs, spickle_set, RIGHT_SPICKLE_PWM);
164 cs_set_process_out(&cobboard.right_spickle.cs, encoders_spi_get_value, RIGHT_SPICKLE_ENCODER);
165 cs_set_consign(&cobboard.right_spickle.cs, 0);
167 /* Blocking detection */
168 bd_init(&cobboard.right_spickle.bd);
169 bd_set_speed_threshold(&cobboard.right_spickle.bd, 150);
170 bd_set_current_thresholds(&cobboard.right_spickle.bd, 500, 8000, 1000000, 200);
174 pid_init(&cobboard.shovel.pid);
175 pid_set_gains(&cobboard.shovel.pid, 1000, 10, 1400);
176 pid_set_maximums(&cobboard.shovel.pid, 0, 10000, 3200); /* max is 18 V */
177 pid_set_out_shift(&cobboard.shovel.pid, 10);
178 pid_set_derivate_filter(&cobboard.shovel.pid, 4);
181 quadramp_init(&cobboard.shovel.qr);
182 quadramp_set_1st_order_vars(&cobboard.shovel.qr, 2500, 2500); /* set speed */
183 quadramp_set_2nd_order_vars(&cobboard.shovel.qr, 50, 20); /* set accel */
186 cs_init(&cobboard.shovel.cs);
187 cs_set_consign_filter(&cobboard.shovel.cs, quadramp_do_filter, &cobboard.shovel.qr);
188 cs_set_correct_filter(&cobboard.shovel.cs, pid_do_filter, &cobboard.shovel.pid);
189 cs_set_process_in(&cobboard.shovel.cs, shovel_set, SHOVEL_PWM);
190 cs_set_process_out(&cobboard.shovel.cs, encoders_spi_get_value, SHOVEL_ENCODER);
191 cs_set_consign(&cobboard.shovel.cs, 0);
193 /* Blocking detection */
194 bd_init(&cobboard.shovel.bd);
195 bd_set_speed_threshold(&cobboard.shovel.bd, 150);
196 bd_set_current_thresholds(&cobboard.shovel.bd, 500, 8000, 1000000, 200);
198 /* set them on (or not) !! */
199 cobboard.left_spickle.on = 0;
200 cobboard.right_spickle.on = 0;
201 cobboard.shovel.on = 0;
203 scheduler_add_periodical_event_priority(do_cs, NULL,
204 CS_PERIOD / SCHEDULER_UNIT,