Skip to content

Create a PIDController Class #6

@theDataSmith

Description

@theDataSmith

Create a class that functions as a PID controller. Its constructor should take 3 arguments, one for each constant: P, I, and D. It should have a function, getCorrection(), that takes an error as an argument, and returns a correction. Here is more specifically what the function needs to do, not necessarily in this order:

  1. Calculate deltaTime (the time since the last getCorrection() was called)
  2. Add error * deltaTime to a variable (like errorSum), used to calculate I
  3. Calculate the proportional error: P * error
  4. Calculate the integral error: I * errorSum
  5. Calculate the derivative error: D * (error - previousError) / deltaTime
  6. Cache the current error as previousError for the next time getCorrection() is called
  7. Return the sum of the three errors

Since deltaTime and the derivative correction both need some information from the previous call to getCorrection(), it would be a good idea to include some kind of start() function, that doesn't return a correction, but does cache the time and an initial error.

Needed Arduino functions:
micros()

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions