forked from akshaydhame2001/ardupilot-software-development
-
Notifications
You must be signed in to change notification settings - Fork 0
/
receive-message.py
56 lines (40 loc) · 1.84 KB
/
receive-message.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
"""
Source: https://www.ardusub.com/developers/pymavlink.html#run-pymavlink-on-the-surface-computer
https://mavlink.io/en/messages/common.html
To receive a message from MAVLink stream, recv_match() is used
type: used for filtering a specific message, ex: SYSTEM_TIME
blocking: used for this function blocks the execution or not
timeout: used for defining maximum blocking time
pymavlink.dialects contains all the MAVLink message dialects
pymavlink.dialects.v20 contains all the MAVLink2 message dialects
pymavlink.dialects.v20.all contains all the MAVLink2 message types and definitions
"""
import time
import pymavlink.mavutil as utility
import pymavlink.dialects.v20.all as dialect
# connect to vehicle
vehicle = utility.mavlink_connection(device="udpin:127.0.0.1:14560")
# wait for a heartbeat
vehicle.wait_heartbeat()
# inform user
print("Connected to system:", vehicle.target_system, ", component:", vehicle.target_component)
# infinite loop
while True:
# try to receive a message
try:
# receive a message
message = vehicle.recv_match(type=dialect.MAVLink_system_time_message.msgname, blocking=True)
# convert received message to dictionary
message = message.to_dict()
# for each field name in field names of this message
for field_name in dialect.MAVLink_system_time_message.fieldnames:
# if the field is system boot time in milliseconds
if field_name == "time_boot_ms":
# print field name and contained field value
print(field_name, message[field_name])
# bare except to catch all the exceptions
except:
# print that no message is received matches the condition
print("no message received from vehicle")
# tiny sleep to cool down the terminal
time.sleep(0.010)