首页 > 解决方案 > 每次触摸传感器时如何累积时间延迟?

问题描述

我现在很难找到累积延迟的方法。

操作是

  1. 当它接触到间隙传感器时,会在 0.001 秒的延迟后测量数据。
  2. 第二次接触时,延迟0.002秒后测量数据
  3. 当到达第三次时,有0.003秒的延迟......

我想像这样实现延迟。这是我的完整代码:

import RPi.GPIO as GPIO
from time import sleep
import time, datetime, sys, timeit, serial
import RPi.GPIO as GPIO
# -------------------------------#
ser = serial.Serial("/dev/ttyAMA0", 115200)
#sys.stdout = open('a_0315test', 'w')  # txt file save code2
GPIO.setmode(GPIO.BCM)
Input_Sig = 17  # any plain GPIO pin
GPIO.setup(Input_Sig, GPIO.IN)
# --------------------------------#
last_time = time.time()  # Initialize
this_time = time.time()  # Initialize
RPM = 0  # Initialize
lidar_delay_time = 1000/1000 / 6 * RPM
# --------------------------------#
# timeout variable can be omitted, if you use specific value in the while condition
#Lidar Run Time Setting Part
# --------------------------------#
def interrupt_service_routine(Input_Sig):
    # only deal with valid edges
    if GPIO.input(Input_Sig) == 0:         # "RISING"
        # sleep(0.005) # edge debounce of 5ms
        global RPM, this_time, last_time
        this_time = time.time()
        RPM = (1 / (this_time - last_time)) * 60
        print('RPM = {:7.1f}'.format(RPM))
        last_time = this_time
        #lidar_delay_time = 1000/1000 / 6 * RPM  # 1 degree per delay second
        return ()
    else:
        one_turn_cnt = 0
        output = 0
        delay_time = 0
        sleep(0.01)
        while True:
            while output < 30:
                count = ser.in_waiting
                if count > 8:
                    recv = ser.read(ser.in_waiting)
                    if recv[0] == 'Y' and recv[1] == 'Y':  # 0x59 is 'Y'
                        low = int(recv[2].encode('hex'), 16)
                        high = int(recv[3].encode('hex'), 16)
                        distance = low + high * 256
                        print(distance, datetime.datetime.utcnow().strftime('%H:%M:%S.%f')[:-3])
                        one_turn_cnt = one_turn_cnt + 1
                        output += 1               
                        # end loop-time
                    continue
            if GPIO.input(Input_Sig) == 0:
                print("Number of output: ", one_turn_cnt)
                sleep(delay_time)
                delay_time += 0.001
                print(delay_time)
                break
    return

# define the event; bountime of 5mSec means that subsequent edges will be ignored for 5mSec
GPIO.add_event_detect(Input_Sig, GPIO.BOTH, callback=interrupt_service_routine, bouncetime=5)

def main():
    try:
        while True:
            for x in range(0, 1200):
                time.sleep(0.5)
    except:
        GPIO.remove_event_detect(Input_Sig)
        GPIO.cleanup([Input_Sig])
        print('Done')
    finally:
        pass
if __name__ == '__main__':
    main()

但输出是:

RPM =   102.7
.
.
.
(40, '07:06:08.929')
(40, '07:06:08.939')
('Number of output: ', 30)
0.001

RPM =   101.1
(40, '07:06:09.280')
.
.
.
(17, '07:06:09.286')
(12, '07:06:09.296')
(7, '07:06:09.306')
('Number of output: ', 30)
0.001

RPM =    37.3
.
.
.
(2, '07:06:10.892')
(3, '07:06:10.895')
('Number of output: ', 30)
0.001

为什么我没有累积 0.001 秒?

请问我想知道怎么做。

标签: pythonraspberry-pi

解决方案


推荐阅读