revert test serpi
[aversive.git] / projects / microb2010 / mechboard / 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.6 2009-11-08 17:25:00 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 LEFT_ARM_ENCODER        ((void *)0)
50 #define RIGHT_ARM_ENCODER       ((void *)1)
51
52 #define LEFT_ARM_PWM            ((void *)&gen.pwm1_4A)
53 #define RIGHT_ARM_PWM           ((void *)&gen.pwm2_4B)
54
55 #define RIGHT_PUMP1_PWM         ((void *)&gen.pwm3_1A)
56 #define RIGHT_PUMP2_PWM         ((void *)&gen.pwm4_1B)
57
58 #define R_ELBOW_AX12 1
59 #define R_WRIST_AX12 2
60 #define L_ELBOW_AX12 4
61 #define L_WRIST_AX12 3
62 #define FINGER_AX12  5
63
64 /** ERROR NUMS */
65 #define E_USER_I2C_PROTO       195
66 #define E_USER_SENSOR          196
67 #define E_USER_ARM             197
68 #define E_USER_FINGER          198
69 #define E_USER_ST_MACH         199
70 #define E_USER_CS              200
71 #define E_USER_AX12            201
72
73 #define LED_PRIO           170
74 #define TIME_PRIO          160
75 #define ADC_PRIO           120
76 #define CS_PRIO            100
77 #define ARM_PRIO            50
78 #define I2C_POLL_PRIO       20
79
80 #define CS_PERIOD 5000L
81
82 #define NB_LOGS 4
83
84 /* generic to all boards */
85 struct genboard {
86         /* command line interface */
87         struct rdline rdl;
88         char prompt[RDLINE_PROMPT_SIZE];
89
90         /* motors */
91         struct pwm_ng pwm1_4A;
92         struct pwm_ng pwm2_4B;
93         struct pwm_ng pwm3_1A;
94         struct pwm_ng pwm4_1B;
95
96         /* servos */
97         struct pwm_ng servo1;
98         struct pwm_ng servo2;
99         struct pwm_ng servo3;
100         struct pwm_ng servo4;
101         
102         /* ax12 interface */
103         AX12 ax12;
104
105         /* log */
106         uint8_t logs[NB_LOGS+1];
107         uint8_t log_level;
108         uint8_t debug;
109 };
110
111 struct cs_block {
112         uint8_t on;
113         struct cs cs;
114         struct pid_filter pid;
115         struct quadramp_filter qr;
116         struct blocking_detection bd;
117 };
118
119 /* mechboard specific */
120 struct mechboard {
121 #define DO_ENCODERS  1
122 #define DO_CS        2
123 #define DO_BD        4
124 #define DO_POWER     8
125         uint8_t flags;                /* misc flags */
126
127         /* control systems */
128         struct cs_block left_arm;
129         struct cs_block right_arm;
130
131         /* robot status */
132         uint8_t our_color;
133         volatile uint8_t lintel_count;
134         volatile uint8_t column_flags;
135         volatile uint8_t status;
136
137         /* local pumps */
138         int16_t pump_right1;
139         int16_t pump_right2;
140         /* remote pump (on mainboard) */
141         int16_t pump_left1;
142         int16_t pump_left2;
143
144         /* remote pump current */
145         int16_t pump_left1_current;
146         int16_t pump_left2_current;
147
148         uint16_t servo_lintel_left;
149         uint16_t servo_lintel_right;
150
151 };
152
153 extern struct genboard gen;
154 extern struct mechboard mechboard;
155
156 /* start the bootloader */
157 void bootloader(void);
158
159 #define DEG(x) (((double)(x)) * (180.0 / M_PI))
160 #define RAD(x) (((double)(x)) * (M_PI / 180.0))
161 #define M_2PI (2*M_PI)
162
163 #define WAIT_COND_OR_TIMEOUT(cond, timeout)                   \
164 ({                                                            \
165         microseconds __us = time_get_us2();                   \
166         uint8_t __ret = 1;                                    \
167         while(! (cond)) {                                     \
168                 if (time_get_us2() - __us > (timeout)*1000L) {\
169                         __ret = 0;                            \
170                         break;                                \
171                 }                                             \
172         }                                                     \
173         __ret;                                                \
174 })