Loading Geofences to APM using pymavlink

Hi all,

I am trying to load a “fences.waypoint” file to my APM using python and pymavlink. My setup is the following: i am using Mission planner SITL to simulate and test my code. i also use pymavlink and python 3. My aim is to use built-in Fences and Dijkstra’s algorithm for pathfinding.

This is the screenshot of an example file.

I am able to write Waypoints as a mission to APM and run it on SITL by using the following code.
I couldn’t understand how to use fenceloader = mavwp.MAVFenceLoader() efficiently. any help will be useful.


        wp = mavwp.MAVWPLoader()

        p = mavutil.mavlink.MAVLink_mission_item_message(self.master.target_system, self.master.target_component, ln_seq, ln_frame,
                                                                    ln_current, ln_autocontinue, ln_param1, ln_param2, ln_param3, ln_param4, ln_x, ln_y, ln_z)
        msg = self.master.recv_match(type = ["COMMAND_ACK"],blocking = True)
        logger.info(f"Set home location: {home_location[0]} {home_location[1]}")
        #send waypoint to airframe

        for i in range(self.wp.count()):
            msg = self.master.recv_match(type=["MISSION_REQUEST"],blocking=True)
            logger.info(f"Sending waypoint {msg.seq}")

Hey Oğuz,

So using that Python code you’re loading the waypoint file successfully but would also like to load a similar file to define boundary/fence polygons?

@ktrussell @Yuri_Rage have either of you guys used pymavlink/Python like this for your rovers? I do all this in ROS so don’t know much.

@rmackay9 hi Randy, hey who would have an idea in this case? From keeping up with Kenny and Yuri lately there have been quite a lot of developments with Rover.

Cheers guys,


I replied in this post of yours with an example code from MAVProxy related with fence operations.
Please look at it.

If you also want fence, take a look at this code. This is for fence drawing and uploading module source code of MAVProxy.

1 Like

I haven’t specifically used pymavlink to upload fences, but it looks very straightforward. As Mustafa says above, the MAVProxy code does a nice job of using the mavwp.MAVFenceLoader class and should probably be the definitive example.

It should just be a matter of using native file IO to read the file, skip index 0 (home), and then use an iterator to upload the waypoints with an instantiation of mavwp.MAVFenceLoader.


Cheers Yuri, that does look the go,