<PID_v1.h> myPID.Compute() function in IntervalTimer on Teensy 3.5

Status
Not open for further replies.

alisondmurray

New member
Hi everybody,

I am trying to use the library <PID_v1.h> in Teensy 3.5. In particular I would like to execute the myPID.Compute(); inside an IntervalTimer function at a frequency of 1 kHz. Inside the function I also increase a counter (motor_control_counter) that I print on the serial monitor every second. The timer seems not to respect the 1000ms.
Does anybody knows what is the maximum delay caused by the PID function?
Why Teensy 3.5 does not stop if the time interval is not respected?

Thank you very much in advance.


Code:
PHP:
void functionMotorControlTimer()
{
  unsigned long t_setpoint = millis() * 1000;
  float freq = 2.0f;
  float sin_setpoint=2.5f*sin(t_setpoint*2*pi*freq)+10.0f;
  Setpoint1=(double)(sin_setpoint);

  noInterrupts();
  Input1 = (double)(LC_1);
  interrupts();

  myPID1.Compute();

  float Output1b = (75.0f / 1865.0f) + 1865.0f - (float)(Output1);
  Output1b = round(Output1b);
  channel_1 = (uint16_t)Output1b;
  
  motor_control_counter++;
  motor_control_time=millis();
}
 
Execution time is rather not predictable, since it is probably influenced by other (hidden, system) interrupts with higher priority, then there is the cache hit or miss problem with "if-then" or for-loops in the compiled assembly, and finally the compiler optimisation type which has a huge impact on such details.

Out of that, are you sure that your PID library is optimized for ARM hardware? I know nothing about this specific lib, but I've already seen libraries with AVR (Arduino) specific code which forced additional compatibility functions of the Teensyduino core to be included in the assembly, slowing naturally down the execution.
 
I was dabbling with PID V1 a couple of years ago using an Arduino UNO with a standard 16MHZ clock. My notes said that PID.compute() took 80 usec. The default myPID.SetSampleTime () is 100 millisec. The PID V1 library was designed for the Arduino (UNO) platform, as I understand it.

I haven't used a Teensy version for comparison.
 
Hi everybody,

I am trying to use the library <PID_v1.h> in Teensy 3.5. In particular I would like to execute the myPID.Compute(); inside an IntervalTimer function at a frequency of 1 kHz. Inside the function I also increase a counter (motor_control_counter) that I print on the serial monitor every second. The timer seems not to respect the 1000ms.
Does anybody knows what is the maximum delay caused by the PID function?
Why Teensy 3.5 does not stop if the time interval is not respected?

Thank you very much in advance.


Code:
PHP:
void functionMotorControlTimer()
{
  unsigned long t_setpoint = millis() * 1000;
  float freq = 2.0f;
  float sin_setpoint=2.5f*sin(t_setpoint*2*pi*freq)+10.0f;
  Setpoint1=(double)(sin_setpoint);

  noInterrupts();
  Input1 = (double)(LC_1);
  interrupts();

  myPID1.Compute();

  float Output1b = (75.0f / 1865.0f) + 1865.0f - (float)(Output1);
  Output1b = round(Output1b);
  channel_1 = (uint16_t)Output1b;
  
  motor_control_counter++;
  motor_control_time=millis();
}

Hi alisondmurray,

I think you have copied and pasted my post from this link:

https://forum.pjrc.com/threads/5053...ute()-function-in-IntervalTimer-on-Teensy-3-5
 
As I recall, the code inside PID_v1 also checks millis() and does nothing if it's not time to do another sample. You'll probably need to delete that code from the PID lib if you want a hardware timer to control the PID timing.
 
Status
Not open for further replies.
Back
Top