SlideShare a Scribd company logo
#include <QTRSensors.h>
#define Kp 0.2 // experiment to determine this, start by something small that
just makes your bot follow the line at a slow speed
#define Kd 4 // experiment to determine this, slowly increase the speeds and
adjust this value. ( Note: Kp < Kd)
#define MaxSpeed 255// max speed of the robot
#define BaseSpeed 100 // this is the speed at which the motors should spin when
the robot is perfectly on the line
#define NUM_SENSORS 8 // number of sensors used
#define speedturn 255
#define rightMotor1 4
#define rightMotor2 5
#define rightMotorPWM 6
#define leftMotor1 8
#define leftMotor2 9
#define leftMotorPWM 3
#define motorPower 8
QTRSensorsAnalog qtra((unsigned char[]) {0,1,2,3,4,5,6,7} ,
NUM_SENSORS, 4, QTR_NO_EMITTER_PIN);
unsigned int sensorValues[NUM_SENSORS];
void setup()
{
pinMode(rightMotor1, OUTPUT);
pinMode(rightMotor2, OUTPUT);
pinMode(rightMotorPWM, OUTPUT);
pinMode(leftMotor1, OUTPUT);
pinMode(leftMotor2, OUTPUT);
pinMode(leftMotorPWM, OUTPUT);
pinMode(motorPower, OUTPUT);
delay(3000);
int i;
for (int i = 0; i < 100; i++) // calibrate for sometime by sliding the sensors
across the line, or you may use auto-calibration instead
{
//comment this part out for automatic calibration
if ( i < 25 || i >= 75 ) // turn to the left and right to expose the sensors to
the brightest and darkest readings that may be encountered
{
move(1, 70, 1);//motor derecho hacia adelante
move(0, 70, 0);//motor izquierdo hacia atras
}
else
{
move(1, 70, 0);//motor derecho hacia atras
move(0, 70, 1);//motor izquierdo hacia adelante
}
qtra.calibrate();
delay(20);
}
wait();
delay(3000); // wait for 2s to position the bot before entering the main loop
}
int lastError = 0;
unsigned int sensors[8];
int position = qtra.readLine(sensors);
void loop()
{
position = qtra.readLine(sensors); // get calibrated readings along with the
line position, refer to the QTR Sensors Arduino Library for more details on line
position.
if(position>6700){
move(1, speedturn, 1);//motor derecho hacia adelante
move(0, speedturn, 0);//motor izquierdo hacia adelante
return;
}
if(position<300){
move(1, speedturn, 0);//motor derecho hacia adelante
move(0, speedturn, 1);//motor izquierdo hacia adelante
return;
}
int error = position - 3500;
int motorSpeed = Kp * error + Kd * (error - lastError);
lastError = error;
int rightMotorSpeed = BaseSpeed + motorSpeed;
int leftMotorSpeed = BaseSpeed - motorSpeed;
if (rightMotorSpeed > MaxSpeed ) rightMotorSpeed = MaxSpeed; // prevent the
motor from going beyond max speed
if (leftMotorSpeed > MaxSpeed ) leftMotorSpeed = MaxSpeed; // prevent the motor
from going beyond max speed
if (rightMotorSpeed < 0)rightMotorSpeed = 0;
if (leftMotorSpeed < 0)leftMotorSpeed = 0;
move(1, rightMotorSpeed, 1);//motor derecho hacia adelante
move(0, leftMotorSpeed, 1);//motor izquierdo hacia adelante
}
void wait(){
digitalWrite(motorPower, LOW);
}
void move(int motor, int speed, int direction){
digitalWrite(motorPower, HIGH); //disable standby
boolean inPin1=HIGH;
boolean inPin2=LOW;
if(direction == 1){
inPin1 = HIGH;
inPin2 = LOW;
}
if(direction == 0){
inPin1 = LOW;
inPin2 = HIGH;
}
if(motor == 0){
digitalWrite(leftMotor1, inPin1);
digitalWrite(leftMotor2, inPin2);
analogWrite(leftMotorPWM, speed);
}
if(motor == 1){
digitalWrite(rightMotor1, inPin1);
digitalWrite(rightMotor2, inPin2);
analogWrite(rightMotorPWM, speed);
}
}

More Related Content

Similar to Final pid2

Open bot
Open bot Open bot
Open bot
Anshuman Dhar
 
Embedded Programming for Quadcopters
Embedded Programming for QuadcoptersEmbedded Programming for Quadcopters
Embedded Programming for Quadcopters
Ryan Boland
 
OpenBot-Code
OpenBot-CodeOpenBot-Code
OpenBot-Code
Anshuman Dhar
 
Edge_AI_Assignment_3.pdf
Edge_AI_Assignment_3.pdfEdge_AI_Assignment_3.pdf
Edge_AI_Assignment_3.pdf
ShivamMishra603376
 
Day 2 slides UNO summer 2010 robotics workshop
Day 2 slides UNO summer 2010 robotics workshopDay 2 slides UNO summer 2010 robotics workshop
Day 2 slides UNO summer 2010 robotics workshop
Raj Dasgupta
 
IISC CPDM Task 1 Report
IISC CPDM Task 1 ReportIISC CPDM Task 1 Report
IISC CPDM Task 1 Report
PARNIKA GUPTA
 
Gulotta_Wright_Parisi_FinalProjectOverview1
Gulotta_Wright_Parisi_FinalProjectOverview1Gulotta_Wright_Parisi_FinalProjectOverview1
Gulotta_Wright_Parisi_FinalProjectOverview1Nicholas Parisi
 
Embedded JavaScript
Embedded JavaScriptEmbedded JavaScript
Embedded JavaScript
Jens Siebert
 
How Automatic Vaccum cleaner works in today world.
How Automatic Vaccum cleaner works in today world.How Automatic Vaccum cleaner works in today world.
How Automatic Vaccum cleaner works in today world.
SatvikMadan
 
Pid lfr
Pid lfrPid lfr
Pid lfr
Tazeen Khan
 
Arduino Final Project
Arduino Final ProjectArduino Final Project
Arduino Final ProjectBach Nguyen
 
Arduino Lecture 3 - Interactive Media CS4062 Semester 2 2009
Arduino Lecture 3 - Interactive Media CS4062 Semester 2 2009Arduino Lecture 3 - Interactive Media CS4062 Semester 2 2009
Arduino Lecture 3 - Interactive Media CS4062 Semester 2 2009
Eoin Brazil
 
Arduino Lecture 3 - Interactive Media CS4062 Semester 2 2009
Arduino Lecture 3 - Interactive Media CS4062 Semester 2 2009Arduino Lecture 3 - Interactive Media CS4062 Semester 2 2009
Arduino Lecture 3 - Interactive Media CS4062 Semester 2 2009
Eoin Brazil
 
Timer counter in arm7(lpc2148)
Timer counter in arm7(lpc2148)Timer counter in arm7(lpc2148)
Timer counter in arm7(lpc2148)
Aarav Soni
 
Twin wheeler modified for arduino simplified serial protocol to sabertooth v22
Twin wheeler modified for arduino simplified serial protocol to sabertooth v22Twin wheeler modified for arduino simplified serial protocol to sabertooth v22
Twin wheeler modified for arduino simplified serial protocol to sabertooth v22
josnihmurni2907
 
project_NathanWendt
project_NathanWendtproject_NathanWendt
project_NathanWendtNathan Wendt
 
Exercises with timers and UART
Exercises with timers and UARTExercises with timers and UART
Exercises with timers and UARTCorrado Santoro
 
Introduction to RobotC
Introduction to RobotCIntroduction to RobotC
Introduction to RobotC
EMJeney
 
Twin wheeler modified for arduino simplified serial protocol to sabertooth v21
Twin wheeler modified for arduino simplified serial protocol to sabertooth v21Twin wheeler modified for arduino simplified serial protocol to sabertooth v21
Twin wheeler modified for arduino simplified serial protocol to sabertooth v21
josnihmurni2907
 

Similar to Final pid2 (20)

Open bot
Open bot Open bot
Open bot
 
Embedded Programming for Quadcopters
Embedded Programming for QuadcoptersEmbedded Programming for Quadcopters
Embedded Programming for Quadcopters
 
OpenBot-Code
OpenBot-CodeOpenBot-Code
OpenBot-Code
 
Edge_AI_Assignment_3.pdf
Edge_AI_Assignment_3.pdfEdge_AI_Assignment_3.pdf
Edge_AI_Assignment_3.pdf
 
Day 2 slides UNO summer 2010 robotics workshop
Day 2 slides UNO summer 2010 robotics workshopDay 2 slides UNO summer 2010 robotics workshop
Day 2 slides UNO summer 2010 robotics workshop
 
IISC CPDM Task 1 Report
IISC CPDM Task 1 ReportIISC CPDM Task 1 Report
IISC CPDM Task 1 Report
 
Gulotta_Wright_Parisi_FinalProjectOverview1
Gulotta_Wright_Parisi_FinalProjectOverview1Gulotta_Wright_Parisi_FinalProjectOverview1
Gulotta_Wright_Parisi_FinalProjectOverview1
 
Embedded JavaScript
Embedded JavaScriptEmbedded JavaScript
Embedded JavaScript
 
How Automatic Vaccum cleaner works in today world.
How Automatic Vaccum cleaner works in today world.How Automatic Vaccum cleaner works in today world.
How Automatic Vaccum cleaner works in today world.
 
Pid lfr
Pid lfrPid lfr
Pid lfr
 
Arduino Final Project
Arduino Final ProjectArduino Final Project
Arduino Final Project
 
Sbaw090908
Sbaw090908Sbaw090908
Sbaw090908
 
Arduino Lecture 3 - Interactive Media CS4062 Semester 2 2009
Arduino Lecture 3 - Interactive Media CS4062 Semester 2 2009Arduino Lecture 3 - Interactive Media CS4062 Semester 2 2009
Arduino Lecture 3 - Interactive Media CS4062 Semester 2 2009
 
Arduino Lecture 3 - Interactive Media CS4062 Semester 2 2009
Arduino Lecture 3 - Interactive Media CS4062 Semester 2 2009Arduino Lecture 3 - Interactive Media CS4062 Semester 2 2009
Arduino Lecture 3 - Interactive Media CS4062 Semester 2 2009
 
Timer counter in arm7(lpc2148)
Timer counter in arm7(lpc2148)Timer counter in arm7(lpc2148)
Timer counter in arm7(lpc2148)
 
Twin wheeler modified for arduino simplified serial protocol to sabertooth v22
Twin wheeler modified for arduino simplified serial protocol to sabertooth v22Twin wheeler modified for arduino simplified serial protocol to sabertooth v22
Twin wheeler modified for arduino simplified serial protocol to sabertooth v22
 
project_NathanWendt
project_NathanWendtproject_NathanWendt
project_NathanWendt
 
Exercises with timers and UART
Exercises with timers and UARTExercises with timers and UART
Exercises with timers and UART
 
Introduction to RobotC
Introduction to RobotCIntroduction to RobotC
Introduction to RobotC
 
Twin wheeler modified for arduino simplified serial protocol to sabertooth v21
Twin wheeler modified for arduino simplified serial protocol to sabertooth v21Twin wheeler modified for arduino simplified serial protocol to sabertooth v21
Twin wheeler modified for arduino simplified serial protocol to sabertooth v21
 

Recently uploaded

Final project report on grocery store management system..pdf
Final project report on grocery store management system..pdfFinal project report on grocery store management system..pdf
Final project report on grocery store management system..pdf
Kamal Acharya
 
MCQ Soil mechanics questions (Soil shear strength).pdf
MCQ Soil mechanics questions (Soil shear strength).pdfMCQ Soil mechanics questions (Soil shear strength).pdf
MCQ Soil mechanics questions (Soil shear strength).pdf
Osamah Alsalih
 
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
Dr.Costas Sachpazis
 
Standard Reomte Control Interface - Neometrix
Standard Reomte Control Interface - NeometrixStandard Reomte Control Interface - Neometrix
Standard Reomte Control Interface - Neometrix
Neometrix_Engineering_Pvt_Ltd
 
weather web application report.pdf
weather web application report.pdfweather web application report.pdf
weather web application report.pdf
Pratik Pawar
 
一比一原版(UofT毕业证)多伦多大学毕业证成绩单如何办理
一比一原版(UofT毕业证)多伦多大学毕业证成绩单如何办理一比一原版(UofT毕业证)多伦多大学毕业证成绩单如何办理
一比一原版(UofT毕业证)多伦多大学毕业证成绩单如何办理
ydteq
 
Investor-Presentation-Q1FY2024 investor presentation document.pptx
Investor-Presentation-Q1FY2024 investor presentation document.pptxInvestor-Presentation-Q1FY2024 investor presentation document.pptx
Investor-Presentation-Q1FY2024 investor presentation document.pptx
AmarGB2
 
English lab ppt no titlespecENG PPTt.pdf
English lab ppt no titlespecENG PPTt.pdfEnglish lab ppt no titlespecENG PPTt.pdf
English lab ppt no titlespecENG PPTt.pdf
BrazilAccount1
 
Hybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdf
Hybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdfHybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdf
Hybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdf
fxintegritypublishin
 
WATER CRISIS and its solutions-pptx 1234
WATER CRISIS and its solutions-pptx 1234WATER CRISIS and its solutions-pptx 1234
WATER CRISIS and its solutions-pptx 1234
AafreenAbuthahir2
 
road safety engineering r s e unit 3.pdf
road safety engineering  r s e unit 3.pdfroad safety engineering  r s e unit 3.pdf
road safety engineering r s e unit 3.pdf
VENKATESHvenky89705
 
power quality voltage fluctuation UNIT - I.pptx
power quality voltage fluctuation UNIT - I.pptxpower quality voltage fluctuation UNIT - I.pptx
power quality voltage fluctuation UNIT - I.pptx
ViniHema
 
NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...
NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...
NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...
Amil Baba Dawood bangali
 
AKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdf
AKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdfAKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdf
AKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdf
SamSarthak3
 
space technology lecture notes on satellite
space technology lecture notes on satellitespace technology lecture notes on satellite
space technology lecture notes on satellite
ongomchris
 
Nuclear Power Economics and Structuring 2024
Nuclear Power Economics and Structuring 2024Nuclear Power Economics and Structuring 2024
Nuclear Power Economics and Structuring 2024
Massimo Talia
 
Runway Orientation Based on the Wind Rose Diagram.pptx
Runway Orientation Based on the Wind Rose Diagram.pptxRunway Orientation Based on the Wind Rose Diagram.pptx
Runway Orientation Based on the Wind Rose Diagram.pptx
SupreethSP4
 
The role of big data in decision making.
The role of big data in decision making.The role of big data in decision making.
The role of big data in decision making.
ankuprajapati0525
 
ML for identifying fraud using open blockchain data.pptx
ML for identifying fraud using open blockchain data.pptxML for identifying fraud using open blockchain data.pptx
ML for identifying fraud using open blockchain data.pptx
Vijay Dialani, PhD
 
Student information management system project report ii.pdf
Student information management system project report ii.pdfStudent information management system project report ii.pdf
Student information management system project report ii.pdf
Kamal Acharya
 

Recently uploaded (20)

Final project report on grocery store management system..pdf
Final project report on grocery store management system..pdfFinal project report on grocery store management system..pdf
Final project report on grocery store management system..pdf
 
MCQ Soil mechanics questions (Soil shear strength).pdf
MCQ Soil mechanics questions (Soil shear strength).pdfMCQ Soil mechanics questions (Soil shear strength).pdf
MCQ Soil mechanics questions (Soil shear strength).pdf
 
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
 
Standard Reomte Control Interface - Neometrix
Standard Reomte Control Interface - NeometrixStandard Reomte Control Interface - Neometrix
Standard Reomte Control Interface - Neometrix
 
weather web application report.pdf
weather web application report.pdfweather web application report.pdf
weather web application report.pdf
 
一比一原版(UofT毕业证)多伦多大学毕业证成绩单如何办理
一比一原版(UofT毕业证)多伦多大学毕业证成绩单如何办理一比一原版(UofT毕业证)多伦多大学毕业证成绩单如何办理
一比一原版(UofT毕业证)多伦多大学毕业证成绩单如何办理
 
Investor-Presentation-Q1FY2024 investor presentation document.pptx
Investor-Presentation-Q1FY2024 investor presentation document.pptxInvestor-Presentation-Q1FY2024 investor presentation document.pptx
Investor-Presentation-Q1FY2024 investor presentation document.pptx
 
English lab ppt no titlespecENG PPTt.pdf
English lab ppt no titlespecENG PPTt.pdfEnglish lab ppt no titlespecENG PPTt.pdf
English lab ppt no titlespecENG PPTt.pdf
 
Hybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdf
Hybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdfHybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdf
Hybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdf
 
WATER CRISIS and its solutions-pptx 1234
WATER CRISIS and its solutions-pptx 1234WATER CRISIS and its solutions-pptx 1234
WATER CRISIS and its solutions-pptx 1234
 
road safety engineering r s e unit 3.pdf
road safety engineering  r s e unit 3.pdfroad safety engineering  r s e unit 3.pdf
road safety engineering r s e unit 3.pdf
 
power quality voltage fluctuation UNIT - I.pptx
power quality voltage fluctuation UNIT - I.pptxpower quality voltage fluctuation UNIT - I.pptx
power quality voltage fluctuation UNIT - I.pptx
 
NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...
NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...
NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...
 
AKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdf
AKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdfAKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdf
AKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdf
 
space technology lecture notes on satellite
space technology lecture notes on satellitespace technology lecture notes on satellite
space technology lecture notes on satellite
 
Nuclear Power Economics and Structuring 2024
Nuclear Power Economics and Structuring 2024Nuclear Power Economics and Structuring 2024
Nuclear Power Economics and Structuring 2024
 
Runway Orientation Based on the Wind Rose Diagram.pptx
Runway Orientation Based on the Wind Rose Diagram.pptxRunway Orientation Based on the Wind Rose Diagram.pptx
Runway Orientation Based on the Wind Rose Diagram.pptx
 
The role of big data in decision making.
The role of big data in decision making.The role of big data in decision making.
The role of big data in decision making.
 
ML for identifying fraud using open blockchain data.pptx
ML for identifying fraud using open blockchain data.pptxML for identifying fraud using open blockchain data.pptx
ML for identifying fraud using open blockchain data.pptx
 
Student information management system project report ii.pdf
Student information management system project report ii.pdfStudent information management system project report ii.pdf
Student information management system project report ii.pdf
 

Final pid2

  • 1. #include <QTRSensors.h> #define Kp 0.2 // experiment to determine this, start by something small that just makes your bot follow the line at a slow speed #define Kd 4 // experiment to determine this, slowly increase the speeds and adjust this value. ( Note: Kp < Kd) #define MaxSpeed 255// max speed of the robot #define BaseSpeed 100 // this is the speed at which the motors should spin when the robot is perfectly on the line #define NUM_SENSORS 8 // number of sensors used #define speedturn 255 #define rightMotor1 4 #define rightMotor2 5 #define rightMotorPWM 6 #define leftMotor1 8 #define leftMotor2 9 #define leftMotorPWM 3 #define motorPower 8 QTRSensorsAnalog qtra((unsigned char[]) {0,1,2,3,4,5,6,7} , NUM_SENSORS, 4, QTR_NO_EMITTER_PIN); unsigned int sensorValues[NUM_SENSORS]; void setup() { pinMode(rightMotor1, OUTPUT); pinMode(rightMotor2, OUTPUT); pinMode(rightMotorPWM, OUTPUT); pinMode(leftMotor1, OUTPUT); pinMode(leftMotor2, OUTPUT); pinMode(leftMotorPWM, OUTPUT); pinMode(motorPower, OUTPUT); delay(3000); int i; for (int i = 0; i < 100; i++) // calibrate for sometime by sliding the sensors across the line, or you may use auto-calibration instead { //comment this part out for automatic calibration
  • 2. if ( i < 25 || i >= 75 ) // turn to the left and right to expose the sensors to the brightest and darkest readings that may be encountered { move(1, 70, 1);//motor derecho hacia adelante move(0, 70, 0);//motor izquierdo hacia atras } else { move(1, 70, 0);//motor derecho hacia atras move(0, 70, 1);//motor izquierdo hacia adelante } qtra.calibrate(); delay(20); } wait(); delay(3000); // wait for 2s to position the bot before entering the main loop } int lastError = 0; unsigned int sensors[8]; int position = qtra.readLine(sensors); void loop() { position = qtra.readLine(sensors); // get calibrated readings along with the line position, refer to the QTR Sensors Arduino Library for more details on line position. if(position>6700){ move(1, speedturn, 1);//motor derecho hacia adelante move(0, speedturn, 0);//motor izquierdo hacia adelante return; } if(position<300){ move(1, speedturn, 0);//motor derecho hacia adelante move(0, speedturn, 1);//motor izquierdo hacia adelante return;
  • 3. } int error = position - 3500; int motorSpeed = Kp * error + Kd * (error - lastError); lastError = error; int rightMotorSpeed = BaseSpeed + motorSpeed; int leftMotorSpeed = BaseSpeed - motorSpeed; if (rightMotorSpeed > MaxSpeed ) rightMotorSpeed = MaxSpeed; // prevent the motor from going beyond max speed if (leftMotorSpeed > MaxSpeed ) leftMotorSpeed = MaxSpeed; // prevent the motor from going beyond max speed if (rightMotorSpeed < 0)rightMotorSpeed = 0; if (leftMotorSpeed < 0)leftMotorSpeed = 0; move(1, rightMotorSpeed, 1);//motor derecho hacia adelante move(0, leftMotorSpeed, 1);//motor izquierdo hacia adelante } void wait(){ digitalWrite(motorPower, LOW); } void move(int motor, int speed, int direction){ digitalWrite(motorPower, HIGH); //disable standby boolean inPin1=HIGH; boolean inPin2=LOW; if(direction == 1){ inPin1 = HIGH; inPin2 = LOW; } if(direction == 0){ inPin1 = LOW; inPin2 = HIGH; } if(motor == 0){
  • 4. digitalWrite(leftMotor1, inPin1); digitalWrite(leftMotor2, inPin2); analogWrite(leftMotorPWM, speed); } if(motor == 1){ digitalWrite(rightMotor1, inPin1); digitalWrite(rightMotor2, inPin2); analogWrite(rightMotorPWM, speed); } }