beacon, prepare integration
[aversive.git] / projects / microb2010 / mainboard / main.h
1 /*  
2  *  Copyright Droids Corporation (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: main.h,v 1.10 2009-11-08 17:24:33 zer0 Exp $
19  *
20  */
21
22 /* was sensorboard in 2009 */
23
24 #define LED_TOGGLE(port, bit) do {              \
25                 if (port & _BV(bit))            \
26                         port &= ~_BV(bit);      \
27                 else                            \
28                         port |= _BV(bit);       \
29         } while(0)
30
31 #ifdef HOST_VERSION
32 #define LED1_ON()
33 #define LED1_OFF()
34 #define LED1_TOGGLE()
35
36 #define LED2_ON()
37 #define LED2_OFF()
38 #define LED2_TOGGLE()
39
40 #define LED3_ON()
41 #define LED3_OFF()
42 #define LED3_TOGGLE()
43
44 #define LED4_ON()
45 #define LED4_OFF()
46 #define LED4_TOGGLE()
47
48 #define BRAKE_DDR()
49 #define BRAKE_ON()
50 #define BRAKE_OFF()
51
52 #else
53
54 #define LED1_ON()       sbi(PORTJ, 2)
55 #define LED1_OFF()      cbi(PORTJ, 2)
56 #define LED1_TOGGLE()   LED_TOGGLE(PORTJ, 2)
57
58 #define LED2_ON()       sbi(PORTJ, 3)
59 #define LED2_OFF()      cbi(PORTJ, 3)
60 #define LED2_TOGGLE()   LED_TOGGLE(PORTJ, 3)
61
62 #define LED3_ON()       sbi(PORTL, 7)
63 #define LED3_OFF()      cbi(PORTL, 7)
64 #define LED3_TOGGLE()   LED_TOGGLE(PORTL, 7)
65
66 #define LED4_ON()       sbi(PORTL, 6)
67 #define LED4_OFF()      cbi(PORTL, 6)
68 #define LED4_TOGGLE()   LED_TOGGLE(PORTL, 6)
69
70 #define BRAKE_DDR()     do { DDRJ |= 0xF0; } while(0)
71 #define BRAKE_ON()      do { PORTJ |= 0xF0; } while(0)
72 #define BRAKE_OFF()     do { PORTJ &= 0x0F; } while(0)
73 #endif
74
75 /* only 90 seconds, don't forget it :) */
76 #define MATCH_TIME 89
77
78 /* decrease track to decrease angle */
79 #define EXT_TRACK_MM 304.61875
80 #define VIRTUAL_TRACK_MM EXT_TRACK_MM
81
82 #define ROBOT_HALF_LENGTH_FRONT 130
83 #define ROBOT_HALF_LENGTH_REAR 120
84 #define ROBOT_WIDTH 320
85
86 /* it is a 1024 imps -> 4096 because we see 1/4 period
87  * and diameter: 55mm -> perimeter 134mm
88  * dist_imp_mm = 4096/134 x 10 -> 304 */
89 /* increase it to go further */
90 #define IMP_ENCODERS 1024
91 #define WHEEL_DIAMETER_MM 42.9
92 #define WHEEL_PERIM_MM (WHEEL_DIAMETER_MM * M_PI)
93 #define IMP_COEF 10.
94 #define DIST_IMP_MM (((IMP_ENCODERS*4) / WHEEL_PERIM_MM) * IMP_COEF)
95
96 #define RIGHT_ENCODER           ((void *)0)
97 #define LEFT_ENCODER            ((void *)1)
98 #define LEFT_COBROLLER_ENCODER  ((void *)2)
99 #define RIGHT_COBROLLER_ENCODER ((void *)3)
100
101 #define RIGHT_PWM           ((void *)&gen.pwm1_4A)
102 #define LEFT_PWM            ((void *)&gen.pwm2_4B)
103 #define LEFT_COBROLLER_PWM  ((void *)&gen.pwm3_1A)
104 #define RIGHT_COBROLLER_PWM ((void *)&gen.pwm4_1B)
105
106 #define SUPPORT_BALLS_R_SERVO ((void *)&gen.servo2)
107 #define SUPPORT_BALLS_L_SERVO ((void *)&gen.servo3)
108
109
110 /** ERROR NUMS */
111 #define E_USER_STRAT           194
112 #define E_USER_I2C_PROTO       195
113 #define E_USER_SENSOR          196
114 #define E_USER_CS              197
115
116 #define LED_PRIO           170
117 #define TIME_PRIO          160
118 #define ADC_PRIO           120
119 #define CS_PRIO            100
120 #define STRAT_PRIO          30
121 #define I2C_POLL_PRIO       20
122 #define BEACON_PRIO         15
123 #define EEPROM_TIME_PRIO    10
124
125 #define CS_PERIOD 5000L /* in microsecond */
126 #define CS_HZ (1000000. / CS_PERIOD)
127
128 #define NB_LOGS 4
129
130 /* generic to all boards */
131 struct genboard {
132         /* command line interface */
133         struct rdline rdl;
134         char prompt[RDLINE_PROMPT_SIZE];
135
136         /* motors */
137         struct pwm_ng pwm1_4A;
138         struct pwm_ng pwm2_4B;
139         struct pwm_ng pwm3_1A;
140         struct pwm_ng pwm4_1B;
141
142         /* servos */
143         struct pwm_ng servo1;
144         struct pwm_ng servo2;
145         struct pwm_ng servo3;
146         struct pwm_ng servo4;
147
148         /* ax12 interface */
149         AX12 ax12;
150
151         /* log */
152         uint8_t logs[NB_LOGS+1];
153         uint8_t log_level;
154         uint8_t debug;
155 };
156
157 struct cs_block {
158         uint8_t on;
159         struct cs cs;
160         struct pid_filter pid;
161         struct quadramp_filter qr;
162         struct blocking_detection bd;
163 };
164
165 /* mainboard specific */
166 struct mainboard {
167 #define DO_ENCODERS  1
168 #define DO_CS        2
169 #define DO_RS        4
170 #define DO_POS       8
171 #define DO_BD       16
172 #define DO_TIMER    32
173 #define DO_POWER    64
174 #define DO_ERRBLOCKING 128
175         uint8_t flags;                /* misc flags */
176
177         /* control systems */
178         struct cs_block angle;
179         struct cs_block distance;
180         struct cs_block left_cobroller;
181         struct cs_block right_cobroller;
182
183         /* x,y positionning */
184         struct robot_system rs;
185         struct robot_position pos;
186         struct trajectory traj;
187
188         /* robot status */
189         uint8_t our_color;
190         volatile int16_t speed_a;     /* current angle speed */
191         volatile int16_t speed_d;     /* current dist speed */
192         int32_t pwm_l;                /* current left pwm */
193         int32_t pwm_r;                /* current right pwm */
194 };
195
196 /* state of cobboard, synchronized through i2c */
197 struct cobboard {
198         uint8_t mode;
199         uint8_t status;
200         uint8_t lspickle;
201         uint8_t rspickle;
202         int16_t left_cobroller_speed;
203         int16_t right_cobroller_speed;
204         uint8_t cob_count;
205 };
206
207 /* state of ballboard, synchronized through i2c */
208 struct ballboard {
209         volatile uint8_t mode;
210         uint8_t status;
211         uint8_t ball_count;
212         uint8_t lcob;
213         uint8_t rcob;
214 };
215
216 /* state of beaconboard, sync'd through uart */
217 struct beaconboard {
218         int16_t oppx;
219         int16_t oppy;
220         int16_t oppa;
221         int16_t oppd;
222         uint16_t posx;
223         uint16_t posy;
224         uint16_t posa; /* between 0 and 3600 */
225 };
226
227 extern struct genboard gen;
228 extern struct mainboard mainboard;
229 extern volatile struct cobboard cobboard;
230 extern volatile struct ballboard ballboard;
231 extern volatile struct beaconboard beaconboard;
232
233 /* start the bootloader */
234 void bootloader(void);
235
236 #define WAIT_COND_OR_TIMEOUT(cond, timeout)                   \
237 ({                                                            \
238         microseconds __us = time_get_us2();                   \
239         uint8_t __ret = 1;                                    \
240         while(! (cond)) {                                     \
241                 if (time_get_us2() - __us > (timeout)*1000L) {\
242                         __ret = 0;                            \
243                         break;                                \
244                 }                                             \
245         }                                                     \
246         if (__ret)                                            \
247                 DEBUG(E_USER_STRAT, "cond is true at line %d",\
248                       __LINE__);                              \
249         else                                                  \
250                 DEBUG(E_USER_STRAT, "timeout at line %d",     \
251                       __LINE__);                              \
252                                                               \
253         __ret;                                                \
254 })