forked from udacity/FCND-FixedWing
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplane_drone.py
140 lines (114 loc) · 5.29 KB
/
plane_drone.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
import numpy as np
from enum import Enum
from udacidrone import Drone
import time
visdom_available= True
try:
import visdom
except:
visdom_available = False
class PlaneMode(Enum):
"""
Constant which isn't defined in Mavlink but useful when dealing with
the airplane simulation
"""
SUB_MODE_MANUAL = 1
SUB_MODE_LONGITUDE = 2
SUB_MODE_LATERAL = 3
SUB_MODE_STABILIZED = 4
SUB_MODE_VTOL_ATTITUDE = 9
SUB_MODE_VTOL_POSITION = 10
class Udaciplane(Drone):
"""
Udaciplane class for use with the Unity Fixed Wing/Flying Car simulation
"""
def __init__(self, connection, tlog_name="TLog.txt"):
super().__init__(connection, tlog_name)
def cmd_stabilized(self, roll, altitude, sideslip, airspeed):
"""Command the stabilized mode of the drone
Args:
roll: in radians
altitude: in meters (positive up)
sideslip: in radians (positive nose left)
airspeed: in meters/sec
"""
self.connection.set_sub_mode(PlaneMode.SUB_MODE_STABILIZED.value)
self.connection.cmd_moment(roll, altitude, sideslip, airspeed)
def cmd_longitude_mode(self, elevator, throttle, roll = 0, sideslip = 0,
t=0):
"""Command the longitude mode while lateral is stabilized
Args:
elevator: in percentage of maximum elevator (-1:1)
throttle: in percentage of maximum throttle RPM (0:1)
roll: in radians
sideslip: in radians (positive nose left)
"""
self.connection.set_sub_mode(PlaneMode.SUB_MODE_LONGITUDE.value)
self.connection.cmd_moment(roll, elevator, sideslip, throttle, t)
def cmd_lateral_mode(self, aileron, rudder, altitude, airspeed):
"""Command the lateral mode while longitudinal mode is stabilized
Args:
aileron: in percentage of maximum aileron (-1:1)
rudder: in percentage of maximum rudder (-1:1)
altitude: in meters (positive up)
airspeed: in meters/sec
"""
self.connection.set_sub_mode(PlaneMode.SUB_MODE_LATERAL.value)
self.connection.cmd_moment(aileron, altitude, rudder, airspeed)
def cmd_controls(self, aileron, elevator, rudder, throttle):
"""Command the manual aircraft controls
Args:
aileron: in percentage of maximum aileron (-1:1)
rudder: in percentage of maximum rudder (-1:1)
elevator: in percentage of maximum elevator (-1:1)
throttle: in percentage of maximum throttle RPM (0:1)
"""
self.connection.set_sub_mode(PlaneMode.SUB_MODE_MANUAL.value)
controls = [aileron, elevator, rudder, throttle]
self.connection.cmd_controls(controls)
def cmd_hybrid(self, aileron, elevator, rudder, throttle, roll_moment, pitch_moment, yaw_moment, thrust):
"""Command the manual aircraft controls, the VTOL moments and total thrust force
Args:
aileron: in percentage of maximum aileron (-1:1)
rudder: in percentage of maximum rudder (-1:1)
elevator: in percentage of maximum elevator (-1:1)
throttle: in percentage of maximum throttle RPM (0:1)
roll_moment: in percentage of maximum roll moment (-1:1)
pitch_moment: in percentage of maximum pitch moment (-1:1)
yaw_moment: in percentage of maximum yaw_moment (-1:1)
thrust: in percentage of maximum thrust (0:1)
"""
self.connection.set_sub_mode(PlaneMode.SUB_MODE_MANUAL.value)
controls = [aileron, elevator, rudder, throttle, roll_moment, pitch_moment, yaw_moment , thrust]
self.connection.cmd_controls(controls)
def cmd_moment(self, roll_moment, pitch_moment, yaw_moment, thrust):
"""Command the VTOL moments and total thrust force
Args:
roll_moment: in percentage of maximum roll moment (-1:1)
pitch_moment: in percentage of maximum pitch moment (-1:1)
yaw_moment: in percentage of maximum yaw_moment (-1:1)
thrust: in percentage of maximum thrust (0:1)
"""
self.connection.set_sub_mode(PlaneMode.SUB_MODE_MANUAL.value)
controls = [0.0, 0.0, 0.0, 0.0, roll_moment, pitch_moment, yaw_moment, thrust]
self.connection.cmd_controls(controls)
def cmd_vtol_position(self, north, east, altitude, heading):
"""Command the local position and drone heading.
Args:
north: local north in meters
east: local east in meters
altitude: altitude above ground in meters
heading: drone yaw in radians
"""
self.connection.set_sub_mode(PlaneMode.SUB_MODE_VTOL_POSITION.value)
self.cmd_position(north, east, altitude, heading)
def cmd_vtol_attitude(self,roll, pitch, yaw_rate, vert_vel):
"""Command the drone through attitude command
Args:
roll: in radians
pitch: in randians
yaw_rate: in radians/second
vert_vel: upward velocity in meters/second
"""
self.connection.set_sub_mode(PlaneMode.SUB_MODE_VTOL_ATTITUDE.value)
self.cmd_attitude(roll, pitch, yaw_rate, vert_vel)