VecH
Posts: 41
Joined: Tue Dec 09, 2014 5:30 pm

pigpio and ultrasonic sensor HC-SR04

Thu Jun 18, 2015 7:37 pm

I use this code for read data from Ultrasonic sensor

Code: Select all

import time
import RPi.GPIO as GPIO
import datetime
GPIO.setmode(GPIO.BCM)
GPIO_TRIGGER = 11
GPIO_ECHO = 9
compensation = -11
print "Ultrasonic Measurement"
GPIO.setup(GPIO_TRIGGER,GPIO.OUT)  # Trigger
GPIO.setup(GPIO_ECHO,GPIO.IN)      # Echo
GPIO.output(GPIO_TRIGGER, False)
time.sleep(0.5)
distance_prev = 0
try:
   while True:
      # Send 10us pulse to trigger
      GPIO.output(GPIO_TRIGGER, True)
      time.sleep(0.00001)
      GPIO.output(GPIO_TRIGGER, False)
      start = time.time()
      while GPIO.input(GPIO_ECHO)==0:
         start = time.time()
      while GPIO.input(GPIO_ECHO)==1:
         stop = time.time()
      # Calculate pulse length
      elapsed = stop-start
      # speed of sound = 34300 cm/s (34300 / 2 = 17150)
      distance = elapsed * 17150
      #print "Distance :",round(distance,2),"cm (",round(distance_prev-distance,2), "cm)"
      print datetime.datetime.now(),"Distance = {}\t\tDiff = {}".format(round(distance,2), round(distance_prev-distance,2))
      distance_prev = distance
      distance = distance + compensation
      time.sleep(10)
except KeyboardInterrupt:
   print("Clean Exit By user")
finally:
   # Reset GPIO settings
   GPIO.cleanup()
The program hangs in 10-20 minutes

I suspect it does not have time to read the data from the port of ECHO and hangs on WHILE

I had a problem earlier when he wrote a program to read RFID key protocol WIEGAND26, use the library pigpio
http://abyz.co.uk/rpi/pigpio/python.html#pigpio.pi
which work with GPIO ports goes through the daemon written in C

I tried to rewrite the program, but it also freezes help run the normal program with the library pigpio

Code: Select all

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import pigpio
import time
import datetime

def waterlevel(wl_gpio_trigger, wl_gpio_echo):
   pi = pigpio.pi()
   wl_timer_start = time.time()
   pi.set_mode(wl_gpio_trigger, pigpio.OUTPUT)
   pi.set_mode(wl_gpio_echo, pigpio.INPUT)
   pi.write(wl_gpio_trigger,False)
   time.sleep(0.5)
   pi.write(wl_gpio_trigger,True)
   time.sleep(0.00001)
   pi.write(wl_gpio_trigger,False)
   while pi.read(wl_gpio_echo)==0:
      wl_timer_start = time.time()
   while pi.read(wl_gpio_echo)==1:
      wl_timer_stop = time.time()
   wl_time_elapsed = wl_timer_start - wl_timer_stop
   pi.stop()
   return wl_time_elapsed;

if __name__ == "__main__":
    try:
       while True:
          print datetime.datetime.now(),"---"
          print "\n",waterlevel(11,9)
          time.sleep(3)

    except KeyboardInterrupt:
       print("Clean Exit By user")
    finally:
       print("Finally")

''' END '''

VecH
Posts: 41
Joined: Tue Dec 09, 2014 5:30 pm

Re: pigpio and ultrasonic sensor HC-SR04

Thu Jun 18, 2015 8:07 pm

Found ready code here
http://abyz.co.uk/rpi/pigpio/misc/sonar/
I will try tomorrow, now almost morning and sleepy

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

Re: pigpio and ultrasonic sensor HC-SR04

Thu Jun 18, 2015 8:08 pm

You need to use a callback with pigpio if you want to get a sonar ranger reading. The pigpio Python module is not suited to busy-spin solutions as each gpio read (or write) requires a round-trip message to the daemon.

Have a look at the Sonar Ranger example at http://abyz.co.uk/rpi/pigpio/examples.html#Python_code

VecH
Posts: 41
Joined: Tue Dec 09, 2014 5:30 pm

Re: pigpio and ultrasonic sensor HC-SR04

Thu Jun 18, 2015 8:57 pm

in what unit of measurement results are displayed?

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

Re: pigpio and ultrasonic sensor HC-SR04

Thu Jun 18, 2015 9:33 pm

VecH wrote:in what unit of measurement results are displayed?
It looks like round trip microseconds.

So round trip cms = round trip time / 1000000.0 * 34030.0

VecH
Posts: 41
Joined: Tue Dec 09, 2014 5:30 pm

Re: pigpio and ultrasonic sensor HC-SR04

Thu Jun 18, 2015 9:46 pm

How do I convert it to millimeters?

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

Re: pigpio and ultrasonic sensor HC-SR04

Thu Jun 18, 2015 10:37 pm

VecH wrote:How do I convert it to millimeters?
Convert it to centimeters then multiply by 10.

User avatar
davidcoton
Posts: 5028
Joined: Mon Sep 01, 2014 2:37 pm
Location: Cambridge, UK
Contact: Website

Re: pigpio and ultrasonic sensor HC-SR04

Thu Jun 18, 2015 11:52 pm

Or use: round trip mm = round trip time / 1000000.0 * 340300.0
or: range = round trip time /1000000.00 * 170150.0 (assumes a direct echo)
or: range = round trip time * 0.170150 (still assumes a direct echo, just simplifies the calculation)

where round trip time is in microseconds, range in millimetres, sound of sound in air is 340.3 metres per second
Signature retired

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

Re: pigpio and ultrasonic sensor HC-SR04

Fri Jun 19, 2015 9:35 am

davidcoton wrote: where round trip time is in microseconds, range in millimetres, speed of sound in air is 340.3 metres per second
FTFY
Doug.
Building Management Systems Engineer.

Return to “General discussion”