RegisterSearchFAQMemberlistUsergroupsLog in
Reply to topic Page 1 of 1
First Script - No Testing
Author Message
Reply with quote
Post First Script - No Testing 
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

Code:

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


View user's profile Send private message Visit poster's website
Reply with quote
Post  
First thing I checked was the use of a common onTick() function, which seemed to be there, so good job.

How is it working now for you?


_________________
Brent Picasso
Founder, Autosport Labs
Facebook | Twitter
View user's profile Send private message Send e-mail
Reply with quote
Post  
Hi Brent -

Thanks for taking the time to reply. Due to some operator error, I didn't get the script saved on the RaceCapture unit before the last session. I did have success with the basic Amazon Fire tablet working fast enough to be a useful tachometer, and streaming telemetry to Podium from the Fire tablet over my iPhones personal hotspot.

Again, due to operator error, I didn't save any logs on the SD card. Since everything else worked automatically, I assumed the logging had also started.

I should have a chance to try loading the script in the shop before the next race at the end of January. I'll report back with my successes or failures...

Merry Christmas to you and the ASL team!

Cheers,
Bryan

View user's profile Send private message Visit poster's website
Reply with quote
Post  
No problem. Thanks, and keep us posted on the progress. Smile


_________________
Brent Picasso
Founder, Autosport Labs
Facebook | Twitter
View user's profile Send private message Send e-mail
Reply with quote
Post  
I had a chance today to test the script - no luck!

The script wouldn't load, giving me an "malformed number" error associated with the first line. When I remove the "--GENERAL--" comment, it would give me the same error associated with the "tickRate = 10" code.

When I copy and paste a basic script off the website, it will load fine.

Any suggestions?

Many thanks!
Bryan

View user's profile Send private message Visit poster's website
Reply with quote
Post  
When you look at the log after writing the script back to RCP, it will tell you what line number the error occurred.

You can also paste your script here if you get stuck.

View user's profile Send private message Send e-mail
Reply with quote
Post  
Got it! To troubleshoot, I moved each of my functions into a separate script, but none would load. When they failed, I noticed that the error location was different in each, and eventually noticed that it corresponded to the first blank line.

In my copy & paste efforts, a number of "space" characters crept into the script, at empty lines and the beginning and end of other lines. Once I got those fixed, I had one missing "end" statement and now I'm up and running.

Thanks for the sounding board - I'm obviously still on the very steep part of the learning curve!

Cheers,
Bryan

View user's profile Send private message Visit poster's website
Display posts from previous:
Reply to topic Page 1 of 1
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You cannot download files in this forum