Lua out of memory with two new lines? (Mk1)
Posted: Sat Oct 01, 2016 1:45 pm
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
With the two GPS lines commented out the Lua script runs fine, with this info:
Info log
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
Code: Select all
--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
Then with those two lines activated, the log shows the following:lua: Stopping...
lua: Initializing...
lua: Loading lua script (len = 2685): SUCCESS!
lua: memory usage: 11KB
Info log
Debug loglua: 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
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?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
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