Page 1 of 1

First Script - No Testing

Posted: Thu Dec 15, 2016 11:58 pm
by Bryan@UnderbiteRacing
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: 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 &#40;&#40;_1stGear - rpmSpeedRatio&#41;^2&#41; < &#40;gearErr^2&#41; then gearPos = 1 end
    if &#40;&#40;_2ndGear - rpmSpeedRatio&#41;^2&#41; < &#40;gearErr^2&#41; then gearPos = 2 end
    if &#40;&#40;_3rdGear - rpmSpeedRatio&#41;^2&#41; < &#40;gearErr^2&#41; then gearPos = 3 end
    if &#40;&#40;_4thGear - rpmSpeedRatio&#41;^2&#41; < &#40;gearErr^2&#41; then gearPos = 4 end
    if &#40;&#40;_5thGear - rpmSpeedRatio&#41;^2&#41; < &#40;gearErr^2&#41; then gearPos = 5 end
 
else gearPos = 0 end
 
setChannel&#40;gearId, gearPos&#41; --outputs to virtual channel
 
end

--START LOGGING--
function loggingStart&#40;&#41;
  if getGpsSpeed&#40;&#41; > 25 then
    startLogging&#40;&#41;
  end
 
--GOPRO CONTROL--
goproSsid = 'UnderbiteRacing'
goproPwd = 'Password'
goproStart = 30
--GoPro will be stopped based on IsLogging&#40;&#41; == 0 below.
--goproStop = 5
debug = 0

port = 4
wifiStatus = 0
lastInitTime = 0
initTimeout = 20000
 
function logMsg&#40;msg&#41;
  println&#40;'&#91;GoProWiFi&#93; ' ..msg&#41;
end
 
function sendCrlf&#40;&#41;
  writeCSer&#40;port, 13&#41;
  writeCSer&#40;port, 10&#41;
end
 
function sendRaw&#40;val&#41;
  for i=1, #val do
    local c = string.sub&#40;val, i, i&#41;
    writeCSer&#40;port, string.byte&#40;c&#41;&#41;
  end
end
 
function sendAt&#40;val&#41;
  if debug == 1 then logMsg&#40;'send&#58; ' ..val&#41; end
  sendRaw&#40;val&#41;
  sendCrlf&#40;&#41;
end
 
function toInt&#40;val&#41;
  return string.sub&#40;val, 1, -3&#41;
end
 
function httpGet&#40;url&#41;
  sendAt&#40;'AT+CIPSTART="TCP","10.5.5.9",80'&#41;
  sleep&#40;500&#41;
  local crlf = string.char&#40;13&#41; ..string.char&#40;10&#41;
  local get = 'GET ' ..url ..' HTTP/1.0' ..crlf ..crlf
  sendAt&#40;'AT+CIPSEND=' ..toInt&#40;#get&#41;&#41;
  sleep&#40;100&#41;
  sendRaw&#40;get&#41;
  sleep&#40;100&#41;
  sendAt&#40;"AT+CIPCLOSE"&#41; 
end
 
function sendGoProShutter&#40;cmd&#41;
  httpGet&#40;'/bacpac/SH?t=' ..goproPwd ..'&p=%' ..cmd&#41;
end
 
function startGoPro&#40;&#41;
  logMsg&#40;'start GoPro'&#41;
  sendGoProShutter&#40;'01'&#41;    
end
 
function stopGoPro&#40;&#41;
  logMsg&#40;'stop GoPro'&#41;
  sendGoProShutter&#40;'00'&#41;    
end
 
recording = 0
 
function initWiFi&#40;&#41;
  logMsg&#40;'initializing'&#41;
  sendAt&#40;'AT+RST'&#41;
  sleep&#40;2000&#41;
  sendAt&#40;'AT+CWMODE_CUR=1'&#41;
  sleep&#40;1000&#41;
  sendAt&#40;'AT+CWJAP_CUR="' ..goproSsid ..'","' ..goproPwd ..'"'&#41;
  wifiStatus = 1
end
 
function processIncoming&#40;&#41;
  local line = readSer&#40;port, 100&#41;
  if line ~= '' and debug == 1 then print&#40;line&#41; end
  if string.match&#40;line, 'WIFI GOT IP'&#41; then 
    wifiStatus = 2
  end
  if wifiStatus == 2 and string.match&#40;line, 'OK'&#41; then
    wifiStatus = 3
    logMsg&#40;'ready for GoPro'&#41;
  end
end
 
function checkGoPro&#40;&#41;
  if wifiStatus == 0 then
    initWiFi&#40;&#41;
    lastInitTime = getUptime&#40;&#41;
    return
  end
  if wifiStatus == 1 and getUptime&#40;&#41; > lastInitTime + initTimeout then
    logMsg&#40;'could not connect to GoPro'&#41;
    wifiStatus = 0
  end
  processIncoming&#40;&#41;
  if wifiStatus ~= 3 then
    return
  end
  trigger1 = getGpsSpeed&#40;&#41;
  trigger2 = isLogging&#40;&#41;
 
  if recording == 0 and trigger1 > goproStart then
    startGoPro&#40;&#41;
    recording = 1
  end
  if recording == 1 and trigger2 == 0 then
    stopGoPro&#40;&#41;
    recording = 0
  end 
end
 
--MAX RPM FLAG--
maxRpmId = addChannel&#40;"MaxRPM", 10&#41;
maxRpm = 0
 
function logMaxRPM&#40;&#41;
  local rpm = getTimerRpm&#40;0&#41;
  if rpm > maxRpm then
    maxRpm = rpm
    setChannel&#40;maxRpmId, maxRpm&#41;
  end
end
 
--MAIN SCRIPT--
function onTick&#40;&#41;
 
  updateFuelAvg&#40;getAnalog&#40;1&#41;&#41;
  calcGear&#40;&#41;
  loggingStart&#40;&#41;
  checkGoPro&#40;&#41;
  logMaxRPM&#40;&#41;
 
end

Posted: Sat Dec 24, 2016 7:42 pm
by brentp
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?

Posted: Sun Dec 25, 2016 1:22 pm
by Bryan@UnderbiteRacing
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

Posted: Mon Dec 26, 2016 6:47 pm
by brentp
No problem. Thanks, and keep us posted on the progress. :)

Posted: Sun Jan 15, 2017 1:03 am
by Bryan@UnderbiteRacing
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

Posted: Sun Jan 15, 2017 7:47 pm
by brentp
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.

Posted: Sat Jan 21, 2017 3:41 pm
by Bryan@UnderbiteRacing
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