20100416
[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(PORTJ, 3)
57 #define LED2_OFF()      cbi(PORTJ, 3)
58 #define LED2_TOGGLE()   LED_TOGGLE(PORTJ, 3)
59
60 #define LED3_ON()       sbi(PORTL, 7)
61 #define LED3_OFF()      cbi(PORTL, 7)
62 #define LED3_TOGGLE()   LED_TOGGLE(PORTL, 7)
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 304.61875
78 #define VIRTUAL_TRACK_MM EXT_TRACK_MM
79
80 #define ROBOT_HALF_LENGTH_FRONT 130
81 #define ROBOT_HALF_LENGTH_REAR 120
82 #define ROBOT_WIDTH 320
83
84 /* it is a 1024 imps -> 4096 because we see 1/4 period
85  * and diameter: 55mm -> perimeter 134mm
86  * dist_imp_mm = 4096/134 x 10 -> 304 */
87 /* increase it to go further */
88 #define IMP_ENCODERS 1024
89 #define WHEEL_DIAMETER_MM 42.9
90 #define WHEEL_PERIM_MM (WHEEL_DIAMETER_MM * M_PI)
91 #define IMP_COEF 10.
92 #define DIST_IMP_MM (((IMP_ENCODERS*4) / WHEEL_PERIM_MM) * IMP_COEF)
93
94 #define RIGHT_ENCODER           ((void *)0)
95 #define LEFT_ENCODER            ((void *)1)
96 #define LEFT_COBROLLER_ENCODER  ((void *)2)
97 #define RIGHT_COBROLLER_ENCODER ((void *)3)
98
99 #define RIGHT_PWM           ((void *)&gen.pwm1_4A)
100 #define LEFT_PWM            ((void *)&gen.pwm2_4B)
101 #define LEFT_COBROLLER_PWM  ((void *)&gen.pwm3_1A)
102 #define RIGHT_COBROLLER_PWM ((void *)&gen.pwm4_1B)
103
104 #define SUPPORT_BALLS_R_SERVO ((void *)&gen.servo2)
105 #define SUPPORT_BALLS_L_SERVO ((void *)&gen.servo3)
106
107
108 /** ERROR NUMS */
109 #define E_USER_STRAT           194
110 #define E_USER_I2C_PROTO       195
111 #define E_USER_SENSOR          196
112 #define E_USER_CS              197
113
114 #define LED_PRIO           170
115 #define TIME_PRIO          160
116 #define ADC_PRIO           120
117 #define CS_PRIO            100
118 #define STRAT_PRIO          30
119 #define I2C_POLL_PRIO       20
120 #define EEPROM_TIME_PRIO    10
121
122 #define CS_PERIOD 5000L /* in microsecond */ 
123 #define CS_HZ (1000000. / CS_PERIOD)
124
125 #define NB_LOGS 4
126
127 /* generic to all boards */
128 struct genboard {
129         /* command line interface */
130         struct rdline rdl;
131         char prompt[RDLINE_PROMPT_SIZE];
132
133         /* motors */
134         struct pwm_ng pwm1_4A;
135         struct pwm_ng pwm2_4B;
136         struct pwm_ng pwm3_1A;
137         struct pwm_ng pwm4_1B;
138
139         /* servos */
140         struct pwm_ng servo1;
141         struct pwm_ng servo2;
142         struct pwm_ng servo3;
143         struct pwm_ng servo4;
144
145         /* ax12 interface */
146         AX12 ax12;
147
148         /* log */
149         uint8_t logs[NB_LOGS+1];
150         uint8_t log_level;
151         uint8_t debug;
152 };
153
154 struct cs_block {
155         uint8_t on;
156         struct cs cs;
157         struct pid_filter pid;
158         struct quadramp_filter qr;
159         struct blocking_detection bd;
160 };
161
162 /* mainboard specific */
163 struct mainboard {
164 #define DO_ENCODERS  1
165 #define DO_CS        2
166 #define DO_RS        4
167 #define DO_POS       8
168 #define DO_BD       16
169 #define DO_TIMER    32
170 #define DO_POWER    64
171 #define DO_ERRBLOCKING 128
172         uint8_t flags;                /* misc flags */
173
174         /* control systems */
175         struct cs_block angle;
176         struct cs_block distance;
177         struct cs_block left_cobroller;
178         struct cs_block right_cobroller;
179
180         /* x,y positionning */
181         struct robot_system rs;
182         struct robot_position pos;
183         struct trajectory traj;
184
185         /* robot status */
186         uint8_t our_color;
187         volatile int16_t speed_a;     /* current angle speed */
188         volatile int16_t speed_d;     /* current dist speed */
189         int32_t pwm_l;                /* current left pwm */
190         int32_t pwm_r;                /* current right pwm */
191 };
192
193 /* state of cobboard, synchronized through i2c */
194 struct cobboard {
195         uint8_t mode;
196         uint8_t status;
197         int16_t left_cobroller_speed;
198         int16_t right_cobroller_speed;
199         uint8_t cob_count;
200 };
201
202 /* state of ballboard, synchronized through i2c */
203 struct ballboard {
204         uint8_t mode;
205         uint8_t status;
206         uint8_t ball_count;
207 };
208
209 extern struct genboard gen;
210 extern struct mainboard mainboard;
211 extern struct cobboard cobboard;
212 extern struct ballboard ballboard;
213
214 /* start the bootloader */
215 void bootloader(void);
216
217 #define WAIT_COND_OR_TIMEOUT(cond, timeout)                   \
218 ({                                                            \
219         microseconds __us = time_get_us2();                   \
220         uint8_t __ret = 1;                                    \
221         while(! (cond)) {                                     \
222                 if (time_get_us2() - __us > (timeout)*1000L) {\
223                         __ret = 0;                            \
224                         break;                                \
225                 }                                             \
226         }                                                     \
227         if (__ret)                                            \
228                 DEBUG(E_USER_STRAT, "cond is true at line %d",\
229                       __LINE__);                              \
230         else                                                  \
231                 DEBUG(E_USER_STRAT, "timeout at line %d",     \
232                       __LINE__);                              \
233                                                               \
234         __ret;                                                \
235 })