revert test serpi
[aversive.git] / projects / microb2010 / tests / hostsim / 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.10 2009-11-08 17:24:33 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()
30 #define LED1_OFF()
31 #define LED1_TOGGLE()
32
33 #define LED2_ON()
34 #define LED2_OFF()
35 #define LED2_TOGGLE() 
36
37 #define LED3_ON() 
38 #define LED3_OFF() 
39 #define LED3_TOGGLE() 
40
41 #define LED4_ON()
42 #define LED4_OFF()
43 #define LED4_TOGGLE()
44
45 #define BRAKE_DDR()
46 #define BRAKE_ON()
47 #define BRAKE_OFF()
48
49 /* only 90 seconds, don't forget it :) */
50 #define MATCH_TIME 89
51
52 /* decrease track to decrease angle */
53 #define EXT_TRACK_MM 302.0188
54 #define VIRTUAL_TRACK_MM EXT_TRACK_MM
55
56 #define ROBOT_LENGTH 320
57 #define ROBOT_WIDTH 320
58
59 /* it is a 2048 imps -> 8192 because we see 1/4 period
60  * and diameter: 55mm -> perimeter 173mm 
61  * 8192/173 -> 473 */
62 /* increase it to go further */
63 #define IMP_ENCODERS 2048
64 #define WHEEL_DIAMETER_MM 55.0
65 #define WHEEL_PERIM_MM (WHEEL_DIAMETER_MM * M_PI)
66 #define IMP_COEF 10.
67 #define DIST_IMP_MM (((IMP_ENCODERS*4) / WHEEL_PERIM_MM) * IMP_COEF)
68
69 #define LEFT_ENCODER        ((void *)0)
70 #define RIGHT_ENCODER       ((void *)1)
71
72 #define LEFT_PWM            ((void *)0)
73 #define RIGHT_PWM           ((void *)1)
74
75 /** ERROR NUMS */
76 #define E_USER_STRAT           194
77 #define E_USER_I2C_PROTO       195
78 #define E_USER_SENSOR          196
79 #define E_USER_CS              197
80
81 #define LED_PRIO           170
82 #define TIME_PRIO          160
83 #define ADC_PRIO           120
84 #define CS_PRIO            100
85 #define STRAT_PRIO          30
86 #define I2C_POLL_PRIO       20
87 #define EEPROM_TIME_PRIO    10
88
89 #define CS_PERIOD 5000L
90
91 #define NB_LOGS 4
92
93 /* generic to all boards */
94 struct genboard {
95         /* command line interface */
96         struct rdline rdl;
97         char prompt[RDLINE_PROMPT_SIZE];
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 /* mainboard specific */
114 struct mainboard {
115 #define DO_ENCODERS  1
116 #define DO_CS        2
117 #define DO_RS        4
118 #define DO_POS       8
119 #define DO_BD       16
120 #define DO_TIMER    32
121 #define DO_POWER    64
122         uint8_t flags;                /* misc flags */
123
124         /* control systems */
125         struct cs_block angle;
126         struct cs_block distance;
127
128         /* x,y positionning */
129         struct robot_system rs;
130         struct robot_position pos;
131         struct trajectory traj;
132
133         /* robot status */
134         uint8_t our_color;
135         volatile int16_t speed_a;     /* current angle speed */
136         volatile int16_t speed_d;     /* current dist speed */
137         int32_t pwm_l;                /* current left pwm */
138         int32_t pwm_r;                /* current right pwm */
139         uint8_t enable_pickup_wheels; /* these PWM are on sensorboard */
140
141 };
142
143 extern struct genboard gen;
144 extern struct mainboard mainboard;
145
146 #define SPEED_DIST_FAST 2500
147 #define SPEED_ANGLE_FAST 2000
148 #define SPEED_DIST_SLOW 1000
149 #define SPEED_ANGLE_SLOW 1000
150 #define SPEED_DIST_VERY_SLOW 400
151 #define SPEED_ANGLE_VERY_SLOW 400
152
153 #define WAIT_COND_OR_TIMEOUT(cond, timeout)                   \
154 ({                                                            \
155         microseconds __us = time_get_us2();                   \
156         uint8_t __ret = 1;                                    \
157         while(! (cond)) {                                     \
158                 if (time_get_us2() - __us > (timeout)*1000L) {\
159                         __ret = 0;                            \
160                         break;                                \
161                 }                                             \
162         }                                                     \
163         if (__ret)                                            \
164                 DEBUG(E_USER_STRAT, "cond is true at line %d",\
165                       __LINE__);                              \
166         else                                                  \
167                 DEBUG(E_USER_STRAT, "timeout at line %d",     \
168                       __LINE__);                              \
169                                                               \
170         __ret;                                                \
171 })