Lua out of memory with two new lines? (Mk1)

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: 227
Joined: Wed Jan 09, 2013 11:20 am
Location: SE Michigan

Lua out of memory with two new lines? (Mk1)

Post by GTIspirit »

I'm trying to add a virtual channel to convert GPS speed from MPH to kph. For some reason, those two lines cause Lua to run out of memory :( :?:

Code: Select all

--Configure virtual channels
SpeedGPS = addChannel&#40;"SpeedGPS", 10, 2, 0, 200, "kmh"&#41;

--Pin assignments for shift light--
--RPM is pin 20 / PulseIn 1
--set the jumpers to ON
--First light is pin 6 / Vout1 10Hz 100 100 0.1 / #2 on shift light - green
--Second light is pin 5 / Vout2 10Hz 100 100 0.1 / #1 on shift light - yellow
--Third light is pin 4 / Vout3 10Hz 100 100 0.1 / #3 on shift light - red

-- CONSTANTS --
FREQ_HZ = 25  --1000*1/FREQ_HZ=loop time in ms. e.g. 25Hz=40ms--
loop_time = 1000 * 1 / FREQ_HZ
MAX_BLINK_PERIOD = 1000 --in ms &#40;1s or 1000ms is reasonable value&#41;--
MAX_ON_TIME = 500 --in ms &#40;0.5s or 500ms, half off/on is reasonable value--
testRPM = 2200  --test interface for use on bench--
LL1 = 5500 --light 1 lower threshold to start blinking--
UL1 = 6000 --light 1 upper threshold above which goes solid--
LL2 = 6000 --light 2 lower threshold to start blinking--
UL2 = 6500 --light 2 upper threshold above which goes solid--
LL3 = 6500 --light 3 lower threshold to start blinking--
UL3 = 7000 --light 3 upper threshold above which goes solid--
-- FUNCTIONS --
function ctON&#40;ul,ll&#41;
 RPM=getTimerRpm&#40;0&#41;
 --RPM=testRPM
 factor = 0.5 * FREQ_HZ * &#40;1 - &#40;ul - RPM&#41; / &#40;ul - ll&#41;&#41;
 if factor >= 0.5 * FREQ_HZ then --can't blink faster than tickRate/2--
      on_time = MAX_ON_TIME
     elseif factor < 1 then --can't blink slower than max period--
      on_time = 0
     else
      on_time = MAX_ON_TIME / factor
 end 
 return on_time / loop_time
end

function ctMAX&#40;ul,ll&#41;
 RPM=getTimerRpm&#40;0&#41;
 --RPM=testRPM
 factor = 0.5 * FREQ_HZ * &#40;1 - &#40;ul - RPM&#41; / &#40;ul - ll&#41;&#41;
 blink_period = MAX_BLINK_PERIOD / factor 
 return blink_period / loop_time
end

-- Ini --
setTickRate&#40;FREQ_HZ&#41;
j = 0
k = 0
l = 0
-- RUN --
function onTick&#40;&#41;
setChannel&#40;SpeedGPS, getGpsSpeed&#40;&#41; * 1.61&#41;
--start light 1 section--
 if j < ctON&#40;UL1,LL1&#41; then
             setAnalogOut&#40;0,0&#41;
        else
             setAnalogOut&#40;0,10&#41; --with value of 5 measured 2.2V and dim light so set to 10, measured 4.75V
 end        
 if j < ctMAX&#40;UL1,LL1&#41; then
  j = j + 1
 else
  j = 0
 end
--end light 1 section--
--start light 2 section--
 if k < ctON&#40;UL2,LL2&#41; then
             setAnalogOut&#40;1,0&#41;
        else
             setAnalogOut&#40;1,10&#41; --with value of 5 measured 2.2V and dim light so set to 10, measured 4.75V
 end        
 if k < ctMAX&#40;UL2,LL2&#41; then
  k = k + 1
 else
  k = 0
 end
--end light 2 section--
--start light 3 section--
 if l < ctON&#40;UL3,LL3&#41; then
             setAnalogOut&#40;2,0&#41;
        else
             setAnalogOut&#40;2,10&#41; --with value of 5 measured 2.2V and dim light so set to 10, measured 4.75V
 end        
 if l < ctMAX&#40;UL3,LL3&#41; then
  l = l + 1
 else
  l = 0
 end
--end light 3 section--
end
With the two GPS lines commented out the Lua script runs fine, with this info:
lua: Stopping...
lua: Initializing...
lua: Loading lua script (len = 2685): SUCCESS!
lua: memory usage: 11KB
Then with those two lines activated, the log shows the following:
Info log
lua: Stopping...
lua: Initializing...
lua: Loading lua script (len = 2681): [lua] Memory ceiling hit: 17545 > 16384
ERROR!
lua: startup script error: (not enough memory)
timebase/logging/telemetry sample rate: 50/25/25
Debug log
RAM Freed: 22
[lua] RAM Freed: 27
[lua] RAM Freed: 19
[lua] RAM Freed: 28
[lua] RAM Freed: 22
[lua] RAM Freed: 23
[lua] RAM Freed: 22
[lua] RAM Freed: 24
[lua] RAM Freed: 29
[lua] RAM Freed: 28
[lua] RAM Freed: 25
[lua] RAM Freed: 25
[lua] RAM Freed: 22
[lua] RAM Freed: 21
[lua] RAM Freed: 23
[lua] RAM Freed: 23
[lua] RAM Freed: 21
[lua] RAM Freed: 34
[lua] RAM Freed: 28
[lua] RAM Freed: 20
[lua] RAM Freed: 26
[lua] RAM Freed: 26
[lua] RAM Freed: 29
[lua] RAM Freed: 25
[lua] RAM Freed: 20
[lua] RAM Freed: 20
[lua] RAM Freed: 22
[lua] RAM Freed: 23
[lua] RAM Freed: 20
[lua] RAM Freed: 25
[lua] RAM Freed: 20
[lua] RAM Freed: 22
[lua] RAM Freed: 26
[lua] RAM Freed: 19
[lua] RAM Freed: 1024
[lua] RAM Freed: 0
[lua] RAM Freed: 192
[lua] RAM Freed: 360
[lua] RAM Freed: 340
Sample buffers allocated: 5
timebase/logging/telemetry sample rate: 50/25/25
How can adding two lines make such a difference in memory usage? Really out of memory, or an overflow caused by incorrect script code on those two lines?

Why does Lua script length decrease when those two lines are activated? That sounds strange.....

P.S. yes, I'm running the latest Mk1 firmware 2.8.7

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

Post by GTIspirit »

Interesting, thanks to JMcDonough for providing some tips to get this working. Looks like I was bumping up against the Lua memory limits. This is what works in the end.

Code: Select all

--Configure virtual channels
SpeedGPS = addChannel&#40;"SpeedGPS", 25, 2, 0, 200, "kmh"&#41;

-- CONSTANTS --
FREQ_HZ = 25  --1000*1/FREQ_HZ=loop time in ms. e.g. 25Hz=40ms--
loop_time = 1000 * 1 / FREQ_HZ
MAX_BLINK_PERIOD = 1200 --in ms &#40;1000ms to 1500ms is reasonable value&#41;--
MAX_ON_TIME = 600 --in ms &#40;500ms, half off/on is reasonable value--
--testRPM = 6200  --test interface for use on bench--
LL1 = 5000 --light 1 lower threshold to start blinking--
UL1 = 6000 --light 1 upper threshold above which goes solid--
LL2 = 6000 --light 2 lower threshold to start blinking--
UL2 = 6500 --light 2 upper threshold above which goes solid--
LL3 = 6500 --light 3 lower threshold to start blinking--
UL3 = 7000 --light 3 upper threshold above which goes solid--
-- FUNCTIONS --
function ctON&#40;ul,ll&#41;
 RPM=getTimerRpm&#40;0&#41;
 --RPM=testRPM
 factor = 0.5 * FREQ_HZ * &#40;1 - &#40;ul - RPM&#41; / &#40;ul - ll&#41;&#41;
 if factor >= 0.5 * FREQ_HZ then --can't blink faster than tickRate/2--
      on_time = MAX_ON_TIME
     elseif factor < 1 then --can't blink slower than max period--
      on_time = 0
     else
      on_time = MAX_ON_TIME / factor
 end 
 return on_time / loop_time
end

function ctMAX&#40;ul,ll&#41;
 RPM=getTimerRpm&#40;0&#41;
 --RPM=testRPM
 factor = 0.5 * FREQ_HZ * &#40;1 - &#40;ul - RPM&#41; / &#40;ul - ll&#41;&#41;
 blink_period = MAX_BLINK_PERIOD / factor 
 return blink_period / loop_time
end

-- Ini --
setTickRate&#40;FREQ_HZ&#41;
j = 0
k = 0
l = 0
-- RUN --
function onTick&#40;&#41;
setChannel&#40;SpeedGPS, getGpsSpeed&#40;&#41;*1.61&#41;
--start light 1 section--
 if j < ctON&#40;UL1,LL1&#41; then
             setAnalogOut&#40;0,0&#41;
        else
             setAnalogOut&#40;0,10&#41; 
 end        
 if j < ctMAX&#40;UL1,LL1&#41; then
  j = j + 1
 else
  j = 0
 end
--end light 1 section--
--start light 2 section--
 if k < ctON&#40;UL2,LL2&#41; then
             setAnalogOut&#40;1,0&#41;
        else
             setAnalogOut&#40;1,10&#41; 
 end        
 if k < ctMAX&#40;UL2,LL2&#41; then
  k = k + 1
 else
  k = 0
 end
--end light 2 section--
--start light 3 section--
 if l < ctON&#40;UL3,LL3&#41; then
             setAnalogOut&#40;2,0&#41;
        else
             setAnalogOut&#40;2,10&#41;
 end        
 if l < ctMAX&#40;UL3,LL3&#41; then
  l = l + 1
 else
  l = 0
 end
--end light 3 section--
end

With virtual channel sample rate set to 10
lua: Loading lua script (len = 2139):
[lua] Memory ceiling hit: 16955 > 16384
ERROR!
lua: startup script error: (not enough memory)
timebase/logging/telemetry sample rate: 50/25/25
Same script, with virtual channel sample rate set to 25
lua: Loading lua script (len = 2140): SUCCESS!
lua: memory usage: 11KB
timebase/logging/telemetry sample rate: 50/25/25
Conclusion is that
1. yes, I'm bumping up against the Lua script memory limit with my bloated variable blinky rate shift light. Removing comment lines helped reduce memory usage to just below the limit.
2. when at the limit it makes a difference if virtual channel sample rate matches tickRate.

So now I can't add any more virtual channels. :( Looks like I'll have to try and get clever to update the variable blinky rate shift light with some more sub-functions to skinny it down.

Post Reply