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

Allow to set resolution and to compute every time the function is called #9

Open
wants to merge 6 commits into
base: master
Choose a base branch
from

Conversation

Lauszus
Copy link

@Lauszus Lauszus commented Jul 19, 2013

First of all. Thanks for a great library. I just started to use it for my balancing robot: https://github.com/TKJElectronics/Balanduino/tree/dev/Firmware/Balanduino, but I wanted to change it a bit for my needs.

This is the changes I have done:

  • I added the function SetResolution, this will allow the user to set the resolution to either milliseconds or microseconds.
  • By calling SetSampleTime with 0. Compute will calculate the angle every time it is called. This is needed for my robot as I want to update the PID loop as fast as possible.

Also do you mind if I fix the indent of the source code?

if (SampleTime > 0)
ratio = (double)NewSampleTime/(double)SampleTime;
else
ratio = (double)NewSampleTime/(double)timeChange; // We will assume the user is calling Compute at a regular interval
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is needed to prevent division with 0.

@enjoyneering
Copy link

if you want to get consistent behavior from the PID, you cannot calculate every time the function is called

see Improving the Beginner’s PID – Sample Time

@MiloMindbender
Copy link

Not sure if this is the issue lauszus is trying to solve, but I had a similar one with my balancing robot. The robot needs to sample an imu and run the pid every 10ms. The main loop is already setup to run at that rate. If you let the pid decide when to run using its internal timer it will get out of sync with the main loop and not always run. So I changed the pid to run every time it is called, set time interval to 10ms and let the main loop sample the imu and run the pid every 10 ms

@enjoyneering
Copy link

enjoyneering commented Jan 14, 2019

did you try set time smaller than 10ms?
setSampleTime(9);

In this case, the PID is synchronized.
unsigned long timeChange = (now - lastTime); if(timeChange>=SampleTime) ......

@MiloMindbender
Copy link

@enjoyneering actually that doesn't work, the loop will execute every 10ms so the PID will always run, but the time constants INSIDE the PID will be set to 9ms so the calculations will be off.

@enjoyneering
Copy link

enjoyneering commented Jan 16, 2019

I agree it will be off, because the sample time is hard-coded to Ki & Kd gains

kp = Kp; ki = Ki * SampleTimeInSec; kd = Kd / SampleTimeInSec;

so in your case you still have to set up sample time to 10

setSampleTime(10);

and then bypass sample time check in the "compute()" function. Am I right?

drf5n pushed a commit to drf5n/Arduino-PID-Library that referenced this pull request Mar 21, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants