HolyC0w

lab6_pt3_VaryingAltitude

Apr 12th, 2023
1,294
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.57 KB | None | 0 0
  1. # Move to each waypoint in turn with a varying altitude
  2. #for waypoint in waypoints:
  3.  
  4.  
  5. from dronekit import connect, VehicleMode, LocationGlobalRelative, Command
  6. from pymavlink import mavutil
  7. import time
  8.  
  9. vehicle = connect('udp:127.0.0.1:14550')
  10.  
  11. vehicle.mode = VehicleMode("GUIDED")
  12.  
  13. # okay, so san francisco is 37.7749° N, 122.4194° W
  14. waypoints = [
  15.     LocationGlobalRelative(37.6205, -122.3880, 10), # altitude of 10 meters
  16.     LocationGlobalRelative(37.6215, -122.3860, 20), # altitude of 20 meters
  17.     LocationGlobalRelative(37.6210, -122.3840, 30), # altitude of 30 meters
  18. ]
  19. # brother decided to stay under 30, somesort of a failsafe mechanism
  20.  
  21. cmds = vehicle.commands
  22. cmds.clear()
  23.  
  24. for i, wp in enumerate(waypoints):
  25.     cmd = Command(0, 0, 0, mavutil.mavlink.MAV_FRAME_GLOBAL_RELATIVE_ALT,
  26.                   mavutil.mavlink.MAV_CMD_NAV_WAYPOINT, 0, 0, 0, 0, 0, 0,
  27.                   wp.lat, wp.lon, wp.alt)
  28.     cmds.add(cmd)
  29. cmds.upload()  
  30.  
  31. vehicle.armed = True
  32. vehicle.simple_takeoff(waypoints[0].alt)
  33.  
  34. while True:
  35.     if abs(vehicle.location.global_relative_frame.alt - waypoints[0].alt) < 1.0:
  36.         print("Reached target altitude")
  37.         break
  38.     time.sleep(1)
  39.  
  40. vehicle.mode = VehicleMode("AUTO")
  41. print("Starting mission")
  42. vehicle.commands.next = 0
  43.  
  44. while True:
  45.     nextwaypoint = vehicle.commands.next
  46.     if nextwaypoint == len(vehicle.commands):
  47.         print("Mission complete")
  48.         vehicle.mode = VehicleMode("RTL")
  49.         break
  50.     time.sleep(1)
  51.  
  52. while True:
  53.     if vehicle.mode.name == "RTL":
  54.         print("Vehicle returning to launch point")
  55.         break
  56.     time.sleep(1)
  57.  
  58. while True:
  59.     if vehicle.mode.name == "LAND":
  60.         print("Vehicle landed")
  61.         break
  62.     time.sleep(1)
  63. vehicle.close()
  64.  
  65. #####################################################################
  66. #####################################################################
  67. from dronekit import connect, VehicleMode, LocationGlobalRelative, Command
  68. from pymavlink import mavutil
  69. import time
  70.  
  71. vehicle = connect('udp:127.0.0.1:14550')
  72.  
  73. vehicle.mode = VehicleMode("GUIDED")
  74.  
  75. # okay, so san francisco is 37.7749° N, 122.4194° W
  76. waypoints = [
  77.     LocationGlobalRelative(37.6205, -122.3880, 10), # altitude of 10 meters
  78.     LocationGlobalRelative(37.6215, -122.3860, 20), # altitude of 20 meters
  79.     LocationGlobalRelative(37.6210, -122.3840, 30), # altitude of 30 meters
  80. ]
  81. # brother decided to stay under 30, somesort of a failsafe mechanism
  82.  
  83. cmds = vehicle.commands
  84. cmds.clear()
  85.  
  86. for i, wp in enumerate(waypoints):
  87.     cmd = Command(0, 0, 0, mavutil.mavlink.MAV_FRAME_GLOBAL_RELATIVE_ALT,
  88.                   mavutil.mavlink.MAV_CMD_NAV_WAYPOINT, 0, 0, 0, 0, 0, 0,
  89.                   wp.lat, wp.lon, wp.alt)
  90.     cmds.add(cmd)
  91. cmds.upload()  
  92.  
  93. vehicle.armed = True
  94. vehicle.simple_takeoff(waypoints[0].alt)
  95.  
  96. while True:
  97.     if abs(vehicle.location.global_relative_frame.alt - waypoints[0].alt) < 1.0:
  98.         print("Reached target altitude")
  99.         break
  100.     time.sleep(1)
  101.  
  102. vehicle.mode = VehicleMode("AUTO")
  103. print("Starting mission")
  104. vehicle.commands.next = 0
  105.  
  106. while True:
  107.     nextwaypoint = vehicle.commands.next
  108.     if nextwaypoint == len(vehicle.commands):
  109.         print("Mission complete")
  110.         vehicle.mode = VehicleMode("RTL")
  111.         break
  112.     time.sleep(1)
  113.  
  114. while True:
  115.     if vehicle.mode.name == "RTL":
  116.         print("Vehicle returning to launch point")
  117.         break
  118.     time.sleep(1)
  119.  
  120. while True:
  121.     if vehicle.mode.name == "LAND":
  122.         print("Vehicle landed")
  123.         break
  124.     time.sleep(1)
  125. vehicle.close()
  126.  
Advertisement
Add Comment
Please, Sign In to add comment