Matt1234
Posts: 33
Joined: Mon Aug 06, 2012 12:34 pm

Smallest delay in Python????

Fri Sep 21, 2012 1:52 pm

Ok.... Lets get this straight: I know the Pi is not a real time microcontroller and so is probably ill equipped to deal with an Ultrasonic distance sensor.... HOWEVER.

As I mentioned before I would like to use a Ultrasonic distance sensor with my Raspberry Pi as part of a mobile robot operating system... To my understanding the distance sensors needs to have a 5v signal (Logic level converter sorted!) applied to it for 10 microseconds.... It then, depending on the time it took, calculates (with the receiver) how far away an object is.... ANYWAY the point is how do you get one of the Pi's GPIO pins to turn on for 10 microseconds in Python with RPi.GPIO.0.3.1a??? What function or module do you need??? Is it required that it turns on for that exact time? What is the closest I can get to 10 microseconds??? I don't need it to be to accurate... All I want is for it to be able to tell the difference between something in front of it or not!!!

Thanks in advance for any ideas! :)

User avatar
Grumpy Mike
Posts: 936
Joined: Sat Sep 10, 2011 7:49 pm
Location: Manchester (England England)
Contact: Website

Re: Smallest delay in Python????

Fri Sep 21, 2012 6:55 pm

Matt1234 wrote:To my understanding the distance sensors needs to have a 5v signal (Logic level converter sorted!) applied to it for 10 microseconds.... It then, depending on the time it took, calculates (with the receiver) how far away an object is.... ANYWAY the point is how do you get one of the Pi's GPIO pins to turn on for 10 microseconds in Python with RPi.GPIO.0.3.1a???
First off read this:=
http://codeandlife.com/2012/07/03/bench ... pio-speed/
So basically you can't. It will some time but other times it will be much longer. Exactly the same goes for timing the echo. The best I think you can do is to use a software loop to time things. About 1 line to 1 uS. And take a few readings, then throw away the silly ones where Linux stole time from you.

Matt1234
Posts: 33
Joined: Mon Aug 06, 2012 12:34 pm

Re: Smallest delay in Python????

Sat Sep 22, 2012 6:47 am

I am no expert on these things and so I am struggling to comprehend some of the information you posted... What do you mean by 'uS' ???? And also 'software loop' ??? What would you do in this situation? Asides from buying an Arduino?

Thanks for your help!!!

cTn
Posts: 49
Joined: Sat Aug 11, 2012 4:11 pm
Contact: Website

Re: Smallest delay in Python????

Sat Sep 22, 2012 10:37 am

Matt1234 wrote:I am no expert on these things and so I am struggling to comprehend some of the information you posted... What do you mean by 'uS' ???? And also 'software loop' ??? What would you do in this situation? Asides from buying an Arduino?

Thanks for your help!!!
You can't set a GPIO pin high for 10us (not with the rpi.gpio library), but there is some pwm kernel driver floating somewhere on this forum, you might be able to strip the pwm part out to achieve what you want without buying an extra microcontroller.

User avatar
jojopi
Posts: 3353
Joined: Tue Oct 11, 2011 8:38 pm

Re: Smallest delay in Python????

Sat Sep 22, 2012 11:19 pm

Grumpy Mike wrote:And take a few readings, then throw away the silly ones where Linux stole time from you.
If you are concerned about being preempted you should request realtime scheduling priority. WiringPi includes a simple call, piHiPri() for this. For other libraries or languages, including python, you can use "sudo chrt -r 1 PROGRAM".

(Your timing can still be messed up by hardware interrupts. In the unlikely event that you can live without USB and network, you can disable it via /sys/devices/platform/bcm2708_usb/bussuspend .)

User avatar
Grumpy Mike
Posts: 936
Joined: Sat Sep 10, 2011 7:49 pm
Location: Manchester (England England)
Contact: Website

Re: Smallest delay in Python????

Mon Sep 24, 2012 5:05 am

Matt1234 wrote:I am no expert on these things and so I am struggling to comprehend some of the information you posted... What do you mean by 'uS' ???? And also 'software loop' ??? What would you do in this situation?
uS are micro seconds or 10 to the minus 6 of a second.
A software loop is a function like a for loop or a while loop, something that repeats some line a set number of times.
Not sure what exactly you are asking for the last part.

@jojopi
In the unlikely event that you can live without USB and network
Not unlikely when doing physical programming.
you can disable it via /sys/devices/platform/bcm2708_usb/bussuspend
Do you not still get time stolen from you for the display updates?

User avatar
JTrevorJ
Posts: 8
Joined: Fri Jul 27, 2012 12:12 am
Location: Tamania

Re: Smallest delay in Python????

Mon Sep 24, 2012 5:41 am

Matt1234 wrote:I am no expert on these things and so I am struggling to comprehend some of the information you posted... What do you mean by 'uS' ???? And also 'software loop' ??? What would you do in this situation? Asides from buying an Arduino?

Thanks for your help!!!
Use a different language. Try C or C++ or any compiled language, then the loop time will be constant. For any interpreted language (read Python in this case) the execution times can very slightly.

User avatar
Grumpy Mike
Posts: 936
Joined: Sat Sep 10, 2011 7:49 pm
Location: Manchester (England England)
Contact: Website

Re: Smallest delay in Python????

Mon Sep 24, 2012 2:40 pm

JTrevorJ wrote:For any interpreted language (read Python in this case) the execution times can very slightly.
This is also true of compiled languages running in a preemptive multitasking environment like Linux. So going to C will push the absolute times down but will still leave as much jitter.

Rasathus
Posts: 45
Joined: Sat Jun 02, 2012 11:37 am
Location: Reading, UK
Contact: Website

Re: Smallest delay in Python????

Mon Sep 24, 2012 7:35 pm

Hi Matt134, did you make any progress with this ? I've been playing with an HY-SRF05 myself, and have a bit of a discussion going over on the Auotmation and sensing forum.

http://www.raspberrypi.org/phpBB3/viewt ... 37&t=18291

I've got a basic implementation of it working in python. I've just written a bit of an article on the data i've gathered from it, and have got some rough code that might help you in getting yours up and running.

http://rasathus.blogspot.co.uk/2012/09/ ... srf05.html

What sensor is it ?
Pi1 - Revision 002 - Used for some circuit experimentation and learning.
Pi2 - Revision 002 - Currently powering the 'Monstrosity Rover' project.
Pi3 - Revision 004 - Raspberry Pi Laptop using a Motorola Lapdock.

Take a look @ rasathus.blogspot.co.uk

User avatar
mahjongg
Forum Moderator
Forum Moderator
Posts: 13366
Joined: Sun Mar 11, 2012 12:19 am
Location: South Holland, The Netherlands

Re: Smallest delay in Python????

Mon Sep 24, 2012 7:45 pm

The problem is that Linux is a preemptive multitasking system, that means that any running task, including your microsecond counter can be interrupted by a task switch (by the kernel) at any time.
Only solution for that is to disable taskswitching during the signal generation (count), but in principle this can only be done by the kernel.

A solution would be not to use linux, but a "bare metal OS" (Arduino style) or an OS that does taskswitching in another way, like RiscOS, which uses cooperative multitasking.

Return to “Python”