Skip to content

Commit a39a302

Browse files
committed
Firmware: Steering calibration
1 parent ea12fd6 commit a39a302

File tree

6 files changed

+71
-6
lines changed

6 files changed

+71
-6
lines changed

Steering/Firmware/Steering_Controller/Steering_Controller/Steering_Controller/global.h

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include <avr/io.h>
1313
#include <stdint.h>
1414
#include <avr/interrupt.h>
15+
#include <stdbool.h>
1516

1617
// Global Variables
1718
#define F_CPU 8000000UL

Steering/Firmware/Steering_Controller/Steering_Controller/Steering_Controller/main.c

+8-3
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,18 @@ int main(void)
3535
timer2_init();
3636
adc_init();
3737

38+
// Sets up PI Controller K values
3839
pi_setup();
3940

40-
calibrate_steering();
41+
// Reads and sets up voltage reference values used for steering (Disabled for Proteus)
42+
//calibrate_steering();
4143

42-
sei();
44+
//****(Enabled for Proteus)
45+
min_val = MAX_LIMIT;
46+
max_val = MIN_LIMIT;
4347
while (1) {
44-
48+
//****(Enabled for Proteus)
49+
calibrate_steering();
4550
}
4651
}
4752

Steering/Firmware/Steering_Controller/Steering_Controller/Steering_Controller/pi_controller.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,13 @@ void pi_controller(){
3737
// Sets the duty cycle on IN_1 or IN_2
3838
if (set_output > 0){ // Turning Right (positive error)
3939
IN_1_OFF;
40-
set_duty_cycle(set_output);
4140
IN_2_ON;
41+
set_duty_cycle(set_output);
4242
}
4343
else{ // Turning Left (negative error)
4444
IN_2_OFF;
45-
set_duty_cycle(set_output * (-1));
4645
IN_1_ON;
46+
set_duty_cycle(set_output * (-1));
4747
}
4848
}
4949

Steering/Firmware/Steering_Controller/Steering_Controller/Steering_Controller/pwm.c

+2
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,9 @@ void set_duty_cycle(int16_t value){
3939
// Checks which PWM output is ON
4040
if(CHECK_IN_1){
4141
OCR0A = t_on;// Sets Duty Cycle
42+
OCR1A = 0;
4243
} else if(CHECK_IN_2){
44+
OCR0A = 0;
4345
OCR1A = t_on;
4446
}
4547
}

Steering/Firmware/Steering_Controller/Steering_Controller/Steering_Controller/steering.c

+51-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,56 @@
88
#include "steering.h"
99

1010
// Finds reference voltage values for each angle
11+
// Works on two assumptions:
12+
// - Low voltage = Left, High voltage = Right
13+
// - Input pins to motor are correctly connected
1114
void calibrate_steering(){
12-
//insert calibration code
15+
// Sets maximum and minimum voltage ranges (Disabled for Proteus)
16+
// min_val = MAX_LIMIT;
17+
// max_val = MIN_LIMIT;
18+
19+
IN_1_ON;
20+
find_ref();
21+
IN_1_OFF;
22+
23+
IN_2_ON;
24+
find_ref();
25+
IN_2_OFF;
26+
27+
set_reference_values();
28+
29+
//Add straighten wheels function
1330
}
31+
32+
// Reads and sets maximum and minimum values
33+
void find_ref(){
34+
set_duty_cycle(MAX_LIMIT);
35+
36+
while(calibration_flag == 0){
37+
adc_val = adc_read();
38+
if(adc_val < min_val){
39+
min_val = adc_val;
40+
}
41+
else if (adc_val > max_val){
42+
max_val = adc_val;
43+
}
44+
else {
45+
calibration_flag = 1; // If steering motor has turned its maximum angle
46+
}
47+
}
48+
calibration_flag = 0; // Clears flag
49+
}
50+
51+
void set_reference_values(){
52+
full_l_turn = min_val;
53+
full_r_turn = max_val;
54+
55+
// Reference voltage for moving straight
56+
straight_turn = (max_val - min_val)/2 + min_val;
57+
58+
// Reference voltage for half left turn
59+
half_l_turn = (straight_turn - full_l_turn)/2 + full_l_turn;
60+
61+
// Reference voltage for half right turn
62+
half_r_turn = (full_r_turn - straight_turn)/2 + straight_turn;
63+
}

Steering/Firmware/Steering_Controller/Steering_Controller/Steering_Controller/steering.h

+7
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,15 @@
1111

1212
#include "global.h"
1313
#include "adc.h"
14+
#include "pwm.h"
1415
#include "led.h"
1516

1617
void calibrate_steering();
18+
void find_ref();
19+
void set_reference_values();
20+
21+
volatile uint16_t min_val;
22+
volatile uint16_t max_val;
23+
volatile uint8_t calibration_flag;
1724

1825
#endif /* STEERING_H_ */

0 commit comments

Comments
 (0)