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.

Thanks.

        wp = mavwp.MAVWPLoader()
               .
               .
               .

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

        for i in range(self.wp.count()):
            msg = self.master.recv_match(type=["MISSION_REQUEST"],blocking=True)
            logger.info(msg)
            self.master.mav.send(self.wp.wp(msg.seq))
            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,
Ben

2 Likes

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.

3 Likes

Cheers Yuri, that does look the go,