Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create a PIDController Class #6

Open
theDataSmith opened this issue Dec 12, 2016 · 0 comments
Open

Create a PIDController Class #6

theDataSmith opened this issue Dec 12, 2016 · 0 comments

Comments

@theDataSmith
Copy link
Contributor

theDataSmith commented Dec 12, 2016

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()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants