vt100: include pgmspace.h as we use PROGMEM macro
[aversive.git] / projects / microb2010 / ballboard / 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 /* was mainboard in 2009 */
23
24 #define LED_TOGGLE(port, bit) do {              \
25                 if (port & _BV(bit))            \
26                         port &= ~_BV(bit);      \
27                 else                            \
28                         port |= _BV(bit);       \
29         } while(0)
30
31 #define LED1_ON()       sbi(PORTJ, 2)
32 #define LED1_OFF()      cbi(PORTJ, 2)
33 #define LED1_TOGGLE()   LED_TOGGLE(PORTJ, 2)
34
35 #define LED2_ON()       sbi(PORTJ, 3)
36 #define LED2_OFF()      cbi(PORTJ, 3)
37 #define LED2_TOGGLE()   LED_TOGGLE(PORTJ, 3)
38
39 #define LED3_ON()       sbi(PORTL, 7)
40 #define LED3_OFF()      cbi(PORTL, 7)
41 #define LED3_TOGGLE()   LED_TOGGLE(PORTL, 7)
42
43 #define LED4_ON()       sbi(PORTL, 6)
44 #define LED4_OFF()      cbi(PORTL, 6)
45 #define LED4_TOGGLE()   LED_TOGGLE(PORTL, 6)
46
47 #define BRAKE_DDR()     do { DDRJ |= 0xF0; } while(0)
48 #define BRAKE_ON()      do { PORTJ |= 0xF0; } while(0)
49 #define BRAKE_OFF()     do { PORTJ &= 0x0F; } while(0)
50
51 #define ROLLER_ENCODER    ((void *)0)
52 #define FORKTRANS_ENCODER ((void *)1)
53 #define FORKROT_ENCODER   ((void *)2)
54 #define BEACON_ENCODER    ((void *)3)
55
56 #define ROLLER_PWM     ((void *)&gen.pwm1_4A)
57 #define FORKTRANS_PWM  ((void *)&gen.pwm2_4B)
58 #define FORKROT_PWM    ((void *)&gen.pwm3_1A)
59 #define BEACON_PWM     ((void *)&gen.pwm4_1B)
60
61 #define BEACON_POS_SENSOR  2
62
63 /** ERROR NUMS */
64 #define E_USER_I2C_PROTO       195
65 #define E_USER_SENSOR          196
66 #define E_USER_ST_MACH         197
67 #define E_USER_BEACON         198
68
69 #define LED_PRIO           170
70 #define TIME_PRIO          160
71 #define ADC_PRIO           120
72 #define CS_PRIO            100
73 #define BEACON_PRIO         80
74 #define I2C_POLL_PRIO       20
75
76 #define CS_PERIOD 5000L
77
78 #define NB_LOGS 4
79
80 /* generic to all boards */
81 struct genboard {
82         /* command line interface */
83         struct rdline rdl;
84         char prompt[RDLINE_PROMPT_SIZE];
85
86         /* motors */
87         struct pwm_ng pwm1_4A;
88         struct pwm_ng pwm2_4B;
89         struct pwm_ng pwm3_1A;
90         struct pwm_ng pwm4_1B;
91
92         /* servos */
93         struct pwm_ng servo1;
94         struct pwm_ng servo2;
95         struct pwm_ng servo3;
96         struct pwm_ng servo4;
97
98         /* ax12 interface */
99         AX12 ax12;
100
101         /* log */
102         uint8_t logs[NB_LOGS+1];
103         uint8_t log_level;
104         uint8_t debug;
105 };
106
107 struct cs_block {
108         uint8_t on;
109         struct cs cs;
110         struct pid_filter pid;
111         struct quadramp_filter qr;
112         struct blocking_detection bd;
113 };
114
115 /* ballboard specific */
116 struct ballboard {
117 #define DO_ENCODERS  1
118 #define DO_CS        2
119 #define DO_BD        4
120 #define DO_POWER     8
121 #define DO_ERRBLOCKING 16
122         uint8_t flags;                /* misc flags */
123
124         /* control systems */
125         struct cs_block roller;
126         struct cs_block forktrans;
127         struct cs_block forkrot;
128         struct cs_block beacon;
129
130         /* robot status */
131         uint8_t our_color;
132 };
133
134 extern struct genboard gen;
135 extern struct ballboard ballboard;
136
137 /* start the bootloader */
138 void bootloader(void);
139
140 #define WAIT_COND_OR_TIMEOUT(cond, timeout)                   \
141 ({                                                            \
142         microseconds __us = time_get_us2();                   \
143         uint8_t __ret = 1;                                    \
144         while(! (cond)) {                                     \
145                 if (time_get_us2() - __us > (timeout)*1000L) {\
146                         __ret = 0;                            \
147                         break;                                \
148                 }                                             \
149         }                                                     \
150         __ret;                                                \
151 })