LogoLogo
  • Welcome!
  • Mission Statement
  • Contributing Guidelines
    • Embed CADs in Wiki Articles
  • VEX Worlds Livestream Archive
    • VEX U
    • V5RC High School
    • V5RC Middle School
    • VIQRC Middle School
    • VIQRC Elementary School
    • JROTC
  • ⚙️Hardware
    • Design Fundamentals
      • Gear Ratios
      • Internal Forces (Stress)
      • Torque
      • RPM
      • Center of Mass
    • Introduction to VEX Parts
      • Structure
        • C-Channels and Angles
        • Fasteners
        • Retainers
        • Gussets and Brackets
        • Bearings
        • Plate Metal and Flat Bars
      • Motion
        • High Strength Components
        • Gears and Sprockets
        • Traction Wheels
        • Mecanum Wheels
        • Omnidirectional Wheels
        • Flex Wheels
    • Robot Decorations
      • Part Dyeing
      • Metal Coloring
      • License Plate Holders
    • Lifts
      • Double Reverse Four Bar (DR4B or RD4B)
      • Four Bar
      • Scissor Lift
      • Six Bar
      • Other Lifts
      • Best Practices
    • Shooting Mechanisms
      • Catapult
      • Flywheel
      • Linear Puncher
    • Drivetrains
      • Tank Drive
      • Mecanum Drive
      • Holonomic Drive
      • Designing a Drivetrain
      • Best Practices
    • Pivots & Joints
    • Pneumatics
      • Best Practices - Pneumatics
    • Intakes
    • Flip Out Mechanisms
    • Defensive Mechanisms
    • Misc. Building Techniques
    • VexU
      • Common Manufacturing Techniques
        • 3D Printing
        • Laser Cutting
      • Custom Manufactured Parts Library
      • Commercial Off The Shelf Parts Library
  • 👑Team Administration
    • New Team Resources
      • Creating The Team
      • Gaining Interest for Robotics Teams
      • Attending Competitions
        • Elimination Bracket
    • Team Dynamics
      • Organization Structure and Longevity
      • Member Allocation and Management
      • How *Not* To Run a Team
    • Team Finances
      • One-Year Team Financial Breakdown
      • Funding Your Teams
    • Hosting Competitions
      • Live Streaming
      • Tournament Manager
        • Competition Electronics
        • Creating a Tournament
        • Tools
          • Field Set Control
          • Connecting Mobile Devices
          • Connecting Raspberry Pis
        • Match Control
          • Inputting Match Scores
          • Inputting Skills Scores
          • Inputting Scores on TM Mobile
        • Displays
        • Alliance Selection
      • Additional Event Partner Resources
    • VexU Organization Management
      • Getting Started in VexU
      • Team / Personnel Management
      • Volunteering At Local Events
  • 📚The Judging Process
    • The Engineering Design Process
      • Test and Refine
    • The Engineering Notebook
      • Segments of the Notebook
      • BLRS2 '23-'24 Engineering Notebook
      • Integrating Inventor Models into Documentation
      • Engineering Notebook Rubric Breakdown
    • The Interview
      • Interview Rubric Breakdown
    • Using Notion for an Engineering Notebook
      • How to Setup a Notebook
      • How to Create Entries
      • How to Export a Notebook
      • Purdue SIGBots Notion Template
        • Game Analysis
        • Identify The Problem
        • Brainstorm Solution
        • Select Best Approach & Plan
        • Build Log
        • Programming Log
        • Testing Solution
        • Tournament Recap
        • Innovative Feature
  • 🖥️VEX CAD
    • CAD Programs
      • Inventor
      • Fusion 360
      • Solidworks
      • OnShape
      • Protobot
    • Making a Chassis
      • Inventor Chassis: The Basics
        • Installation
        • User Interface Overview
        • Dark Mode
        • Assemblies
        • Placing Parts
        • Navigating CAD
        • Changing Visual Style
        • Grounding
        • Connecting Two C-Channels
        • Modifying Existing Constraints
        • Toggling Visibility on Existing Parts
        • Completing Half of the Chassis
          • Inner Drive Channel
          • Bearing Flats
          • Motors
          • Wheels
          • Sprockets
          • Spacers, Washers and Standoffs
          • Spacers Cont.
        • Creating Mid-Plane
        • Mirroring
      • Inventor Chassis: Best Practices
        • File Structure
        • Subassemblies
        • Wheel Subassembly
        • Origin Planes
        • Cross Brace
        • Drive Channels
        • Simple Motor iMates
        • Replacing Simple Electronics
        • Completing Half of the Drive
          • Bearing Flats (Best Practice)
          • Wheels
          • Powered Gear
          • Spacer Boxing
          • Spacers, Washers and Standoffs (Best Practice)
        • Model Browser Folders
        • Mirroring (Best Practice)
        • Model Browser Folder (Right)
        • Main Assembly
      • Fusion 360 Chassis
      • Solidworks Chassis, Chain, and Custom Plastic
    • Remembering The Best
      • 62A Skyrise
      • 400X Nothing But Net
      • 2587Z Nothing But Net
      • 365X Starstruck
      • 62A In The Zone
      • 202Z In The Zone
      • 5225A In The Zone
      • 169A Turning Point
      • 929U Turning Point
      • 7K Tower Takeover
      • 5225A Tower Takeover
      • 62A Change Up
    • Scuff Controller
  • 💻Software
    • Odometry
    • Path Planning
    • Robotics Basics
      • Arcade Drive
      • Tank Drive
      • Joystick Deadzones
      • Curvature (Cheesy) Drive
      • Subsystem Toggling
    • Organizing Code
      • Code Style
      • Code Styling Guide
      • Writing Good Comments
      • Version Control
    • Control Algorithms
      • Bang Bang
      • PID Controller
      • Basic Pure Pursuit
      • Flywheel Velocity Control
      • Kalman Filter
      • Take Back Half (TBH) Controller
      • RAMSETE Controller
    • Competition Specific
      • Operator Control
      • Autonomous Control
    • C++ Basics for VEX Robotics
      • Basic Control Flow
      • Enumerations
      • Namespaces (::)
      • Multiple Files (C/C++)
    • VEX Programming Software
      • PROS
        • OkapiLib
      • vexide
      • Robot Mesh Studio (RMS)
      • EasyC
      • RobotC
      • VEXcode
      • Midnight C
    • General
      • Stall Detection
      • Register Programming
      • Sensors and Odometry in Autonomous
      • Embedded Programming Tips
      • Debugging
      • Bit Shift
      • Bit Mask
      • Autoformatting
      • Finite State Machine
      • Data Logging
    • Object Recognition
      • Red Green Buoy
      • AMS
      • OpenCV
      • OpenNI
    • 🤖AI in VRC: Pac-Man Pete
  • ⚡VEX Electronics
    • V5 ESD Protection Board
    • VEX Electronics
      • VEX V5 Brain
        • V5 Electronics Observations and Issues
      • VEX Controller
      • VEXnet and V5 Robot Radio
      • VEX Battery
      • VEX Motors
    • VEX Sensors
      • 3-Pin / ADI Sensors
        • Encoder
        • Potentiometer
        • Limit Switch
        • Bumper Switch
        • Accelerometer
        • Gyroscope
        • Ultrasonic
        • Line Tracker
        • LED Indicator
      • Smart Port Sensors
        • GPS Sensor
        • Rotation Sensor
        • Vision Sensor
        • Optical Sensor
        • Distance Sensor
        • Inertial Sensor (IMU)
        • 3-Wire Expander
    • V5 Brain Wiring Guide
    • Legacy
      • VEX Cortex
      • Power Expander
      • VEX Motor Controller
      • VEX Cortex Wiring Guide
  • General Electronics
    • General Topics
      • External Boards
        • ASUS Tinker Board S
        • Arduino
        • Beagleboard
        • Leaflabs Maple
        • LattePanda
        • Meadow F7 Micro
        • Netduino
        • ODROID-XU4
        • Pandaboard
        • Raspberry Pi
      • Analog-Digital Converter (ADC)
      • Bit-Bang
      • GPIO
      • I2C
      • Jitter
      • Line Noise
      • List of Tools
      • Output Drive
      • Power Consumption
      • Radius Array
      • Resettable Fuse (PTC)
      • SPI
      • Slew Rate
      • Stalling
      • USART
      • UART
      • 5 Volt Tolerant
      • DC Motor Basics
Powered by GitBook
LogoLogo

This work is licensed under a Attribution-ShareAlike 2.0 Generic License

On this page
  • Concept
  • Tuning Terminology
  • Tuning PID
  • Examples
  • Refinements
  • SIGBots Usage
  • Teams Contributed to this Article:

Was this helpful?

Edit on GitHub
Export as PDF
  1. Software
  2. Control Algorithms

PID Controller

A bang-bang control only uses the current value, a PID control schemes also may utilize information about the past values and predicted future values to optimize control

PreviousBang BangNextBasic Pure Pursuit

Last updated 5 months ago

Was this helpful?

Concept

In Robotics:

In terms of robotics, this would help robots stop gradually as it reaches a point, instead of having it stop moving its motors or breaking hard as it reaches its target. This is necessary as robots have momentum that can only be gradually slowed down to a halt to maintain accuracy, instead of using a algorithm that does not account for this.

Real World Usage:

Consider a kitchen stove which is to be heated to 350 degrees Fahrenheit. Initially, the stove begins at room temperature, hopefully less than 100 F, and so must obviously become warmer. control might naively try to turn the stove heat fully on if the temperature is less than 345 F and turn it fully off if the temperature is greater than 355 F. While this might appear to keep the temperature within a +/- 5 degree range of the target, it is entirely possible that the temperature could exceed these bounds, as turning the heating coils "off" will probably still contribute a fair amount of heat to the stove temperature while the coils cool down, leading to wild temperature oscillations.

One might try to improve this scheme by adding additional with the oven elements on at some partial amount, but this is difficult to maintain and adapt. A more intelligent scheme will use the other information that every analog sensor tells a smart controller:

  • History - Analog sensors have past values that provide a story of how the system reacted to previous changes (the integral term). This is generally determined by adding input values to a running total.

  • Future - The current rate of change of an analog sensor is often useful in predicting the next few values (the derivative term). This is generally computed by finding the difference between the last two input values.

In the kitchen oven case, derivative control would be important to prevent oscillating around the target value; the heater can be turned down before the target temperature is reached when derivative control predicts that the temperature will reach the desired set-point soon. Likewise, if the temperature is not rising as quickly as it should, perhaps because the door is open, the heater temperature will increase as the offsets from the set-point accumulate in the integral term.

Tuning Terminology

For any PID controller, the exact impact of the three terms (proportional, integral, and derivative) must be assessed for each implementation. Constant choice must balance several factors:

  • Settling time - The mechanism will sometimes oscillate (go back and forth) one or more times around the target before it finally stops.

  • Overshoot - The distance that a mechanism exceeds over the target value.

  • Steady-state error - The distance between the robot and the target after stopping, which is different from overshoot as it may be a value under the target value.

    • This is more common on a drive train, while overshoot is more common on other mechanisms controlled by PID.

  • Rise time - The speed at which the mechanism reaches its maximum velocity.

Some of these values might not be used if the values that come before it make the controller smooth enough. Most of the time spent coding PID will most likely not be spent on the algorithm itself, but rather refining these constants to reduce the behavior described above.

So practically speaking, how do we modify each of the terms when observing how the robot moves?

Tuning PID

It must be noted that while these are not completely accurate descriptions of what each of the variables do, this is a very surface level way to abstract a subsystem's movement and its relation to each variable. Note that in other articles, P, I, and D may be referred to as kP, kI, and kD as the k simply indicates that the variables are intended to be constants.

  • The Proportional (P) Value:

    • Assuming we have our PID algorithm written properly, we first start with increasing P which we can define as how aggressively we want the motors to slow down as it gets to the target area, and also speed up on release. For example, if the robot does not slow down fast enough we may want to lower P, but if it slows down too quickly we may want to increase P. There may be some instances where P may be enough for a certain movement.

    • Sometimes the robot might not be consistent enough on just P as the steady state error may be too high, the overshoot/settling time may be too high, but that is where the other constants are useful.

    • In a PID loop, the P values will always be non-zero.

  • The Derivative (D) Value:

    • Increasing D helps to the slow down the robot as it approaches its final destination. Note that this is in complete contrast to the integral constant (I) and should be used if the robot is overshooting near the end, or to make control of the robot in its final position more consistent.

    • For example, if the robot slows down too much when trying to get to its target position, increasing D may help compensate it. This is a symptom that may occur if P is decreased too much in an effort to prevent overshooting.

  • The Integral (I) Value:

    • Increasing I helps to speed up the robot slightly faster it reaches its final destination, and to some degree counteracts D. It's useful if the robot has a slow settling time as it speeds up the rate at which it approaches the target value.

    • This might seem counterintuitive to use if we are using D as well, but it might be beneficial to use all 3 constants to create optimal movement that is both fast and consistent.

    • For example, if the robot slows down too much when trying to get to its target position, increasing I may help compensate it.

  • General Tuning Tips:

    • Be sure to only adjust one constant at a time between testing.

    • For drives it is common practice to start with tuning first P, then D, and finally I.

    • For other mechanisms that may be velocity controlled, first start with P, then I, and then D.

    • Remember, you may not use all three constants. Sometimes a P, PD, or PI combination may be enough to achieve quick and consistent motion.

Examples

Purdue SIGbots made a basic implementation of PID in PROS below.

Refinements

Naive PID controllers can suffer from integral windup if some external condition prevents the setpoint from being reached for an extended period of time, causing a huge value to load into the integral term and leading to undesired behavior. For example, if a PID controller is supposed to raise a robot arm, and the robot is disabled for ten minutes, integral windup would accumulate and cause wild oscillations. While the most intuitive method would be to only use the last N input values, a more memory-efficient method is to multiply the integral term by a constant slightly less than one before each accumulation, so the controller "forgets" old integrated error values.

For even better PID control, if some other information about the process is known that allows more accurate estimation of future values, that equation can be substituted for the derivative term. In the stove example mentioned above, one might install a second sensor to measure the room temperature and use Newton's Law of Cooling to estimate the rate at which heat would be lost to the environment based on the current temperature.

SIGBots Usage

Teams Contributed to this Article:

Check out this for generalized PID code made in RobotC.

George Gillard has a explaining the concepts with example code.

PID controllers have been used by Purdue SIGBots for almost every lift mechanism and even the CC/CV algorithm. A detailed understanding of this control structure is thus invaluable to writing a consistent autonomous routine and a driver-friendly operator control. It remains a fundemental part of , a PROS template written by BLRS software developers.

(Purdue SIGBots)

💻
bang-bang
Bang-bang
intermediate states
VEX Forum thread
paper
ARMS
BLRS
https://github.com/purduesigbots/PIDLib/blob/main/PIDlib_project/src/pid/pidController.c
#include "../../include/pid/pidController.h"

void pid_init(pid_Controller *pid, double kp, double ki, double kd) {
    pid->kp = kp;
    pid->ki = ki;
    pid->kd = kd;
    pid->prevError = 0;
    pid->integral = 0;
}

double pid_update(pid_Controller *pid, double setpint, double processVariable) {
    // Calculate error
    double error = pid->setpoint - processVariable;

    // Calculate proportional term
    double proportional = pid->kp * error;

    // Calculate integral term
    pid->integral += pid->ki * error;

    // Calculate derivative term
    double derivative = pid->kd * (error - pid->prevError);

    // Calculate PID output
    double output = proportional + pid->integral + derivative;

    // Update previous error
    pid->prevError = error;

    return output;
}

double pid_getKp(pid_Controller *pid) {
    return pid->kp;
}

double pid_getKi(pid_Controller *pid) {
    return pid->ki;
}

double pid_getKd(pid_Controller *pid) {
    return pid->kd;
}

void pid_setKp(pid_Controller *pid, double newKp) {
    pid->kp = newKp;
}

void pid_setKi(pid_Controller *pid, double newKi) {
    pid->ki = newKi;
}

void pid_setKd(pid_Controller *pid, double newKd) {
    pid->kd = newKd;
}