trajectories on hostsim
[aversive.git] / projects / microb2010 / mainboard / strat.c
1 /*  
2  *  Copyright Droids, Microb Technology (2009)
3  * 
4  *  This program is free software; you can redistribute it and/or modify
5  *  it under the terms of the GNU General Public License as published by
6  *  the Free Software Foundation; either version 2 of the License, or
7  *  (at your option) any later version.
8  *
9  *  This program is distributed in the hope that it will be useful,
10  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  *  GNU General Public License for more details.
13  *
14  *  You should have received a copy of the GNU General Public License
15  *  along with this program; if not, write to the Free Software
16  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17  *
18  *  Revision : $Id: strat.c,v 1.6 2009-11-08 17:24:33 zer0 Exp $
19  *
20  *  Olivier MATZ <zer0@droids-corp.org> 
21  */
22
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <math.h>
27
28 #include <aversive/pgmspace.h>
29 #include <aversive/queue.h>
30 #include <aversive/wait.h>
31 #include <aversive/error.h>
32
33 #include <ax12.h>
34 #include <uart.h>
35 #include <pwm_ng.h>
36 #include <clock_time.h>
37 #include <spi.h>
38
39 #include <pid.h>
40 #include <quadramp.h>
41 #include <control_system_manager.h>
42 #include <trajectory_manager.h>
43 #include <vect_base.h>
44 #include <lines.h>
45 #include <polygon.h>
46 #include <obstacle_avoidance.h>
47 #include <blocking_detection_manager.h>
48 #include <robot_system.h>
49 #include <position_manager.h>
50
51 #include <diagnostic.h>
52
53 #include <rdline.h>
54 #include <parse.h>
55
56 #include "../common/i2c_commands.h"
57 #include "i2c_protocol.h"
58 #include "main.h"
59 #include "strat.h"
60 #include "strat_base.h"
61 #include "strat_utils.h"
62 #include "sensor.h"
63 #include "actuator.h"
64
65 #define COL_DISP_MARGIN 400 /* stop 40 cm in front of dispenser */
66 #define COL_SCAN_PRE_MARGIN 250
67
68 struct strat_infos strat_infos = {
69         /* conf */
70         .conf = {
71                 .flags = 0,
72         },
73
74 };
75
76 /*************************************************************/
77
78 /*                  INIT                                     */
79
80 /*************************************************************/
81
82 void strat_set_bounding_box(void)
83 {
84         if (get_color() == I2C_COLOR_YELLOW) {
85                 strat_infos.area_bbox.x1 = 300;
86                 strat_infos.area_bbox.y1 = 200;
87                 strat_infos.area_bbox.x2 = 2720; /* needed for c1 */
88                 strat_infos.area_bbox.y2 = 1800;
89         }
90         else {
91                 strat_infos.area_bbox.x1 = 200;
92                 strat_infos.area_bbox.y1 = 300;
93                 strat_infos.area_bbox.x2 = 2720; /* needed for c1 */
94                 strat_infos.area_bbox.y2 = 1900;
95         }
96
97         polygon_set_boundingbox(strat_infos.area_bbox.x1,
98                                 strat_infos.area_bbox.y1,
99                                 strat_infos.area_bbox.x2,
100                                 strat_infos.area_bbox.y2);
101 }
102
103 /* called before each strat, and before the start switch */
104 void strat_preinit(void)
105 {
106         time_reset();
107         interrupt_traj_reset();
108         mainboard.flags =  DO_ENCODERS | DO_CS | DO_RS |
109                 DO_POS | DO_BD | DO_POWER;
110
111         i2c_cobboard_mode_init();
112         strat_dump_conf();
113         strat_dump_infos(__FUNCTION__);
114 }
115
116 void strat_dump_conf(void)
117 {
118         if (!strat_infos.dump_enabled)
119                 return;
120
121         printf_P(PSTR("-- conf --\r\n"));
122
123 }
124
125 /* display current information about the state of the game */
126 void strat_dump_infos(const char *caller)
127 {
128         if (!strat_infos.dump_enabled)
129                 return;
130
131         printf_P(PSTR("%s() dump strat infos:\r\n"), caller);
132 }
133
134 /* init current area state before a match. Dump update user conf
135  * here */
136 void strat_reset_infos(void)
137 {
138 }
139
140 /* call it just before launching the strat */
141 void strat_init(void)
142 {
143         /* XXX init rollers, .. */
144         strat_reset_infos();
145
146         /* we consider that the color is correctly set */
147
148         strat_set_speed(SPEED_DIST_FAST, SPEED_ANGLE_FAST);
149         time_reset();
150         interrupt_traj_reset();
151
152         /* used in strat_base for END_TIMER */
153         mainboard.flags = DO_ENCODERS | DO_CS | DO_RS | 
154                 DO_POS | DO_BD | DO_TIMER | DO_POWER;
155 }
156
157
158 /* call it after each strat */
159 void strat_exit(void)
160 {
161         uint8_t flags;
162
163         mainboard.flags &= ~(DO_TIMER);
164         strat_hardstop();
165         time_reset();
166         wait_ms(1000);
167         IRQ_LOCK(flags);
168         mainboard.flags &= ~(DO_CS);
169         pwm_ng_set(LEFT_PWM, 0);
170         pwm_ng_set(RIGHT_PWM, 0);
171         IRQ_UNLOCK(flags);
172 }
173
174 /* called periodically */
175 void strat_event(void *dummy)
176 {
177         /* limit speed when opponent is close */
178         strat_limit_speed();
179 }
180
181 static uint8_t strat_beginning(void)
182 {
183         return END_TRAJ;
184 }
185
186 /* dump state (every 5 s max) */
187 #define DUMP_RATE_LIMIT(dump, last_print)               \
188         do {                                            \
189                 if (time_get_s() - last_print > 5) {    \
190                         dump();                         \
191                         last_print = time_get_s();      \
192                 }                                       \
193         } while (0)
194
195
196 uint8_t strat_main(void)
197 {
198         uint8_t err;
199
200         /* do static cols + first temple */
201         err = strat_beginning();
202
203         return err;
204 }