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