Reserch On Code of PID Controller

Download as pdf or txt
Download as pdf or txt
You are on page 1of 2

Small Reserch on Code of PID Controller for SELF

BALNCING ROBOT Ashutosh (21BEE1151)

A PID controller, which stands for Proportional-Integral-Derivative controller, is commonly used in self-
balancing robots to maintain stability and control their movements. The PID controller continuously adjusts
the output to the robot's motors based on the difference between the desired angle and the current angle of
the robot. Here's a brief overview of the code structure for a PID controller used in a self-balancing robot:

1.Initialization:
 Define and initialize the PID gains (Kp, Ki, Kd) based on the system requirements and tuning.
 Initialize variables for error terms: previous_error, integral_term, and derivative_term.
 Set the target angle or position for the robot to maintain its balance.

2.Control Loop:
 Read the current angle or position of the robot from the sensors.
 Calculate the error term by subtracting the current angle from the target angle.
 Calculate the proportional term by multiplying the error term by the proportional gain (Kp).
 Calculate the integral term by summing up the error terms over time and multiplying by the integral
gain (Ki).
 Calculate the derivative term by finding the rate of change of the error term and multiplying by the
derivative gain (Kd).
 Compute the control output as the sum of the proportional, integral, and derivative terms.
 Apply the control output to the motors or actuators of the robot to adjust their speed or position.

3.Update:

 Update the previous_error with the current error for the next iteration.
 Update the integral_term with the sum of previous integral_term and current error.
 Update the derivative_term with the difference between the current error and previous error.

4.Repeat the control loop until the robot reaches its desired state or an external
condition is met (e.g., time limit).

 It's important to note that PID controllers may require tuning to achieve optimal performance for a
particular system. Tuning involves adjusting the proportional, integral, and derivative gains to balance
stability, responsiveness, and overshoot. Various tuning methods, such as manual tuning or autotuning
algorithms, can be employed to determine suitable values for the PID gains.
 Sample code (in embeded C language)
// Constants

#define Kp 1.0 // Proportional gain

#define Ki 0.5 // Integral gain

#define Kd 0.2 // Derivative gain

// Variables

float targetAngle = 0.0; // Target angle for balancing

float currentAngle = 0.0; // Current angle of the robot

float prevError = 0.0; // Previous error for derivative term

float integral = 0.0; // Accumulated error for integral term

// Function to calculate PID control output

float calculatePID(float target, float current, float dt) {

// Calculate error

float error = target - current;

// Proportional term

float pTerm = Kp * error;

// Integral term

integral += error * dt;

float iTerm = Ki * integral;

// Derivative term

float dTerm = Kd * (error - prevError) / dt;

// Calculate PID control output

float output = pTerm + iTerm + dTerm;

// Update previous error

prevError = error;

return output;

int main() {

// Simulation loop

float dt = 0.01; // Time step of the simulation

float simulationTime = 10.0; // Total simulation time in seconds

int numIterations = simulationTime / dt;

for (int i = 0; i < numIterations; i++) {

// Read sensor data and update current angle

// Calculate PID control output

float controlOutput = calculatePID(targetAngle, currentAngle, dt);

// Apply the control output to the motors

// Update the current angle based on the motor response

// Wait for the next time step

return 0;

You might also like