Applying GeoFence on pixhawk Autopilot using MAVLink

Hey guys,

I’m experimenting with a pixhawk HKPilot32 autopilot and am trying to set up geofence using MAVLink through an Arduino.

As far as my research goes, it appears MAVLink supports enabling the geofence through command MAV_CMD_DO_FENCE_ENABLE. Also, MAV_MISSION_TYPE reveals if fenced mission is enabled. Although I can’t seem to find any command that sets the lat/lng of a polygon geofence, it seems nuttX controller module can handle this through navigator command.

I can confirm that QGC allows the (circle and polygon) geofence to be set (I haven’t actually tested it yet). However, I’m not sure if QGC is doing this through NuttX or uORB.

So, my questions are as follows:

  1. Is there any MAVLink command that allows circle and polygon geofence to be set?
  2. How is a MAV_CMD_* sent to pixhawk? Should mavlink_msg_mission_item_encode() be used along with COMMAND_LONG and pass MAV_CMD_DO_FENCE_ENABLE as command?
  3. Is there a direct way to inquire which commands an autopilot firmware supports?
  4. Is QGC communicating with pixhawk through uORB or NuttX to set the geofence coordinates and enable/disable them?

I’m very new to autopilots (DroneSDK, MAVLink, pixhawk) so any help is appreciated!


  1. To set a circle (radius) geofence, there is just this parameter:

  2. Good question. So I understand that you can set a geofence “mission plan” by setting MAV_MISSION_TYPE to geofence and then you upload that. I started doing that in this pull request for the Dronecode SDK as well. I don’t think you need to send the command to enable it because I don’t see this command being used/parsed by the PX4 firmware anyway.

  3. Not really :frowning: , I usually use grep and also grep without the MAV_CMD_ prefix.

  4. The communication is through MAVLink, using a param for the radius, and using mission items with geofence type for polygons.

Thanks for your reply!

  1. I’m experimenting with the commands and the capabilities so I’ll test the radius geofence from the parameter list too.

Interestingly, I came to find out that the polygon geofence can be set through geofence.txt file on SD card - pending testing. I’m looking into how the Return Point/Rally Points and action-on-breach can be set through MAVLink and how to take back control after it has returned to the specified Return Point.
The resources I’ve been coming across are from ArduPilot and Pixhawk and some seem to be old. So, it makes me wonder, how similar are ArduPilot and Pixhawk systems when it comes to navigation? Are there few subtle differences between these two or are they different in many ways?

  1. No, I’m afraid I’m not uploading anything as the goal is to set these configurations through MAVLink. Thanks for the link to your pull request. I’m looking further into this.

  2. How do you go about executing grep?

  3. So no uORB for comms - gotcha; NuttX is an RTOS - silly me :grin: How are the mission items sent over MAVLink?

So Pixhawk is the hardware and PX4 is the software, just to clarify. And in general the mission and geofence protocol is the same but there are some differences that I know of (e.g. ArduPilot missions are 1-based, PX4 missions are 0-based), and also not all command are interpreted the same or supported for both.

\2. How do you go about executing grep?

Oh, that’s just searching in code. Use your favorite editor or IDE, or GitHub.
Or look at the grep manual :slight_smile:

\3. Have a look at the docs here:

1 Like

Ah, then I can’t use the headers from ardupilotmega for pixhawk. Cool cool

Will look into the the rest and post updates as I progress. Thanks for the support, Julian!

1 Like