remove CVS
[aversive.git] / projects / microb2009 / sensorboard / 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 <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 "beacon.h"
48 #include "scanner.h"
49 #include "actuator.h"
50
51 /* called every 5 ms */
52 static void do_cs(void *dummy) 
53 {
54         /* read encoders */
55         if (sensorboard.flags & DO_ENCODERS) {
56                 encoders_spi_manage(NULL);
57         }
58         /* control system */
59         if (sensorboard.flags & DO_CS) {
60                 if (sensorboard.beacon.on)
61                         cs_manage(&sensorboard.beacon.cs);
62                 if (sensorboard.scanner.on)
63                         cs_manage(&sensorboard.scanner.cs);
64         }
65         if (sensorboard.flags & DO_BD) {
66                 bd_manage_from_cs(&sensorboard.beacon.bd, &sensorboard.beacon.cs);
67                 bd_manage_from_cs(&sensorboard.scanner.bd, &sensorboard.scanner.cs);
68         }
69         if (sensorboard.flags & DO_POWER)
70                 BRAKE_OFF();
71         else
72                 BRAKE_ON();
73 }
74
75 void dump_cs(const char *name, struct cs *cs)
76 {
77         printf_P(PSTR("%s cons=% .5ld fcons=% .5ld err=% .5ld "
78                       "in=% .5ld out=% .5ld\r\n"), 
79                  name, cs_get_consign(cs), cs_get_filtered_consign(cs),
80                  cs_get_error(cs), cs_get_filtered_feedback(cs),
81                  cs_get_out(cs));
82 }
83
84 void dump_pid(const char *name, struct pid_filter *pid)
85 {
86         printf_P(PSTR("%s P=% .8ld I=% .8ld D=% .8ld out=% .8ld\r\n"),
87                  name,
88                  pid_get_value_in(pid) * pid_get_gain_P(pid),
89                  pid_get_value_I(pid) * pid_get_gain_I(pid),
90                  pid_get_value_D(pid) * pid_get_gain_D(pid),
91                  pid_get_value_out(pid));
92 }
93
94 void microb_cs_init(void)
95 {
96         /* ---- CS beacon */
97         /* PID */
98         pid_init(&sensorboard.beacon.pid);
99         pid_set_gains(&sensorboard.beacon.pid, 80, 80, 250);
100         pid_set_maximums(&sensorboard.beacon.pid, 0, 10000, 2000);
101         pid_set_out_shift(&sensorboard.beacon.pid, 6);
102         pid_set_derivate_filter(&sensorboard.beacon.pid, 6);
103
104         /* CS */
105         cs_init(&sensorboard.beacon.cs);
106         cs_set_correct_filter(&sensorboard.beacon.cs, pid_do_filter, &sensorboard.beacon.pid);
107         cs_set_process_in(&sensorboard.beacon.cs, pwm_ng_set, BEACON_PWM);
108         cs_set_process_out(&sensorboard.beacon.cs, encoders_spi_update_beacon_speed, BEACON_ENCODER);
109         cs_set_consign(&sensorboard.beacon.cs, 0);
110
111         /* ---- CS scanner */
112         /* PID */
113         pid_init(&sensorboard.scanner.pid);
114         pid_set_gains(&sensorboard.scanner.pid, 200, 5, 250);
115         pid_set_maximums(&sensorboard.scanner.pid, 0, 10000, 2047);
116         pid_set_out_shift(&sensorboard.scanner.pid, 6);
117         pid_set_derivate_filter(&sensorboard.scanner.pid, 6);
118
119         /* QUADRAMP */
120         quadramp_init(&sensorboard.scanner.qr);
121         quadramp_set_1st_order_vars(&sensorboard.scanner.qr, 200, 200); /* set speed */
122         quadramp_set_2nd_order_vars(&sensorboard.scanner.qr, 20, 20); /* set accel */
123
124         /* CS */
125         cs_init(&sensorboard.scanner.cs);
126         cs_set_consign_filter(&sensorboard.scanner.cs, quadramp_do_filter, &sensorboard.scanner.qr);
127         cs_set_correct_filter(&sensorboard.scanner.cs, pid_do_filter, &sensorboard.scanner.pid);
128         cs_set_process_in(&sensorboard.scanner.cs, pwm_ng_set, SCANNER_PWM);
129         cs_set_process_out(&sensorboard.scanner.cs, encoders_spi_update_scanner, SCANNER_ENCODER);
130         cs_set_consign(&sensorboard.scanner.cs, 0);
131
132         /* Blocking detection */
133         bd_init(&sensorboard.scanner.bd);
134         bd_set_speed_threshold(&sensorboard.scanner.bd, 150);
135         bd_set_current_thresholds(&sensorboard.scanner.bd, 500, 8000, 1000000, 40);
136
137         /* set them on !! */
138         sensorboard.beacon.on = 0;
139         sensorboard.scanner.on = 1;
140
141
142         scheduler_add_periodical_event_priority(do_cs, NULL, 
143                                                 5000L / SCHEDULER_UNIT, 
144                                                 CS_PRIO);
145 }