【问题标题】:How to time interval between event in lua如何在lua中设置事件之间的时间间隔
【发布时间】:2019-06-12 21:46:56
【问题描述】:

我正在用 lua 编写一个简单的脚本。如果 LED 亮起,它会将计数器加 1。如果 LED 熄灭超过 1 秒,它会重置计数器。

那么我们究竟如何在 lua 中为这样的事件计时?

这就是我迄今为止所拥有并正在测试的多种方式。

function ReadADC1()
    local adc_voltage_value = 0
    adc_voltage_value = tonumber(adc.readadc()) * 2 -- 0.10 --get dec number out of this -- need to know where package adc come from
    --convert to voltage
    adc_voltage_value = adc_voltage_value *0.000537109375 --get V
    adc_voltage_value = math.floor(adc_voltage_value *1000 +0.5) --since number is base off resolution

    --print (adc_voltage_value)
    return adc_voltage_value

end
-- end of readADC1() TESTED

function interval()
local counter1 =0 
ledValue = ReadADC1()
if (ledValue >= OnThreshHold) then
    ledStatus = 1
    t1=os.time()
else
    ledStatus = 0
    t2 = os.time()
end

--counter1 =0
for i=1,20 do
if (ledStatus == 1) then -- if led is off for more than 1 second, reset counter = 0
counter1 = counter1 + 1
elseif ((ledStatus ==0 and ((os.difftime(os.time(),t2)/100000) > 1000))) then -- increment counter when led is on
counter1 = 0

end
end
print (counter1)

结束 我确定间隔的逻辑是错误的,因为 os.time 返回一个巨大的数字(我假设它在 psecond 而不是秒)。

欢迎提出任何建议或解决方案。在此之前我尝试过 vmstarttimer 和 vmstoptimmer,但不确定它是如何工作的。

编辑:

function counter()

    local ledValue = ReadADC1()
        local t1 = os.clock()   
        while true do
            local t2 = os.clock()
            local dt = t2 - t1
            t1 = t2

            if ((ledValue < OnThreshHold) and (dt < 1)) then -- if led is off for less than 1 second
                ledCounter = ledCounter + 1
            elseif ((ledValue < OnThreshHold) and (dt > 1)) then-- if led is off for more than 1 second
                ledCounter = 0;
            else
                ledCounter = ledCounter
            end
            print (ledCounter)
        end

end

最终,它将返回 ledCounter 而不是打印计数器,因为我会将计数器的值插入另一个函数,该函数将打印与计数器数量相对应的消息

【问题讨论】:

    标签: lua


    【解决方案1】:

    您可以使用os.clock,它会在几秒钟内返回您的程序运行时间。

    返回程序使用的 CPU 时间的近似值(以秒为单位)。 Source

    这个函数可以这样使用。

    local t1 = os.clock() -- begin
    local t2 = os.clock() -- end
    local dt = t2 - t1    -- calulate delta time
    
    -- or looped
    
    local t1 = os.clock() -- begin
    while true do
        local t2 = os.clock() -- end
        local dt = t2 - t1    -- calulate delta time
        t1 = t2               -- reset t1
    
        -- use dt ...
    end
    
    -- or wait for time elapsed
    -- runs until 1 second passed    
    
    local t1 = os.clock()
    while (os.clock() - t1) < 1 do
        -- do stuff while dt is smaller than 1
    
        -- could even reset timer (t1) to current to 
        -- repeat waiting
        -- t1 = os.clock() | ...
    end
    
    -- logic for your example
    
    function counter()
        local time = os.clock()
        local lastLedOn = false
        local counter = 0
    
        while true do
            if os.clock() - time > 1.0 then
                break
            end
    
            if getLedValue() == on then -- replace
                time = os.clock()
    
                if not lastLedOn then
                    lastLedOn = true
                    counter = counter + 1
    
                    -- print(counter) | or here if you want to print repeatedly
                end
            end
        end
    
        print(counter)
    end -- was unable to test it
    

    【讨论】:

    • 这不会只测量程序在开始和结束之间执行任何内容的增量时间,而不是 LED 开启和关闭之间的时间(使用脉冲边缘)
    • 您可以指定开始和结束。在我的示例中,t1 是测量的开始,t2 是测量的结束。 dt 是 t1 和 t2 之间的时间。在循环示例中,dt 是循环每次迭代之间的时间(以秒为单位)。
    • 你能看看我编辑的代码,如果我理解错了告诉我吗?它只为我的计数器返回 0。谢谢
    • @JackVu 我认为您对第一部分的理解有误。为您的问题提供了完整的逻辑示例。
    猜你喜欢
    • 2019-05-23
    • 2014-10-03
    • 2015-02-24
    • 1970-01-01
    • 1970-01-01
    • 2020-06-21
    • 1970-01-01
    • 2015-12-08
    • 1970-01-01
    相关资源
    最近更新 更多