hostsim and cob handling
[aversive.git] / projects / microb2010 / cobboard / 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_SPICKLE_ENCODER   ((void *)0)
50 #define RIGHT_SPICKLE_ENCODER  ((void *)1)
51 #define SHOVEL_ENCODER         ((void *)2)
52
53 #define SERVO_DOOR_PWM         ((void *)&gen.servo2)
54
55 #define LEFT_SPICKLE_PWM       ((void *)&gen.pwm1_4A)
56 #define RIGHT_SPICKLE_PWM      ((void *)&gen.pwm2_4B)
57 #define SHOVEL_PWM             ((void *)&gen.pwm3_1A)
58
59 /** ERROR NUMS */
60 #define E_USER_I2C_PROTO       195
61 #define E_USER_SENSOR          196
62 #define E_USER_SPICKLE         197
63 #define E_USER_ST_MACH         199
64 #define E_USER_CS              200
65 #define E_USER_AX12            201
66
67 #define LED_PRIO           170
68 #define TIME_PRIO          160
69 #define SPICKLE_PRIO       130
70 #define ADC_PRIO           120
71 #define CS_PRIO            100
72 #define I2C_POLL_PRIO       20
73
74 #define CS_PERIOD 5000L
75
76 #define NB_LOGS 4
77
78 /* generic to all boards */
79 struct genboard {
80         /* command line interface */
81         struct rdline rdl;
82         char prompt[RDLINE_PROMPT_SIZE];
83
84         /* motors */
85         struct pwm_ng pwm1_4A;
86         struct pwm_ng pwm2_4B;
87         struct pwm_ng pwm3_1A;
88         struct pwm_ng pwm4_1B;
89
90         /* servos */
91         struct pwm_ng servo1;
92         struct pwm_ng servo2;
93         struct pwm_ng servo3;
94         struct pwm_ng servo4;
95         
96         /* ax12 interface */
97         AX12 ax12;
98
99         /* log */
100         uint8_t logs[NB_LOGS+1];
101         uint8_t log_level;
102         uint8_t debug;
103 };
104
105 struct cs_block {
106         uint8_t on;
107         struct cs cs;
108         struct pid_filter pid;
109         struct quadramp_filter qr;
110         struct blocking_detection bd;
111 };
112
113 /* cobboard specific */
114 struct cobboard {
115 #define DO_ENCODERS  1
116 #define DO_CS        2
117 #define DO_BD        4
118 #define DO_POWER     8
119         uint8_t flags;                /* misc flags */
120
121         /* control systems */
122         struct cs_block left_spickle;
123         struct cs_block right_spickle;
124         struct cs_block shovel;
125
126         /* robot status */
127         uint8_t our_color;
128         volatile uint8_t cob_count;
129         volatile uint8_t status;
130
131         /* synchronized to mainboard */
132         int16_t left_cobroller_speed;
133         int16_t right_cobroller_speed;
134 };
135
136 extern struct genboard gen;
137 extern struct cobboard cobboard;
138
139 /* start the bootloader */
140 void bootloader(void);
141
142 #define DEG(x) (((double)(x)) * (180.0 / M_PI))
143 #define RAD(x) (((double)(x)) * (M_PI / 180.0))
144 #define M_2PI (2*M_PI)
145
146 #define WAIT_COND_OR_TIMEOUT(cond, timeout)                   \
147 ({                                                            \
148         microseconds __us = time_get_us2();                   \
149         uint8_t __ret = 1;                                    \
150         while(! (cond)) {                                     \
151                 if (time_get_us2() - __us > (timeout)*1000L) {\
152                         __ret = 0;                            \
153                         break;                                \
154                 }                                             \
155         }                                                     \
156         __ret;                                                \
157 })