[Solved] Help with Lua script Startup script errors

Discussion on the Lua Scripting capabilities for RaceCapture/Pro. Also see the <a href="http://autosportlabs.net/RaceCapturePro_Lua_Scripting">Lua Scripting Guide</a>

Moderators: JeffC, stieg

Post Reply
GTIspirit
Posts: 249
Joined: Wed Jan 09, 2013 11:20 am
Location: SE Michigan

[Solved] Help with Lua script Startup script errors

Post by GTIspirit »

Could someone please help with this Lua script startup script error, thanks!
[lua] Startup script error: ([string "batt_u_warn = 11..."]:19.0: 'then' expected near '=')
[lua] Failure: Failed to load script
It's like the Lua script doesn't like that first line.
I also don't see any missing "then" statements.
So I'm a bit puzzled why this doesn't work.

Code: Select all

batt_u_warn = 11
tmot_warn = 112
tmot_low = 60
toil_warn = 250
toil_low = 60

setTickRate&#40;10&#41; --TickRate is in Hz

-- Set ShiftX2 configuration parameters with default parameters
-- orientation&#58; 0=normal, 1=inverted &#40;display above LED bar&#41;
-- brightness&#58; 0-100%, 0 = auto brightness
-- can bus&#58; 0=CAN1, 1=CAN2
sxSetConfig&#40;0,0,1&#41;
--warning errors--
if getChannel&#40;"tmot"&#41; < tmot_low or getChannel&#40;"toil"&#41; < toil_low
 then WarnLvl = 1
elseif getChannel&#40;"batt_u"&#41; < batt_u_warn or getChannel&#40;"tmot"&#41; > tmot_warn or getChannel&#40;"toil"&#41; > toil_warn
 then WarnLvl = 2
elseif oilamp_b = 1
 then WarnLvl = 4
else WarnLvl = 0
end

--config shift light
sxCfgLinearGraph&#40;0,0,0,7500&#41; --left to right graph, smooth style, 0 - 7500 RPM range
sxSetLinearThresh&#40;0,0,5800,0,255,0,0&#41; --green at 5800 RPM
sxSetLinearThresh&#40;1,0,6500,255,255,0,0&#41; --yellow at 6500 RPM
sxSetLinearThresh&#40;2,0,7000,255,0,0,0&#41; --red+flash at 7000 RPM
sxSetLinearThresh&#40;3,0,7200,255,0,0,10&#41;  --red flash at 7200rpm
--configure side LED warnings
sxSetAlertThresh&#40;0,0,1,0,0,255,0&#41; -- blue solid right LED at WarnLvl 1
sxSetAlertThresh&#40;1,0,1,0,0,255,0&#41; -- blue solid left LED at WarnLvl 1
sxSetAlertThresh&#40;0,1,2,255,255,0,0&#41; -- yellow solid right LED at WarnLvl 2
sxSetAlertThresh&#40;1,1,2,255,255,0,0&#41; -- yellow solid left LED at WarnLvl 2
sxSetAlertThresh&#40;0,2,4,255,0,0,10&#41; -- red flash right LED at WarnLvl 4
sxSetAlertThresh&#40;1,2,4,255,0,0,10&#41; --red flash left LED at WarnLvl 4

function onTick&#40;&#41;
  sxUpdateAlert&#40;0,WarnLvl&#41; --set right LED
  sxUpdateAlert&#40;1,WarnLvl&#41;  --set left LED
--Start section rev graph
  sxUpdateLinearGraph&#40;getChannel&#40;"rev"&#41;&#41;
  --local nmot = 1900  --10000 all green, 1000 all red flashing,11000 all yellow, 6000 none
  --sxUpdateLinearGraph&#40;nmot&#41;
--End section rev graph
--Start section send GPS speed on CAN
  local speed = getGpsSpeed&#40;&#41;*1.6092*255  --convert to kph and rescale for increased precision
  speedL = bit.band&#40;speed, 0xFF&#41; --mask out high byte
  speedH = bit.rshift&#40;speed, 8&#41; --shift high byte to the right
  --format the speed in a CAN message. Speed is in the first two bytes
  local msg = &#123;speedL,speedH&#125; 
  txCAN&#40;1, 1917, 0, msg&#41;
--End section send GPS speed on CAN
end
Basically, I'm trying to set both side lights as follows:
* Solid blue when engine hasn't warmed up yet.
* Solid yellow for warning
* Blinking red for critical error

Used part of this script for inspiration:
viewtopic.php?t=5849
Last edited by GTIspirit on Fri May 15, 2020 12:52 pm, edited 2 times in total.

GTIspirit
Posts: 249
Joined: Wed Jan 09, 2013 11:20 am
Location: SE Michigan

Post by GTIspirit »

Well, I found a few errors, but it still doesn't work.

* then has to be at the end of the line
* I forgot getChannel in front of oilamp_b, and missed an l in oillamp_b
* The comparator if oillamp_b is set should be double equals ==

But it still seems like Lua script doesn't like the first line.
When the first line is blank I get this:
[lua] Startup script error: ([string "..."]:16.0: attempt to compare nil with number)
When the first line is batt_u_warn = 11 I get this:
[lua] Startup script error: ([string "batt_u_warn = 11..."]:15.0: attempt to compare nil with number)
When the first line is --test then I get this:
[lua] Startup script error: ([string "--test..."]:16.0: attempt to compare nil with number)
It the numbers 15.0 or 16.0 refer to line numbers, then it hints at the first IF statement. But I don't see anything amiss with the syntax. :(

I double checked all the channels for correct spelling, in case that was the reason for the nil. They all agree with the signals in the CAN mapping.

Here's the updated script:

Code: Select all

batt_u_warn = 11
tmot_warn = 112
tmot_low = 60
toil_warn = 250
toil_low = 60

setTickRate&#40;10&#41; --TickRate is in Hz

-- Set ShiftX2 configuration parameters with default parameters
-- orientation&#58; 0=normal, 1=inverted &#40;display above LED bar&#41;
-- brightness&#58; 0-100%, 0 = auto brightness
-- can bus&#58; 0=CAN1, 1=CAN2
sxSetConfig&#40;0,0,1&#41;
--warning errors--
if getChannel&#40;"tmot"&#41; < tmot_low or getChannel&#40;"toil"&#41; < toil_low then 
WarnLvl = 1
elseif getChannel&#40;"batt_u"&#41; < batt_u_warn or getChannel&#40;"tmot"&#41; > tmot_warn or getChannel&#40;"toil"&#41; > toil_warn then 
WarnLvl = 2
elseif getChannel&#40;"oillamp_b"&#41; == 1 then 
WarnLvl = 4
else WarnLvl = 0
end

--config shift light
sxCfgLinearGraph&#40;0,0,0,7500&#41; --left to right graph, smooth style, 0 - 7500 RPM range
sxSetLinearThresh&#40;0,0,5800,0,255,0,0&#41; --green at 5800 RPM
sxSetLinearThresh&#40;1,0,6500,255,255,0,0&#41; --yellow at 6500 RPM
sxSetLinearThresh&#40;2,0,7000,255,0,0,0&#41; --red+flash at 7000 RPM
sxSetLinearThresh&#40;3,0,7200,255,0,0,10&#41;  --red flash at 7200rpm
--configure side LED warnings
sxSetAlertThresh&#40;0,0,1,0,0,255,0&#41; -- blue solid right LED at WarnLvl 1
sxSetAlertThresh&#40;1,0,1,0,0,255,0&#41; -- blue solid left LED at WarnLvl 1
sxSetAlertThresh&#40;0,1,2,255,255,0,0&#41; -- yellow solid right LED at WarnLvl 2
sxSetAlertThresh&#40;1,1,2,255,255,0,0&#41; -- yellow solid left LED at WarnLvl 2
sxSetAlertThresh&#40;0,2,4,255,0,0,10&#41; -- red flash right LED at WarnLvl 4
sxSetAlertThresh&#40;1,2,4,255,0,0,10&#41; --red flash left LED at WarnLvl 4

function onTick&#40;&#41;
  sxUpdateAlert&#40;0,WarnLvl&#41; --set right LED
  sxUpdateAlert&#40;1,WarnLvl&#41;  --set left LED
--Start section rev graph
  sxUpdateLinearGraph&#40;getChannel&#40;"rev"&#41;&#41;
  --local nmot = 1900  --10000 all green, 1000 all red flashing,11000 all yellow, 6000 none
  --sxUpdateLinearGraph&#40;nmot&#41;
--End section rev graph
--Start section send GPS speed on CAN
  local speed = getGpsSpeed&#40;&#41;*1.6092*255  --convert to kph and rescale for increased precision
  speedL = bit.band&#40;speed, 0xFF&#41; --mask out high byte
  speedH = bit.rshift&#40;speed, 8&#41; --shift high byte to the right
  --format the speed in a CAN message. Speed is in the first two bytes
  local msg = &#123;speedL,speedH&#125; 
  txCAN&#40;1, 1917, 0, msg&#41;
--End section send GPS speed on CAN
end

GTIspirit
Posts: 249
Joined: Wed Jan 09, 2013 11:20 am
Location: SE Michigan

Post by GTIspirit »

I did a bit of debugging from the comfort of my desktop computer.
* Commented out various lines of the IF statements to try and isolate which variable (channel) was causing the problem.
* Noticed there was a problem with all the lines. :(
* Implemented some print statements to try and figure out what was the "nil" (All CAN signals are 0 with the RC Track sitting on my desk not connected to anything)
* noticed that printing rev didn't cause a nil, but printing other variables did.
* Went down the list of CAN signals to see what worked, and what didn't.
* Made an interesting observation, only those with 50Hz worked properly. Changing sample rate to a lower rate causes a nil when printing.

Bug report will be submitted.

GTIspirit
Posts: 249
Joined: Wed Jan 09, 2013 11:20 am
Location: SE Michigan

Post by GTIspirit »

The following code works fine.

Code: Select all

batt_u_warn = 11
tmot_warn = 112
tmot_low = 60
toil_warn = 250
toil_low = 60

setTickRate&#40;10&#41; --TickRate is in Hz

-- Set ShiftX2 configuration parameters with default parameters
-- orientation&#58; 0=normal, 1=inverted &#40;display above LED bar&#41;
-- brightness&#58; 0-100%, 0 = auto brightness
-- can bus&#58; 0=CAN1, 1=CAN2
sxSetConfig&#40;0,0,1&#41;
--warning errors--
if getChannel&#40;"tmot"&#41; < tmot_low or getChannel&#40;"toil"&#41; < toil_low then 
WarnLvl = 1
elseif getChannel&#40;"batt_u"&#41; < batt_u_warn or getChannel&#40;"tmot"&#41; > tmot_warn or getChannel&#40;"toil"&#41; > toil_warn then 
WarnLvl = 2
elseif getChannel&#40;"oillamp_b"&#41; == 1 then 
WarnLvl = 4
else WarnLvl = 0
end

--config shift light
sxCfgLinearGraph&#40;0,0,0,7500&#41; --left to right graph, smooth style, 0 - 7500 RPM range
sxSetLinearThresh&#40;0,0,5800,0,255,0,0&#41; --green at 5800 RPM
sxSetLinearThresh&#40;1,0,6500,255,255,0,0&#41; --yellow at 6500 RPM
sxSetLinearThresh&#40;2,0,7000,255,0,0,0&#41; --red+flash at 7000 RPM
sxSetLinearThresh&#40;3,0,7200,255,0,0,10&#41;  --red flash at 7200rpm
--configure side LED warnings
sxSetAlertThresh&#40;0,0,1,0,0,255,0&#41; -- blue solid right LED at WarnLvl 1
sxSetAlertThresh&#40;1,0,1,0,0,255,0&#41; -- blue solid left LED at WarnLvl 1
sxSetAlertThresh&#40;0,1,2,255,255,0,0&#41; -- yellow solid right LED at WarnLvl 2
sxSetAlertThresh&#40;1,1,2,255,255,0,0&#41; -- yellow solid left LED at WarnLvl 2
sxSetAlertThresh&#40;0,2,4,255,0,0,10&#41; -- red flash right LED at WarnLvl 4
sxSetAlertThresh&#40;1,2,4,255,0,0,10&#41; --red flash left LED at WarnLvl 4

function onTick&#40;&#41;
  sxUpdateAlert&#40;0,WarnLvl&#41; --set right LED
  sxUpdateAlert&#40;1,WarnLvl&#41;  --set left LED
--Start section rev graph
  sxUpdateLinearGraph&#40;getChannel&#40;"rev"&#41;&#41;
  --local nmot = 1900  --10000 all green, 1000 all red flashing,11000 all yellow, 6000 none
  --sxUpdateLinearGraph&#40;nmot&#41;
--End section rev graph
--Start section send GPS speed on CAN
  local speed = getGpsSpeed&#40;&#41;*1.6092*255  --convert to kph and rescale for increased precision
  speedL = bit.band&#40;speed, 0xFF&#41; --mask out high byte
  speedH = bit.rshift&#40;speed, 8&#41; --shift high byte to the right
  --format the speed in a CAN message. Speed is in the first two bytes
  local msg = &#123;speedL,speedH&#125; 
  txCAN&#40;1, 1917, 0, msg&#41;
--End section send GPS speed on CAN
println&#40;'Warning level&#58; ' ..WarnLvl&#41;

--println&#40;'ti_1&#58; ' ..getChannel&#40;"ti_1"&#41;&#41; --checked OK
--println&#40;'ath&#58; ' ..getChannel&#40;"ath"&#41;&#41; --checked OK
--println&#40;'ign_1&#58; ' ..getChannel&#40;"ign_1"&#41;&#41; --checked OK
--println&#40;'rev&#58; ' ..getChannel&#40;"rev"&#41;&#41;
--println&#40;'tibase&#58; ' ..getChannel&#40;"tibase"&#41;&#41; --checked OK
--println&#40;'timap&#58; ' ..getChannel&#40;"timap"&#41;&#41; --checked OK
--println&#40;'lamctrl_k&#58; ' ..getChannel&#40;"lamctrl_k"&#41;&#41; --checked OK
--println&#40;'gear&#58; ' ..getChannel&#40;"gear"&#41;&#41; --checked NOK
--println&#40;'batt_u&#58; ' ..getChannel&#40;"batt_u"&#41;&#41; --checked NOK
--println&#40;'tmot&#58; ' ..getChannel&#40;"tmot"&#41;&#41; --checked NOK
--println&#40;'toil&#58; ' ..getChannel&#40;"toil"&#41;&#41; --checked NOK
--println&#40;'lam&#58; ' ..getChannel&#40;"lam"&#41;&#41; --checked OK
--println&#40;'tair&#58; ' ..getChannel&#40;"tair"&#41;&#41; --checked NOK
--println&#40;'mappos&#58; ' ..getChannel&#40;"mappos"&#41;&#41; --checked NOK
--println&#40;'oillamp_b&#58; ' ..getChannel&#40;"oillamp_b"&#41;&#41;  --NOK with 1 5 10 25Hz OK with 50Hz
--println&#40;'fuellap&#58; ' ..getChannel&#40;"fuellap"&#41;&#41; --NOK with 1 25Hz OK with 50Hz
end

Found a bug in the system for CAN rates <50Hz. Bug reported here:
viewtopic.php?p=30092#30092

brentp
Site Admin
Posts: 6282
Joined: Wed Jan 24, 2007 6:36 am

Post by brentp »

Glad you figured it out. Best for debugging is to simplify and reduce until you spot the problem.
Brent Picasso
CEO and Founder, Autosport Labs
Facebook | Twitter

Post Reply