| Wheelchair Control MCU firmware

- Main Page
- Modules
- Data Structures
- Files

- File List
- Globals

All Data Structures Files Functions Variables Enumerations Groups Pages
MiscInits.c
Go to the documentation of this file.
1
9 //includes
10 #include "MiscInits.h"
11
12 //functions
13 void TIMER0_Init(void) //initializes TIMER0 (period 1 ms, used to clear UART Rx index every 4ms)
14 {
15 //8-bit timer, interrupt flag is set on everflow
16 //overflow interrupt period: 16 MHz / 64(prescaler) / 256 (8-bit resolution) = 1 ms
17
18 TCNT0 = 0x00; //reset timer
19
20 OCR0 = 0x00; //reset OCR0 (not necessary)
21
22 TCCR0 = 0x03; //normal mode, prescaler F_CPU/64
23
24 TIMSK |= (1<<TOIE0); //enable TIMER0 overflow interrupt
25 TIMSK &= ~(1<<OCIE0); //disable TIMER0 output compare match interrupt
26 }
27
28 void TIMER1_Init(void) //initializes TIMER1 (period 20 ms, used to generate R/C signal for Sabertooth 2x25)
29 {
30 //16-bit timer, R/C generation on OCR1X and TCNT1 match (non-inverting mode), TCNT1 top = ICR1
31 //R/C signal period 20 ms, positive pulse length: 1.5 ms: Stop motor OCR1X = 375
32 // 1 ms: Full speed backwards OCR1X = 250
33 // 2 ms: Full speed forward OCR1X = 500
34 //No interrupts enabled for TIMER1
35
36 TCNT1 = 0x00; //reset counter/timer register
37
38 ICR1 = 5000; //top of the timer, 16 MHz / 64 (prescaler) / 5000 = 20 ms
39
40 TCCR1A = 0xA2; //output compare signal on pins OC1X (DDRD reg. for OC1X must be set to output!), fast-PWM mode (two MSB bits), top = ICR1
41 TCCR1B = 0x1B; //no noise canceller, fast-PWM mode (two LSB bits), top = ICR1, prescaler: F_CPU/64 (must be the same as presc. value of TIMER0)
42
43 OCR1A = 375; //1.5 ms -> motor stop (left wheel)
44 OCR1B = 375; //1.5 ms -> motor stop (right wheel)
45
46 TIMSK &= ~( (1<<TICIE1) | (1<<OCIE1A) | (1<<OCIE1B) | (1<<TOIE1) ); //disable all interrupts for TIMER1
47 }
48
49 void TIMER2_Init(void) //initializes TIMER2 (period 2 ms)
50 {
51 //8-bit timer, interrupt flag is set on everflow
52 //overflow interrupt period: 16 MHz / 128(prescaler) / 256 (8-bit resolution) = 2 ms
53
54 TCNT2 = 0x00; //reset counter/timer register
55
56 OCR2 = 0x00; //reset OCR2 (not necessary)
57
58 ASSR = 0x00; //synchronous mode
59
60 TCCR2 = 0x05; //normal mode, prescaler: F_CPU/128
61
62 TIMSK |= (1<<TOIE2); //enable TIMER2 overflow interrupt
63 TIMSK &= ~(1<<OCIE2); //disable TIMER2 output compare match interrupt
64 }
65
66 void PORTS_Init(void) //initializes I/O port A - D (input/output, pull-ups etc.)
67 {
68 //Pin state: 0 - Output, low
69 // 1 - Output, high
70 // P - Input, pull-up enabled
71 // - - Input, pull-up disabled
72
73
74 //PORT A: 7 6 5 4 3 2 1 0
75 DDRA = 0x06; //DIRECTION: In In In In In Out Out In
76 PORTA = 0x00; //PIN STATE: - - - - - 0 0 -
77
78 //PORT B: 7 6 5 4 3 2 1 0
79 DDRB = 0x40; //DIRECTION: In Out In In In In In In
80 PORTB = 0x44; //PIN STATE: - 1 - - - P - -
81
82 //PORT C: 7 6 5 4 3 2 1 0
83 DDRC = 0xFC; //DIRECTION: Out Out Out Out Out Out In In
84 PORTC = 0x03; //PIN STATE: 0 0 0 0 0 0 P P
85
86 //PORT D: 7 6 5 4 3 2 1 0
87 DDRD = 0x30; //DIRECTION: In In Out Out In In In In
88 PORTD = 0x00; //PIN STATE: - - 0 0 - - - -
89 }
90
91 void AC_Disable(void) //disables analog comparator
92 {
93 ACSR |= (1<<ACD); //disable comparator
94 SFIOR &= ~(1<<ACME); //disable AC multiplexer
95 }
96
97 void ADC_Init(void) //initializes A/D converter
98 {
99 ADCSRA = 0x85; //enable A/D converter, disable interrupt on complete conversion, 500 kHz clock
100 ADMUX = 0x44; //reference voltage on AVCC pin, measured voltage on pin ADC4
101 SFIOR &= ~(0xE0); //does not matter, ADATE bit in ADCSRA (=autorun) is set to 0
102 }
103
104 void EXT_INT_Init(void) //initializes registers of external interrupts
105 {
106 MCUCR |= ( (1 << ISC10) | (1 << ISC00) ); //ext. interrupts 0 and 1 sense: both edges
107 MCUCSR &= ~(1<<ISC2); //ext. interrupt 2 sense: falling edge
108
109 GIFR = 0xE0; //reset ext. interrupt flags (write '1' to reset!)
110
111 GICR |= 0xE0; //enable external interrputs 0, 1, 2
112 }
113
114 void Init_ISRsSharedstruct(t_ISRsSharedStruct * sharedStruct) //Initializes inter-ISRs shared structure
115 {
116 sharedStruct->UARTRxIndex = 0; //Index in UARTReceivedData[] array
117 sharedStruct->Timer0Prescaler = 0; //Software prescaler for TIMER0
118 sharedStruct->UARTWatchdog = 0; //UART watchdog counter
119 sharedStruct->BufferOdoL = 0; sharedStruct->BufferOdoR = 0; //Couts impulses from wheel speed sensor for calculation of odometry
120 sharedStruct->BufferSpeedL = 0; sharedStruct->BufferSpeedR = 0; //Counts impulses from wheel speed sensor for calculation of current wheel speed
121 sharedStruct->MomSpeedR = SPEED_OFFSET; sharedStruct->MomSpeedL = SPEED_OFFSET; //Momentum (speed) of the motor
122 sharedStruct->EnableSpRegR = FALSE; sharedStruct->EnableSpRegL = FALSE; //Determines whether speed regulator of the motor is enabled
123 sharedStruct->EnableWallFollowing = FALSE; //Determines ehether wall-following is enabled
124 sharedStruct->ActualRPML = 0; sharedStruct->ActualRPMR = 0; //Dctual RPM of right and left wheel (impulses per 50 ms)
125 sharedStruct->RegIntegratorR = 0; sharedStruct->RegIntegratorL = 0; //Integrators value
126 sharedStruct->SRF08_DistanceFront = 0; sharedStruct->SRF08_DistanceRear = 0; //Distance measured by SRF08 sensors
127 sharedStruct->y0_des = 70; sharedStruct->V_des = SPEED_OFFSET; //Desired value of y0 (distance from the tracked wall), desired speed (cm/s)
128 sharedStruct->Ky = 0; sharedStruct->Kfi = 0; //Position loop gain, angular speed loop gain
129 sharedStruct->X = 0; sharedStruct->Y = 0; sharedStruct->Phi = 0; //Odometry data (X, Y [mm]; Phi [mRad])
130 sharedStruct->EnablePositionRegulator = FALSE; //Determines whether position regulator is enabled
131 sharedStruct->Xdes = 0; sharedStruct->Ydes = 0; //Desired coordinates for the position regulator
132 sharedStruct->TotalImpulsesR = 0; sharedStruct->TotalImpulsesR = 0; //Total number of impulses captured by encoders
133 sharedStruct->RegulateAngleOnly = 0; //If enabled, position regulator regulates only angle
134 }