RegisterSearchFAQMemberlistUsergroupsLog in
Reply to topic Page 1 of 1
Lua out of memory with two new lines? (Mk1)
Author Message
Reply with quote
Post Lua out of memory with two new lines? (Mk1) 
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 Sad Question

Code:
--Configure virtual channels
SpeedGPS = addChannel("SpeedGPS", 10, 2, 0, 200, "kmh")

--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 (1s or 1000ms is reasonable value)--
MAX_ON_TIME = 500 --in ms (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(ul,ll)
 RPM=getTimerRpm(0)
 --RPM=testRPM
 factor = 0.5 * FREQ_HZ * (1 - (ul - RPM) / (ul - ll))
 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(ul,ll)
 RPM=getTimerRpm(0)
 --RPM=testRPM
 factor = 0.5 * FREQ_HZ * (1 - (ul - RPM) / (ul - ll))
 blink_period = MAX_BLINK_PERIOD / factor
 return blink_period / loop_time
end

-- Ini --
setTickRate(FREQ_HZ)
j = 0
k = 0
l = 0
-- RUN --
function onTick()
setChannel(SpeedGPS, getGpsSpeed() * 1.61)
--start light 1 section--
 if j < ctON(UL1,LL1) then
             setAnalogOut(0,0)
        else
             setAnalogOut(0,10) --with value of 5 measured 2.2V and dim light so set to 10, measured 4.75V
 end       
 if j < ctMAX(UL1,LL1) then
  j = j + 1
 else
  j = 0
 end
--end light 1 section--
--start light 2 section--
 if k < ctON(UL2,LL2) then
             setAnalogOut(1,0)
        else
             setAnalogOut(1,10) --with value of 5 measured 2.2V and dim light so set to 10, measured 4.75V
 end       
 if k < ctMAX(UL2,LL2) then
  k = k + 1
 else
  k = 0
 end
--end light 2 section--
--start light 3 section--
 if l < ctON(UL3,LL3) then
             setAnalogOut(2,0)
        else
             setAnalogOut(2,10) --with value of 5 measured 2.2V and dim light so set to 10, measured 4.75V
 end       
 if l < ctMAX(UL3,LL3) 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:
Quote:
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
Quote:
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
Quote:
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

View user's profile Send private message
Reply with quote
Post  
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:
--Configure virtual channels
SpeedGPS = addChannel("SpeedGPS", 25, 2, 0, 200, "kmh")

-- 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 (1000ms to 1500ms is reasonable value)--
MAX_ON_TIME = 600 --in ms (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(ul,ll)
 RPM=getTimerRpm(0)
 --RPM=testRPM
 factor = 0.5 * FREQ_HZ * (1 - (ul - RPM) / (ul - ll))
 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(ul,ll)
 RPM=getTimerRpm(0)
 --RPM=testRPM
 factor = 0.5 * FREQ_HZ * (1 - (ul - RPM) / (ul - ll))
 blink_period = MAX_BLINK_PERIOD / factor
 return blink_period / loop_time
end

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



With virtual channel sample rate set to 10
Quote:
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
Quote:
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. Sad 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.

View user's profile Send private message
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