Controlling a plane using LUA


I am currently looking for a way to control a plane using a LUA script.

In general my target is to let one plane follow another one much like it is currently available for copters in follow mode.

If I understood that correctly since Arduplane 4.2 the command vehicle:set_target_throttle_rate_rpy
is available to control the plane in certain flight modes. In my opinion and for my use case the downside of this command is that I have to run control loops for throttle, roll-, pitch- and yaw-rate which I find a bit complicated for what I would like to archive.

For Arducopter there are commands like vehicle:set_target_angle_and_climbrate or vehicle:set_target_location which would allow a much easier control.

Is there a way to control the plane using LUA in a similay way to using a RC control? e.g. putting the plane in FBWA/FBWB and sending throttle/speed, target roll, target pitch/height or just sending target speed + target direction + target height?

I hope someone can give me a hint or send me in the right direction for further research. Although I am using Arduplane since at least 3 years as a user, looking at the actual code, using scripting and trying to do something outside of the standard features is new to me :slight_smile:

See the examples in this folder:

ardupilot/libraries/AP_Scripting/examples/Aerobatics at master · ArduPilot/ardupilot (

The scripts there command aerobatic maneuvers, and surely a subset of those methods can be leveraged to your use.

I am not well versed in ArduPlane (or follow mode for that matter), so I’m afraid I can’t be of specific help with respect to scripted formation flying.

I think it may be possible to use GUIDED mode and set the target as the other aircraft’s location while keeping the speed similar (modulating it to keep a safe distance, and keeping in mind that any mismatched turn geometry will cause variations in follow distance). This method will require the use of a GCS to monitor both aircraft and send the commands.

Thank you for your suggestions.
I was also looking at the aerobatics examples and the use of control loops for pitch, roll, yaw, height scared be a bit to be honest, especially considering that there must be some form of control loop within the code to stabilize the plane doing the movement requested by vehicle:set_target_throttle_rate_rpy

After a bit more research into guided mode I found the following mavlink commands available for Arduplane that would fit my use case perfectly:

Would it be possible to provide a similar interface for lua scripts or is there a way to “inject” mavlink messages using lua?

The following Lua bindings probably satisfy most of what you need:

singleton AP_Vehicle method set_target_pos_NED boolean Vector3f boolean float -360 +360 boolean float'skip_check boolean boolean
singleton AP_Vehicle method set_target_posvel_NED boolean Vector3f Vector3f
singleton AP_Vehicle method set_target_posvelaccel_NED boolean Vector3f Vector3f Vector3f boolean float -360 +360 boolean float'skip_check boolean
singleton AP_Vehicle method set_target_velaccel_NED boolean Vector3f Vector3f boolean float -360 +360 boolean float'skip_check boolean
singleton AP_Vehicle method set_target_velocity_NED boolean Vector3f

The expected argument values can be gleaned from AP_Vehicle.h in the vicinity of line 195.

There is presently no binding for sending MAVLink messages via Lua.

Unfortunately as far as I can see these commands are not available for Plane, only for Copter?

Indeed. It appears that the virtual set_*_NED methods are only overriden in Copter. I missed that in my cursory search of the code.