| Wheelchair Control MCU firmware

- Main Page
- Modules
- Data Structures
- Files

- File List
- Globals

All Data Structures Files Functions Variables Enumerations Groups Pages
Defines.h
Go to the documentation of this file.
1
11 #ifndef _DEFINES //Define once
12 #define _DEFINES
13
14 //Includes
15 #include <avr/io.h>
16
17 //Debug mode
18 //#define DEBUG /*!<Uncomment to enable debugging using AVR studio's built-in debugger*/
19
20 //Assembler substitutes
21 #define GLOBAL_INTERRUPTS_ENABLE() asm("sei")
22 #define GLOBAL_INTERRUPTS_DISABLE() asm("cli")
23 #define WATCHDOG_RESET() asm("WDR")
24 #define WATCHDOG_INITIALIZE() WATCHDOG_RESET(); WDTCR = ( (1<<WDE) | 0x05 )
26 //SRF08 constants
27 #define SRF08_RANGE (2500)
28 #define SRF08_GAIN (0x03)
29 #define SRF08_LOWEST_ADDR (0xE0)
30 #define SRF08_HIGHEST_ADDR (0xE8)
31 #define SCL_CLOCK (50000UL)
33 //Wheels inputs
34 #define RIGHT_WHEEL_DIR ( PIND & (1 << PD7) )
35 #define LEFT_WHEEL_DIR ( PIND & (1 << PD6) )
36 #define RIGHT_WHEEL_STEP ( PIND & (1 << PD3) )
37 #define LEFT_WHEEL_STEP ( PIND & (1 << PD2) )
39 //Conversion constants
40 #define UCHAR_TO_RC_SIGNAL (247)
41 #define SPEED_OFFSET (128)
42 #define REAR_CORRECTION_OFFSET (128)
43 #define REG_OUTPUT_MAX (255)
44 #define REG_OUTPUT_MIN (0)
45 #define INTEGRATOR_MAX (2550)
46 #define INTEGRATOR_MIN (-2550)
48 //MCU clock frequency
49 #ifndef F_CPU
50 #define F_CPU (16e6)
51 #endif
52
53 //Car's steering wheel position
54 #define STEERING_WHEEL_LEFT (FALSE)
56 //Signal LEDs
57 #define LED_IN (1 << PA1)
58 #define LED_OUT (1 << PA2)
60 //Blink LEDs for a specified period
61 #define BLINK_LEDS_MS(DURATION) PORTA |= (LED_IN | LED_OUT); DELAY_MS(DURATION); PORTA &= ~(LED_IN | LED_OUT); DELAY_MS(DURATION)
63 //Delay
64 #ifndef DEBUG
65 #define DELAY_US(DURATION) _delay_us(DURATION)
66 #define DELAY_MS(DURATION) _delay_ms(DURATION)
67 #else
68 #define DELAY_US(DURATION); //Do not use _delay_us() macro to enable debugging in AVR studio
69 #define DELAY_MS(DURATION); //Do not use _delay_ms() macro to enable debugging in AVR studio
70 #endif
71
72 //Odometry
73 #define PI_MRAD (3141)
74 #define TWO_PI_MRAD (6283)
75 #define WHEEL_INCREMENT_SCALE (10)
77 //Position regulator
78 #define REG_ANGLE_EPSILON (0.1)
79 #define WTEMP_SCALE (10)
80 #define OMEGA_SCALE (0.1)
82 //UART WATCHDOG
83 #define UART_WATCHDOG_TIMEOUT (20)
85 //Timers control
86 #define TIMER_0_RESET() (TCNT0 = 0x00);
87 #define TIMER_1_OFF() (TCCR1B = 0x18);
89 //Timer 2 overflow interrupt enable/disable
90 #define TIMER2_OVF_INT_ENABLE() (TIMSK |= (1<<TOIE2))
91 #define TIMER2_OVF_INT_DISABLE() (TIMSK &= ~(1<<TOIE2))
93 //Encoder signals interrupts enable/disable
94 #define ENCODER_INTERRUPT_ENABLE() (GICR |= (0xC0))
95 #define ENCODER_INTERRUPT_DISABLE() (GICR &= ~(0xC0))
97 //Safety feedback loop
98 #define SRF08_SAFETY_RANGE (10)
100 //Others
101 #define FALSE (0)
102 #define TRUE (!FALSE)
107 typedef enum
108 {
109 TIMER2_SEND_SRF08_COMMAND = 0,
110 TIMER2_READ_SRF08_DATA = 23,
111 TIMER2_SPEED_REGULATORS = 24
112
113 } Timer2Intervals;
114
118 typedef enum
119 {
120 COMMAND_IDENTIFIER,
121 DATA1,
122 DATA2,
123 RX_CHECKSUM_BYTE,
124
125 RX_PACKET_LENGTH
126
127 } RxPacketByte;
128
132 typedef enum
133 {
134 TX_CHECKSUM_BYTE = 11,
135
136 TX_PACKET_LENGTH
137
138 } TxPacketByte;
139
143 typedef enum
144 {
145 //Odometry headers
146 ODOMETRY_SET_X = 0xA0,
147 ODOMETRY_SET_Y,
148 ODOMETRY_SET_PHI,
149 ODOMETRY_RESET_ALL,
150
151 //Motors control
152 MOTORS_RIGHT_MOMENTUM = 0xC0,
153 MOTORS_LEFT_MOMENTUM,
154 MOTORS_RIGHT_SPEED_CMPS,
155 MOTORS_LEFT_SPEED_CMPS,
156 MOTORS_BOTH_MOMENTUM,
157 MOTORS_BOTH_SPEED_IMPPS,
158 MOTORS_WALL_FOLLOWING,
159 MOTORS_POSITION_REGULATOR,
160
161 //Regulators
162 REGULATORS_X_DESIRED = 0xD0,
163 REGULATORS_Y_DESIRED,
164 REGULATORS_GAMMA,
165 REGULATORS_EPSILON,
166 REGULATORS_ANGLE_GAIN,
167 REGULATORS_POSITION_GAIN,
168 REGULATORS_SPEED,
169
170 //Configuration
171 CONFIGURATION_RIGHT_WHEEL_CIRCUM = 0xE0,
172 CONFIGURATION_LEFT_WHEEL_CIRCUM,
173 CONFIGURATION_TRACK,
174 CONFIGURATION_IMP_PER_ROUND,
175 CONFIGURATION_SRF08_SAFETY_ADDRESSES,
176 CONFIGURATION_MAX_ACCELERATION,
177 CONFIGURATION_KP,
178 CONFIGURATION_KD,
179 CONFIGURATION_KI,
180 CONFIGURATION_SRF08_ADDRESSES,
181 CONFIGURATION_REG_ERR_THRESH,
182 CONFIGURATION_OMEGA_0,
183 CONFIGURATION_L,
184 CONFIGURATION_KSI,
185 CONFIGURATION_OMEGA_THRESH,
186 CONFIGURATION_REAR_CORRECTION,
187
188 //Status data requests
189 SEND_ODOMETRY = 0xF0,
190 SEND_CONFIG_1,
191 SEND_CONFIG_2,
192 SEND_SRF08,
193 SEND_REGULATORS
194
195 } PacketHeaders;
196
200 typedef struct
201 {
202 uint8_t SRF08_Front, SRF08_Rear;
203 uint8_t ImpPerRoundR, ImpPerRoundL;
204 uint16_t CircumR, CircumL;
205 uint16_t Track;
206 uint8_t MaxAcc;
207 uint8_t Kp, Ki, Kd;
208 uint8_t RegErrorFilter;
209 uint8_t Omega0, L, Ksi;
210 uint8_t OmegaThresh;
211 uint8_t SRF08_RearCorrection;
212 uint8_t RightWheelIncrement, LeftWheelIncrement;
213 uint8_t SRF08_SafetyFirst, SRF08_SafetySecond;
214 uint8_t RegGamma, RegEpsilon, RegAngleGain, RegPositionGain, RegSpeed;
215 } t_ConfigStruct;
216
220 typedef struct
221 {
222 volatile uint8_t UARTRxIndex, Timer0Prescaler;
223 volatile uint8_t UARTWatchdog;
224 volatile int16_t BufferOdoL, BufferOdoR;
225 volatile int16_t BufferSpeedL, BufferSpeedR;
226 volatile uint8_t MomSpeedR, MomSpeedL;
227 volatile uint8_t EnableSpRegR, EnableSpRegL;
228 volatile uint8_t EnableWallFollowing;
229 volatile int16_t ActualRPML, ActualRPMR;
230 volatile int16_t RegIntegratorR, RegIntegratorL;
231 volatile uint8_t SRF08_DistanceFront, SRF08_DistanceRear;
232 volatile uint8_t y0_des, V_des;
233 volatile int16_t Ky, Kfi;
234 volatile int16_t X, Y, Phi;
235 volatile uint8_t EnablePositionRegulator;
236 volatile int16_t Xdes, Ydes;
237 volatile int16_t TotalImpulsesR, TotalImpulsesL;
238 volatile int16_t RegulateAngleOnly;
239 } t_ISRsSharedStruct;
240
241
242 #endif //_DEFINES
243