Uncategorized

Microsecond programming notes

Asked 
Active 2 days ago
Viewed 27 times
1

I am converting an Arduino code to python3. I am trying to read data from IMU 9250 sensor from the I2C interface of raspberry-pi. I am facing issue with the timing libraries of raspberry-pi and arduino. The micros() api of Arduino gives time in microsecond as a 8 digit number. I used datetime library available in python to get time in microseconds which gives a 6 digit number. I need it to be in 8 digit number for precise calculation purpose.

import datetime
Now = datetime.datetime.now().microsecond
# >>> 439014

I have tried getting time in milliseconds and converting it into microseconds using the time library in python and moving ahead with the code, but the precision is lost with this approach.

How can i get time in microseconds as a 8 digit number python? If it is not possible to get the microseconds as a 8 digit number, then how should i approach with the problem?

  • Hi @TheLazy, Ah, let me see. A couple of months ago I wrote quick and dirty python functions to find the elapsed time between two events, accurate up to the order of 10 microseconds, in decimal. Just now I tested the elapsed time to do 100 python “do nothings” (passes) and found it about 50 uS. I have tided up the code for lazy newbies: You just copy the two “time” and “print” modules in the penzu entry, and run it. In other words, it is “plug and play”, no nothing library is needed: penzu.com/p/b01f2332. Happy python programming and cheers. – tlfong01 2 days ago    
  • I included some outdated I2C test functions in the penzu file in case you need to know more about my hardware/software config. In case you have not noticed, Rpi4B can do I2C at a speed higher then 800kHz (not too many devices on the same bus, not too long connecting wires). So you can calculated the elapsed time by calculating the I2C cycle time (1/800k second). I also usually find the moving average of say 100 events and average out the 100 event. I once played with 24MHz quartz clock and used binary counter to count up to sub uS. I forgot why I need so high accuracy, perhaps just for fun. – tlfong01 2 days ago   
  • Your using MPU9250 do remind me one thing. I tried to use Rp3B+ I2C block read the MPU, but found SMBus does not fully support bus/cycle stretch, and only get around is to lower I2C speed. Unluckily Rpi3B+ I2C speed, because of a design error, can only use the fixed 100kHz speed (though official doc says otherwise). Anyway, I switched to Bosch BNO (for other reasons) but still need to lower I2C speed to 10 kHz (See MagPi84 page 39). So the bad news is that if you lower the I2C speed to 10kHz, then everything goes very slow, … 😦 – tlfong01 2 days ago   

0

Arduino code does not map well to the Pi for many reasons – principally because it is a multi-tasking OS. Python makes the task more difficult. Frankly you are better looking for a Pi solution or write new code.

You can get µSec precision timing on the Pi using the pigpio python library e.g. http://abyz.me.uk/rpi/pigpio/python.html#get_current_tick

This is similar to micros()

http://abyz.me.uk/rpi/pigpio/code/DHT.py is an clever example using this to perform a task normally difficult on the Pi

PS I don’t understand 8 digit or 6 digit comments. AFAIK both return an unsigned 32 bit quantity

Categories: Uncategorized

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: