Help creating a Lua script

Hey everyone!

This is my first post here, though I have been building/flying UAV’s for many years now.

I do not have a programming background, but am interested in diving into the world of lua scripting.

I’d like to create a script that can pull positional data from a GPS antenna that is attached to a pixhawk.

Would anyone be willing to assist me in creating this project or point me in the right direction? I’ve read through the lua scripting section of the ardupilot documentation section already. What I’m attempting to do seems very doable, however I’m not really sure where/how to get started.

Any assistance would be greatly appreciated! Thank you.

I’m on the road right now and probably can’t assist super directly, but I have several videos published on the subject.

Post your work here, and we can help guide the solution.

1 Like

Thank you @Yuri_Rage! I’ve actually watched the second video you posted already. I’ll check out the other one.

1 Like

What, exactly, are you intending to do?


What I’d like to do is pull positional data from a GPS that is connected to a pixhawk, on an aircraft in-flight. I’d like the script to run when you power on the aircraft and I’d like the script to run continuously until that aircraft is powered down. I’d like it to compile into a readable format so that it can be parsed and saved to the onboard SD card.

I’m looking for the following:

UTC time
fix type

That info is already available via the onboard logs in a very high degree of detail.

Right but if I want to poll this data once a second or once every two seconds, and then compile it so that I can create……let’s say point cloud data……will it do that?

Have a look here: should do what you need. I think you can output multiple formats other than MATLab.

You can also output KML from Mission Planner’s log analysis tool and glean GPS data from that.

Writing a Lua script for this seems a waste of time.


So the problem with using the native log analysis tool and python (as I understand it) is the size of the data that gets compiled. The files are too large to chew through at the end of flight operations (i.e. would take hours), and so I need something that will pull only the specific data points that I previously mentioned. This will cut down on processing time. In addition, I’m looking for something that will compile the data in an excel spreadsheet. should be able to pull only the tags you need, and I think there’s even an option to output CSV. It’s worth a try.

Otherwise, there are examples of creating text logs with Lua that you can reference for your proposed script.

Right on, I’ll give a try.

So how do I actually use/implement it?
Where do I dl it from?

Sorry for all the questions, and I appreciate your patience with me.

Download link below. You’ll need pymavlink to be available in the environment.

The list of arguments and their usage is near the top of the script.

1 Like

After our Discord chat, I put some effort into the script. It was slightly more challenging than expected due to the lack of RTC bindings that would provide UTC time more directly than having to calculate it from GPS data. If not for that complication, the whole thing would only be about 40 lines!

This is only minimally tested in SITL, and I have not tried it on a vehicle with more than one GPS, so that functionality remains to be seen (though it should log all connected GPS instances).

Each time the autopilot is armed, a new file is created in the root directory of the SD card.

The resulting files should open without issue in Excel. To format the date column, use a custom format with the string dd mmm yyyy, hh:mm:ss.0.

To get coordinate decimal degrees, use an Excel formula like =C2/1e7. This math is best left to Excel vs the logging script, since floating point error will be mitigated better than if it were done in Lua.

Example output in an Excel table:

gps-logger.lua (3.8 KB)

1 Like


This is looking great! I’m going to give it a try today.

Would it be possible to split the date and time into separate columns?


timestamp:gmatch(‘%S+’) would do that. You’d need another header column in the CSV file as well.

EDIT: Had a few mins this morning to show how that would work (script attached below).

Format the time column in Excel with hh:mm:ss.00

If you’re logging more than one GPS, you can use Excel to sort the columns by GPS instance, date, and time (in that order).

gps-logger.lua (4.1 KB)