0% found this document useful (0 votes)
19 views9 pages

LCS Assigment 2

The document contains code and explanations for a line following and obstacle avoiding robot. It provides the state space model and transfer function model that could represent the dynamics of such a robot. The state space model defines the position error, orientation error, and distance to obstacles as states and linear and angular velocity as inputs. The code controls two DC motors to follow a line and avoid obstacles using infrared and ultrasonic sensors. It defines functions for movement, sensing obstacles, and turning in response to sensor readings.

Uploaded by

Maryam anjum
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views9 pages

LCS Assigment 2

The document contains code and explanations for a line following and obstacle avoiding robot. It provides the state space model and transfer function model that could represent the dynamics of such a robot. The state space model defines the position error, orientation error, and distance to obstacles as states and linear and angular velocity as inputs. The code controls two DC motors to follow a line and avoid obstacles using infrared and ultrasonic sensors. It defines functions for movement, sensing obstacles, and turning in response to sensor readings.

Uploaded by

Maryam anjum
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

Department of Electrical Engineering

Bahria School of Engineering and Applied

Sciences

Submitted By

AFAQ RASHID 01-133202-001


BENISH RAZA 01-133202-025
AHSAN HAMEED 01-133202-017

SUBMITTED TO
MA’AM MARYAM IQBAL
Department of Electrical Engineering

Bahria School of Engineering and Applied Sciences

Linear Control System


Assignment # 02
Department of Electrical Engineering

Bahria School of Engineering and Applied Sciences

State space equations or transfer function of the


system.

The state space equation or transfer function of a line following and obstacle avoidance robot will
depend on the specific design of the robot and its control system. However, here is an example of
a possible state space model for such a system:

State space model:

Let x1 be the position error between the robot's current position and the desired path, x2 be the
orientation error between the robot's current orientation and the desired orientation, and x3 be the
distance to the nearest obstacle. Let u1 be the linear velocity of the robot, and u2 be the angular
velocity of the robot. Then, the state space equations can be written as:

ẋ1 = -k1x1 + k2x2

ẋ2 = -k3x1 - k4x2

ẋ3 = -k5x3 + k6

y1 = x1

y2 = x3

where k1 to k6 are positive constants representing the gains of the system, and y1 and y2 are the
outputs of the system representing the position error and distance to the nearest obstacle,
respectively. The input to the system is the linear and angular velocities u1 and u2.

Transfer function model:

Alternatively, the system can be represented in the frequency domain using transfer functions. The
transfer function model is obtained by taking the Laplace transform of the state space equations,
which yields:

Y1(s) = G11(s)U1(s) + G12(s)U2(s)

Y2(s) = G21(s)U1(s) + G22(s)U2(s)

where Y1(s) and Y2(s) are the Laplace transforms of the output signals, U1(s) and U2(s) are the
Laplace transforms of the input signals, and G11(s), G12(s), G21(s), and G22(s) are the transfer
functions of the system.
Department of Electrical Engineering

Bahria School of Engineering and Applied Sciences


Again, the specific form of the transfer functions will depend on the design of the robot and its
control system, but the above equations provide a general framework for representing the
dynamics of a line following and obstacle avoidance robot.

Code:-

#define enA 10//Enable1 L298 Pin enA


#define in1 9 //Motor1  L298 Pin in1
#define in2 8 //Motor1  L298 Pin in1
#define in3 7 //Motor2  L298 Pin in1
#define in4 6 //Motor2  L298 Pin in1
#define enB 5 //Enable2 L298 Pin enB

#define L_S A0 //ir sensor Left


#define R_S A1 //ir sensor Right

#define echo A2    //Echo pin


#define trigger A3 //Trigger pin

#define servo A5

int Set=15;
int distance_L, distance_F, distance_R;

void setup(){ // put your setup code here, to run once

Serial.begin(9600); // start serial communication at 9600bps

pinMode(R_S, INPUT); // declare if sensor as input


pinMode(L_S, INPUT); // declare ir sensor as input

pinMode(echo, INPUT );// declare ultrasonic sensor Echo pin as input


pinMode(trigger, OUTPUT); // declare ultrasonic sensor Trigger pin as Output

pinMode(enA, OUTPUT); // declare as output for L298 Pin enA


pinMode(in1, OUTPUT); // declare as output for L298 Pin in1
pinMode(in2, OUTPUT); // declare as output for L298 Pin in2
pinMode(in3, OUTPUT); // declare as output for L298 Pin in3
pinMode(in4, OUTPUT); // declare as output for L298 Pin in4
pinMode(enB, OUTPUT); // declare as output for L298 Pin enB

analogWrite(enA, 200); // Write The Duty Cycle 0 to 255 Enable Pin A for Motor1
Speed
Department of Electrical Engineering

Bahria School of Engineering and Applied Sciences

analogWrite(enB, 200); // Write The Duty Cycle 0 to 255 Enable Pin B for Motor2
Speed

pinMode(servo, OUTPUT);

 for (int angle = 70; angle <= 140; angle += 5)  {


   servoPulse(servo, angle);  }
 for (int angle = 140; angle >= 0; angle -= 5)  {
   servoPulse(servo, angle);  }

 for (int angle = 0; angle <= 70; angle += 5)  {


   servoPulse(servo, angle);  }

distance_F = Ultrasonic_read();

delay(500);
}

void loop(){
//==============================================
//     Line Follower and Obstacle Avoiding
//==============================================

distance_F = Ultrasonic_read();
Serial.print("D F=");Serial.println(distance_F);

//if Right Sensor and Left Sensor are at White color then it will call forword
function
 if((digitalRead(R_S) == 0)&&(digitalRead(L_S) == 0)){
  if(distance_F > Set){forword();}
                  else{Check_side();}
 }

//if Right Sensor is Black and Left Sensor is White then it will call turn Right
function
else if((digitalRead(R_S) == 1)&&(digitalRead(L_S) == 0)){turnRight();}

//if Right Sensor is White and Left Sensor is Black then it will call turn Left
function
else if((digitalRead(R_S) == 0)&&(digitalRead(L_S) == 1)){turnLeft();}

delay(10);
}
Department of Electrical Engineering

Bahria School of Engineering and Applied Sciences

void servoPulse (int pin, int angle){


int pwm = (angle*11) + 500;      // Convert angle to microseconds
 digitalWrite(pin, HIGH);
 delayMicroseconds(pwm);
 digitalWrite(pin, LOW);
 delay(50); // Refresh cycle of servo
}
//**********************Ultrasonic_read****************************
long Ultrasonic_read(){
  digitalWrite(trigger, LOW);
  delayMicroseconds(2);
  digitalWrite(trigger, HIGH);
  delayMicroseconds(10);
  long time = pulseIn (echo, HIGH);
  return time / 29 / 2;
}
void compareDistance(){
    if(distance_L > distance_R){
  turnLeft();
  delay(500);
  forword();
  delay(600);
  turnRight();
  delay(500);
  forword();
  delay(600);
  turnRight();
  delay(400);
  }
  else{
  turnRight();
  delay(500);
  forword();
  delay(600);
  turnLeft();
  delay(500);
  forword();
  delay(600);

  turnLeft();
  delay(400);
  }
}

void Check_side(){
Department of Electrical Engineering

Bahria School of Engineering and Applied Sciences


    Stop();

    delay(100);
 for (int angle = 70; angle <= 140; angle += 5)  {
   servoPulse(servo, angle);  }
    delay(300);
    distance_R = Ultrasonic_read();
    Serial.print("D R=");Serial.println(distance_R);
    delay(100);
  for (int angle = 140; angle >= 0; angle -= 5)  {
   servoPulse(servo, angle);  }
    delay(500);
    distance_L = Ultrasonic_read();
    Serial.print("D L=");Serial.println(distance_L);
    delay(100);
 for (int angle = 0; angle <= 70; angle += 5)  {
   servoPulse(servo, angle);  }
    delay(300);
    compareDistance();
}

void forword(){  //forword


digitalWrite(in1, LOW); //Left Motor backword Pin
digitalWrite(in2, HIGH); //Left Motor forword Pin
digitalWrite(in3, HIGH); //Right Motor forword Pin
digitalWrite(in4, LOW); //Right Motor backword Pin
}

void backword(){ //backword


digitalWrite(in1, HIGH); //Left Motor backword Pin
digitalWrite(in2, LOW); //Left Motor forword Pin
digitalWrite(in3, LOW); //Right Motor forword Pin
digitalWrite(in4, HIGH); //Right Motor backword Pin
}

void turnRight(){ //turnRight


digitalWrite(in1, LOW); //Left Motor backword Pin
digitalWrite(in2, HIGH); //Left Motor forword Pin
digitalWrite(in3, LOW); //Right Motor forword Pin
digitalWrite(in4, HIGH); //Right Motor backword Pin
}

void turnLeft(){ //turnLeft


digitalWrite(in1, HIGH); //Left Motor backword Pin
digitalWrite(in2, LOW); //Left Motor forword Pin
digitalWrite(in3, HIGH); //Right Motor forword Pin
Department of Electrical Engineering

Bahria School of Engineering and Applied Sciences

digitalWrite(in4, LOW); //Right Motor backword Pin


}

void Stop(){ //stop


digitalWrite(in1, LOW); //Left Motor backword Pin
digitalWrite(in2, LOW); //Left Motor forword Pin
digitalWrite(in3, LOW); //Right Motor forword Pin
digitalWrite(in4, LOW); //Right Motor backword Pin
}

Output: -
Department of Electrical Engineering

Bahria School of Engineering and Applied Sciences

You might also like