-
Notifications
You must be signed in to change notification settings - Fork 597
/
mavtest.py
executable file
·72 lines (53 loc) · 1.89 KB
/
mavtest.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
#!/usr/bin/env python
"""
Generate a message using different MAVLink versions, put in a buffer and then read from it.
"""
from __future__ import print_function
from builtins import object
from pymavlink.dialects.v10 import ardupilotmega as mavlink1
from pymavlink.dialects.v20 import ardupilotmega as mavlink2
class fifo(object):
def __init__(self):
self.buf = []
def write(self, data):
self.buf += data
return len(data)
def read(self):
return self.buf.pop(0)
def test_protocol(mavlink, signing=False):
# we will use a fifo as an encode/decode buffer
f = fifo()
print("Creating MAVLink message...")
# create a mavlink instance, which will do IO on file object 'f'
mav = mavlink.MAVLink(f)
if signing:
mav.signing.secret_key = bytearray(chr(42)*32, 'utf-8' )
mav.signing.link_id = 0
mav.signing.timestamp = 0
mav.signing.sign_outgoing = True
# set the WP_RADIUS parameter on the MAV at the end of the link
mav.param_set_send(7, 1, b"WP_RADIUS", 101, mavlink.MAV_PARAM_TYPE_REAL32)
# alternatively, produce a MAVLink_param_set object
# this can be sent via your own transport if you like
m = mav.param_set_encode(7, 1, b"WP_RADIUS", 101, mavlink.MAV_PARAM_TYPE_REAL32)
m.pack(mav)
# get the encoded message as a buffer
b = m.get_msgbuf()
bi=[]
for c in b:
bi.append(int(c))
print("Buffer containing the encoded message:")
print(bi)
print("Decoding message...")
# decode an incoming message
m2 = mav.decode(b)
# show what fields it has
print("Got a message with id %u and fields %s" % (m2.get_msgId(), m2.get_fieldnames()))
# print out the fields
print(m2)
print("Testing mavlink1\n")
test_protocol(mavlink1)
print("\nTesting mavlink2\n")
test_protocol(mavlink2)
print("\nTesting mavlink2 with signing\n")
test_protocol(mavlink2, True)