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