trajectory: follow a line
[aversive.git] / projects / microb2010 / tests / hostsim / 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.9 2009-11-08 17:24:33 zer0 Exp $
20  *
21  */
22
23 #include <stdio.h>
24 #include <string.h>
25 #include <stdint.h>
26
27 #include <aversive.h>
28 #include <aversive/error.h>
29
30 #include <timer.h>
31 #include <scheduler.h>
32 #include <clock_time.h>
33
34 #include <pid.h>
35 #include <quadramp.h>
36 #include <control_system_manager.h>
37 #include <trajectory_manager.h>
38 #include <blocking_detection_manager.h>
39 #include <robot_system.h>
40 #include <position_manager.h>
41
42 #include <parse.h>
43 #include <rdline.h>
44
45 #include "robotsim.h"
46 #include "strat.h"
47 #include "actuator.h"
48 #include "main.h"
49
50 /* called every 5 ms */
51 static void do_cs(void *dummy) 
52 {
53         static uint16_t cpt = 0;
54         static int32_t old_a = 0, old_d = 0;
55
56         robotsim_update();
57
58 #if 0
59         /* read encoders */
60         if (mainboard.flags & DO_ENCODERS) {
61                 encoders_spi_manage(NULL);
62         }
63 #endif
64
65         /* XXX there is an issue which is probably related to avr-libc
66          * 1.6.2 (debian): this code using fixed_point lib does not
67          * work with it */
68         /* robot system, conversion to angle,distance */
69         if (mainboard.flags & DO_RS) {
70                 int16_t a,d;
71                 rs_update(&mainboard.rs); /* takes about 0.5 ms */
72                 /* process and store current speed */
73                 a = rs_get_angle(&mainboard.rs);
74                 d = rs_get_distance(&mainboard.rs);
75                 mainboard.speed_a = a - old_a;
76                 mainboard.speed_d = d - old_d;
77                 old_a = a;
78                 old_d = d;
79         }
80
81         /* control system */
82         if (mainboard.flags & DO_CS) {
83                 if (mainboard.angle.on)
84                         cs_manage(&mainboard.angle.cs);
85                 if (mainboard.distance.on)
86                         cs_manage(&mainboard.distance.cs);
87         }
88         if ((cpt & 1) && (mainboard.flags & DO_POS)) {
89                 /* about 1.5ms (worst case without centrifugal force
90                  * compensation) */
91                 position_manage(&mainboard.pos);
92         }
93         if (mainboard.flags & DO_BD) {
94                 bd_manage_from_cs(&mainboard.angle.bd, &mainboard.angle.cs);
95                 bd_manage_from_cs(&mainboard.distance.bd, &mainboard.distance.cs);
96         }
97 #if 0
98         if (mainboard.flags & DO_TIMER) {
99                 uint8_t second;
100                 /* the robot should stop correctly in the strat, but
101                  * in some cases, we must force the stop from an
102                  * interrupt */
103                 second = time_get_s();
104                 if (second >= MATCH_TIME + 2) {
105                         pwm_ng_set(LEFT_PWM, 0);
106                         pwm_ng_set(RIGHT_PWM, 0);
107                         printf_P(PSTR("END OF TIME\r\n"));
108                         while(1);
109                 }
110         }
111         /* brakes */
112         if (mainboard.flags & DO_POWER)
113                 BRAKE_OFF();
114         else
115                 BRAKE_ON();
116 #endif
117         cpt++;
118
119         if ((cpt & 7) == 0)
120                 robotsim_dump();
121         //dump_cs("distance", &mainboard.distance.cs);
122         //dump_cs("angle", &mainboard.angle.cs);
123 }
124
125 void dump_cs_debug(const char *name, struct cs *cs)
126 {
127         DEBUG(E_USER_CS, "%s cons=% .5"PRIi32" fcons=% .5"PRIi32" err=% .5"PRIi32" "
128               "in=% .5"PRIi32" out=% .5"PRIi32"", 
129               name, cs_get_consign(cs), cs_get_filtered_consign(cs),
130               cs_get_error(cs), cs_get_filtered_feedback(cs),
131               cs_get_out(cs));
132 }
133
134 void dump_cs(const char *name, struct cs *cs)
135 {
136         printf_P(PSTR("%s cons=% .5"PRIi32" fcons=% .5"PRIi32" err=% .5"PRIi32" "
137                       "in=% .5"PRIi32" out=% .5"PRIi32"\r\n"), 
138                  name, cs_get_consign(cs), cs_get_filtered_consign(cs),
139                  cs_get_error(cs), cs_get_filtered_feedback(cs),
140                  cs_get_out(cs));
141 }
142
143 void dump_pid(const char *name, struct pid_filter *pid)
144 {
145         printf_P(PSTR("%s P=% .8"PRIi32" I=% .8"PRIi32" D=% .8"PRIi32" out=% .8"PRIi32"\r\n"),
146                  name,
147                  pid_get_value_in(pid) * pid_get_gain_P(pid),
148                  pid_get_value_I(pid) * pid_get_gain_I(pid),
149                  pid_get_value_D(pid) * pid_get_gain_D(pid),
150                  pid_get_value_out(pid));
151 }
152
153 void microb_cs_init(void)
154 {
155         /* ROBOT_SYSTEM */
156         rs_init(&mainboard.rs);
157         rs_set_left_pwm(&mainboard.rs, pwm_set_and_save, LEFT_PWM);
158         rs_set_right_pwm(&mainboard.rs,  pwm_set_and_save, RIGHT_PWM);
159         /* increase gain to decrease dist, increase left and it will turn more left */
160         rs_set_left_ext_encoder(&mainboard.rs, robotsim_encoder_get,
161                                 LEFT_ENCODER, IMP_COEF);
162         rs_set_right_ext_encoder(&mainboard.rs, robotsim_encoder_get,
163                                  RIGHT_ENCODER, IMP_COEF);
164         /* rs will use external encoders */
165         rs_set_flags(&mainboard.rs, RS_USE_EXT);
166
167         /* POSITION MANAGER */
168         position_init(&mainboard.pos);
169         position_set_physical_params(&mainboard.pos, VIRTUAL_TRACK_MM, DIST_IMP_MM);
170         position_set_related_robot_system(&mainboard.pos, &mainboard.rs);
171         position_set_centrifugal_coef(&mainboard.pos, 0.000016);
172         position_use_ext(&mainboard.pos);
173
174         /* TRAJECTORY MANAGER */
175         trajectory_init(&mainboard.traj);
176         trajectory_set_cs(&mainboard.traj, &mainboard.distance.cs,
177                           &mainboard.angle.cs);
178         trajectory_set_robot_params(&mainboard.traj, &mainboard.rs, &mainboard.pos);
179         trajectory_set_speed(&mainboard.traj, SPEED_DIST_FAST, SPEED_ANGLE_FAST); /* d, a */
180         /* distance window, angle window, angle start */
181         trajectory_set_windows(&mainboard.traj, 200., 5.0, 30.);
182
183         /* ---- CS angle */
184         /* PID */
185         pid_init(&mainboard.angle.pid);
186         pid_set_gains(&mainboard.angle.pid, 500, 10, 7000);
187         pid_set_maximums(&mainboard.angle.pid, 0, 20000, 4095);
188         pid_set_out_shift(&mainboard.angle.pid, 10);
189         pid_set_derivate_filter(&mainboard.angle.pid, 4);
190
191         /* QUADRAMP */
192         quadramp_init(&mainboard.angle.qr);
193         quadramp_set_1st_order_vars(&mainboard.angle.qr, 2000, 2000); /* set speed */
194         quadramp_set_2nd_order_vars(&mainboard.angle.qr, 13, 13); /* set accel */
195
196         /* CS */
197         cs_init(&mainboard.angle.cs);
198         cs_set_consign_filter(&mainboard.angle.cs, quadramp_do_filter, &mainboard.angle.qr);
199         cs_set_correct_filter(&mainboard.angle.cs, pid_do_filter, &mainboard.angle.pid);
200         cs_set_process_in(&mainboard.angle.cs, rs_set_angle, &mainboard.rs);
201         cs_set_process_out(&mainboard.angle.cs, rs_get_angle, &mainboard.rs);
202         cs_set_consign(&mainboard.angle.cs, 0);
203
204         /* Blocking detection */
205         bd_init(&mainboard.angle.bd);
206         bd_set_speed_threshold(&mainboard.angle.bd, 80);
207         bd_set_current_thresholds(&mainboard.angle.bd, 500, 8000, 1000000, 50);
208
209         /* ---- CS distance */
210         /* PID */
211         pid_init(&mainboard.distance.pid);
212         pid_set_gains(&mainboard.distance.pid, 500, 10, 7000);
213         pid_set_maximums(&mainboard.distance.pid, 0, 2000, 4095);
214         pid_set_out_shift(&mainboard.distance.pid, 10);
215         pid_set_derivate_filter(&mainboard.distance.pid, 6);
216
217         /* QUADRAMP */
218         quadramp_init(&mainboard.distance.qr);
219         quadramp_set_1st_order_vars(&mainboard.distance.qr, 2000, 2000); /* set speed */
220         quadramp_set_2nd_order_vars(&mainboard.distance.qr, 17, 17); /* set accel */
221
222         /* CS */
223         cs_init(&mainboard.distance.cs);
224         cs_set_consign_filter(&mainboard.distance.cs, quadramp_do_filter, &mainboard.distance.qr);
225         cs_set_correct_filter(&mainboard.distance.cs, pid_do_filter, &mainboard.distance.pid);
226         cs_set_process_in(&mainboard.distance.cs, rs_set_distance, &mainboard.rs);
227         cs_set_process_out(&mainboard.distance.cs, rs_get_distance, &mainboard.rs);
228         cs_set_consign(&mainboard.distance.cs, 0);
229
230         /* Blocking detection */
231         bd_init(&mainboard.distance.bd);
232         bd_set_speed_threshold(&mainboard.distance.bd, 60);
233         bd_set_current_thresholds(&mainboard.distance.bd, 500, 8000, 1000000, 50);
234
235         /* set them on !! */
236         mainboard.angle.on = 1;
237         mainboard.distance.on = 1;
238
239
240         scheduler_add_periodical_event_priority(do_cs, NULL,
241                                                 5000L / SCHEDULER_UNIT,
242                                                 CS_PRIO);
243 }