better movements
[aversive.git] / projects / microb2009 / sensorboard / 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.4 2009-05-27 20:04:07 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 #define BEACON_ENCODER        ((void *)0)
50 #define SCANNER_ENCODER       ((void *)1)
51
52 #define BEACON_PWM            ((void *)&gen.pwm1_4A)
53 #define SCANNER_PWM           ((void *)&gen.pwm2_4B)
54 #define PICKUP_WHEEL_L_PWM    ((void *)&gen.pwm3_1A)
55 #define PICKUP_WHEEL_R_PWM    ((void *)&gen.pwm4_1B)
56
57 #define BEACON_POS_SENSOR  2
58 #define SCANNER_POS_SENSOR 7
59 #define SCANNER_MAXCOLUMN_SENSOR 1
60
61
62
63 #define SCANNER_POS_OUT 179
64 #define SCANNER_POS_CALIBRE 370
65 #define SCANNER_POS_IN 400
66
67
68 /** ERROR NUMS */
69 #define E_USER_I2C_PROTO       195
70 #define E_USER_SENSOR          196
71 #define E_USER_BEACON          197
72 #define E_USER_SCANNER         198
73 #define E_USER_IMGPROCESS      199
74
75 #define LED_PRIO           170
76 #define TIME_PRIO          160
77 #define ADC_PRIO           120
78 #define CS_PRIO            100
79 #define BEACON_PRIO         80
80 #define I2C_POLL_PRIO       20
81
82 #define CS_PERIOD 5000L
83
84 #define NB_LOGS 4
85
86 /* generic to all boards */
87 struct genboard {
88         /* command line interface */
89         struct rdline rdl;
90         char prompt[RDLINE_PROMPT_SIZE];
91
92         /* motors */
93         struct pwm_ng pwm1_4A;
94         struct pwm_ng pwm2_4B;
95         struct pwm_ng pwm3_1A;
96         struct pwm_ng pwm4_1B;
97
98         /* servos */
99         struct pwm_ng servo1;
100         struct pwm_ng servo2;
101         struct pwm_ng servo3;
102         struct pwm_ng servo4;
103         
104         /* ax12 interface */
105         AX12 ax12;
106
107         /* log */
108         uint8_t logs[NB_LOGS+1];
109         uint8_t log_level;
110         uint8_t debug;
111 };
112
113 struct cs_block {
114         uint8_t on;
115         struct cs cs;
116         struct pid_filter pid;
117         struct quadramp_filter qr;
118         struct blocking_detection bd;
119 };
120
121 /* sensorboard specific */
122 struct sensorboard {
123 #define DO_ENCODERS  1
124 #define DO_CS        2
125 #define DO_BD        4
126 #define DO_POWER     8
127         uint8_t flags;                /* misc flags */
128
129         /* control systems */
130         struct cs_block beacon;
131         struct cs_block scanner;
132
133         /* robot status */
134         uint8_t our_color;
135 };
136
137 extern struct genboard gen;
138 extern struct sensorboard sensorboard;
139
140 /* start the bootloader */
141 void bootloader(void);
142
143 #define wait_cond_or_timeout(cond, timeout)                   \
144 ({                                                            \
145         microseconds __us = time_get_us2();                   \
146         uint8_t __ret = 1;                                    \
147         while(! (cond)) {                                     \
148                 if (time_get_us2() - __us > (timeout)*1000L) {\
149                         __ret = 0;                            \
150                         break;                                \
151                 }                                             \
152         }                                                     \
153         __ret;                                                \
154 })