-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path03_read_telemetry.py
151 lines (64 loc) · 2.91 KB
/
03_read_telemetry.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
140
141
142
143
144
145
146
147
148
149
150
"""
This script will introduce multiple things:
> Run the simulator inside dronekit
> Read and handle telemetry from the UAV
> Read and change parameters
"""
from dronekit import connect, VehicleMode
import time
#--- Start the Software In The Loop (SITL)
#import dronekit_sitl
#
#sitl = dronekit_sitl.start_default() #(sitl.start)
#connection_string = sitl.connection_string()
connection_string = "/dev/ttyAMA0"
#--- Now that we have started the SITL and we have the connection string (basically the ip and udp port)...
print(">>>> Connecting with the UAV <<<")
vehicle = connect(connection_string, baud=57600, wait_ready=True) #- wait_ready flag hold the program untill all the parameters are been read (=, not .)
#-- Read information from the autopilot:
#- Version and attributes
vehicle.wait_ready('autopilot_version')
print('Autopilot version: %s'%vehicle.version)
#- Does the firmware support the companion pc to set the attitude?
print('Supports set attitude from companion: %s'%vehicle.capabilities.set_attitude_target_local_ned)
#- Read the actual position
print('Position: %s'% vehicle.location.global_relative_frame)
#- Read the actual attitude roll, pitch, yaw
print('Attitude: %s'% vehicle.attitude)
#- Read the actual velocity (m/s)
print('Velocity: %s'%vehicle.velocity) #- North, east, down
#- When did we receive the last heartbeat
print('Last Heartbeat: %s'%vehicle.last_heartbeat)
#- Is the vehicle good to Arm?
print('Is the vehicle armable: %s'%vehicle.is_armable)
#- Which is the total ground speed? Note: this is settable
print('Groundspeed: %s'% vehicle.groundspeed) #(%)
#- What is the actual flight mode? Note: this is settable
print('Mode: %s'% vehicle.mode.name)
#- Is the vehicle armed Note: this is settable
print('Armed: %s'%vehicle.armed)
#- Is thestate estimation filter ok?
print('EKF Ok: %s'%vehicle.ekf_ok)
#----- Adding a listener
#-- dronekit updates the variables as soon as it receives an update from the UAV
#-- you can define a callback function for predefined messages or define one for
#-- any mavlink message
def attitude_callback(self, attr_name, value):
print(vehicle.attitude)
print("")
print("Adding an attitude listener")
vehicle.add_attribute_listener('attitude', attitude_callback) #-- message type, callback function
time.sleep(5)
#--- Now we print the attitude from the callback for 5 seconds, then we remove the callback
vehicle.remove_attribute_listener('attitude', attitude_callback) #(.remove)
#--- You can create a callback even with decorators, check the documentation out for more details
#---- PARAMETERS
print("Maximum Throttle: %d"%vehicle.parameters['THR_MIN'])
#-- You can read and write the parameters
vehicle.parameters['THR_MIN'] = 50
time.sleep(1)
print("Maximum Throttle: %d"%vehicle.parameters['THR_MIN'])
#--- Now we close the simulation
vehicle.close()
#sitl.stop()
print("done")