-
-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathtakeoff.py
More file actions
69 lines (50 loc) · 2.95 KB
/
Copy pathtakeoff.py
File metadata and controls
69 lines (50 loc) · 2.95 KB
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
# takeoff.py
import argparse
from pymavlink import mavutil
from utilities.connect_to_sysid import connect_to_sysid
from utilities.wait_for_position_aiding import wait_until_position_aiding
from utilities.get_autopilot_info import get_autopilot_info
def takeoff(mav_connection, takeoff_altitude: float, tgt_sys_id: int = 1, tgt_comp_id=1):
print("Heartbeat from system (system %u component %u)" %
(mav_connection.target_system, mav_connection.target_component))
wait_until_position_aiding(mav_connection)
autopilot_info = get_autopilot_info(mav_connection, tgt_sys_id)
if autopilot_info["autopilot"] == "ardupilotmega":
print("Connected to ArduPilot autopilot")
mode_id = mav_connection.mode_mapping()["GUIDED"]
takeoff_params = [0, 0, 0, 0, 0, 0, takeoff_altitude]
elif autopilot_info["autopilot"] == "px4":
print("Connected to PX4 autopilot")
print(mav_connection.mode_mapping())
mode_id = mav_connection.mode_mapping()["TAKEOFF"][1]
print(mode_id)
msg = mav_connection.recv_match(type='GLOBAL_POSITION_INT', blocking=True)
starting_alt = msg.alt / 1000
takeoff_params = [0, 0, 0, 0, float("NAN"), float("NAN"), starting_alt + takeoff_altitude]
else:
raise ValueError("Autopilot not supported")
# Change mode to guided (Ardupilot) or takeoff (PX4)
mav_connection.mav.command_long_send(tgt_sys_id, tgt_comp_id, mavutil.mavlink.MAV_CMD_DO_SET_MODE,
0, mavutil.mavlink.MAV_MODE_FLAG_CUSTOM_MODE_ENABLED, mode_id, 0, 0, 0, 0, 0)
ack_msg = mav_connection.recv_match(type='COMMAND_ACK', blocking=True, timeout=3)
print(f"Change Mode ACK: {ack_msg}")
# Arm the UAS
mav_connection.mav.command_long_send(tgt_sys_id, tgt_comp_id,
mavutil.mavlink.MAV_CMD_COMPONENT_ARM_DISARM, 0, 1, 0, 0, 0, 0, 0, 0)
arm_msg = mav_connection.recv_match(type='COMMAND_ACK', blocking=True, timeout=3)
print(f"Arm ACK: {arm_msg}")
# Command Takeoff
mav_connection.mav.command_long_send(tgt_sys_id, tgt_comp_id,
mavutil.mavlink.MAV_CMD_NAV_TAKEOFF, 0, takeoff_params[0], takeoff_params[1], takeoff_params[2], takeoff_params[3], takeoff_params[4], takeoff_params[5], takeoff_params[6])
takeoff_msg = mav_connection.recv_match(type='COMMAND_ACK', blocking=True, timeout=3)
print(f"Takeoff ACK: {takeoff_msg}")
return takeoff_msg.result
def main():
parser = argparse.ArgumentParser(description="A simple script to command a UAV to takeoff.")
parser.add_argument("--altitude", type=int, help="Altitude for the UAV to reach upon takeoff.", default=10)
parser.add_argument("--sysid", type=int, help="System ID of the UAV to command.", default=1)
args = parser.parse_args()
mav_connection = connect_to_sysid('udpin:localhost:14551', args.sysid)
takeoff(mav_connection, args.altitude)
if __name__ == "__main__":
main()