Skip to content

Commit 6af21ec

Browse files
committed
add v3 script
1 parent 5a85000 commit 6af21ec

File tree

1 file changed

+111
-0
lines changed

1 file changed

+111
-0
lines changed

snapshot/v3.lua

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
--------------------------------------------
2+
-- main: Main tick function for racecapture device.
3+
--
4+
-- Uncomment calls in onTick() to run scripts.
5+
6+
LOG_LEVEL = 'info'
7+
TICKS = 50
8+
9+
function info(str)
10+
if LOG_LEVEL == 'info' or LOG_LEVEL == 'verbose' then print(str) end
11+
end
12+
13+
function verbose(str)
14+
if LOG_LEVEL == 'verbose' then print(str) end
15+
end
16+
17+
setTickRate(TICKS)
18+
function onTick()
19+
tick_frequency()
20+
-- tick_mock()
21+
tick_vminmax_simple()
22+
-- tick_brakebias()
23+
end
24+
--------------------------------------------
25+
-- frequency: A script to help run actions less frequently than
26+
-- the global TICK frequency.
27+
28+
-- current tick
29+
local tick = 0
30+
31+
-- returns true if this is the tick to run an action.
32+
-- E.g., pass in 10 to have an action trigger 10 times a second.
33+
function should_run(hz)
34+
return tick % (TICKS/hz) == 0
35+
end
36+
37+
function tick_frequency()
38+
tick = tick + 1
39+
end
40+
41+
42+
--------------------------------------------
43+
-- vminmax_simple: simple window based VMin and VMax signal.
44+
45+
-- window size in seconds to keep track of first and last value.
46+
local WINDOW_SIZE_SEC = 5
47+
-- how many per seconds to record
48+
local FREQUENCY = 10
49+
-- minimum difference between edges and mid value to be considered a local value.
50+
local MIN_DELTA_MPH = 10
51+
52+
-- name, Hz, precision, min, max, units
53+
local minId = addChannel('VMin', 5, 0, 0, 200, 'mph')
54+
local maxId = addChannel('VMax', 5, 0, 0, 200, 'mph')
55+
56+
-- recorded values
57+
local vs = {}
58+
-- we record a value every TICKS/FREQUENCY counts.
59+
local tick_count = 0
60+
61+
-- local min/max calculation.
62+
-- Returns signalId and value to update it to.
63+
function __vminmax_signal()
64+
verbose('\n'..table.concat(vs, ','))
65+
first, mid, last = vs[1], vs[math.ceil(#vs/2)], vs[#vs]
66+
info('\nfirst='..first..', mid='..mid..', last='..last..' @'..tick)
67+
-- too flat?
68+
if math.abs(mid-first) < MIN_DELTA_MPH or math.abs(mid-last) < MIN_DELTA_MPH then
69+
verbose('\n too similar.')
70+
return 0, nil
71+
end
72+
-- maxima?
73+
if first < mid and last < mid then
74+
info('\n new max='..mid)
75+
return maxId, mid
76+
end
77+
-- minima?
78+
if first > mid and last > mid then
79+
info('\n new min='..mid)
80+
return minId, mid
81+
end
82+
verbose('\n no hill.')
83+
return 0, nil
84+
end
85+
86+
function tick_vminmax_simple()
87+
local speed = getChannel('Speed')
88+
if speed ~= nil and should_run(FREQUENCY) then
89+
-- append, resize, update.
90+
table.insert(vs, speed)
91+
if #vs > WINDOW_SIZE_SEC*FREQUENCY then table.remove(vs, 1) end
92+
if #vs == WINDOW_SIZE_SEC*FREQUENCY then
93+
local id, value = __vminmax_signal()
94+
if id ~= 0 then
95+
setChannel(id, value)
96+
end
97+
end
98+
end
99+
end
100+
101+
--------------------------------------------
102+
-- brakebias: Adds BrakeBias signal.
103+
local bbid = addChannel('BrakeBias', 5, 1, 0, 100, '%')
104+
105+
function tick_brakebias()
106+
local f = getChannel('BrakeF')
107+
local b = getChannel('BrakeR')
108+
-- only update when brake applied
109+
if b ~= 0 then setChannel(bbid, f/(f+b)*100) end
110+
end
111+

0 commit comments

Comments
 (0)