shamakh96
Posts: 9
Joined: Wed Sep 02, 2015 11:57 am

nanosecond

Wed Sep 09, 2015 6:00 pm

hi everybody , i was trying to calculate time by nanosecond .. on raspberry pi (Python)

i heard about a module (cTimer) for python that can measure nanosecond , but i don't know how to install it on raspberry

i did :

sudo apt-get install cTimer

it says : unable to locate package cTimer


what should i do?

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

Re: nanosecond

Wed Sep 09, 2015 6:03 pm

For what purpose? Python can't time anything with anything approaching that accuracy.

Heater
Posts: 16318
Joined: Tue Jul 17, 2012 3:02 pm

Re: nanosecond

Wed Sep 09, 2015 6:25 pm

There is a world of difference between having a time given to a nano second resolution and actually having a nanosecond accuracy of the time measurement.

It is quite possible that a system has a counter that is incremented at 1 Giga Hertz. Once per nanosecond. Perhaps the Pi does, I have no idea.

But now, how many instructions of the CPU does it take to read that counter and the event you are trying to time and do anything useful with it?

Try writing the simplest possible loop in Python that reads such a counter records the resulting time differences and prints the results after the timing loop is done. I'm sure you will see a resolution hundreds or thousands of times less than 1ns.

Doing the same in C might get you closer. Or assembler.

But then, the OS can always take control at any time to do something else and mess with your results. Unless you arrange for your timing process to have a high priority.


What actually is it you are trying to time?
Memory in C++ is a leaky abstraction .

shamakh96
Posts: 9
Joined: Wed Sep 02, 2015 11:57 am

Re: nanosecond

Sat Sep 12, 2015 12:22 am

Heater wrote:There is a world of difference between having a time given to a nano second resolution and actually having a nanosecond accuracy of the time measurement.

It is quite possible that a system has a counter that is incremented at 1 Giga Hertz. Once per nanosecond. Perhaps the Pi does, I have no idea.

But now, how many instructions of the CPU does it take to read that counter and the event you are trying to time and do anything useful with it?

Try writing the simplest possible loop in Python that reads such a counter records the resulting time differences and prints the results after the timing loop is done. I'm sure you will see a resolution hundreds or thousands of times less than 1ns.

Doing the same in C might get you closer. Or assembler.

But then, the OS can always take control at any time to do something else and mess with your results. Unless you arrange for your timing process to have a high priority.


What actually is it you are trying to time?

the speed of light in my home :D

i was working with the module (datetime) , but i notice that it's accuracy is the microsecond (6 digits after the dot)

the smallest code i could write :D

from datetime import datetime
print ( datetime.now() - datetime.now() )

it took about 0.000100 s , more or less

Heater
Posts: 16318
Joined: Tue Jul 17, 2012 3:02 pm

Re: nanosecond

Sat Sep 12, 2015 12:54 am

Interesting. What is you hardware set up for generating and detecting the light?
Memory in C++ is a leaky abstraction .

shamakh96
Posts: 9
Joined: Wed Sep 02, 2015 11:57 am

Re: nanosecond

Fri Sep 18, 2015 5:18 am

Heater wrote:Interesting. What is you hardware set up for generating and detecting the light?

perhaps using an ordinary laser (works by a relay) .. pointed exactly on a LDR (detect the change of light)

Heater
Posts: 16318
Joined: Tue Jul 17, 2012 3:02 pm

Re: nanosecond

Fri Sep 18, 2015 5:54 am

shamakh96,

Do you have any idea of what the speed of light is? Let's just say it is fast.

300,000,000 meters per second. Or about one foot per nanosecond.

An LDR has a very slow response time. 100ms as far as I can tell. See here: http://electronicsarea.com/photoresistor-ldr/

So, in the time it takes the LDR to turn on or off light will have moved 30,000,000 meters! You won't get any meaningful measurement with that. Forget about relays. You are going to need some fast laser pulses and fast detectors.

Here is one way to do it that is about the simplest I have ever seen: http://arxiv.org/pdf/0705.3996.pdf
Memory in C++ is a leaky abstraction .

User avatar
mntmst
Posts: 57
Joined: Thu Sep 27, 2012 11:26 pm
Location: Fairview, Oregon

Re: nanosecond

Fri Sep 18, 2015 6:00 am

Measuring the time of flight for light requires sub-nanosecond timing (impossible with an RPi directly) for under a meter resolution. I've made several devices for measuring the length and characteristics of light-links using the PIC micro-controller CTMU module for about 0.1 nanosecond resolution with the 12bit ADC on the uC. The device has an external SPI interface for triggers and data accusation that the RPi could access. The experimental device in the link (pretty cool stuff) above could be used to with it to measure the speed of light with the correct calibration.

Image
Prototype device.
Image
Time delay at the light-link transceivers (50Mbps) with 6 inch jumper used to calibrate the zero length reference.
The ADC count at 'zero' is ~48 with every additional count meaning a ~0.1 extra meter of cable length.

Some prototype hardware information and testing software for the board.
https://github.com/nsaspook/e220

shamakh96
Posts: 9
Joined: Wed Sep 02, 2015 11:57 am

Re: nanosecond

Fri Sep 18, 2015 6:11 am

Heater wrote:shamakh96,

Do you have any idea of what the speed of light is? Let's just say it is fast.

300,000,000 meters per second. Or about one foot per nanosecond.

An LDR has a very slow response time. 100ms as far as I can tell. See here: http://electronicsarea.com/photoresistor-ldr/

So, in the time it takes the LDR to turn on or off light will have moved 30,000,000 meters! You won't get any meaningful measurement with that. Forget about relays. You are going to need some fast laser pulses and fast detectors.

Here is one way to do it that is about the simplest I have ever seen: http://arxiv.org/pdf/0705.3996.pdf

:D :D , i'm sure i know the the speed of light , i just didn't know the low response of the LDR (i had a feeling :D )

but .. the light generator and detector wasn't my major problem , i will find others (i hope so :v)

the problem is the processor / controller that can handle that accuracy

i was thinking of using my laptop processor , is it applicable ?

User avatar
morphy_richards
Posts: 1603
Joined: Mon Mar 05, 2012 3:26 pm
Location: Epping Forest
Contact: Website

Re: nanosecond

Fri Sep 18, 2015 7:08 am

Why are you trying to measure the speed of light in your home ? I am asking out of curiosity.
Eg. Have you significantly bent spacetime? Moved to another universe with different c constant ?

BMS Doug
Posts: 3823
Joined: Thu Mar 27, 2014 2:42 pm
Location: London, UK

Re: nanosecond

Fri Sep 18, 2015 8:05 am

morphy_richards wrote:Moved to another universe with different c constant ?
How can we tell unless we keep measuring the speed of light to see if it has changed?
Doug.
Building Management Systems Engineer.

scotty101
Posts: 4013
Joined: Fri Jun 08, 2012 6:03 pm

Re: nanosecond

Fri Sep 18, 2015 9:10 am

A computer is not going to be able to do this alone.

You need some high speed electronics (read as very expensive) to be able to capture something at that level of precision.
Electronic and Computer Engineer
Pi Interests: Home Automation, IOT, Python and Tkinter

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

Re: nanosecond

Fri Sep 18, 2015 10:27 am


BMS Doug
Posts: 3823
Joined: Thu Mar 27, 2014 2:42 pm
Location: London, UK

Re: nanosecond

Fri Sep 18, 2015 10:33 am

joan wrote:...or take a different approach.

http://www.pas.rochester.edu/~pavone/pa ... ration.htm
That is very cool.
Doug.
Building Management Systems Engineer.

Heater
Posts: 16318
Joined: Tue Jul 17, 2012 3:02 pm

Re: nanosecond

Fri Sep 18, 2015 11:02 am

The rotating mirror, Foucault, method is cool. We did that back in school. I worry about people lashing mirrors to motors that rotate at 30000rpm and what a bad experience that can lead to.

Fizeau's method is a bit safer as things spin more slowly.

But I to have always been intrigued by the idea of the direct time of flight method. We have plenty of fast electronics required to do it. As shown in the article I linked to above. I don't think that need be so expensive.

Using a micro-controller to generate the transmitted signal is easy. No so sure about counting the time of flight though.
Last edited by Heater on Fri Sep 18, 2015 4:27 pm, edited 1 time in total.
Memory in C++ is a leaky abstraction .

User avatar
mntmst
Posts: 57
Joined: Thu Sep 27, 2012 11:26 pm
Location: Fairview, Oregon

Re: nanosecond

Fri Sep 18, 2015 4:23 pm

Heater wrote:.

Using a micro-controller to generate the transmitted signal is easy. No so sure about counting the time of flight though.
You can measure time as the rate of change of an electrical parameter in a circuit. The http://ww1.microchip.com/downloads/en/A ... 01375a.pdf CTMU device module can measure time by the voltage on a capacitor that's charged from a constant current source. (resulting in a linear ramp of voltage over time)
It's only good to about 1 foot resolution in a measurement of several thousand feet so measuring possible changes in the speed of light over time as the universe changes might be a problem. :lol:


The CTMU is a constant current source that can be turned on and off in less than 1 nanosecond (ns). This current can be converted to a time-dependent voltage with the addition of a capacitor, using the following standard equation:
I = C(dV/dT)
Solving for dT and integrating
T = (C/I)V

I is the output current (typical value ~0.55uA) of the CTMU current source, C is the input capacitance (typical less than 4 pF if the ADC input Mux is disabled) of the ADC plus any stray capacitance, and V is measured by the ADC. This results in the ability to calculate what T is.
Image
The current output is connected to the 12-bit ADC. So, we have a current source that can be turned on and off in much less than 1 ns and an ADC with a fixed capacitance for an input. The analog-to-digital input is charged for an unknown amount of time, after which a voltage measurement is made using the ADC. Knowing C and I, and measuring V, we can calculate T. We can then discharge the capacitor with another switch and restart the process.
https://www.flickr.com/photos/nsaspook/ ... 477730187/
Typical waveform generated by the CTMU as Capacitance is varied but Time is constant. If we have constant Capacitance and I (current) and vary Time we get a ramp voltage to calculate the value of T.

The first pulse (light transmit pulse) closes the switch, starts the charging of the analog-to-digital input capacitor and creates a linear voltage ramp The second pulse (light receiver) opens the switch and stops charging the analog-to-digital input capacitor. The voltage can now be measured and the time calculated.

Heater
Posts: 16318
Joined: Tue Jul 17, 2012 3:02 pm

Re: nanosecond

Fri Sep 18, 2015 4:43 pm

mntmst,

I like it. Now, assuming I have pulses at 1MHz on my light souce I will get 1MHz back on my receiver. With a phase delay dependent on the time of flight of light. How can we tweak that CTMU set up to make a continuous phase measurement and hence speed of light measurement?

That is a brilliant app note:

"Really Complex Applications

47. Solving World Hunger
By mass deploying inexpensive temperature (# 28)
and humidity sensors (# 10), it becomes possible to
make continuous, fine resolution measurements of
climatic variation over large agricultural areas. This
makes it possible, at least in theory, to create a
closed-loop system of more precise water and nutrient
delivery. This, in turn, can push crop yields to their maximum.
Do this in enough places, and there will be
enough food to feed everyone, everywhere. (Note that
it was never said that this would be easy.)"
Memory in C++ is a leaky abstraction .

User avatar
mntmst
Posts: 57
Joined: Thu Sep 27, 2012 11:26 pm
Location: Fairview, Oregon

Re: nanosecond

Sat Sep 19, 2015 9:27 am

Heater wrote:mntmst,

I like it. Now, assuming I have pulses at 1MHz on my light souce I will get 1MHz back on my receiver. With a phase delay dependent on the time of flight of light. How can we tweak that CTMU set up to make a continuous phase measurement and hence speed of light measurement?

That is a brilliant app note:
What's important is not the phase of the received signal to a reference signal. A system using something like the CTMU measures the time between the leading edge of the light pulse leaving the transmitter to the time that leading edge is received by the detector while traveling in a light-link or is reflected off a surface back to it. As long as the ADC voltage is a linear function of time of travel between edges it can be calibrated by using at least two known distances to create a voltage slope function that can be used to calculate an unknown distance from that voltage.

On the app note:
Yes, even engineers can have a little fun sometimes. :lol:

Heater
Posts: 16318
Joined: Tue Jul 17, 2012 3:02 pm

Re: nanosecond

Sat Sep 19, 2015 1:27 pm

mntmst,

I agree that what you described is measuring the difference in time between two edges. It happens to be using the charging of a capacitor through a constant current source. That gives a linear rise in voltage with time so the ADC can effectively measure the time.

However, if your signal is continuous. Like that 1Mhz I suggested. Then the time difference between edges is effectively a phase shift.

I notice your "simplified CTMU block diagram" shows both edges running into an XOR gate. That is a classic phase detector technique https://jaunty-electronics.com/blog/201 ... -detector/

If the two inputs have zero delay between edges, that is a phase difference of zero, the output of the integrator is zero.

If the inputs have a delay between their edges the output of the integrator can ramp up. That is a non-zero phase difference.

What I'm not seeing is when to hit the capacitor discharge switch. Evey cycle, 1MHz. Or let it integrate up over 10 or 100 cycles or more. Which might make for more resolution.
Memory in C++ is a leaky abstraction .

User avatar
mntmst
Posts: 57
Joined: Thu Sep 27, 2012 11:26 pm
Location: Fairview, Oregon

Re: nanosecond

Sat Sep 19, 2015 9:42 pm

What you say is true with a CW signal but you don't use continuous signals in this mode, only light pulses with the the fastest rise time possible from the transmit/detector hardware. The receiver is used as a light level detector for time measurement not to decode the phase shift of a signal. It's really a matter of semantics here to say time or phase but the relative phase difference is not important because we don't care about the total duration of the pulse only it's leading edge for the measurement. The duration of the pulse is dependent on the speed at which were can complete one measurement cycle.

The capacitor discharge switch is closed after every ADC measurement is completed then the cycle repeats. You can integrate measurements to make a stable reading over time for a set distance but you really can't increase the real resolution of the measurements unless the voltage level signal has a Gaussian noise distribution (or quantization noise is added to the signal) for extra bits of resolution. This 'extra' resolution will likely just be noise as it will exceed the accuracy of the ADC to repeat voltage measurements generated by the circuit. ADC accuracy and resolution are not the same thing.
http://m.eet.com/media/1060300/FreescaleADC_018.pdf

Hoagie
Posts: 119
Joined: Sun Sep 23, 2012 7:19 pm

Re: nanosecond

Sun Oct 23, 2016 7:28 am

I know this is an old thread, but if anyone is still reading it, they may be interested to see that it is actually possible.

http://www.ceirogstudios.co.uk/sol/sol.html

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

Re: nanosecond

Sun Oct 23, 2016 9:57 am

shamakh96 wrote:but .. the light generator and detector wasn't my major problem , i will find others (i hope so :v)

the problem is the processor / controller that can handle that accuracy

i was thinking of using my laptop processor , is it applicable ?
The x86 has the rdtsc (or better, rdtscp which is serialised) instruction to read a 64-bit clock that runs at the CPU clock frequency (the time stamp counter - TSC). Modern x86 CPU's have "constant rate" TSC's and they are synchronized across cores (see /proc/cpuinfo flags). The TSC is available in user space (unlike the clocks on the Pi). Constant rate TSC's are not affected by frequency scaling or sleep states. As your process may be moved from core to core by the OS, its important that the TSC is synchronized between cores.

So with a single instruction, you can read a 64-bit monotonically increasing timer with a resolution rather better than a nanosecond.

In C, (gcc) you can read it with an intrinsic: "uint64_t time = __builtin_ia32_rdtsc()"

There is always a slight measuring overhead, the execution time of rdtsc for example, (which you can measure and remove) but its far far better than anything you can do in Python.

Rdtsp ensures that all instructions prior to it are completed beforehand.
If that isn't enough, you can insert barriers with "asm( "mfence" ::: "memory" ) for the compiler and for the CPU.
Last edited by jahboater on Sun Oct 23, 2016 1:12 pm, edited 2 times in total.

User avatar
johnb_summers
Posts: 285
Joined: Thu Aug 04, 2016 7:48 pm
Location: Bushey UK

Re: nanosecond

Sun Oct 23, 2016 10:06 am

I have done the same with sound and have a patent for it http://www.google.co.uk/patents/US5638824 as you will see the measuring is done outside the CPU, wont be accurate if you use the CPU, the drawing in the patent shows this, doing it with light is going to be very hard, the USA liked my patent and gave the Lawrence Livermore a 10 million grant and my patent to develop the same using radar(daily express), the speed of light, for this they had to develop a super fast clock and none was done inside the CPU.

PS: everything has a slew rate, the slew rate will be your biggest foe, the best warrior you have is the schmitt trigger, but designing hardware taking all this into account is a real quest, don't matter what your education is its a real quest, education only increase the time it takes, that relay will show what chaos exist at such speeds, that relay alone with have variations that will make your results appear random, lol, wont be, it will be the current flow around you, at those speeds you can see things that no one knows exist, temperature makes all the difference yet can be unpredictable in such small packets of time, I hope you succeed even though many here think its a fools quest, maybe your eyes will be the first to see such beautiful chaos, make sense of it and control it, so man takes another step forward.
MyPi Developer
http://mypiworld.com/
http://mypi.tech/

https://www.youtube.com/watch?v=US2nyRgg-SY&nohtml5=False

Heater
Posts: 16318
Joined: Tue Jul 17, 2012 3:02 pm

Re: nanosecond

Sun Oct 23, 2016 11:08 am

Hoagie,

Thanks for that link to ceirogstudios. What a great experiment an write up. Would never have guessed it was possible.

I love the style:
In this case it tells us the speed of light in ns per paving slab.
Memory in C++ is a leaky abstraction .

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

Re: nanosecond

Sun Oct 23, 2016 11:32 am

I got lost between the original code and the conclusion. It seemed that the actual result for the speed of light was fairly arbitrary.

Return to “General discussion”