Hello there, I have hooked up an ultrasonic sensor to my Raspberry Pi-enabled robot in order to get continuous distance-to-obstacle reading.
The sensor is properly connected via GPIO and already reads the distance properly when running a simple script. However, I need to integrate the sensor reading logic to an existing script provided by PiBorg called YetiBorg.py The way I have decided to go about implementing the sensor reading is by creating a Thread object and update the distance attribute of this very same object from the run() function. The idea is to encapsulate the distance reading within the sensor object as much as possible by i. creating a sensor/thread object and by ii. avoiding global variables. Below the script I have come up along with the error print. I believe there are multiple issues, but at least it gives an idea of what I currently want to achieve and how. Can a charitable soul advise whether my approach makes sense and whether I am using the right type of object for the task at hands? I am looking for guidance and willing try completely different approach/objects if necessary. Thanks in advance for sending me into the right direction. Marc ----Python Shell---- 2.7.9 ----OS----- Raspian Pixel on Raspberry Pi Zero ---traceback---- Traceback (most recent call last): File "/home/pi/Desktop/distance sensor class object.py", line 57, in <module> SensorA = Sensor(interval=1, gpio_trig=23, gpio_echo=24) File"/home/pi/Desktop/distance sensor class object.py", line 23, in __init__self.start() RuntimeError: thread.__init__() not called ------sensor.py----- import threading import RPi.GPIO as GPIO import time #GPIO Mode (BOARD / BCM) GPIO.setmode(GPIO.BCM) class Sensor(threading.Thread): """ultrasonic sensor continous distance reading at given interval in seconds""" def __init__(self,interval, gpio_trig, gpio_echo): self.inter = interval self.trig = gpio_trig self.echo = gpio_echo #set GPIO pins direction (IN / OUT) GPIO.setup(gpio_trig, GPIO.OUT) GPIO.setup(gpio_echo, GPIO.IN) self.dist = 0 self.terminated = False self.start() def run(self): while not self.terminated: # set Trigger to HIGH GPIO.output(gpio_trig, True) # set Trigger to LOW after 0.01ms time.sleep(0.00001) GPIO.output(gpio_trig, False) StartTime = time.time() StopTime = time.time() # save StartTime while GPIO.input(gpio_echo) == 0: StartTime = time.time() # save time of arrival while GPIO.input(gpio_echo) == 1: StopTime = time.time() # time difference between start and arrival TimeElapsed = StopTime - StartTime # multiply by sonic speed (34300 cm/s) # and divide by 2, because there and back self.dist = (TimeElapsed * 34300) / 2 time.sleep(self.inter) def get_dist(self): return self.dist #Sensor object "instanciated" with GPIO programmable pins 23 and 24 SensorA = Sensor(interval=1, gpio_trig=23, gpio_echo=24) try: while True: print("Measured Distance = %.1f cm" % SensorA.get_dist()) except KeyboardInterrupt: GPIO.cleanup() SensorA.terminated = True _______________________________________________ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor