better cobboard commands in strat_event
[aversive.git] / projects / microb2010 / cobboard / 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-04-24 19:30:42 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 #include "spickle.h"
49
50 #define DEBUG_CPLD
51
52 /* called every 5 ms */
53 static void do_cs(__attribute__((unused)) void *dummy)
54 {
55         /* read encoders */
56         if (cobboard.flags & DO_ENCODERS) {
57                 encoders_spi_manage(NULL);
58         }
59 #ifdef DEBUG_CPLD
60         cobboard.left_spickle.prev = cobboard.left_spickle.cs.filtered_feedback_value;
61         cobboard.right_spickle.prev = cobboard.right_spickle.cs.filtered_feedback_value;
62         cobboard.shovel.prev = cobboard.shovel.cs.filtered_feedback_value;
63 #endif
64         /* control system */
65         if (cobboard.flags & DO_CS) {
66                 if (cobboard.left_spickle.on)
67                         cs_manage(&cobboard.left_spickle.cs);
68                 if (cobboard.right_spickle.on)
69                         cs_manage(&cobboard.right_spickle.cs);
70                 if (cobboard.shovel.on)
71                         cs_manage(&cobboard.shovel.cs);
72         }
73 #ifdef DEBUG_CPLD
74         {
75                 extern int16_t g_encoders_spi_previous[4];
76                 int32_t ls, rs, sh;
77
78                 ls = (cobboard.left_spickle.prev - cobboard.left_spickle.cs.filtered_feedback_value);
79                 rs = (cobboard.right_spickle.prev - cobboard.right_spickle.cs.filtered_feedback_value);
80                 sh = (cobboard.shovel.prev - cobboard.shovel.cs.filtered_feedback_value);
81                 if (ls < -3000 || ls > 3000 ||
82                     rs < -3000 || rs > 3000 ||
83                     sh < -3000 || sh > 3000) {
84                         printf_P(PSTR("left_spickle %ld "), ls);
85                         printf_P(PSTR("right_spickle %ld "), rs);
86                         printf_P(PSTR("shovel %ld "), sh);
87                         printf_P(PSTR("/ %d %d %d %d\r\n"),
88                                  g_encoders_spi_previous[0],
89                                  g_encoders_spi_previous[1],
90                                  g_encoders_spi_previous[2],
91                                  g_encoders_spi_previous[3]);
92                         BRAKE_ON();
93                         while (1);
94                 }
95         }
96 #endif
97
98         if ((cobboard.flags & DO_BD) && (cobboard.flags & DO_POWER)) {
99                 bd_manage_from_cs(&cobboard.left_spickle.bd, &cobboard.left_spickle.cs);
100                 bd_manage_from_cs(&cobboard.right_spickle.bd, &cobboard.right_spickle.cs);
101                 bd_manage_from_cs(&cobboard.shovel.bd, &cobboard.shovel.cs);
102
103                 /* urgent case: stop power on blocking */
104                 if (cobboard.flags & DO_ERRBLOCKING) {
105                         if (/* bd_get(&cobboard.left_spickle.bd) || */
106                             /* bd_get(&cobboard.right_spickle.bd) || */
107                             bd_get(&cobboard.shovel.bd)) {
108                                 printf_P(PSTR("MOTOR BLOCKED STOP ALL\r\n"));
109                                 cobboard.flags &= ~(DO_POWER | DO_ERRBLOCKING);
110                         }
111                 }
112         }
113         if (cobboard.flags & DO_POWER)
114                 BRAKE_OFF();
115         else
116                 BRAKE_ON();
117 }
118
119 void dump_cs_debug(const char *name, struct cs *cs)
120 {
121         DEBUG(E_USER_CS, "%s cons=% .5ld fcons=% .5ld err=% .5ld "
122               "in=% .5ld out=% .5ld",
123               name, cs_get_consign(cs), cs_get_filtered_consign(cs),
124               cs_get_error(cs), cs_get_filtered_feedback(cs),
125               cs_get_out(cs));
126 }
127
128 void dump_cs(const char *name, struct cs *cs)
129 {
130         printf_P(PSTR("%s cons=% .5ld fcons=% .5ld err=% .5ld "
131                       "in=% .5ld out=% .5ld\r\n"),
132                  name, cs_get_consign(cs), cs_get_filtered_consign(cs),
133                  cs_get_error(cs), cs_get_filtered_feedback(cs),
134                  cs_get_out(cs));
135 }
136
137 void dump_pid(const char *name, struct pid_filter *pid)
138 {
139         printf_P(PSTR("%s P=% .8ld I=% .8ld D=% .8ld out=% .8ld\r\n"),
140                  name,
141                  pid_get_value_in(pid) * pid_get_gain_P(pid),
142                  pid_get_value_I(pid) * pid_get_gain_I(pid),
143                  pid_get_value_D(pid) * pid_get_gain_D(pid),
144                  pid_get_value_out(pid));
145 }
146
147 void microb_cs_init(void)
148 {
149         /* ---- CS left_spickle */
150         /* PID */
151         pid_init(&cobboard.left_spickle.pid);
152         pid_set_gains(&cobboard.left_spickle.pid, 400, 10, 1500);
153         pid_set_maximums(&cobboard.left_spickle.pid, 0, 25000, 4095);
154         pid_set_out_shift(&cobboard.left_spickle.pid, 10);
155         pid_set_derivate_filter(&cobboard.left_spickle.pid, 4);
156
157         /* CS */
158         cs_init(&cobboard.left_spickle.cs);
159         cs_set_correct_filter(&cobboard.left_spickle.cs, pid_do_filter, &cobboard.left_spickle.pid);
160         cs_set_process_in(&cobboard.left_spickle.cs, spickle_set, LEFT_SPICKLE_PWM);
161         cs_set_process_out(&cobboard.left_spickle.cs, encoders_spi_get_value, LEFT_SPICKLE_ENCODER);
162         cs_set_consign(&cobboard.left_spickle.cs, 0);
163
164         /* Blocking detection */
165         bd_init(&cobboard.left_spickle.bd);
166         bd_set_speed_threshold(&cobboard.left_spickle.bd, 150);
167         bd_set_current_thresholds(&cobboard.left_spickle.bd, 500, 8000, 1000000, 200);
168
169         /* ---- CS right_spickle */
170         /* PID */
171         pid_init(&cobboard.right_spickle.pid);
172         pid_set_gains(&cobboard.right_spickle.pid, 400, 10, 1500);
173         pid_set_maximums(&cobboard.right_spickle.pid, 0, 25000, 4095);
174         pid_set_out_shift(&cobboard.right_spickle.pid, 10);
175         pid_set_derivate_filter(&cobboard.right_spickle.pid, 4);
176
177         /* CS */
178         cs_init(&cobboard.right_spickle.cs);
179         cs_set_correct_filter(&cobboard.right_spickle.cs, pid_do_filter, &cobboard.right_spickle.pid);
180         cs_set_process_in(&cobboard.right_spickle.cs, spickle_set, RIGHT_SPICKLE_PWM);
181         cs_set_process_out(&cobboard.right_spickle.cs, encoders_spi_get_value, RIGHT_SPICKLE_ENCODER);
182         cs_set_consign(&cobboard.right_spickle.cs, 0);
183
184         /* Blocking detection */
185         bd_init(&cobboard.right_spickle.bd);
186         bd_set_speed_threshold(&cobboard.right_spickle.bd, 150);
187         bd_set_current_thresholds(&cobboard.right_spickle.bd, 500, 8000, 1000000, 200);
188
189         /* ---- CS shovel */
190         /* PID */
191         pid_init(&cobboard.shovel.pid);
192         pid_set_gains(&cobboard.shovel.pid, 1000, 10, 1400);
193         pid_set_maximums(&cobboard.shovel.pid, 0, 10000, 3200); /* max is 18 V */
194         pid_set_out_shift(&cobboard.shovel.pid, 10);
195         pid_set_derivate_filter(&cobboard.shovel.pid, 4);
196
197         /* quadramp */
198         quadramp_init(&cobboard.shovel.qr);
199         quadramp_set_1st_order_vars(&cobboard.shovel.qr, 2500, 2500); /* set speed */
200         quadramp_set_2nd_order_vars(&cobboard.shovel.qr, 50, 20); /* set accel */
201
202         /* CS */
203         cs_init(&cobboard.shovel.cs);
204         cs_set_consign_filter(&cobboard.shovel.cs, quadramp_do_filter, &cobboard.shovel.qr);
205         cs_set_correct_filter(&cobboard.shovel.cs, pid_do_filter, &cobboard.shovel.pid);
206         cs_set_process_in(&cobboard.shovel.cs, pwm_ng_set, SHOVEL_PWM);
207         cs_set_process_out(&cobboard.shovel.cs, encoders_spi_get_value, SHOVEL_ENCODER);
208         cs_set_consign(&cobboard.shovel.cs, 0);
209
210         /* Blocking detection */
211         bd_init(&cobboard.shovel.bd);
212         bd_set_speed_threshold(&cobboard.shovel.bd, 150);
213         bd_set_current_thresholds(&cobboard.shovel.bd, 500, 8000, 1000000, 200);
214
215         /* set them on (or not) !! */
216         cobboard.left_spickle.on = 0;
217         cobboard.right_spickle.on = 0;
218         cobboard.shovel.on = 0;
219
220         scheduler_add_periodical_event_priority(do_cs, NULL,
221                                                 CS_PERIOD / SCHEDULER_UNIT,
222                                                 CS_PRIO);
223 }