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>
40 #include <control_system_manager.h>
41 #include <blocking_detection_manager.h>
49 /* called every 5 ms */
50 static void do_cs(__attribute__((unused)) void *dummy)
53 if (mechboard.flags & DO_ENCODERS) {
54 encoders_spi_manage(NULL);
57 if (mechboard.flags & DO_CS) {
58 if (mechboard.left_arm.on)
59 cs_manage(&mechboard.left_arm.cs);
60 if (mechboard.right_arm.on)
61 cs_manage(&mechboard.right_arm.cs);
63 if (mechboard.flags & DO_BD) {
64 bd_manage_from_cs(&mechboard.left_arm.bd, &mechboard.left_arm.cs);
65 bd_manage_from_cs(&mechboard.right_arm.bd, &mechboard.right_arm.cs);
67 if (mechboard.flags & DO_POWER)
73 void dump_cs_debug(const char *name, struct cs *cs)
75 DEBUG(E_USER_CS, "%s cons=% .5ld fcons=% .5ld err=% .5ld "
76 "in=% .5ld out=% .5ld",
77 name, cs_get_consign(cs), cs_get_filtered_consign(cs),
78 cs_get_error(cs), cs_get_filtered_feedback(cs),
82 void dump_cs(const char *name, struct cs *cs)
84 printf_P(PSTR("%s cons=% .5ld fcons=% .5ld err=% .5ld "
85 "in=% .5ld out=% .5ld\r\n"),
86 name, cs_get_consign(cs), cs_get_filtered_consign(cs),
87 cs_get_error(cs), cs_get_filtered_feedback(cs),
91 void dump_pid(const char *name, struct pid_filter *pid)
93 printf_P(PSTR("%s P=% .8ld I=% .8ld D=% .8ld out=% .8ld\r\n"),
95 pid_get_value_in(pid) * pid_get_gain_P(pid),
96 pid_get_value_I(pid) * pid_get_gain_I(pid),
97 pid_get_value_D(pid) * pid_get_gain_D(pid),
98 pid_get_value_out(pid));
101 void microb_cs_init(void)
103 /* ---- CS left_arm */
105 pid_init(&mechboard.left_arm.pid);
106 pid_set_gains(&mechboard.left_arm.pid, 500, 40, 5000);
107 pid_set_maximums(&mechboard.left_arm.pid, 0, 5000, 2400); /* max is 12 V */
108 pid_set_out_shift(&mechboard.left_arm.pid, 10);
109 pid_set_derivate_filter(&mechboard.left_arm.pid, 4);
112 quadramp_init(&mechboard.left_arm.qr);
113 quadramp_set_1st_order_vars(&mechboard.left_arm.qr, 2000, 2000); /* set speed */
114 quadramp_set_2nd_order_vars(&mechboard.left_arm.qr, 20, 20); /* set accel */
117 cs_init(&mechboard.left_arm.cs);
118 cs_set_consign_filter(&mechboard.left_arm.cs, quadramp_do_filter, &mechboard.left_arm.qr);
119 cs_set_correct_filter(&mechboard.left_arm.cs, pid_do_filter, &mechboard.left_arm.pid);
120 cs_set_process_in(&mechboard.left_arm.cs, pwm_ng_set, LEFT_ARM_PWM);
121 cs_set_process_out(&mechboard.left_arm.cs, encoders_spi_get_value, LEFT_ARM_ENCODER);
122 cs_set_consign(&mechboard.left_arm.cs, 0);
124 /* Blocking detection */
125 bd_init(&mechboard.left_arm.bd);
126 bd_set_speed_threshold(&mechboard.left_arm.bd, 150);
127 bd_set_current_thresholds(&mechboard.left_arm.bd, 500, 8000, 1000000, 40);
129 /* ---- CS right_arm */
131 pid_init(&mechboard.right_arm.pid);
132 pid_set_gains(&mechboard.right_arm.pid, 500, 40, 5000);
133 pid_set_maximums(&mechboard.right_arm.pid, 0, 5000, 2400); /* max is 12 V */
134 pid_set_out_shift(&mechboard.right_arm.pid, 10);
135 pid_set_derivate_filter(&mechboard.right_arm.pid, 6);
138 quadramp_init(&mechboard.right_arm.qr);
139 quadramp_set_1st_order_vars(&mechboard.right_arm.qr, 1000, 1000); /* set speed */
140 quadramp_set_2nd_order_vars(&mechboard.right_arm.qr, 20, 20); /* set accel */
143 cs_init(&mechboard.right_arm.cs);
144 cs_set_consign_filter(&mechboard.right_arm.cs, quadramp_do_filter, &mechboard.right_arm.qr);
145 cs_set_correct_filter(&mechboard.right_arm.cs, pid_do_filter, &mechboard.right_arm.pid);
146 cs_set_process_in(&mechboard.right_arm.cs, pwm_ng_set, RIGHT_ARM_PWM);
147 cs_set_process_out(&mechboard.right_arm.cs, encoders_spi_get_value, RIGHT_ARM_ENCODER);
148 cs_set_consign(&mechboard.right_arm.cs, 0);
150 /* Blocking detection */
151 bd_init(&mechboard.right_arm.bd);
152 bd_set_speed_threshold(&mechboard.right_arm.bd, 150);
153 bd_set_current_thresholds(&mechboard.right_arm.bd, 500, 8000, 1000000, 40);
156 mechboard.left_arm.on = 1;
157 mechboard.right_arm.on = 1;
160 scheduler_add_periodical_event_priority(do_cs, NULL,
161 CS_PERIOD / SCHEDULER_UNIT,