-
Notifications
You must be signed in to change notification settings - Fork 6
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
Not getting default SampleTime after multiple compilations #3
Comments
Made another run trying to figure this out. I instrumented PID_v2.cpp to report the actual value of the private SampleTime member as follows: /* SetSampleTime(...) *********************************************************
* sets the period, in Milliseconds, at which the calculation is performed
******************************************************************************/
void PID::SetSampleTime(int NewSampleTime) {
if (NewSampleTime > 0) {
PIDSerial.printf("In SetSampleTime, SampleTime now %2.1f, Changing to %2.1f\n",
(double)NewSampleTime, (double)SampleTime);
double ratio = (double)NewSampleTime / (double)SampleTime;
ki *= ratio;
kd /= ratio;
SampleTime = (unsigned long)NewSampleTime;
}
}
using this Arduino code: void setup()
{
Serial.begin(115200);
#pragma region MPU6050
mySerial.printf("\nChecking for MPU6050 IMU at I2C Addr 0x%x\n", MPU6050_I2C_ADDR);
mpu.initialize();
// verify connection
Serial.println(mpu.testConnection() ? F("MPU6050 connection successful") : F("MPU6050 connection failed"));
float StartSec = 0; //used to time MPU6050 init
Serial.println(F("Initializing DMP..."));
devStatus = mpu.dmpInitialize();
// make sure it worked (returns 0 if successful)
if (devStatus == 0)
{
// turn on the DMP, now that it's ready
Serial.println(F("Enabling DMP..."));
mpu.setDMPEnabled(true);
// set our DMP Ready flag so the main loop() function knows it's okay to use it
Serial.println(F("DMP ready! Waiting for MPU6050 drift rate to settle..."));
dmpReady = true;
// get expected DMP packet size for later comparison
packetSize = mpu.dmpGetFIFOPacketSize();
bMPU6050Ready = true;
StartSec = millis() / 1000.f;
mySerial.printf("MPU6050 Ready at %2.2f Sec\n", StartSec);
}
else
{
// ERROR!
// 1 = initial memory load failed
// 2 = DMP configuration updates failed
// (if it's going to break, usually the code will be 1)
Serial.print(F("DMP Initialization failed (code "));
Serial.print(devStatus);
Serial.println(F(")"));
bMPU6050Ready = false;
}
#pragma endregion MPU6050
TurnRatePID.SetSampleTime(100);
mySerial.printf("TurnRatePID started with Kp/Ki/Kd = %2.1f,%2.1f,%2.1f, kp/ki/kd = = %2.2f,%2.3f,%2.2f, SampleTime = %lu\n",
TurnRatePID.GetKp(), TurnRatePID.GetKi(), TurnRatePID.GetKd(), TurnRatePID.Getkp(), TurnRatePID.Getki(), TurnRatePID.Getkd(), TurnRatePID.GetSampleTime());
And I got this output:
Looking at the output, it seems clear from the value of private member variable kd (I added access functions), it is getting scaled initially as if the SampleTime is the correct (100 mSec) value, even though the reported value is 30, not 100. However, after SetSampleTime() executes, it appears that the internal kd value gets divided by 0.1 (i.e. SampleTimeInSec) again - and this is very unexpected behavior. In addition, I cannot for the life of me figure out how the internal private member can be one number, but the retrieved value is something else. TIA, Frank |
Yeah, it's definitely weird. However I've noticed that in your initial post you're printing out In the followup though you're using controller's method |
I'm using PID_v2 in an Arduino application, and I get the following output:
From this code:
I expected the SampleTime to be 100 after the default PID constructor, but it isn't - it's still the same value I set later in the code. I have tried a full 'Clean' build, but get the same (unexpected) result.
What gives?
Frank
The text was updated successfully, but these errors were encountered: