#!/usr/bin/env python # -*- coding: utf-8 -*- import u12 import time from threading import Lock import os import numpy import ctypes threadLock = Lock() lj = u12.U12() def distance(): numscans = 500 Voltages = [ [0] * 4095 for row in range(4)] stateIOout = [ [0] * 4095] threadLock.acquire() lj.eDigitalOut(idNum = -1, channel = 0, writeD = 0, state = 0) time.sleep(0.1) lj.eDigitalOut(idNum = -1, channel = 0, writeD = 0, state = 1) threadLock.release() print(lj.aiBurst(idNum = -1, updateIO = 1, ledOn = 1, numChannels = 1, channels = [0, 0, 0, 0], scanRate = 8192, numScans = numscans)) counter = 0 for x in range(0, (numscans-1)): if Voltages[x][0] > 1: counter = counter + 1 Pulse_time = 1 / scanrate * Counter Scale_val = (Pulse_time - 0.0001) / 0.0179 distanz = (2.97 * Scale_val + 0.03)/100 return distance if __name__ == '__main__': try: while True: distance_cm = distance() print ("Distance = %.1f cm" % distance_cm) time.sleep(1) except KeyboardInterrupt: print("Measuring stopped")