0% found this document useful (0 votes)
25 views5 pages

Lcs Project Report PDF

Uploaded by

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

Lcs Project Report PDF

Uploaded by

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

December 30, 2024 [LINEAR CONTROL SYSTEMS (115965)]

PID BASED POSITION CONTROL


1 5 9
Zoha Laghari (15172) Laiba Binte Tariq(15131) Uzair Ahmed(15065)
2 6 10
Summaya (15090) Waqar Yamin(14995) Zain ul abideen(14118)
3 7
Ashoor Zainab(15018) M. Abdullah(15046)
4 8
Kisa Fatima(14915) M. Zafar(15170)

Department of Avionics, College of Engineering, Pakistan Air Force- Karachi Institute of Economics and
Technology.
Email:
1
[email protected]
2
[email protected]
3
[email protected]

ABSTRACT
This project implements a PID-based position control system using a Nisca servo DC motor with an Nf5475e encoder,
an L298N motor driver, and a potentiometer. The objective is to control the motor's position accurately by adjusting
the output voltage based on the feedback from the encoder. The PID controller, which consists of proportional, integral,
and derivative components, helps achieve stable and precise positioning.

1. INTRODUCTION PID Algorithm:


Position control systems are critical in A control loop mechanism used to maintain the
applications where precise motor control is motor’s position by minimizing the error
necessary, such as robotics, CNC machines, between the desired and actual position.
and automation systems. This project focuses
on developing a PID-based control system to 2. WORKING PRINCIPLE
adjust the position of a Nisca servo DC motor.
By using a potentiometer to set a target position Reading the Current Position:
and an encoder to measure the current position, The motor has an encoder (a sensor) attached to
the system continuously adjusts the motor's it. This encoder measures the motor's current
movement to minimize the error between the position and sends this information to the
desired and actual positions. Arduino (or microcontroller).
Calculating the Error:
Hardware:
Nisca Servo DC Motor (Nf5475e) with The Arduino compares the target position with
Encoder: the current position (from the encoder).
A DC motor with a built-in encoder to provide The difference between these two values is
feedback on the motor's position. called the error.
L298N Motor Driver: If the motor hasn’t reached the target, there will
A dual H-Bridge motor driver used to control be some error.
the DC motor's direction and speed.
Arduino Microcontroller: Adjusting Motor Movement (Using PID):
Used to implement the PID algorithm and
interface with the motor driver and encoder.. The PID controller is like a smart brain that
Power Supply: decides how the motor should move to reduce
Provides the necessary power for the motor and the error.
the controller Proportional (P): Moves the motor faster if the
. error is large and slower if the error is small.
Software : Integral (I): Corrects small errors over time
 Arduino IDE that the proportional part might miss.

1
December 30, 2024 [LINEAR CONTROL SYSTEMS (115965)]

Derivative (D): Prevents the motor from 5. CIRCUIT DIAGRAM


overshooting or oscillating around the target by
slowing it down at the right time.
Controlling the Motor:

Based on the PID calculation, the Arduino


sends a signal to the L298N motor driver.
The motor driver adjusts the motor's speed and
direction:
If the motor is behind the target, it moves
forward.
If it goes past the target, it moves backward
slightly to correct itself.
Feedback Loop:
This process repeats continuously
The encoder keeps measuring the motor's
position.
The Arduino keeps comparing the position with
the target.
The PID controller keeps making adjustments.
This feedback loop ensures the motor 6. CODE
eventually stops at the exact target position.
#include <util/atomic.h> // For atomic access to
shared variables
3. BLOCK DIAGRAM
// Pin Definitions
const int encoderPinA = 2; // Encoder Channel A
const int encoderPinB = 3; // Encoder Channel B
const int motorPin1 = A2; // Motor direction pin
1
const int motorPin2 = A3; // Motor direction pin
2
const int pwmPin = 5; // PWM pin for motor
speed
const int ledPin = 13; // Onboard LED

// PID Constants
float kp = 1.50; // Proportional gain
float ki = 0.09; // Integral gain
float kd = 0.01; // Derivative gain
int deadband = 5; // Deadband for control
int boostvalue = 70; // Boost value for holding
torque

// Variables
4. FRONT PANEL volatile long encoderPos = 0; // Encoder position
(updated by ISR)
long targetPos = 0; // Target position
float eintegral = 0; // Integral term accumulator
float eprev = 0; // Previous error
float error = 0; // Error
float customError = 0; // Custom error offset for
tuning
unsigned long prevT = 0; // Time of last loop
bool display_flag = false; // Debug display flag

// LED Blink Variables


unsigned long lastBlinkTime = 0;
const unsigned long blinkInterval = 500;

2
December 30, 2024 [LINEAR CONTROL SYSTEMS (115965)]

// Function Prototypes eintegral += error * deltaT; // Integral term


void processCommand(String command);
void serialDisplay(); // Anti-windup for Integral Term
void encoderISR(); const float maxIntegral = 200.0;
if (eintegral > maxIntegral) eintegral =
void setup() { maxIntegral;
// Motor Pins if (eintegral < -maxIntegral) eintegral = -
pinMode(motorPin1, OUTPUT); maxIntegral;
pinMode(motorPin2, OUTPUT);
pinMode(pwmPin, OUTPUT); // PID Control Signal
int controlSignal = kp * error + ki * eintegral + kd
// Encoder Pins * dedt;
pinMode(encoderPinA, INPUT_PULLUP); controlSignal = constrain(controlSignal, -255,
pinMode(encoderPinB, INPUT_PULLUP); 255);

// Attach interrupts for encoder // Apply Deadband


if (abs(error) > deadband) {
attachInterrupt(digitalPinToInterrupt(encoderPinA) if (controlSignal > 0) {
, encoderISR, CHANGE); controlSignal += boostvalue;
analogWrite(pwmPin, controlSignal);
attachInterrupt(digitalPinToInterrupt(encoderPinB) digitalWrite(motorPin1, LOW);
, encoderISR, CHANGE); digitalWrite(motorPin2, HIGH);
} else {
// LED Pin controlSignal -= boostvalue;
pinMode(ledPin, OUTPUT); analogWrite(pwmPin, -controlSignal);
digitalWrite(ledPin, LOW); digitalWrite(motorPin1, HIGH);
digitalWrite(motorPin2, LOW);
// Initialize motor }
analogWrite(pwmPin, 0); } else {
digitalWrite(motorPin1, LOW); // Stop Motor if within Deadband
digitalWrite(motorPin2, LOW); analogWrite(pwmPin, 0);
digitalWrite(motorPin1, LOW);
// Serial Monitor digitalWrite(motorPin2, LOW);
Serial.begin(115200); }
}
eprev = error; // Update previous error
void loop() {
// Handle Serial Commands // LED Blink
if (Serial.available()) { if (millis() - lastBlinkTime >= blinkInterval) {
String command = Serial.readStringUntil('\n'); lastBlinkTime = millis();
processCommand(command); digitalWrite(ledPin, !digitalRead(ledPin));
} }

// Time Management // Debug Display


unsigned long currT = micros(); if (display_flag) serialDisplay();
float deltaT = (currT - prevT) / 1.0e6; // Convert to }
seconds
prevT = currT; // Encoder Interrupt Service Routine
void encoderISR() {
// Read Encoder Position Atomically static bool lastStateA = LOW;
long pos; static bool lastStateB = LOW;

ATOMIC_BLOCK(ATOMIC_RESTORESTATE) bool stateA = digitalRead(encoderPinA);


{ bool stateB = digitalRead(encoderPinB);
pos = encoderPos;
} if (stateA != lastStateA) {
encoderPos += (stateA == stateB) ? 1 : -1;
// PID Error Calculation }
error = pos - targetPos + customError; // Include lastStateA = stateA;
custom error offset lastStateB = stateB;
float dedt = (error - eprev) / deltaT; // Derivative }
term

3
December 30, 2024 [LINEAR CONTROL SYSTEMS (115965)]

// Serial Command Processing Serial.print(" ce:"); //////////custom error


void processCommand(String command) { Serial.print(customError);
command.trim(); Serial.print(" kp:");
if (command.length() == 0) return; Serial.print(kp);
Serial.print(" ki:");
if (command.startsWith("c") || Serial.print(ki);
command.startsWith("a")) { Serial.print(" kd:");
int pulses = command.substring(1).toInt(); Serial.print(kd);
targetPos += (command.startsWith("c") ? pulses : Serial.print(" b:"); /////boost value
-pulses); Serial.print(boostvalue);
Serial.print("Target Position: "); Serial.print(" d:");////////dead band
Serial.println(targetPos); Serial.println(deadband);
} else if (command.startsWith("p")) {
kp = command.substring(1).toFloat(); 7. DIMENSIONS
Serial.print("kp set to: ");
Serial.println(kp);
} else if (command.startsWith("i")) {
ki = command.substring(1).toFloat();
Serial.print("ki set to: ");
Serial.println(ki);

} else if (command.startsWith("d")) {
kd = command.substring(1).toFloat();
Serial.print("kd set to: ");
Serial.println(kd);
} else if (command.startsWith("b")) {
deadband = command.substring(1).toInt();
Serial.print("Deadband set to: ");
Serial.println(deadband);
} else if (command.startsWith("u")) {
boostvalue = command.substring(1).toInt();
Serial.print("Boost set to: ");
Serial.println(boostvalue);
} else if (command.startsWith("e")) {
customError = command.substring(1).toFloat();
Serial.print("Custom Error Offset set to: ");
Serial.println(customError);
} else if (command.startsWith("o")) {
encoderPos = 0;
targetPos = 0;
error = 0;
eintegral = 0;
customError = 0;
eprev = 0;
Serial.println("Encoder and target position
reset.");
} else if (command.startsWith("f")) {
display_flag = command.substring(1).toInt() > 0;
Serial.print("Debug display ");
Serial.println(display_flag ? "enabled" :
"disabled");
}
} 8. APPLICATIONS

// Debug Serial Display Robotics: Precise motor control for positioning


void serialDisplay() { robotic arms or mobile robots.
Serial.print("cp:"); ////current position
Automation: Used in systems that require
Serial.print(encoderPos);
Serial.print(" tp:"); //////target position exact position control for actuators or
Serial.print(targetPos); mechanisms.
Serial.print(" er:");////////error CNC Machines: Position control is essential in
Serial.print(error); automated machining processes.

4
December 30, 2024 [LINEAR CONTROL SYSTEMS (115965)]

Servo Systems: Any application requiring


accurate and repeatable motion control.

8. FUTURE RECOMMENDATION

1. Advanced Motor Driver


Upgrade the Motor Driver: Replace the L298n
with a more efficient motor driver, such as the
TB6612FNG or VNH2SP30. These drivers have
better thermal management and higher
efficiency.
PWM Frequency Optimization: Use a motor
driver that supports higher PWM frequencies to
reduce motor noise and improve precision.
2. Improved Feedback System
Encoder Resolution: Upgrade to a high-
resolution encoder for finer position control.
Dual Feedback: Use both the encoder and 10. CONCLUSION
potentiometer for redundancy or combined This PID-based position control system offers
feedback for higher accuracy. an effective solution for controlling the position
3. Algorithm Enhancements of a DC motor. By using the Nf5475e encoder
Adaptive PID Tuning: Implement adaptive or for feedback and the L298N motor driver for
auto-tuning PID algorithms to dynamically controlling motor movement, the system ensures
adjust PID parameters for different loads or precise and stable positioning. The use of a
operating conditions. potentiometer to set the desired position and the
Feedforward Control: Combine PID with implementation of a PID controller provides a
feedforward control for better performance in robust solution for various applications
highly dynamic systems. requiring accurate position control.
4. Integration with Sensors
Inertial Measurement Unit (IMU): Integrate an 11. ACKNOWLEDGEMENT
IMU (like the MPU6050) to account for external We would like to express my gratitude to SIR
disturbances and improve stability in dynamic ARSALAN REHMAT for their guidance and
environments. academic encouragement. Their attitude and
Limit Switches: Add limit switches to ensure the care have helped us to complete this project on
motor operates within safe boundaries. time. I would also like to thank SIR MALIK
5. Software Improvements ADNAN AMEER for his advice and
State Estimation: Use Kalman filters or cooperation.
complementary filters to smooth noisy feedback We are also grateful to COE Lab In charges at
signals. KIET for providing the necessary resources and
facilities to conduct this project.
9. THE COMPLETE PROJECT Special thanks to the technical staff for their
assistance with the MATLAB software and
hardware components.

12. REFERENCES
https://ftn.canon/en/product/motor/encoder.
html
https://forum.arduino.cc/t/position-control-
of-a-motor-using-feedback-with-encoder-
and-arduino-uno/1280945
https://nevonprojects.com/arduino-pid-
based-dc-motor-position-control-system/

You might also like