-
Notifications
You must be signed in to change notification settings - Fork 0
/
coordinate_flight.py
58 lines (47 loc) · 2.44 KB
/
coordinate_flight.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
###################################################
# Coordinate flight json
# Developer: Joshua Chang
# Date: 5/1/2024
# Description: Description: This script will move the drone to GPS coordinates specified in a JSON file.
# The coordinates are in geographic coordinate system (latitude, longitude) and the drone will move to each location
# On environment 'Purdue_airport3_7'
##################################################
import src.airsim as airsim
from src.utils import read_coordinates_from_json
import json
class DroneController:
def __init__(self):
self.client = airsim.MultirotorClient()
self.vehicle_name = 'drone2'
self.client.confirmConnection()
self.client.enableApiControl(True, self.vehicle_name)
self.client.armDisarm(True, self.vehicle_name)
def move_drone_to_gps_location(self, path_coordinates, altitude, velocity):
print("Taking off...")
self.client.takeoffAsync(vehicle_name=self.vehicle_name).join()
gps = self.client.getGpsData(vehicle_name=self.vehicle_name)
print('drone taking off')
print('current gps location: ', gps)
current_latitude = gps.gnss.geo_point.latitude
current_longitude = gps.gnss.geo_point.longitude
print(f"Moving to GPS location: Latitude={current_latitude}, Longitude={current_longitude}, Altitude={altitude} at {velocity} m/s")
self.client.moveToGPSAsync(current_latitude, current_longitude, altitude, velocity, vehicle_name=self.vehicle_name).join()
for coord in path_coordinates:
latitude, longitude = coord
print(f"Moving to GPS location: Latitude={latitude}, Longitude={longitude}, Altitude={altitude} at {velocity} m/s")
self.client.moveToGPSAsync(latitude=latitude, longitude=longitude, altitude=altitude, velocity=velocity, vehicle_name=self.vehicle_name).join()
print("Landing...")
self.client.landAsync().join()
def disarm(self):
# Cleanup by disarming and releasing API control
self.client.armDisarm(False)
self.client.enableApiControl(False)
def main():
controller = DroneController()
path_coordinates = read_coordinates_from_json('test_coordinates/uav_path_test_1.json', 'path')
altitude = 150
velocity = 15 # meters per second
controller.move_drone_to_gps_location(path_coordinates, altitude, velocity)
controller.disarm()
if __name__ == '__main__':
main()