Skip to content

Commit 11eb26b

Browse files
committed
snapshot: v4-beta
1 parent a99cf57 commit 11eb26b

File tree

1 file changed

+144
-0
lines changed

1 file changed

+144
-0
lines changed

snapshot/v4-beta.lua

Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
--------------------------------------------
2+
-- main: Main tick function for racecapture device.
3+
--
4+
-- Uncomment calls in onTick() to run scripts.
5+
6+
-- 'info', 'verbose', or nil
7+
LOG_LEVEL = 'info'
8+
TICKS = 50
9+
10+
function info(str)
11+
if LOG_LEVEL == 'info' or LOG_LEVEL == 'verbose' then print(str) end
12+
end
13+
14+
function verbose(str)
15+
if LOG_LEVEL == 'verbose' then print(str) end
16+
end
17+
18+
setTickRate(TICKS)
19+
function onTick()
20+
tick_frequency()
21+
tick_vminmax_simple()
22+
tick_besttimetoday()
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+
-- besttimetoday: Keeps track of the best lap time today
44+
--
45+
-- channel: BestTimeT "Best lap time today"
46+
-- channel: LapDeltaT "Lap time delta vs best time today"
47+
-- var: best_time_day
48+
-- var: best_time_today
49+
--
50+
51+
local Y, M, D = getDateTime()
52+
local TODAY = string.format('%d%d%d', Y, M, D)
53+
54+
local bestTimeToday = loadVar('best_time_today')
55+
local deltaId = addChannel('LapDeltaT', 10, 3, -5, 5, 's')
56+
local bestId = addChannel('BestTimeT', 1, 4, 0, 5, 'min')
57+
58+
function __besttime_update(bestTime)
59+
if bestTime ~= nil then info('\nbest time today update: '..bestTime) end
60+
saveVar('best_time_today', bestTime)
61+
setChannel(bestId, bestTime)
62+
bestTimeToday = bestTime
63+
end
64+
65+
-- reset?
66+
if loadVar('best_time_day') ~= TODAY then
67+
info('\nwelcome to a new day')
68+
saveVar('best_time_day', TODAY)
69+
__besttime_update(nil)
70+
end
71+
72+
function tick_besttimetoday()
73+
-- track best time today
74+
local bestTimeSession = getBestTime()
75+
if bestTimeToday == nil or bestTimeSession < bestTimeToday then
76+
__besttime_update(bestTimeSession)
77+
end
78+
-- calculate delta
79+
setChannel(deltaId, 60*(getPredTime()-bestTimeToday))
80+
end
81+
82+
--------------------------------------------
83+
-- vminmax_simple: simple window based VMin and VMax signal.
84+
--
85+
-- channel: VMin
86+
-- channel: VMax
87+
--
88+
89+
-- window size in seconds to keep track of first and last value.
90+
local WINDOW_SIZE_SEC = 5
91+
-- how many per seconds to record
92+
local FREQUENCY = 10
93+
-- minimum difference between edges and mid value to be considered a local value.
94+
local MIN_DELTA_MPH = 10
95+
96+
-- name, Hz, precision, min, max, units
97+
local minId = addChannel('VMin', 5, 0, 0, 200, 'mph')
98+
local maxId = addChannel('VMax', 5, 0, 0, 200, 'mph')
99+
100+
-- recorded values
101+
local vs = {}
102+
-- we record a value every TICKS/FREQUENCY counts.
103+
local tick_count = 0
104+
105+
-- local min/max calculation.
106+
-- Returns signalId and value to update it to.
107+
function __vminmax_signal()
108+
verbose('\n'..table.concat(vs, ','))
109+
first, mid, last = vs[1], vs[math.ceil(#vs/2)], vs[#vs]
110+
info('\nfirst='..first..', mid='..mid..', last='..last..' @'..tick)
111+
-- too flat?
112+
if math.abs(mid-first) < MIN_DELTA_MPH or math.abs(mid-last) < MIN_DELTA_MPH then
113+
verbose('\n too similar.')
114+
return 0, nil
115+
end
116+
-- maxima?
117+
if first < mid and last < mid then
118+
info('\n new max='..mid)
119+
return maxId, mid
120+
end
121+
-- minima?
122+
if first > mid and last > mid then
123+
info('\n new min='..mid)
124+
return minId, mid
125+
end
126+
verbose('\n no hill.')
127+
return 0, nil
128+
end
129+
130+
function tick_vminmax_simple()
131+
local speed = getChannel('Speed')
132+
if speed ~= nil and should_run(FREQUENCY) then
133+
-- append, resize, update.
134+
table.insert(vs, speed)
135+
if #vs > WINDOW_SIZE_SEC*FREQUENCY then table.remove(vs, 1) end
136+
if #vs == WINDOW_SIZE_SEC*FREQUENCY then
137+
local id, value = __vminmax_signal()
138+
if id ~= 0 then
139+
setChannel(id, value)
140+
end
141+
end
142+
end
143+
end
144+

0 commit comments

Comments
 (0)