df80b793ff7e4c3936593f1225641ae818f92229
[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_db.h"
61 #include "strat_base.h"
62 #include "strat_corn.h"
63 #include "strat_utils.h"
64 #include "sensor.h"
65 #include "actuator.h"
66
67 #define COL_DISP_MARGIN 400 /* stop 40 cm in front of dispenser */
68 #define COL_SCAN_PRE_MARGIN 250
69
70 struct strat_conf strat_conf;
71
72 /*************************************************************/
73
74 /*                  INIT                                     */
75
76 /*************************************************************/
77
78 /* called before each strat, and before the start switch */
79 void strat_preinit(void)
80 {
81         time_reset();
82         interrupt_traj_reset();
83         mainboard.flags =  DO_ENCODERS | DO_CS | DO_RS |
84                 DO_POS | DO_BD | DO_POWER;
85
86         //i2c_cobboard_mode_init();
87         strat_conf_dump(__FUNCTION__);
88         strat_db_dump(__FUNCTION__);
89 }
90
91 void strat_conf_dump(const char *caller)
92 {
93         if (!strat_conf.dump_enabled)
94                 return;
95
96         printf_P(PSTR("-- conf --\r\n"));
97
98 }
99
100 /* call it just before launching the strat */
101 void strat_init(void)
102 {
103         /* XXX init rollers, .. */
104
105         strat_db_init();
106
107         /* we consider that the color is correctly set */
108
109         strat_set_speed(SPEED_DIST_FAST, SPEED_ANGLE_FAST);
110         time_reset();
111         interrupt_traj_reset();
112
113         i2c_cobboard_set_mode(I2C_COBBOARD_MODE_HARVEST);
114         i2c_cobboard_harvest(I2C_LEFT_SIDE);
115         i2c_cobboard_harvest(I2C_RIGHT_SIDE);
116         i2c_ballboard_set_mode(I2C_BALLBOARD_MODE_HARVEST);
117
118         /* used in strat_base for END_TIMER */
119         mainboard.flags = DO_ENCODERS | DO_CS | DO_RS |
120                 DO_POS | DO_BD | DO_TIMER | DO_POWER;
121 }
122
123
124 /* call it after each strat */
125 void strat_exit(void)
126 {
127 #ifndef HOST_VERSION
128         uint8_t flags;
129 #endif
130
131         mainboard.flags &= ~(DO_TIMER);
132         strat_hardstop();
133         time_reset();
134         wait_ms(100);
135 #ifndef HOST_VERSION
136         IRQ_LOCK(flags);
137         mainboard.flags &= ~(DO_CS);
138         IRQ_UNLOCK(flags);
139         pwm_ng_set(LEFT_PWM, 0);
140         pwm_ng_set(RIGHT_PWM, 0);
141 #endif
142 }
143
144 /* called periodically */
145 void strat_event(void *dummy)
146 {
147 #if 0
148         /* pack or deploy spickle */
149         if (strat_infos.status.flags & STRAT_STATUS_LHARVEST) {
150                 if (sensor_get(S_LCOB_PRESENT)) {
151                         if (sensor_get(S_LCOB_WHITE))
152                                 i2c_ballboard_set_mode();
153                         else
154                                 ;
155                 }
156         }
157 #endif
158         /* limit speed when opponent is close */
159         strat_limit_speed();
160 }
161
162 static uint8_t strat_beginning(void)
163 {
164         uint8_t err;
165
166         strat_set_speed(250, SPEED_ANGLE_FAST);
167
168  l1:
169         err = line2line(LINE_UP, 0, LINE_R_DOWN, 2);
170         if (!TRAJ_SUCCESS(err)) {
171                 trajectory_hardstop(&mainboard.traj);
172                 time_wait_ms(2000);
173                 goto l1;
174         }
175
176  l2:
177         err = line2line(LINE_R_DOWN, 2, LINE_R_UP, 2);
178         if (!TRAJ_SUCCESS(err)) {
179                 trajectory_hardstop(&mainboard.traj);
180                 time_wait_ms(2000);
181                 goto l2;
182         }
183
184  l3:
185         err = line2line(LINE_R_UP, 2, LINE_UP, 5);
186         if (!TRAJ_SUCCESS(err)) {
187                 trajectory_hardstop(&mainboard.traj);
188                 time_wait_ms(2000);
189                 goto l3;
190         }
191
192         trajectory_hardstop(&mainboard.traj);
193
194         /* ball ejection */
195         trajectory_a_abs(&mainboard.traj, COLOR_A(90));
196         i2c_ballboard_set_mode(I2C_BALLBOARD_MODE_EJECT);
197         time_wait_ms(2000);
198
199         /* half turn */
200         trajectory_goto_xy_abs(&mainboard.traj, 2625, COLOR_Y(1847));
201         err = wait_traj_end(END_INTR|END_TRAJ);
202         i2c_cobboard_pack(I2C_LEFT_SIDE);
203         i2c_cobboard_pack(I2C_RIGHT_SIDE);
204         trajectory_a_rel(&mainboard.traj, COLOR_A(180));
205         err = wait_traj_end(END_INTR|END_TRAJ);
206
207         /* cob ejection */
208         trajectory_d_rel(&mainboard.traj, -100);
209         err = wait_traj_end(END_INTR|END_TRAJ);
210         i2c_cobboard_set_mode(I2C_COBBOARD_MODE_EJECT);
211         time_wait_ms(2000);
212
213         trajectory_hardstop(&mainboard.traj);
214         return END_TRAJ;
215 }
216
217 /* dump state (every 5 s max) */
218 #define DUMP_RATE_LIMIT(dump, last_print)               \
219         do {                                            \
220                 if (time_get_s() - last_print > 5) {    \
221                         dump();                         \
222                         last_print = time_get_s();      \
223                 }                                       \
224         } while (0)
225
226
227 uint8_t strat_main(void)
228 {
229         uint8_t err;
230
231         /* */
232         err = strat_beginning();
233
234         return err;
235 }