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 !
1 Answer 1
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.
-
\$\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\$zacharoni16– zacharoni162012年04月27日 23:06:59 +00:00Commented 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\$AngryEE– AngryEE2012年04月27日 23:50:45 +00:00Commented 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\$zacharoni16– zacharoni162012年04月28日 00:45:32 +00:00Commented 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\$AngryEE– AngryEE2012年04月28日 13:10:24 +00:00Commented Apr 28, 2012 at 13:10