Intro to FRC Programming - Romi
  • INTRODUCTION
    • Overview
    • Important Links
  • 💽Setup
    • Setting up the Romi's Software
    • Downloading Essential Tools
    • APCS vs FRC
  • How To Code in VSC
    • VSC- Intro
    • A Tour Through VSC
    • Creating a Regular Java Project
  • Intro to Java
    • What is Java?
    • Beginning Steps
    • 🟩Fundimentals of Java
      • Variables
      • Operations
      • Methods
      • Comments
      • If Statements and Conditions
      • Boolean Zen
      • Loops
      • Challenge- Create a Box House
    • 🟨Advanced Concepts
      • Objects
      • Scanners
      • Null Objects
      • Arrays
      • Errors
      • For-Each Loops
    • 🟥Object Oriented Programming
      • Basics of OOP
      • Instance Classes
      • Static Classes
  • 🕑Romi Curriculum- Timed Base
    • Romi Curriculum- Introduction
    • Creating a WPILIB Project
    • Running Your Code
    • The Robot Class
    • Subsystems
      • RomiDrivetrain
    • Cool stuff i will rename this category later
      • Spark Motors
      • PIDs
      • External Controllers
      • Encoders
  • 🖥️Romi Curriculum- Command Based
    • Command Based Code
    • RobotContainer
    • Commands
    • CommandScheduler
  • UNRELATED IMPORTANT CODING KNOWLEDGE
    • Constants
  • SAMPLE CODE
    • Tank Drive Example
      • RobotContainer
      • TankDriveSubsystem
      • MoveRobotCommand
    • Worktop Systems Sample Java Code
      • Belt Elevator Sample Code
      • Rotating Arm Sample Code
Powered by GitBook
On this page
  • Creating PIDController Objects
  • Using PIDController Objects
  • Tuning a PIDController Object
  1. Romi Curriculum- Timed Base
  2. Cool stuff i will rename this category later

PIDs

PreviousSpark MotorsNextExternal Controllers

Last updated 1 year ago

Creating PIDController Objects

PIDs are ways to make a robot change its speed based on how far it is from its destination. PID controllers are created as PIDController objects.

Parameters:

  • 3 Integers(ints)-> It's kP, kI, and kD.

kP, kI, and kD are 3 variables that can alter the way a PID acts. The graph below is the ideal way a PID should move the robot, but in reality, the line would oscillate around the destination or never reach the destination.

Now let's talk bout what each of the parameters does in the object:

  • Kp: The "P" stands for proportional and is the main parameter that creates the main and determines the speed/slope of the robot. The problem with this component is that it causes a lot of oscillation due to the momentum from moving a physical robot causing it not to stop short of the diestination.

  • Ki: The "I" stands for integral and helps add extra acceleration when the Kp isn't nearing the destination, but not reaching it. However, I would suggest leaving this value at 0 since it causes a lot of inaccuracies and if you get your Kp and Ki right, you won't need the Ki anyway.

  • Kd: Helps counter the momentum caused by the Kp parameter applying negative acceleration as the robot gets closer to the destination.

Here is a video that summarizes how the parameters affect the graph and why


Using PIDController Objects

To use a PID, you have to use the pid.calculate() method. This method has 2 parameters, the current point, and the desired point. You can get the current point from Encoder objects. This method returns a double that will be used as a speed value that can be set to Spark motors. This is an example with our PIDController object being named pid and our Encoder object being named encoder:

// will precisely move the robot 12 units using PID
double speed = pid.calculate(encoder.getDistance(), 12)

Due to the PIDCOntroller object using encoder values, remember to reset your encoder values often to avoid problems


Tuning a PIDController Object

PIDs have different parameter values for everything, because of this, there is a process called tuning a PID. To do this you have to continuously test the motor with different parameters until one of the parameter sets creates accurate results. For our purposes, we will change the go distance function to learn tuning

Conditions:

Here is the answer code (don't worry about the PIDController object parameters for now).

Answer code
public void goDistance(double distance){
    double pid.caluclate(m_rightEncoder.getDistance(), distance);
    go(speed,speed);
    System.out.println(m_rightEncoder.getDistance());
}

Now, if you haven't already, fill in the parameters of Ki only in the PIDController and call that method into the autonomous period autonomousPeriodic() method in the Robot class and run the program with the distance being 12 (inches). You should notice the robot, not going even close to the right distance. Now tune the kP value until it is about 0.001 inches away from 12 (inches) and not occilating. Here are some tips for tuning the PID's kP component.

  • If the distance is undershot, increase the kP

  • If the distance is overshot, decrease the kP

  • The kP values should be small (probably less than 1)

  • Use the distances printed out to the console

After you get a good distance. I would suggest trying to make the accuracy even better using the kD component.

🕑
Drawing