HolyC0w

lab5_pt1_takeoff_land_new location

Apr 12th, 2023
795
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.51 KB | None | 0 0
  1. ######## Program to take off the drone and land in new location:
  2.  
  3. from dronekit import connect, VehicleMode, LocationGlobalRelative
  4. import time
  5.  
  6. # Connect to the vehicle
  7. vehicle = connect('udp:127.0.0.1:14550')
  8.  
  9. # Arm and take off
  10. vehicle.mode = VehicleMode("GUIDED")
  11. vehicle.armed = True
  12. vehicle.simple_takeoff(10)
  13.  
  14. # Wait for the drone to reach a certain altitude
  15. while True:
  16.     altitude = vehicle.location.global_relative_frame.alt
  17.     if altitude >= 9.5:  # target altitude - 0.5 meters
  18.         break
  19.     time.sleep(1)
  20.  
  21. # Move the drone to a new location
  22. new_location = LocationGlobalRelative(37.793105, -122.398768, 20)
  23. vehicle.simple_goto(new_location)
  24.  
  25. # Wait for the drone to reach the new location
  26. while True:
  27.     distance = vehicle.location.global_relative_frame.distance_to(new_location)
  28.     if distance <= 1:  # target radius in meters
  29.         break
  30.     time.sleep(1)
  31.  
  32. # Land the drone
  33. vehicle.mode = VehicleMode("LAND")
  34.  
  35. # Close the connection
  36. vehicle.close()
  37.  
  38. #####################################################################
  39. #####################################################################
  40. from dronekit import connect, VehicleMode, LocationGlobalRelative
  41. from pymavlink import mavutil
  42. import time
  43. import math
  44.  
  45. # Set up connection to vehicle
  46. vehicle = connect('udp:127.0.0.1:14550')
  47.  
  48. # Set vehicle mode to GUIDED
  49. vehicle.mode = VehicleMode("GUIDED")
  50.  
  51. # Define base location and target location
  52. base_location = LocationGlobalRelative(37.6189, -122.3750, 10)
  53. target_location = LocationGlobalRelative(37.6200, -122.3770, 20)
  54.  
  55. # Arm and takeoff
  56. vehicle.armed = True
  57. vehicle.simple_takeoff(base_location.alt)
  58.  
  59. # Wait for takeoff to complete
  60. while True:
  61.     if abs(vehicle.location.global_relative_frame.alt - base_location.alt) < 1.0:
  62.         print("Reached target altitude")
  63.         break
  64.     time.sleep(1)
  65.  
  66. # Go to target location
  67. vehicle.simple_goto(target_location)
  68.  
  69. def distance_to(self, other):
  70.     dlat = other.lat - self.lat
  71.     dlong = other.lon - self.lon
  72.     return math.sqrt((dlat*dlat) + (dlong*dlong)) * 1.113195e5
  73.  
  74.  
  75. # Wait for arrival at target location
  76. while True:
  77.     if distance_to(vehicle.location.global_relative_frame, target_location) < 1.0:
  78.         print("Reached target location")
  79.         break
  80.     time.sleep(1)
  81.    
  82. # Set mode to RTL and land
  83. vehicle.mode = VehicleMode("RTL")
  84. while True:
  85.     if vehicle.mode.name == "LAND":
  86.         print("Vehicle landed")
  87.         break
  88.     time.sleep(1)
  89.  
  90. # Close connection to vehicle
  91. vehicle.close()
  92.  
Advertisement
Add Comment
Please, Sign In to add comment