Lua Scripts not working on Pixhawk 4

I am unable to run lua scripts on a Pixhawk 4. I have successfully run the scripts on an orange cube, so I believe I followed the process correctly and I know the scripts work.

I get an error message:
Lua: Couldn’t allocate a lua state

I found a forum post stating it was a memory problem, so I changed the SCR_HEAP_SIZE as per Lua Scripts — Copter documentation. It was originally 1024, and increasing it to 2048 gave me the same error massage. When I tried increasing it to 4096 I got a different error:
Lua: Panic: not enough mem, increase scr_heap_size

I also had trouble staying connected to the drone with Mission Planner when I increased the heap size to 4096.

If I increase the heap to the maximum, 1048576, I get different errors:
Scripting: Unable to allocate memory
PreArm: Scripting out of memory

Are the errors due to the size of the heap, or are there other parameters that require changes?

I am using an 8GB SD card.

Try 44000. Your initial values were way too low, and your last attempt took all the RAM away from other processes.


Thank you for sharing your insight.

I can run the hello_world.lua script when I set it to 44000, but it fails if I try adding other scripts. I tried adding more scripts that are essentially hello_world with a different string, and I can only add two or three scripts before I get Lua: Panic: not enough mem, increase scr_heap_size

When I try increasing the 44000 it still disconnects Mission Planner from the drone.

How do you determine the correct heap size for the given scripts?

I see the options in Mission Planner are 1024 to 1048576, and the default is 102400. Do you know why I didn’t have the default column previously?

At 102400, I can run a few small scripts, but I would like to know more about how to determine the correct size of the heap for the scripts that are uploaded to the drone. What happens to the drone if it’s flying and there is not enough memory? Is there a failsafe that must be set?

Please point me to documentation about this that I may have missed.

Thank you.

No idea why you didn’t have the default set. 100k of scripting memory will run more than just a small script or two. You can safely increase it within reason (an order of magnitude is probably not “within reason”). The scripting engine will crash/quit gracefully and allow critical processes to continue if it overruns the allotted memory space (I was a bit hyperbolic in my first reply here). If scripting is performing critical functions, I recommend incremental testing before fully deploying all desired/required scripts.

I appreciate your quick responses.

I have it on the default, 102400, but it stopped working.

I was able to run some small scripts, and now I am getting error messages again:
Scripting: unable to allocate memory
Scripting: out of memory

I have not changed any parameters since it started working when I set the SCR_HEAP_SIZE to 102400, and the only thing I have in there now is the hello_world.lua script.

I’m afraid it’s likely a hardware limitation. The typical Pixhawk 4 specs are a slightly dated processor with 512kb of RAM, where most newer autopilots have an H7 processor and 1MB or more RAM.

That was my concern, but I it seems odd that it worked for a while. I’ll use a newer one.

Thank you for taking the time to help me.

sigh same problem, I can’t get LUA scripts to run. This is my script:

local number = math.random()

function update() – this is the loop which periodically runs
gcs:send_text(0, “hello, world”) – send the traditional message

gcs:send_named_float(‘Lua Float’,number) – send a value
number = number + math.random() – change the value

return update, 1000 – reschedules the loop

I have it stored in a file called circle.lua and uploaded into the /APM/scripts/ directory:


SCR_ENABLE is set:


I reboot Pixhawk (power-off/power-on) and I don’t get the expected once-per-second message in Mission Planner messages window

Can anyone offer steps to diagnose?? Ardurover 4.3.0-beta2

@Yuri_Rage ?

EDIT: Disregard my previous. There is no entry point to your function. Add:

return update()

as the very last line of your script.

“number” is a poor variable name, as it is also a type name. The script will almost certainly work with the added line, but it’s poor practice to use a type name as a variable identifier.

Don’t use spaces in named float identifiers. They are limited to 5 characters, and white space should be ignored (but also avoided in the script itself).

After uploading a script change, do I need to recycle the power, or can I do a soft reboot (from the control-F screen in MAVProxy:)
Or is there some other way to cause ardurover to re-load the scripts directory?

Actions page. Top left drop-down. There’s a script stop/restart action.

Thanks! I get this error:

Ah, I see you’re using a Pixhawk 4, which, as I recall, has some RAM limitations as well. Decrease SCR_HEAP_SIZE to around 44000-64000 and see what happens.

Working now. Thank you!

Hello, i was facing same problem with hello world message on my pixhawk 6C, but after following your steps it is working.

It only works when i go to action tab and do stop and restart action.

When i connect my pixhwak to mission planner it doesn’t give hello world message, even after arming.

It only gives message after stop and restart action. Is this the usual case? Does all lua scrips start automatically when we connect the FC to mission planner and arm it or everytime stop and restart action must be performed?

Scripts start at boot. That one exits after running once.

1 Like

Hi gang. This worked for me! Thanks @Yuri_Rage - your suggestion about the Pixhawk 4 needing less script memory allocated worked.