-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdrone_client.py
118 lines (89 loc) · 2.78 KB
/
drone_client.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
import airsim
import drone_types
class DroneClient:
def __init__(self):
self.client = airsim.MultirotorClient()
self.future = None
def __del__(self):
if self.future is not None:
self.future.join()
def connect(self):
"""
Connect to simulation
Args:
none
Returns:
none
"""
self.client.confirmConnection()
self.client.enableApiControl(True)
self.client.armDisarm(True)
def isConnected(self):
"""
Check if client is connected to simulation
Args:
none
Returns:
bool: true if connected. otherwise return false
"""
return self.client.isApiControlEnabled()
def getPose(self):
"""
Get the pose of the drone
Args:
none
Returns:
DroneTypes.Pose : the pose of the drone
"""
drone_pose = self.client.simGetVehiclePose()
res = drone_types.Pose()
res.pos.x_m = drone_pose.position.x_val
res.pos.y_m = drone_pose.position.y_val
res.pos.z_m = drone_pose.position.z_val
euler = airsim.utils.to_eularian_angles(drone_pose.orientation)
res.orientation.x_rad = euler[0]
res.orientation.y_rad = euler[1]
res.orientation.z_rad = euler[2]
return res
def getLidarData(self):
point_cloud = drone_types.PointCloud()
lidar_data = self.client.getLidarData()
point_cloud.points = lidar_data.point_cloud
return point_cloud
def flyToPosition(self, x: float, y: float, z: float, v: float):
"""
Fly the drone to position
Args:
x : float - x coordinate
y : float - y coordinate
z : float - z coordinate
v : float - the velocity which the drone fly to position
Returns:
none
"""
self.future = self.client.moveToPositionAsync(x, y, z, v, drivetrain=airsim.DrivetrainType.ForwardOnly,
yaw_mode=airsim.YawMode(False, 0.0))
def setAtPosition(self, x: float, y: float, z: float):
"""
Set the drone at position instantly
Args:
x : float - x coordinate
y : float - y coordinate
z : float - z coordinate
Returns:
none
"""
pos = airsim.Vector3r(x, y, z)
q = airsim.Quaternionr(1, 0, 0, 0)
pose = airsim.Pose(pos, q)
self.client.simSetVehiclePose(pose, True)
self.flyToPosition(x, y, z, 1)
def reset(self):
"""
Returns the drone to start position
Args:
none
Returns:
none
"""
self.client.reset()