Last active
November 3, 2017 16:07
-
-
Save brentpicasso/b95470a86578c251776a83784cab8c91 to your computer and use it in GitHub Desktop.
Lapping simulator with pit stops
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 | |
dist = 0 | |
speed = 30 | |
speedDir = 0 | |
maxSpeedDir = 2 | |
minSpeedDir = -2 | |
lapCount = 0 | |
currentLap = 1 | |
lapTime = 0 | |
elapsedTime = 0 | |
onPitStopLap = 0 | |
pitStopLaps = 4 --how often we stop for a pit stop | |
pitStopTime = 0 | |
pitStopStart = 0 | |
basePitStopTime = 20000 | |
maxDist = 3 | |
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) | |
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 | |
if onPitStopLap == 0 and speed < minSpeed and speedDir < 0 then speedDir = -speedDir end | |
if onPitStopLap == 1 then | |
speedDir = -1 | |
end | |
speedDir = speedDir + (math.random() * 2) - 1 | |
speed = speed + speedDir | |
if onPitStopLap > 0 and speed < 0 then speed = 0 end | |
if onPitStopLap > 0 then | |
if getUptime() > pitStopStart + pitStopTime then | |
onPitStopLap = 3 | |
speedDir = 1 | |
println('leaving pit stop') | |
end | |
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) | |
end | |
function checkPitStop() | |
if lapCount > 0 and onPitStopLap == 0 and lapCount % pitStopLaps == 0 then | |
onPitStopLap = 1 | |
pitStopStart = getUptime() | |
pitStopTime = basePitStopTime + (math.random() * 1000) | |
println('on pit stop for ' ..pitStopTime ..' sec.') | |
end | |
end | |
function onTick() | |
checkNewLap() | |
updateSpeed() | |
updateDistance() | |
checkPitStop() | |
end | |
setTickRate(tickRate) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment