-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvminmax_simple.lua
More file actions
64 lines (59 loc) · 1.66 KB
/
Copy pathvminmax_simple.lua
File metadata and controls
64 lines (59 loc) · 1.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
--------------------------------------------
-- vminmax_simple: simple window based VMin and VMax signal.
--
-- OUT
-- channel: VMin
-- channel: VMax
--
-- IN
-- Speed
--
-- window size in seconds to keep track of first and last value.
local WINDOW_SIZE_SEC = 5
-- how many per seconds to record
local FREQUENCY = 10
-- minimum difference between edges and mid value to be considered a local value.
local MIN_DELTA_MPH = 10
-- name, Hz, precision, min, max, units
local minId = addChannel('VMin', 5, 0, 0, 200, 'mph')
local maxId = addChannel('VMax', 5, 0, 0, 200, 'mph')
-- recorded values
local vs = {}
-- local min/max calculation.
-- Returns signalId and value to update it to.
function __vminmax_signal()
verbose(table.concat(vs, ','))
local first, mid, last = vs[1], vs[math.ceil(#vs/2)], vs[#vs]
verbose('first='..first..', mid='..mid..', last='..last..' @'..tick)
-- too flat?
if math.abs(mid-first) < MIN_DELTA_MPH or math.abs(mid-last) < MIN_DELTA_MPH then
verbose(' too similar.')
return 0, nil
end
-- maxima?
if first < mid and last < mid then
info(' new max='..mid)
return maxId, mid
end
-- minima?
if first > mid and last > mid then
info(' new min='..mid)
return minId, mid
end
verbose(' no hill.')
return 0, nil
end
function tick_vminmax_simple()
local speed = getChannel('Speed')
if speed ~= nil and should_run(FREQUENCY) then
-- append, resize, update.
table.insert(vs, speed)
if #vs > WINDOW_SIZE_SEC*FREQUENCY then table.remove(vs, 1) end
if #vs == WINDOW_SIZE_SEC*FREQUENCY then
local id, value = __vminmax_signal()
if id ~= 0 then
setChannel(id, value)
end
end
end
end