addremove
[aversive.git] / projects / microb2009 / mechboard / 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 <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 /* called every 5 ms */
50 static void do_cs(__attribute__((unused)) void *dummy) 
51 {
52         /* read encoders */
53         if (mechboard.flags & DO_ENCODERS) {
54                 encoders_spi_manage(NULL);
55         }
56         /* control system */
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);
62         }
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);
66         }
67         if (mechboard.flags & DO_POWER)
68                 BRAKE_OFF();
69         else
70                 BRAKE_ON();
71 }
72
73 void dump_cs_debug(const char *name, struct cs *cs)
74 {
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),
79               cs_get_out(cs));
80 }
81
82 void dump_cs(const char *name, struct cs *cs)
83 {
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),
88                  cs_get_out(cs));
89 }
90
91 void dump_pid(const char *name, struct pid_filter *pid)
92 {
93         printf_P(PSTR("%s P=% .8ld I=% .8ld D=% .8ld out=% .8ld\r\n"),
94                  name,
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));
99 }
100
101 void microb_cs_init(void)
102 {
103         /* ---- CS left_arm */
104         /* PID */
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);
110
111         /* QUADRAMP */
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 */
115
116         /* CS */
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);
123
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);
128
129         /* ---- CS right_arm */
130         /* PID */
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);
136
137         /* QUADRAMP */
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 */
141
142         /* CS */
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);
149
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);
154
155         /* set them on !! */
156         mechboard.left_arm.on = 1;
157         mechboard.right_arm.on = 1;
158
159
160         scheduler_add_periodical_event_priority(do_cs, NULL, 
161                                                 CS_PERIOD / SCHEDULER_UNIT, 
162                                                 CS_PRIO);
163 }