HolyC0w

lab6_pt2_FixedAltitude

Apr 12th, 2023 (edited)
972
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.22 KB | None | 0 0
  1. #This program sets a fixed altitude of 20 meters for all waypoints. The program also sets the #vehicle mode to RTL (Return to Launch) instead of LAND, which will cause the vehicle to #automatically return to its launch point and land.
  2.  
  3. from dronekit import connect, VehicleMode, Command
  4. from pymavlink import mavutil
  5. import time
  6.  
  7. vehicle = connect('udp:127.0.0.1:14550')
  8.  
  9. vehicle.mode = VehicleMode("GUIDED")
  10.  
  11. ground_altitude = vehicle.location.global_frame.alt # so it somehow pisses itself off if the global frame is used
  12. target_altitude = 20.0
  13. altitude = ground_altitude + target_altitude
  14.  
  15. cmds = vehicle.commands
  16. # dude refuses to work if commands are not cleared even before actually executing anything reeeee
  17. cmds.clear()
  18.  
  19. # waypoints
  20. wp1 = vehicle.location.global_frame
  21. wp1.alt = altitude
  22. wp2 = vehicle.location.global_frame
  23. wp2.lat += 0.0001
  24. wp2.lon += 0.0001
  25. wp2.alt = altitude
  26. wp3 = vehicle.location.global_frame
  27. wp3.lat += 0.0001
  28. wp3.lon -= 0.0001
  29. wp3.alt = altitude
  30.  
  31. # return to launch stuff for MAV, according to some random reddit dude 11 years ago, should always have the links at 0
  32. cmd = Command(0, 0, 0, mavutil.mavlink.MAV_FRAME_GLOBAL_RELATIVE_ALT,
  33.               mavutil.mavlink.MAV_CMD_NAV_RETURN_TO_LAUNCH, 0, 0, 0, 0, 0, 0,
  34.               0, 0, 0)
  35. # adding waypoints
  36. cmds.add(cmd)
  37. cmds.upload()
  38.  
  39. # arm and yeet
  40. vehicle.armed = True
  41. vehicle.simple_takeoff(target_altitude)
  42.  
  43. while True:
  44.     if abs(vehicle.location.global_relative_frame.alt - altitude) < 1.0:
  45.         print("Reached target altitude")
  46.         break
  47.     time.sleep(1)
  48.  
  49. vehicle.mode = VehicleMode("AUTO")
  50. print("Starting mission")
  51. vehicle.commands.next = 0
  52.  
  53. # monitor it
  54. while True:
  55.     nextwaypoint = vehicle.commands.next
  56.     if nextwaypoint == len(vehicle.commands):
  57.         print("Mission complete")
  58.         vehicle.mode = VehicleMode("RTL")
  59.         break
  60.     time.sleep(1)
  61.  
  62. while True:
  63.     if vehicle.mode.name == "RTL":
  64.         print("Vehicle returning to launch point")
  65.         break
  66.     time.sleep(1)
  67.  
  68. while True:
  69.     if vehicle.mode.name == "LAND":
  70.         print("Vehicle landed")
  71.         break
  72.     time.sleep(1)
  73.  
  74. # sayonara
  75. vehicle.close()
  76.  
  77. #####################################################################
  78. #####################################################################
  79. from dronekit import connect, VehicleMode, Command
  80. from pymavlink import mavutil
  81. import time
  82.  
  83. vehicle = connect('udp:127.0.0.1:14550')
  84.  
  85. vehicle.mode = VehicleMode("GUIDED")
  86.  
  87. ground_altitude = vehicle.location.global_frame.alt # so it somehow pisses itself off if the global frame is used
  88. target_altitude = 20.0
  89. altitude = ground_altitude + target_altitude
  90.  
  91. cmds = vehicle.commands
  92. # dude refuses to work if commands are not cleared even before actually executing anything reeeee
  93. cmds.clear()
  94.  
  95. # waypoints
  96. wp1 = vehicle.location.global_frame
  97. wp1.alt = altitude
  98. wp2 = vehicle.location.global_frame
  99. wp2.lat += 0.0001
  100. wp2.lon += 0.0001
  101. wp2.alt = altitude
  102. wp3 = vehicle.location.global_frame
  103. wp3.lat += 0.0001
  104. wp3.lon -= 0.0001
  105. wp3.alt = altitude
  106.  
  107. # return to launch stuff for MAV, according to some random reddit dude 11 years ago, should always have the links at 0
  108. cmd = Command(0, 0, 0, mavutil.mavlink.MAV_FRAME_GLOBAL_RELATIVE_ALT,
  109.               mavutil.mavlink.MAV_CMD_NAV_RETURN_TO_LAUNCH, 0, 0, 0, 0, 0, 0,
  110.               0, 0, 0)
  111. # adding waypoints
  112. cmds.add(cmd)
  113. cmds.upload()
  114.  
  115. # arm and yeet
  116. vehicle.armed = True
  117. vehicle.simple_takeoff(target_altitude)
  118.  
  119. while True:
  120.     if abs(vehicle.location.global_relative_frame.alt - altitude) < 1.0:
  121.         print("Reached target altitude")
  122.         break
  123.     time.sleep(1)
  124.  
  125. vehicle.mode = VehicleMode("AUTO")
  126. print("Starting mission")
  127. vehicle.commands.next = 0
  128.  
  129. # monitor it
  130. while True:
  131.     nextwaypoint = vehicle.commands.next
  132.     if nextwaypoint == len(vehicle.commands):
  133.         print("Mission complete")
  134.         vehicle.mode = VehicleMode("RTL")
  135.         break
  136.     time.sleep(1)
  137.  
  138. while True:
  139.     if vehicle.mode.name == "RTL":
  140.         print("Vehicle returning to launch point")
  141.         break
  142.     time.sleep(1)
  143.  
  144. while True:
  145.     if vehicle.mode.name == "LAND":
  146.         print("Vehicle landed")
  147.         break
  148.     time.sleep(1)
  149.  
  150. # sayonara
  151. vehicle.close()
  152.  
Advertisement
Add Comment
Please, Sign In to add comment