forked from mustafa-gokce/ardupilot-software-development
-
Notifications
You must be signed in to change notification settings - Fork 1
/
mission-editing.py
100 lines (81 loc) · 3.45 KB
/
mission-editing.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
"""
Source: https://dronekit-python.readthedocs.io/en/latest/automodule.html
https://mavlink.io/en/messages/common.html#mav_commands
MAV_CMD_NAV_WAYPOINT = navigate to a waypoint
MAV_CMD_NAV_RETURN_TO_LAUNCH = return to launch location
MAV_CMD_NAV_LAND = land to a location
MAV_CMD_NAV_TAKEOFF = takeoff to desired altitude
"""
import time
import dronekit
target_locations = ((-35.362048, 149.164489, 30),
(-35.362184, 149.162649, 20),
(-35.363580, 149.162826, 25),
(-35.363496, 149.165149, 10))
print("Trying to connect to the vehicle...")
vehicle = dronekit.connect(ip="127.0.0.1:14550", wait_ready=True)
print("Connected to the vehicle.")
vehicle.parameters["RTL_ALT"] = 3000 # 3000 centimeters = 30 meters
mission_items = vehicle.commands
mission_items.download()
mission_items.wait_ready()
mission_items.clear()
# takeoff command
mission_item = dronekit.Command(0, 0, 0,
dronekit.mavutil.mavlink.MAV_FRAME_GLOBAL_RELATIVE_ALT,
dronekit.mavutil.mavlink.MAV_CMD_NAV_TAKEOFF,
0, 0, 0, 0, 0, 0, 0, 0, 30)
mission_items.add(mission_item)
# navigation locations
for location in target_locations:
mission_item = dronekit.Command(0, 0, 0,
dronekit.mavutil.mavlink.MAV_FRAME_GLOBAL_RELATIVE_ALT,
dronekit.mavutil.mavlink.MAV_CMD_NAV_WAYPOINT,
0, 0, 0, 0, 0, 0,
location[0], location[1], location[2])
mission_items.add(mission_item)
# return to launch command
mission_item = dronekit.Command(0, 0, 0,
dronekit.mavutil.mavlink.MAV_FRAME_GLOBAL_RELATIVE_ALT,
dronekit.mavutil.mavlink.MAV_CMD_NAV_RETURN_TO_LAUNCH,
0, 0, 0, 0, 0, 0, 0, 0, 0)
mission_items.add(mission_item)
# land command
mission_item = dronekit.Command(0, 0, 0,
dronekit.mavutil.mavlink.MAV_FRAME_GLOBAL_RELATIVE_ALT,
dronekit.mavutil.mavlink.MAV_CMD_NAV_LAND,
0, 0, 0, 0, 0, 0, 0, 0, 0)
mission_items.add(mission_item)
# upload commands
mission_items.upload()
print("Uploaded all mission items.")
while vehicle.mode.name != "GUIDED":
vehicle.mode = dronekit.VehicleMode("GUIDED")
time.sleep(1)
print("Vehicle is in GUIDED mode.")
vehicle.send_calibrate_barometer()
while not vehicle.is_armable:
time.sleep(1)
print("Vehicle is armable:", vehicle.is_armable)
while not vehicle.armed:
vehicle.armed = True
time.sleep(1)
print("Vehicle is armed:", vehicle.armed)
while vehicle.mode.name != "AUTO":
vehicle.mode = dronekit.VehicleMode("AUTO")
time.sleep(1)
print("Vehicle is in AUTO mode.")
vehicle.send_mavlink(
vehicle.message_factory.command_long_encode(
0, 0, dronekit.mavutil.mavlink.MAV_CMD_MISSION_START, 0, 0, 0, 0, 0, 0, 0, 0))
try:
while True:
print("Latitude:", vehicle.location.global_relative_frame.lat,
"Longitude:", vehicle.location.global_relative_frame.lat,
"Altitude:", vehicle.location.global_relative_frame.alt,
"Target Number:", vehicle.commands.next)
time.sleep(3)
except KeyboardInterrupt:
print("Closing the vehicle...")
vehicle.close()
print("Closed the vehicle.")