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