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