Getting distance synchronously
This commit is contained in:
parent
df6790cde9
commit
da0d715d49
2 changed files with 37 additions and 77 deletions
|
@ -12,7 +12,6 @@ class GpioSensorDistancePlugin(Plugin):
|
|||
def __init__(self, trigger_pin, echo_pin):
|
||||
self.trigger_pin = trigger_pin
|
||||
self.echo_pin = echo_pin
|
||||
self.measured_distance_mm = None
|
||||
self._is_probing = False
|
||||
|
||||
gpio.setmode(gpio.BCM)
|
||||
|
@ -24,16 +23,11 @@ class GpioSensorDistancePlugin(Plugin):
|
|||
time.sleep(1)
|
||||
|
||||
|
||||
def start(self):
|
||||
self._is_probing = True
|
||||
logging.info('Received distance sensor start command')
|
||||
|
||||
def _run():
|
||||
def get_distance(self):
|
||||
gpio.setmode(gpio.BCM)
|
||||
gpio.setup(self.trigger_pin, gpio.OUT)
|
||||
gpio.setup(self.echo_pin, gpio.IN)
|
||||
|
||||
while self._is_probing:
|
||||
gpio.output(self.trigger_pin, True)
|
||||
time.sleep(0.00001) # 1 us pulse to trigger echo measurement
|
||||
gpio.output(self.trigger_pin, False)
|
||||
|
@ -61,38 +55,13 @@ class GpioSensorDistancePlugin(Plugin):
|
|||
break
|
||||
|
||||
if read_timeout:
|
||||
continue
|
||||
return None
|
||||
|
||||
pulse_end = pulse_off
|
||||
pulse_duration = pulse_end - pulse_start
|
||||
|
||||
# s = vt where v = avg speed of sound
|
||||
self.measured_distance_mm = round(pulse_duration * 171500.0, 2)
|
||||
|
||||
gpio.output(self.trigger_pin, False)
|
||||
time.sleep(0.1)
|
||||
|
||||
gpio.cleanup()
|
||||
self.measured_distance_mm = None
|
||||
|
||||
self._probe_thread = threading.Thread(target=_run)
|
||||
self._probe_thread.start()
|
||||
|
||||
return Response(output={'status': 'probing'})
|
||||
|
||||
|
||||
def get_distance(self):
|
||||
return self.measured_distance_mm
|
||||
|
||||
|
||||
def stop(self):
|
||||
self._is_probing = False
|
||||
self.measured_distance_mm = None
|
||||
|
||||
if self._probe_thread and threading.get_ident() != self._probe_thread.ident:
|
||||
self._probe_thread.join()
|
||||
|
||||
return Response(output={'status':'stopped'})
|
||||
return round(pulse_duration * 171500.0, 2)
|
||||
|
||||
|
||||
# vim:sw=4:ts=4:et:
|
||||
|
|
|
@ -40,7 +40,6 @@ class GpioZeroborgPlugin(Plugin):
|
|||
self.v_in = v_in
|
||||
self.v_out = v_out
|
||||
self.max_power = v_out / float(v_in)
|
||||
self.distance_sensor = None
|
||||
self.auto_mode = False
|
||||
|
||||
self.zb = ZeroBorg.ZeroBorg()
|
||||
|
@ -50,16 +49,11 @@ class GpioZeroborgPlugin(Plugin):
|
|||
|
||||
|
||||
def get_distance(self):
|
||||
if not self.distance_sensor:
|
||||
self.distance_sensor = get_plugin('gpio.sensor.distance')
|
||||
|
||||
if not self.distance_sensor:
|
||||
distance_sensor = get_plugin('gpio.sensor.distance')
|
||||
if not distance_sensor:
|
||||
raise RuntimeError('No gpio.sensor.distance configuration found')
|
||||
|
||||
if self.distance_sensor.get_distance() is None:
|
||||
self.distance_sensor.start()
|
||||
|
||||
return self.distance_sensor.get_distance()
|
||||
return distance_sensor.get_distance()
|
||||
|
||||
|
||||
def drive(self, direction):
|
||||
|
@ -126,9 +120,6 @@ class GpioZeroborgPlugin(Plugin):
|
|||
self.zb.MotorsOff()
|
||||
self.zb.ResetEpo()
|
||||
|
||||
if self.distance_sensor:
|
||||
self.distance_sensor.stop()
|
||||
|
||||
return Response(output={'status':'stopped'})
|
||||
|
||||
|
||||
|
|
Loading…
Reference in a new issue