Lua with modules embedded in firmware, cannot find module

Hello,

I have a Lua script that sends MAVLink messages, and hence imports the MAVLink module thussly:
local mavlink_msgs = require("MAVLink/mavlink_msgs")

I’ve tested this on a Cube with an SD card and it works perfectly.
The file sctricture looks like this:

sd/
    scripts/
        myscript.lua
        modules/
            MAVLink/
                mavlink_msgs.lua
                mavlink_msg_HEARTBEAT.lua

However now I’m trying to embed it in the firmware to use on a SkyStarsH7HD, which doesn’t have an SD card, and it’s throwing the following error:

Debug: Lua: @ROMFS/scripts/myscript.lua:164: module ‘MAVLink/mavlink_msgs’ not found: MAVLink/mavlink_msgs …

So myscript.lua is obviously running from onboard flash, therefore the embedding process worked, at least for the main script.

I have tried compiling the firmware with the script in ardupilot/ROMFS_custom, and also in ardupilot/libraries/AP_HAL_Chibios/hwdef/SkystarsH7HD
Both with the same results

In these cases, the file structure is the same, a la:


ardupilot/
    ROMFS_custom/
        scripts/
            myscript.lua
            modules/
                MAVLink/
                    mavlink_msgs.lua
                    mavlink_msg_HEARTBEAT.lua

I tried changing my import line to
local mavlink_msgs = require("@ROMFS/scripts/modules/MAVLink/mavlink_msgs")
but no dice.

Has anybody successfully imported modules with scripts embedded in firmware?

Thanks in advance!

First, I have no knowledge about LUA scripts but I want to learn so I read all these Threads and try to analysis.
In this case I might see a mismatch in your file structure:
I believe you are searching for mavlink_msgs in a directory MAVLink/ but this does’t exist in your file structure.

Oh yes, good catch. Sorry, that was a mistake in my post, the code location was correct. I have corrected the original post.

Currently I am experimenting with a similar setup, trying to receive MAVLink messages, and I used the same Lua modules from the repository. But I couldn’t figure out where to put them on the SD card (except the main script), because the documentation only stated

Upload scripts (files with extension .lua) to the autopilot’s SD card’s APM/scripts folder.

Which I did, but where to put the required modules? I tried putting them next to the script like in APM/scripts/MAVLink/... but that relative path did not work. Only when defining an absolute path in the call, e.g. require("/APM/scripts/MAVLink/mavlink/msgs").

Now I see that modules are searched in module subdirectory next to the current script. And that is the actual relative path. Thank you @phinchey for pointing that out and providing the correct directory structure! I nowhere did find this bit of information …


About your problem, is your require call with @ROMFS an absolute path? Or maybe trying "/scripts/modules/..."?

Thanks for the ideas, everyone. And welcome to the forum, Takyu :slight_smile:
So I went back and tried:

  • @ROMFS/scripts/modules/MAVLink/mavlink_msgs
  • ROMFS/scripts/modules/MAVLink/mavlink_msgs
  • scripts/modules/MAVLink/mavlink_msgs
  • modules/MAVLink/mavlink_msgs
  • MAVLink/mavlink_msgs
  • ./modules/MAVLink/mavlink_msgs
  • ./MAVLink/mavlink_msgs

thumbs down for all of them.

Now I want to just print out the filesystem, like ‘ls’ in linux. Anyone have any idea how to do that?
I’m trying this but it just returns nil

function scandir()
  local file = io.open('.', 'r')
  gcs:send_text( 6, file)
end
scandir()

I’ve tried opening ‘.’, ‘/’, and ‘myscript.lua’, all return nil

It’s not a traditional file system, and that may be problematic. I will try and escalate this on Discord and see if we can get some answers.

EDIT: Try using the dev version of ArduPilot. A lot of fixes are in place in the master branch that have not been backported to 4.5.

Yes, that did it! Thanks Yuri!

I checked out master, and now local mavlink_msgs = require("MAVLink/mavlink_msgs") works when embedded from ardupilot/libraries/AP_HAL_Chibios/hwdef/SkystarsH7HD/scripts/modules/MAVLink/mavlink_msgs.lua just as well as when included on the SD card at /APM/scripts/modules/MAVLink/mavlink_msgs.lua