-
Notifications
You must be signed in to change notification settings - Fork 0
/
connect_vehicle.py
executable file
·58 lines (51 loc) · 1.51 KB
/
connect_vehicle.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
# Standard import
import os
import argparse
import builtins
import time
import math
import socket
from time import sleep
# Library imports
from dronekit import connect, VehicleMode, LocationGlobalRelative, APIException
import dronekit_sitl
# Helper function to connect to a vehicle on a given IP.
def connectVehicle():
"""
Returns a connection to a vehicle on a given IP.
Inputs:
conn_str: IP and port address to connect to.
Ex: --connect :14550" listen UDP.
Default is to connect to SITL on local host.
"""
# Get command line inputs from the user.
# This is needed in case of connecting to the real vehicle.
cmdopts = argparse.ArgumentParser(description="Parameters to connect plane")
cmdopts.add_argument("--connect")
args = cmdopts.parse_args()
conn_str = args.connect
# Connect sitl to default localhost.
# This will also launch SITL and connect to it on 127.0.0.1:14551
if not conn_str:
sitl = dronekit_sitl.start_default()
conn_str = sitl.connection_string()
# Now, we have connection IP, Connect to the Vehicle.
print("Connecting to vehicle on: {}".format(conn_str))
try:
vehicle = connect(conn_str, wait_ready=True)
# Bad TCP connection
except socket.error:
print("No server exists!")
# Bad TTY connection
except exceptions.OSError as e:
print("No serial exists!")
# API Error
except APIException:
print("Timeout!")
# Other error
except:
print("Some other error!")
else:
print("Connected successfully.")
return vehicle
#vehicle_obj = connectVehicle()