Controls Lab
Controls Lab
Experiment 3 Line
follower using
SPARKV
Group Number 16
Aviral Vishesh Goel, 22B2156
Siddick Khatri, 22b4241
Samarth Sirsat, 22b3988
October 24, 2024
1
1 AIM
To design and implement a PID controller for the Spark V robot that allows it to
follow a continuous black track, the IR sensors on the robot will be used for
feedback. The objective is to tune the PID parameters so that the robot completes the
track in under 30 seconds.
For this particular task, we will only use the LEFT and RIGHT movement commands
in the algorithm. Speed control is managed using Pulse Width Modulation (PWM).
The robot is equipped with three IR sensors beneath its chassis, each providing a
reading between 0 and 255 based on the surface reflectivity.
2 Arduino Code
// -O0
// 7372800Hz
#include <avr/io.h>
#include <avr/interrupt.h>
#include <util/delay.h>
#include "lcd.c"
2
PORTB = PORTB & 0xF0; // set initial value of the PORTB3 to PORTB0 pins to
logic 0
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
}
void left (int control_signal) //Left wheel backward, Right wheel forward
{
if(control_signal>255) control_signal=255;
motion_set(0x05);
//OCR1BL = control_signal/100;
//OCR1AL = control_signal/100;
OCR1BL = control_signal;
OCR1AL = control_signal;
}
void right (int control_signal) //Left wheel forward, Right wheel backward
{
3
if(control_signal>205) control_signal=135;
motion_set(0x0A);
OCR1BL = control_signal +120;
OCR1AL = control_signal+
120;
}
4
}
5
OCR1AL = 0x7F; //speedleft
OCR1BH = 0x00;
OCR1BL = 0x7F;
ICR1H = 0x00;
ICR1L = 0xFF;
TCCR1A = 0xA1;
TCCR1B = 0x0D; //start Timer
}
//This Function accepts the Channel Number and returns the corresponding
Analog Value
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();
lcd_set_4bit();
lcd_init();
int control_signal,erro,diff;
int prev_erro=0;
int odd=0;
timer1_init();
while(1)
{
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);
if(r>15 && l>15){
6
forward();
}
if(r<10 && c<10 && l<10){
forward();
_delay_ms(300);
}
else if(r-c>t1){
erro=r-c;
if(erro==prev_erro){forward();
continue;}
diff=erro-prev_erro;
control_signal=(P*erro)-(D*diff);
if(diff>55){
soft_stop();
}
else{
right(control_signal);
}
}
else if(l-c>t1){
erro=l-c;
if(erro==prev_erro){forward();
continue;}
diff=erro-prev_erro;
control_signal=(P*erro)-(D*diff);
if(diff>60){
soft_stop();
}
else{
left(control_signal);
}
}
else{
if(r>50 && l>50){
soft_stop();
}
else{motion_set(0x06);}
}
prev_erro=erro;
_delay_ms(25);
7
hard_stop();
_delay_ms(300);
hard_stop();
_delay_ms(300);
// hard_stop();
// _delay_ms(300);
//
// right(); //Left wheel forward, Right wheel backward
// _delay_ms(1000);
//
// hard_stop();
// _delay_ms(300);
//
// soft_left(); //Left wheel stationary, Right wheel forward
// _delay_ms(1000);
//
// hard_stop();
// _delay_ms(300);
//
// soft_right(); //Left wheel forward, Right wheel is stationary
// _delay_ms(1000);
//
// hard_stop();
// _delay_ms(300);
//
// soft_left_2(); //Left wheel backward, right wheel stationary
// _delay_ms(1000);
//
// hard_stop();
// _delay_ms(300);
//
// soft_right_2(); //Left wheel stationary, Right wheel backward
// _delay_ms(1000);
//
// hard_stop();
8
// _delay_ms(300);
}
• We made a crucial mistake of not noting the sensor values across the
different regions of the track. Because of this we faced a lot of issues in
tuning the PID parameters.
• The bot allotted to our team had one faulty center which always lead the bot
to prefer one side more than needed using the turns.
4 Results
• P=6
• D=1
• I=0
• Adding the derivative term to the controller reduced these oscillations and
then the system met the required track.