1
\$\begingroup\$

I am implementing a PID controller on the Arduino, I transformed my transfer function from the S domain to the Z domain and I require the sample time in seconds. This is how long it takes to get a sample from a position sensor.

This is my question:

I measured the execution time of the entire function with the millis() command. The is the time of the function execution 349uS. which is roughly 2865 samples per second. But the actual command overhead of AnalogRead() is 100uS so would my "true" sample time be 449us?

Thanks !

asked Apr 27, 2012 at 19:41
\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

The sample time is the time between calls of your function, not the time it takes your function to run. For example:

Loop start:

Analog read or other function calls

PID function call - Time 0

Analog write or PWM write function calls

Loop Restarts...

Analog read....

PID function call - Time 1

Analog write or PWM write...

Your sample time is the difference between Time 1 and Time 0.

answered Apr 27, 2012 at 20:17
\$\endgroup\$
4
  • \$\begingroup\$ Heres an example void loop() { //Analog Read //Process data // Average data // whatever call PID(); } The analogread is in a loop, so it cant read another "sample" until the other code in the loop executes. I was thinking this is the case or am I wrong? like if you have a timestamp before the Analogread() and a timestamp at the end of the loop and subtract thats the time it takes to get another sample? \$\endgroup\$ Commented Apr 27, 2012 at 23:06
  • \$\begingroup\$ You are right about the analog read, but not right about the second question. You want to measure the rate at which your loop executes. You can't necessarily get that by having a timestamp at the beginning and at the end. If the loop runs right away again then yes, it's okay, but you can't guarantee that. You need to measure exactly what you're looking for: the times at which your PID function executes. So the timestamp has to be in one place in the loop and to get the rate you subtract a later one from an earlier one. \$\endgroup\$ Commented Apr 27, 2012 at 23:50
  • \$\begingroup\$ Ah okay, I think I'm confuzzled now :( Thanks for your help though! Is there a tutorial or blog or info on this problem? Its kinda confusing lol \$\endgroup\$ Commented Apr 28, 2012 at 0:45
  • \$\begingroup\$ It's not too confusing: just read the time once in your loop and subtract that value from the time you read in the last loop. That's your sample time. \$\endgroup\$ Commented Apr 28, 2012 at 13:10

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.