Last active
June 17, 2017 21:45
-
-
Save brentpicasso/3f394fbc912a61c739e697ddb2fadd46 to your computer and use it in GitHub Desktop.
ShiftX2 predictive timer simulator
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--this script adequately simulates varying speeds and periodic pit stops | |
--disable all GPS channels | |
--disable lap timing | |
shiftx_can = 1 | |
shiftx_id = 0xE3700 | |
dist = 0 | |
speed = 30 | |
speedDir = 0 | |
maxSpeedDir = 2 | |
minSpeedDir = -2 | |
lapCount = 0 | |
currentLap = 1 | |
lapTime = 0 | |
elapsedTime = 0 | |
maxDist = 6 | |
tickRate = 10 | |
tickInterval = 1/tickRate | |
minSpeed = 25 | |
maxSpeed = 100 | |
speedId = addChannel("Speed", 10, 1, 0, 150) | |
distId = addChannel("Distance", 10, 2,0.1, 4) | |
lapCountId = addChannel("LapCount", 10) | |
currentLapId = addChannel("CurrentLap", 10) | |
lapTimeId = addChannel("LapTime", 10, 4) | |
elapsedTimeId = addChannel("ElapsedTime", 10, 4) | |
predTimeId = addChannel("PredTime", 5, 4) | |
setChannel(lapCountId, lapCount) | |
setChannel(currentLapId, currentLap) | |
function updateSpeed() | |
if speedDir > maxSpeedDir then speedDir = maxSpeedDir end | |
if speedDir < minSpeedDir then speedDir = minSpeedDir end | |
if speed > maxSpeed and speedDir > 0 then speedDir = -speedDir end | |
speedDir = speedDir + math.random(-1, 1) | |
speed = speed + speedDir | |
if speed < 0 then speed = 0 end | |
setChannel(speedId, speed) | |
end | |
function updateDistance() | |
--convert miles per second to miles per hour | |
dist = dist + speed * (tickInterval * 0.00277778) | |
--println(speedDir ..' ' ..speed ..' ' ..dist) | |
setChannel(distId, dist) | |
end | |
function checkNewLap() | |
elapsedTime = elapsedTime + tickInterval | |
if dist > maxDist then | |
dist = 0 | |
speedDir = 0 | |
currentLap = currentLap + 1 | |
lapCount = lapCount + 1 | |
lapTime = elapsedTime | |
elapsedTime = 0 | |
speedDir = 0 | |
onPitStopLap = 0 | |
end | |
setChannel(lapCountId, lapCount) | |
setChannel(currentLapId, currentLap) | |
setChannel(lapTimeId, lapTime / 60) | |
setChannel(elapsedTimeId, elapsedTime / 60) | |
local predTime = lapTime / 60 | |
local ptVariance = ((readOBD2(16) / 7.5) - 10) | |
println('ptvar ' .. ptVariance) | |
predTime = predTime - (ptVariance / 20) | |
local graphTime = (ptVariance + 10) * 10 | |
println('gt ' .. graphTime) | |
updateLinearGraph(graphTime) | |
setChannel(predTimeId, predTime) | |
end | |
--- shiftx2 | |
function shiftx_tx(offset, data) | |
txCAN(shiftx_can, shiftx_id + offset, 1, data) | |
sleep(10) | |
end | |
function splitWord(value) | |
return bit.band(value, 0xFF), bit.rshift(bit.band(value, 0xFF00),8) | |
end | |
function setBaseConfig(bright) | |
shiftx_tx(3,{bright}) | |
end | |
function setLed(index, leds, red, green, blue, flash) | |
shiftx_tx(10,{index, leds, red, green, blue, flash}) | |
end | |
function setLinearThreshold(id, threshold, red, green, blue, flash) | |
local lowt | |
local hight | |
lowt, hight = splitWord(threshold) | |
shiftx_tx(41, {id, 0, lowt, hight, red, green, blue, flash}) | |
end | |
function configLinearGraph(renderStyle, linearStyle, lowRange, highRange) | |
local lowRangeLow | |
local lowRangeHigh | |
local highRangeLow | |
local highRangeHigh | |
lowRangeLow, lowRangeHigh = splitWord(lowRange) | |
highRangeLow, highRangeHigh = splitWord(highRange) | |
shiftx_tx(40, {renderStyle, linearStyle, lowRangeLow, lowRangeHigh, highRangeLow, highRangeHigh}) | |
sleep(10) | |
end | |
function updateLinearGraph(value) | |
low, high = splitWord(value) | |
shiftx_tx(42,{low, high}) | |
end | |
function onTick() | |
checkNewLap() | |
updateSpeed() | |
updateDistance() | |
end | |
setTickRate(tickRate) | |
setBaseConfig(2) | |
configLinearGraph( 1, 0, 0, 200) | |
setLinearThreshold(0, 0, 255, 0, 0, 0) | |
setLinearThreshold(1, 70, 255, 127, 0, 0) | |
setLinearThreshold(2, 100, 0, 255, 0, 0) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment