ballboard blocking detection
[aversive.git] / projects / microb2010 / ballboard / cs.c
1 /*
2  *  Copyright Droids Corporation
3  *  Olivier Matz <zer0@droids-corp.org>
4  *
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.
9  *
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.
14  *
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
18  *
19  *  Revision : $Id: cs.c,v 1.4 2009-05-27 20:04:07 zer0 Exp $
20  *
21  */
22
23 #include <stdio.h>
24 #include <string.h>
25
26 #include <aversive.h>
27 #include <aversive/error.h>
28
29 #include <ax12.h>
30 #include <spi.h>
31 #include <encoders_spi.h>
32 #include <pwm_ng.h>
33 #include <timer.h>
34 #include <scheduler.h>
35 #include <clock_time.h>
36 #include <adc.h>
37
38 #include <pid.h>
39 #include <quadramp.h>
40 #include <control_system_manager.h>
41 #include <blocking_detection_manager.h>
42
43 #include <parse.h>
44 #include <rdline.h>
45
46 #include "main.h"
47 #include "actuator.h"
48
49 int32_t encoders_spi_update_roller_speed(void *number)
50 {
51         static volatile int32_t roller_pos;
52         int32_t tmp, speed;
53         tmp = encoders_spi_get_value(number);
54         speed = tmp - roller_pos;
55         roller_pos = tmp;
56         return speed;
57 }
58
59 /* called every 5 ms */
60 static void do_cs(void *dummy)
61 {
62         /* read encoders */
63         if (ballboard.flags & DO_ENCODERS) {
64                 encoders_spi_manage(NULL);
65         }
66         /* control system */
67         if (ballboard.flags & DO_CS) {
68                 if (ballboard.roller.on)
69                         cs_manage(&ballboard.roller.cs);
70                 if (ballboard.forktrans.on)
71                         cs_manage(&ballboard.forktrans.cs);
72                 if (ballboard.forkrot.on)
73                         cs_manage(&ballboard.forkrot.cs);
74         }
75         if ((ballboard.flags & DO_BD) && (ballboard.flags & DO_POWER)) {
76                 bd_manage_from_cs(&ballboard.forktrans.bd, &ballboard.forktrans.cs);
77                 bd_manage_from_cs(&ballboard.forkrot.bd, &ballboard.forkrot.cs);
78                 bd_manage_from_speed_cmd(&ballboard.roller.bd,
79                                          cs_get_filtered_feedback(&ballboard.roller.cs),
80                                          cs_get_out(&ballboard.roller.cs));
81
82                 /* urgent case: stop power on blocking */
83                 if (ballboard.flags & DO_ERRBLOCKING) {
84                         if (bd_get(&ballboard.forktrans.bd) ||
85                             bd_get(&ballboard.forkrot.bd)) {
86                                 printf_P(PSTR("MOTOR BLOCKED STOP ALL\r\n"));
87                                 ballboard.flags &= ~(DO_POWER | DO_ERRBLOCKING);
88                         }
89                 }
90         }
91         if (ballboard.flags & DO_POWER)
92                 BRAKE_OFF();
93         else
94                 BRAKE_ON();
95 }
96
97 void dump_cs(const char *name, struct cs *cs)
98 {
99         printf_P(PSTR("%s cons=% .5ld fcons=% .5ld err=% .5ld "
100                       "in=% .5ld out=% .5ld\r\n"),
101                  name, cs_get_consign(cs), cs_get_filtered_consign(cs),
102                  cs_get_error(cs), cs_get_filtered_feedback(cs),
103                  cs_get_out(cs));
104 }
105
106 void dump_pid(const char *name, struct pid_filter *pid)
107 {
108         printf_P(PSTR("%s P=% .8ld I=% .8ld D=% .8ld out=% .8ld\r\n"),
109                  name,
110                  pid_get_value_in(pid) * pid_get_gain_P(pid),
111                  pid_get_value_I(pid) * pid_get_gain_I(pid),
112                  pid_get_value_D(pid) * pid_get_gain_D(pid),
113                  pid_get_value_out(pid));
114 }
115
116 void microb_cs_init(void)
117 {
118         /* ---- CS roller */
119         /* PID */
120         pid_init(&ballboard.roller.pid);
121         pid_set_gains(&ballboard.roller.pid, 80, 80, 250);
122         pid_set_maximums(&ballboard.roller.pid, 0, 10000, 4095);
123         pid_set_out_shift(&ballboard.roller.pid, 6);
124         pid_set_derivate_filter(&ballboard.roller.pid, 6);
125
126         /* QUADRAMP (used as a ramp filter) */
127         quadramp_init(&ballboard.roller.qr);
128         quadramp_set_1st_order_vars(&ballboard.roller.qr, 20, 20);
129         quadramp_set_2nd_order_vars(&ballboard.roller.qr, 0, 0);
130
131         /* CS */
132         cs_init(&ballboard.roller.cs);
133         cs_set_consign_filter(&ballboard.roller.cs, quadramp_do_filter, &ballboard.roller.qr);
134         cs_set_correct_filter(&ballboard.roller.cs, pid_do_filter, &ballboard.roller.pid);
135         cs_set_process_in(&ballboard.roller.cs, pwm_ng_set, ROLLER_PWM);
136         cs_set_process_out(&ballboard.roller.cs, encoders_spi_update_roller_speed, ROLLER_ENCODER);
137         cs_set_consign(&ballboard.roller.cs, 0);
138
139         /* Blocking detection */
140         bd_init(&ballboard.roller.bd);
141 #define ROLLER_SPEED_THRES (ROLLER_SPEED * 0.75)
142         bd_set_speed_threshold(&ballboard.roller.bd, ROLLER_SPEED_THRES);
143         bd_set_current_thresholds(&ballboard.roller.bd, 500, 1500, 1000000, 20);
144
145         /* ---- CS forktrans */
146         /* PID */
147         pid_init(&ballboard.forktrans.pid);
148         pid_set_gains(&ballboard.forktrans.pid, 30, 5, 0);
149         pid_set_maximums(&ballboard.forktrans.pid, 0, 10000, 2047);
150         pid_set_out_shift(&ballboard.forktrans.pid, 6);
151         pid_set_derivate_filter(&ballboard.forktrans.pid, 6);
152
153         /* QUADRAMP */
154         quadramp_init(&ballboard.forktrans.qr);
155         quadramp_set_1st_order_vars(&ballboard.forktrans.qr, 200, 200); /* set speed */
156         quadramp_set_2nd_order_vars(&ballboard.forktrans.qr, 20, 20); /* set accel */
157
158         /* CS */
159         cs_init(&ballboard.forktrans.cs);
160         cs_set_consign_filter(&ballboard.forktrans.cs, quadramp_do_filter, &ballboard.forktrans.qr);
161         cs_set_correct_filter(&ballboard.forktrans.cs, pid_do_filter, &ballboard.forktrans.pid);
162         cs_set_process_in(&ballboard.forktrans.cs, pwm_ng_set, FORKTRANS_PWM);
163         cs_set_process_out(&ballboard.forktrans.cs, encoders_spi_get_value, FORKTRANS_ENCODER);
164         cs_set_consign(&ballboard.forktrans.cs, 0);
165
166         /* Blocking detection */
167         bd_init(&ballboard.forktrans.bd);
168         bd_set_speed_threshold(&ballboard.forktrans.bd, 150);
169         bd_set_current_thresholds(&ballboard.forktrans.bd, 500, 8000, 1000000, 200);
170
171         /* ---- CS forkrot */
172         /* PID */
173         pid_init(&ballboard.forkrot.pid);
174         pid_set_gains(&ballboard.forkrot.pid, 30, 5, 0);
175         pid_set_maximums(&ballboard.forkrot.pid, 0, 10000, 2047);
176         pid_set_out_shift(&ballboard.forkrot.pid, 6);
177         pid_set_derivate_filter(&ballboard.forkrot.pid, 6);
178
179         /* QUADRAMP */
180         quadramp_init(&ballboard.forkrot.qr);
181         quadramp_set_1st_order_vars(&ballboard.forkrot.qr, 800, 800); /* set speed */
182         quadramp_set_2nd_order_vars(&ballboard.forkrot.qr, 20, 20); /* set accel */
183
184         /* CS */
185         cs_init(&ballboard.forkrot.cs);
186         cs_set_consign_filter(&ballboard.forkrot.cs, quadramp_do_filter, &ballboard.forkrot.qr);
187         cs_set_correct_filter(&ballboard.forkrot.cs, pid_do_filter, &ballboard.forkrot.pid);
188         cs_set_process_in(&ballboard.forkrot.cs, pwm_ng_set, FORKROT_PWM);
189         cs_set_process_out(&ballboard.forkrot.cs, encoders_spi_get_value, FORKROT_ENCODER);
190         cs_set_consign(&ballboard.forkrot.cs, 0);
191
192         /* Blocking detection */
193         bd_init(&ballboard.forkrot.bd);
194         bd_set_speed_threshold(&ballboard.forkrot.bd, 150);
195         bd_set_current_thresholds(&ballboard.forkrot.bd, 500, 8000, 1000000, 200);
196
197         /* set them on !! */
198         ballboard.roller.on = 1;
199         ballboard.forktrans.on = 1;
200         ballboard.forkrot.on = 1;
201
202
203         scheduler_add_periodical_event_priority(do_cs, NULL,
204                                                 5000L / SCHEDULER_UNIT,
205                                                 CS_PRIO);
206 }