Skip to content

Instantly share code, notes, and snippets.

@Paulooh007
Created May 25, 2023 17:29
Show Gist options
  • Save Paulooh007/50a87f78acc4b9f823c77bc6e5b3963a to your computer and use it in GitHub Desktop.
Save Paulooh007/50a87f78acc4b9f823c77bc6e5b3963a to your computer and use it in GitHub Desktop.
import math
from pymavlink import mavutil
class mission_item:
def __init__(self, l, current, x,y,z):
self.seq = l
self.frame = mavutil.mavlink.MAV_FRAME_GLOBAL_RELATIVE_ALT
self.command = mavutil.mavlink.MAV_CMD_NAV_WAYPOINT
self.current = current
self.auto = 1
self.param1 = 0.0
self.param2 = 2.00
self.param3 = 20.00
self.param4 = math.nan
self.param5 = x
self.param6 = y
self.param7 = z
self.mission_type = 0
def arm(the_connection):
print("*-- Arming*")
the_connection.mav.command_long_send(
the_connection.target_system,
the_connection.target_component,
mavutil.mavlink.MAV_CMD_COMPONENT_ARM_DISARM,
0,1,0,0,0,0,0,0
)
ack(the_connection, "COMMAND_ACK")
def takeoff(the_connection):
print("*-- Takeoff Initiated*")
the_connection.mav.command_long_send(
the_connection.target_system,
the_connection.target_component,
mavutil.mavlink.MAV_CMD_NAV_TAKEOFF,
0,0,0,0,math.nan,0,0,10)
ack(the_connection, "COMMAND_ACK")
def upload_mission(the_connection, mission_items):
n = len(mission_items)
print("*-- Sending Message Out*")
the_connection.mav.mission_count_send(
the_connection.target_system,
the_connection.target_component,
n,0)
ack(the_connection, "MISSION_REQUEST")
for waypoint in mission_items:
print("*-- Creating a waypoint*")
the_connection.mav.mission_item_send(
the_connection.target_system,
the_connection.target_component,
waypoint.seq,
waypoint.frame,
waypoint.command,
waypoint.current,
waypoint.auto,
waypoint.param1,
waypoint.param2,
waypoint.param3,
waypoint.param4,
waypoint.param5,
waypoint.param6,
waypoint.param7,
waypoint.mission_type
)
if waypoint != mission_items[-1]:
ack(the_connection, "MISSION_REQUEST")
ack(the_connection, "MISSION_ACK")
def set_return(the_connection):
print("*-- Set Return To Launch*")
the_connection.mav.command_long_send(
the_connection.target_system,
the_connection.target_component,
mavutil.mavlink.MAV_CMD_NAV_RETURN_TO_LAUNCH,
0,0,0,0,0,0,0,0)
ack(the_connection, "COMMAND_ACK")
def start_mission(the_connection):
print("*-- Mission Start*")
the_connection.mav.command_long_send(
the_connection.target_system,
the_connection.target_component,
mavutil.mavlink.MAV_CMD_MISSION_START,
0,0,0,0,0,0,0,0)
ack(the_connection, "COMMAND_ACK")
def ack(the_connection, keyword):
print("-- Message Read " + \
str(the_connection.recv_match(type=keyword, blocking=True))
)
if __name__ == "__main__":
print("-- Program Started")
# the_connection = mavutil.mavlink_connection('COM7', 57600) #telemetry
the_connection = mavutil.mavlink_connection('udpin:172.26.64.1:14550')
# Wait for the first heartbeat
while the_connection.target_system == 0:
print("-- Checking for heartbeat")
the_connection.wait_heartbeat()
print("Heartbeat from system (system %u component %u)" % \
(the_connection.target_system,
the_connection.target_component))
mission_waypoints = []
mission_waypoints.append(mission_item(0, 0, 7.5197184, 4.5203102, 10))
mission_waypoints.append(mission_item(1, 0, 7.5183386, 4.5264412, 10))
mission_waypoints.append(mission_item(2, 0, 7.5183386, 4.5264412, 5))
upload_mission(the_connection, mission_waypoints)
arm(the_connection)
takeoff(the_connection)
start_mission(the_connection)
for mission_item in mission_waypoints:
print("-- Message Read " +
str(the_connection.recv_match(
type="MISSION_ITEM_REACHED",
condition="MISSION_ITEM_REACHED.seq == {0}".format(mission_item.seq),
blocking=True)))
set_return(the_connection)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment