Professional Documents
Culture Documents
Experiment 2
Group 1, Friday Batch
Contents
1 Objectives 2
2 Control Algorithm 2
2.1 Methodology . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
2.2 Theory (PID Controller) . . . . . . . . . . . . . . . . . . . . . 2
2.3 Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
3 Challenges Faced 9
1
1 Objectives
• To design a PID controller for the Spark V robot to make it follow a
the track using IR sensors
2 Control Algorithm
2.1 Methodology
Following are the steps we followed:
1. The readings of the IR sensors were used to compute the error term.
e(t) = Reading from the Right IR Sensor - Reading from the Left IR Sensor
This error helps us determine the speed of the motors.
The integral term basically adds up the past errors and multiplies them by
a constant (Ki ) to produce a correction term. This would help us counter
small and consistent errors.
2
The derivative term is an estimate of the future trend of the error based on its
current rate of change (Kd * rate of change of error). It acts as a dampening
mechanism to reduce overshooting and oscillations caused by rapid changes
in error. It basically prevents the robot from making sudden corrections.
Thus every response has its own significance and can be used as a combina-
tion to build a stable control system.
where Kp , Ki , Kd , vf are all non-negative, and denote the coefficients for the
proportional, integral, and derivative terms, and scaling for Cout respectively
2.3 Code
#include <avr/io.h>
#include <avr/interrupt.h>
#include <util/delay.h>
#include "lcd.c"
3
DDRD = DDRD | 0x30; //Setting PD4 and PD5 pins as output for PWM generation
PORTD = PORTD | 0x30; //PD4 and PD5 pins are for velocity control using PWM
}
4
motion_set(0x04);
}
5
void init_devices (void)
{
cli(); //Clears the global interrupts
port_init();
adc_init();
timer1_init();
sei(); //Enables the global interrupts
}
6
TCCR1B = 0x00; //stop
TCNT1H = 0xFF; //setup
TCNT1L = 0x01;
OCR1AH = 0x00;
OCR1AL = 0xFF;
OCR1BH = 0x00;
OCR1BL = 0xFF;
ICR1H = 0x00;
ICR1L = 0xFF;
TCCR1A = 0xA1;
TCCR1B = 0x0D; //start Timer
}
//This Function accepts the Channel Number and returns the corresponding Analog Va
unsigned char ADC_Conversion(unsigned char Ch)
{
unsigned char a;
Ch = Ch & 0x07;
ADMUX= 0x20| Ch;
ADCSRA = ADCSRA | 0x40; //Set start conversion bit
while((ADCSRA&0x10)==0); //Wait for ADC conversion to complete
a=ADCH;
ADCSRA = ADCSRA|0x10; //clear ADIF (ADC Interrupt Flag) by writing 1 to it
return a;
}
//Main Function
int main(void)
{
init_devices();
//speed(100,100);
lcd_set_4bit();
7
lcd_init();
int kp = 40;
int ki = 0.005;
int kd = 35;
int pre = 0;
int total = 0;
int turn = 20;
while(1)
{
total += l-r;
l=ADC_Conversion(3);
c=ADC_Conversion(4);
r=ADC_Conversion(5);
lcd_print(1, 1, l, 3);
lcd_print(1, 5, c, 3);
lcd_print(1, 9, r, 3);
8
}
pre = total;
_delay_ms(10);
}
3 Challenges Faced
1. IR sensor calibration
There was a mitchmatch in the readings of the IR sesnors, and we had
to calibrate these readings by tuning the potentiometers. After getting
the left and right sensor values were nearly same, and we implemented
the conditional code accordingly
2. Hyperparameter tuning
We had to fine tune the base speed, by trail and error. We then had to
tune the values of Kp , Ki &Kd to get the best results. In the end, after
making sure the bot completes the track without getting out of track
anywhere, we adjusted the base speed again, so that the bot finishes
the track in given time.
3. Junction Turns
Initially, our bot failed at sharp turns, as the sensing required more
time. We resolved this problem by reducing the base speed. At junc-
tions, the bot used to take left/right turns instead of going forward. We
resolved this issue by firstly identifying the junction point is reached,
and then giving a delay while going forward. No condition other than
forward movement is executed for the set delay. Hence, the bot moved
in the desired direction at junctions.
9
4 Observations and Results
4.1 Observations
• Increasing Kp made the bot more responsive at turns but made more
oscillations and overshoots.
• With proper tuning of the base-speed many challenges faced were solved.
• A higher base speed would cause turns to be missed.
• A lower base speed would cause over time.
4.2 Results
Following are the values of Kp , Ki & Kd :
• Kp : 40
• Ki : 35
• Kd : 0.005
Our bot completed the track in 25s, and it faced no problems in finishing
the track in time.
10