Pi_flori_17
Posts: 7
Joined: Fri Oct 14, 2016 4:00 pm

Raspberry pi 3 : Accuracy of function millis() in C

Sun Jan 15, 2017 2:17 am

hello ,

i progammed a code for time measuremnts with a light barrier, where the endtime is displayed on a WIFI linked android smartphone.

For the time-measurement of start - and endtime i use the function millis() from the wiringpi library.

My Question is : How accurately is my measurement ?( I display the time in seconds with 3 decimal places.)

Does the function millis() of the library wiringpi Pi really use the Raspberr pi3 1,2 GHz clock signal ?
What is the frequency of the interrupt thats behind the function millis() in this case ?

Does millis() work for the raspberry pi 3 in other way because of the 1,2GHz Clock as it works for arduino unoR3 for instance with 16Mhz Clock ??

I would be very happy if someone could explain this to me .

Greetings from Austria

User avatar
Gert van Loo
Posts: 2487
Joined: Tue Aug 02, 2011 7:27 am
Contact: Website

Re: Raspberry pi 3 : Accuracy of function millis() in C

Sun Jan 15, 2017 8:05 am

The OS on the PI is not a real time operating system.
Therefore millis() will always be an estimate.
You just need a few interrupts more or none and the time will be off by a certain amount.
For accurate timing use a microprocessor and then one without OS.

jahboater
Posts: 5759
Joined: Wed Feb 04, 2015 6:38 pm
Location: West Dorset

Re: Raspberry pi 3 : Accuracy of function millis() in C

Sun Jan 15, 2017 10:46 am

Pi_flori_17 wrote: My Question is : How accurately is my measurement ?( I display the time in seconds with 3 decimal places.)

Does the function millis() of the library wiringpi Pi really use the Raspberr pi3 1,2 GHz clock signal ?
What is the frequency of the interrupt thats behind the function millis() in this case ?
Another source of time is the standard posix clock functions:

Code: Select all

  struct timespec now;
  clock_gettime( CLOCK_MONOTONIC_RAW, &now );
  return (uint64_t)now.tv_sec * 1000000000U + (uint64_t)now.tv_nsec;
There are a variety of clocks. CLOCK_REALTIME does what it says.
CLOCK_MONOTONIC_RAW I think is pretty close to the system clock.
CLOCK_MONOTONIC is similar but adjusted slightly all the time by NTP.
And others, see "man clock_gettime"

Code: Select all

CLOCK_MONOTONIC_RAW (since Linux 2.6.28; Linux-specific)
  Similar  to  CLOCK_MONOTONIC,  but  provides access to a raw hardware-based
  time that is not subject to NTP adjustments or the incremental  adjustments
  performed by adjtime(3).

User avatar
joan
Posts: 14936
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: Raspberry pi 3 : Accuracy of function millis() in C

Sun Jan 15, 2017 1:21 pm

It's not clear what you are timing. If the time start and time end are detected by the GPIO have a look at my pigpio library. It will allow you to get timings within 5 microseconds of the actual time interval.

User avatar
gordon@drogon.net
Posts: 2020
Joined: Tue Feb 07, 2012 2:14 pm
Location: Devon, UK
Contact: Website

Re: Raspberry pi 3 : Accuracy of function millis() in C

Sun Jan 15, 2017 11:03 pm

Pi_flori_17 wrote:hello ,

i progammed a code for time measuremnts with a light barrier, where the endtime is displayed on a WIFI linked android smartphone.

For the time-measurement of start - and endtime i use the function millis() from the wiringpi library.

My Question is : How accurately is my measurement ?( I display the time in seconds with 3 decimal places.)

Does the function millis() of the library wiringpi Pi really use the Raspberr pi3 1,2 GHz clock signal ?
What is the frequency of the interrupt thats behind the function millis() in this case ?

Does millis() work for the raspberry pi 3 in other way because of the 1,2GHz Clock as it works for arduino unoR3 for instance with 16Mhz Clock ??

I would be very happy if someone could explain this to me .

Greetings from Austria
Of-course you could always read the source code. tl;dr the processor clock 1.2Ghz, or whatever has no effect.

The answer is the same as all delay functions under Unix/Linux... the time will be no shorter than actually happens, but may (and very probably will) be longer.

Since you're too lazy to read the source code, I'll tell you. wiringPiSetup*() initialises a "timer" to the current value of gettimeofday(). This is the epoch or start of time as far as wiringPi is concerned. The millis() function uses gettimeofday() to read the time at that instant, then subtracts it from the epoch and returns that value as an unsigned 32-bit integer. Due to Linux scheduling, leap seconds, etc. the time will very probably be longer. I should probably use clock_gettime (.._raw), but it's taken from code I wrote 20 years ago and never bothered to change it.

See also this: https://projects.drogon.net/and-on-the- ... t-stopped/

-Gordon
--
Gordons projects: https://projects.drogon.net/

1dot0
Posts: 430
Joined: Mon Nov 28, 2016 12:31 pm

Re: Raspberry pi 3 : Accuracy of function millis() in C

Mon Jan 16, 2017 8:11 am

Although the uint 32 bit overflow issue about millis() is known to me from Arduinos (AVR and ARM as well) - but for the Pi perhaps it will not be quite as simple as to re-define all time and millis() using variables just as " long long " (uint 64bit int) ? :geek:

User avatar
gordon@drogon.net
Posts: 2020
Joined: Tue Feb 07, 2012 2:14 pm
Location: Devon, UK
Contact: Website

Re: Raspberry pi 3 : Accuracy of function millis() in C

Mon Jan 16, 2017 8:23 am

1dot0 wrote:Although the uint 32 bit overflow issue about millis() is known to me from Arduinos (AVR and ARM as well) - but for the Pi perhaps it will not be quite as simple as to re-define all time and millis() using variables just as " long long " (uint 64bit int) ? :geek:
Left as an exercise to the user...

It would be far better to cope with timer wrap as you're just putting the problem off until a 64-bit millisecond timer wraps - which admittedly is just over half a billion years away, however its good to be ready.

-Gordon
--
Gordons projects: https://projects.drogon.net/

1dot0
Posts: 430
Joined: Mon Nov 28, 2016 12:31 pm

Re: Raspberry pi 3 : Accuracy of function millis() in C

Mon Jan 16, 2017 8:44 am

as millis() both in the wiringPi libs and in the related user code had to be redefined then probably - just for the survival of millis() and for the easiest way for backwards-compatibility the most simple wrap surely would do, even if it's just until in the year 500000025, if man is still alive :ugeek:

jahboater
Posts: 5759
Joined: Wed Feb 04, 2015 6:38 pm
Location: West Dorset

Re: Raspberry pi 3 : Accuracy of function millis() in C

Mon Jan 16, 2017 9:48 am

Since the question was about accuracy, a bigger problem is that the result from gettimeofday(), is affected by jumps in the system time (by someone setting the clock) and by NTP which seems to affect the result continuously by small amounts. It could in theory jump back in time which would really screw up your measurements!

Better would be to use a guaranteed monotonic clock such as clock_gettime( CLOCK_MONOTONIC ).

If you compare the times from CLOCK_MONOTONIC to CLOCK_MONOTONIC_RAW you can see the NTP adjustments all the time. CLOCK_MONOTONIC_RAW on the Pi just seems to update every microsecond whereas CLOCK_MONOTONIC makes irregular nanosecond changes because of the adjustments.

I think CLOCK_MONOTONIC starts from when the system boots, not the epoch, so any 32-bit time_t overflow (on the 32-bit Raspbian) will be delayed a few years :)

Note that POSIX.1-2008 marks gettimeofday() obsolete.

Also for interest, there is no "time" system call on the Pi, instead the library function time() calls clock_gettime and just returns the tv_sec field.

Pi_flori_17
Posts: 7
Joined: Fri Oct 14, 2016 4:00 pm

Re: Raspberry pi 3 : Accuracy of function millis() in C

Wed Jan 18, 2017 1:29 am

joan wrote:It's not clear what you are timing. If the time start and time end are detected by the GPIO have a look at my pigpio library. It will allow you to get timings within 5 microseconds of the actual time interval.

Hello Joan, thank you for the answer.

I am reading a digital pin, where the output signal of the light barrier is connected. The Input is HIGH when the light barrier has been crossed. I am asking for that signal with if ( input = HIGH) { " start_time = store the actual time, for instance your gpioTick() function " }

for the endtime i do the same and then i calculate the end time.
This whole proccess i working in a while loop that is active when the pi that runs as a server , has a new client connection.
When the end time is calculated , it gets sended to the client (android app)

I have not looked at the source code of gpioTick() so far , so i do not know on what time function it really depends ?
But i will look. I need a time base that is at least accurate on 1/100 second ? Do i achieve that witch the function gpioTick() ?

What if i have the following situation :

If i would youse gpioTick with your event callback function (So when Falling Edge for instance is detected) whould i it cause a big timing problem that makes the timing bad.

What is the advantage of gpioTick() on the contrary to millis() from wiringpi that is based on gettimeoftheday() ?

And i heard of a processor's free-running 1MHz timer on the older raspberry pis, so there must also be one on the Broadcom Chip BCM2837 of Pi3 .
Would the use of it achieve the accuratest timings ? Is this timer not effected of the linux proccesses ?

Sorry for that amount of questions, i am a little confused but i want to understand it and use the appropriatest functions in a correct way . I know i should just test alle the things but maybe you can help me with your experience .

Pi_flori_17
Posts: 7
Joined: Fri Oct 14, 2016 4:00 pm

Re: Raspberry pi 3 : Accuracy of function millis() in C

Wed Jan 18, 2017 1:31 am

gordon@drogon.net wrote:
Pi_flori_17 wrote:hello ,

i progammed a code for time measuremnts with a light barrier, where the endtime is displayed on a WIFI linked android smartphone.

For the time-measurement of start - and endtime i use the function millis() from the wiringpi library.

My Question is : How accurately is my measurement ?( I display the time in seconds with 3 decimal places.)

Does the function millis() of the library wiringpi Pi really use the Raspberr pi3 1,2 GHz clock signal ?
What is the frequency of the interrupt thats behind the function millis() in this case ?

Does millis() work for the raspberry pi 3 in other way because of the 1,2GHz Clock as it works for arduino unoR3 for instance with 16Mhz Clock ??

I would be very happy if someone could explain this to me .

Greetings from Austria
Of-course you could always read the source code. tl;dr the processor clock 1.2Ghz, or whatever has no effect.

The answer is the same as all delay functions under Unix/Linux... the time will be no shorter than actually happens, but may (and very probably will) be longer.

Since you're too lazy to read the source code, I'll tell you. wiringPiSetup*() initialises a "timer" to the current value of gettimeofday(). This is the epoch or start of time as far as wiringPi is concerned. The millis() function uses gettimeofday() to read the time at that instant, then subtracts it from the epoch and returns that value as an unsigned 32-bit integer. Due to Linux scheduling, leap seconds, etc. the time will very probably be longer. I should probably use clock_gettime (.._raw), but it's taken from code I wrote 20 years ago and never bothered to change it.

See also this: https://projects.drogon.net/and-on-the- ... t-stopped/

-Gordon

Dear Mr. Gordon ,

thank you for the detailed explanation.

jahboater
Posts: 5759
Joined: Wed Feb 04, 2015 6:38 pm
Location: West Dorset

Re: Raspberry pi 3 : Accuracy of function millis() in C

Wed Jan 18, 2017 9:25 am

Pi_flori_17 wrote:And i heard of a processor's free-running 1MHz timer on the older raspberry pis, so there must also be one on the Broadcom Chip BCM2837 of Pi3 .
Would the use of it achieve the accuratest timings ? Is this timer not effected of the linux proccesses ?
You will likely need root privilege to read that timer directly (I don't know why, on x86 rdtsc is freely available in user space). The standard library function clock_gettime/CLOCK_MONOTONIC, which is a system call into the kernel, gives portable, user access to some hi-res independent timer.

Why are you interested? You asked for 1/100 sec accuracy which any timer should give you (apart from clock updates to Time Of Day based clocks). If gpioTick gives you 5 microsecond accuracy that's 2000 times better than you need.

User avatar
joan
Posts: 14936
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: Raspberry pi 3 : Accuracy of function millis() in C

Wed Jan 18, 2017 10:01 am

Pi_flori_17 wrote: ...
I am reading a digital pin, where the output signal of the light barrier is connected. The Input is HIGH when the light barrier has been crossed. I am asking for that signal with if ( input = HIGH) { " start_time = store the actual time, for instance your gpioTick() function " }

for the endtime i do the same and then i calculate the end time.
...
To get an accurate time you need to accurately know the start and end times.

If both the start time AND the end time cause a GPIO edge you get an accurate time (doesn't have to be the same GPIO). pigpio automatically time-stamps all GPIO level transitions against the free running microsecond clock. You must use callbacks to get the time. Python is as accurate as C as far as this is concerned. See http://abyz.co.uk/rpi/pigpio/examples.h ... monitor_py for a Python example.

Pi_flori_17
Posts: 7
Joined: Fri Oct 14, 2016 4:00 pm

Re: Raspberry pi 3 : Accuracy of function millis() in C

Sat Jan 21, 2017 7:06 pm

joan wrote:
Pi_flori_17 wrote: ...
I am reading a digital pin, where the output signal of the light barrier is connected. The Input is HIGH when the light barrier has been crossed. I am asking for that signal with if ( input = HIGH) { " start_time = store the actual time, for instance your gpioTick() function " }

for the endtime i do the same and then i calculate the end time.
...
To get an accurate time you need to accurately know the start and end times.

If both the start time AND the end time cause a GPIO edge you get an accurate time (doesn't have to be the same GPIO). pigpio automatically time-stamps all GPIO level transitions against the free running microsecond clock. You must use callbacks to get the time. Python is as accurate as C as far as this is concerned. See http://abyz.co.uk/rpi/pigpio/examples.h ... monitor_py for a Python example.
Thank you again,

but i have one last question to you please. Does systReg[SYST_CLO] of your gpioTick() access the 1MHz Hardware Timer of the RPi ? It would be very interesting to know what the real timebase of this function is ...

User avatar
joan
Posts: 14936
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: Raspberry pi 3 : Accuracy of function millis() in C

Sat Jan 21, 2017 8:04 pm

Pi_flori_17 wrote: ...
but i have one last question to you please. Does systReg[SYST_CLO] of your gpioTick() access the 1MHz Hardware Timer of the RPi ? It would be very interesting to know what the real timebase of this function is ...
Yes, that is correct.

May I point out that gpioTick() it is of little use for very accurate timing purposes. If you use it to time a GPIO interrupt there is an unknown period of time between when the interrupt happened and when you get a chance to call gpioTick(). That time is likely to be circa 50µs but may occasionally be circa 50ms.

Similar considerations apply if you use it to time the start and end of two events. The process may have been suspended between the calls.

Summary, for accurate timing use callbacks with gpioSetAlertFunc().

1dot0
Posts: 430
Joined: Mon Nov 28, 2016 12:31 pm

Re: Raspberry pi 3 : Accuracy of function millis() in C

Mon Jan 23, 2017 5:53 pm

hello,
although I am not the original poster... but reading through this topic I am now very curious which actually is the - short - answer to the question:

how to measure the time (e.g., between 2 GPIO pin states) most accurately by which command exactly (if not by millis() ) ?

User avatar
joan
Posts: 14936
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: Raspberry pi 3 : Accuracy of function millis() in C

Mon Jan 23, 2017 6:03 pm

1dot0 wrote:hello,
although I am not the original poster... but reading through this topic I am now very curious which actually is the - short - answer to the question:

how to measure the time (e.g., between 2 GPIO pin states) most accurately by which command exactly (if not by millis() ) ?
Between two GPIO level changes? pigpio callbacks (I am the author so take that with a pinch of salt). Provided that the level change is greater than the sample period (default 5µs).

The only alternative (which will be more accurate on occasion) is busy spins.

1dot0
Posts: 430
Joined: Mon Nov 28, 2016 12:31 pm

Re: Raspberry pi 3 : Accuracy of function millis() in C

Mon Jan 23, 2017 8:21 pm

well, tbh, I still don't get it -
So how would the real source code actually look like?

User avatar
joan
Posts: 14936
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: Raspberry pi 3 : Accuracy of function millis() in C

Mon Jan 23, 2017 8:40 pm

1dot0 wrote:well, tbh, I still don't get it -
So how would the real source code actually look like?
See http://abyz.co.uk/rpi/pigpio/examples.h ... monitor_py
You are quite right, it is not simple, it is like a crossword, easy once you know the answer.

1dot0
Posts: 430
Joined: Mon Nov 28, 2016 12:31 pm

Re: Raspberry pi 3 : Accuracy of function millis() in C

Mon Jan 23, 2017 8:51 pm

hmm - Python code? Is this a riddle about finding a needle in a hay stack? :shock:
is there no C code which can simply be posted? :?

User avatar
joan
Posts: 14936
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: Raspberry pi 3 : Accuracy of function millis() in C

Tue Jan 24, 2017 2:29 am

1dot0 wrote:hmm - Python code? Is this a riddle about finding a needle in a hay stack? :shock:
is there no C code which can simply be posted? :?
http://abyz.co.uk/rpi/pigpio/examples.html

1dot0
Posts: 430
Joined: Mon Nov 28, 2016 12:31 pm

Re: Raspberry pi 3 : Accuracy of function millis() in C

Tue Jan 24, 2017 8:10 am

actually I expected not the entire pigpio API example repository but just a short explanation about a special API function like e.g.,

unsigned long t0, t1, dt;
// get start time
t0 = millis();
// do something
// then get final time
t1 = millis();
// then compute elapsed time
dt = t1-t0;


(of course with YOUR time polling function, instead of millis() ).
as I don't think that my question was so misleading, I guess you're just not willing to explain it, regrettably.

Return to “C/C++”