First Script - No Testing
Posted: Thu Dec 15, 2016 11:58 pm
Hi folks -
Due to some circumstances beyond my control, I've run out of time to test my first Lua script in the garage before I head to the track this weekend. I'd really appreciate it if you could have a look to see if there are any fatal flaws in the way I've assembled the individual scripts borrowed from this site.
Thanks in advance for any insights!
Cheers,
Bryan
Due to some circumstances beyond my control, I've run out of time to test my first Lua script in the garage before I head to the track this weekend. I'd really appreciate it if you could have a look to see if there are any fatal flaws in the way I've assembled the individual scripts borrowed from this site.
Thanks in advance for any insights!
Cheers,
Bryan
Code: Select all
tickRate = 10
setTickRate(tickRate)
--FUEL LEVEL--
fuel2Id = addChannel("FuelLevel",10,2,0,8,"gal")
maxAvg = 100
fuelAvg={}
fuel2Index = 1
function updateFuelAvg(value)
local i
if #fuelAvg == 0 then
for i = 1, maxAvg do fuelAvg[i]=0 end
end
fuelAvg[fuel2Index] = value
fuel2Index = fuel2Index + 1
if fuel2Index > maxAvg then fuel2Index = 1 end
local sum = 0
for i = 1, #fuelAvg do
sum = sum + fuelAvg[i]
end
setChannel(fuel2Id, sum / maxAvg)
end
--GEAR CALCULATION--
local _1stGear = 3.136
local _2ndGear = 1.888
local _3rdGear = 1.330
local _4thGear = 1.000
local _5thGear = 0.814
local FinalDrive = 4.30
local TireDia = 23.0
local gearErr = 0.1
local rpmSpeedRatio = 0
local gearPos = 0 --this is the gear channel variable
function calcGear() --updates gear position every second by default
local speed = getGpsSpeed()
local rpm = getTimerRpm(0)
gearId = addChannel("Gear",5)
if speed > 10 then
rpmSpeedRatio = (rpm/speed)/(FinalDrive*1056/(TireDia*3.14159))
if ((_1stGear - rpmSpeedRatio)^2) < (gearErr^2) then gearPos = 1 end
if ((_2ndGear - rpmSpeedRatio)^2) < (gearErr^2) then gearPos = 2 end
if ((_3rdGear - rpmSpeedRatio)^2) < (gearErr^2) then gearPos = 3 end
if ((_4thGear - rpmSpeedRatio)^2) < (gearErr^2) then gearPos = 4 end
if ((_5thGear - rpmSpeedRatio)^2) < (gearErr^2) then gearPos = 5 end
else gearPos = 0 end
setChannel(gearId, gearPos) --outputs to virtual channel
end
--START LOGGING--
function loggingStart()
if getGpsSpeed() > 25 then
startLogging()
end
--GOPRO CONTROL--
goproSsid = 'UnderbiteRacing'
goproPwd = 'Password'
goproStart = 30
--GoPro will be stopped based on IsLogging() == 0 below.
--goproStop = 5
debug = 0
port = 4
wifiStatus = 0
lastInitTime = 0
initTimeout = 20000
function logMsg(msg)
println('[GoProWiFi] ' ..msg)
end
function sendCrlf()
writeCSer(port, 13)
writeCSer(port, 10)
end
function sendRaw(val)
for i=1, #val do
local c = string.sub(val, i, i)
writeCSer(port, string.byte(c))
end
end
function sendAt(val)
if debug == 1 then logMsg('send: ' ..val) end
sendRaw(val)
sendCrlf()
end
function toInt(val)
return string.sub(val, 1, -3)
end
function httpGet(url)
sendAt('AT+CIPSTART="TCP","10.5.5.9",80')
sleep(500)
local crlf = string.char(13) ..string.char(10)
local get = 'GET ' ..url ..' HTTP/1.0' ..crlf ..crlf
sendAt('AT+CIPSEND=' ..toInt(#get))
sleep(100)
sendRaw(get)
sleep(100)
sendAt("AT+CIPCLOSE")
end
function sendGoProShutter(cmd)
httpGet('/bacpac/SH?t=' ..goproPwd ..'&p=%' ..cmd)
end
function startGoPro()
logMsg('start GoPro')
sendGoProShutter('01')
end
function stopGoPro()
logMsg('stop GoPro')
sendGoProShutter('00')
end
recording = 0
function initWiFi()
logMsg('initializing')
sendAt('AT+RST')
sleep(2000)
sendAt('AT+CWMODE_CUR=1')
sleep(1000)
sendAt('AT+CWJAP_CUR="' ..goproSsid ..'","' ..goproPwd ..'"')
wifiStatus = 1
end
function processIncoming()
local line = readSer(port, 100)
if line ~= '' and debug == 1 then print(line) end
if string.match(line, 'WIFI GOT IP') then
wifiStatus = 2
end
if wifiStatus == 2 and string.match(line, 'OK') then
wifiStatus = 3
logMsg('ready for GoPro')
end
end
function checkGoPro()
if wifiStatus == 0 then
initWiFi()
lastInitTime = getUptime()
return
end
if wifiStatus == 1 and getUptime() > lastInitTime + initTimeout then
logMsg('could not connect to GoPro')
wifiStatus = 0
end
processIncoming()
if wifiStatus ~= 3 then
return
end
trigger1 = getGpsSpeed()
trigger2 = isLogging()
if recording == 0 and trigger1 > goproStart then
startGoPro()
recording = 1
end
if recording == 1 and trigger2 == 0 then
stopGoPro()
recording = 0
end
end
--MAX RPM FLAG--
maxRpmId = addChannel("MaxRPM", 10)
maxRpm = 0
function logMaxRPM()
local rpm = getTimerRpm(0)
if rpm > maxRpm then
maxRpm = rpm
setChannel(maxRpmId, maxRpm)
end
end
--MAIN SCRIPT--
function onTick()
updateFuelAvg(getAnalog(1))
calcGear()
loggingStart()
checkGoPro()
logMaxRPM()
end