mavtest.py 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. #!/usr/bin/env python
  2. """
  3. Generate a message using different MAVLink versions, put in a buffer and then read from it.
  4. """
  5. from __future__ import print_function
  6. from builtins import object
  7. from pymavlink.dialects.v10 import ardupilotmega as mavlink1
  8. from pymavlink.dialects.v20 import ardupilotmega as mavlink2
  9. class fifo(object):
  10. def __init__(self):
  11. self.buf = []
  12. def write(self, data):
  13. self.buf += data
  14. return len(data)
  15. def read(self):
  16. return self.buf.pop(0)
  17. def test_protocol(mavlink, signing=False):
  18. # we will use a fifo as an encode/decode buffer
  19. f = fifo()
  20. print("Creating MAVLink message...")
  21. # create a mavlink instance, which will do IO on file object 'f'
  22. mav = mavlink.MAVLink(f)
  23. if signing:
  24. mav.signing.secret_key = chr(42)*32
  25. mav.signing.link_id = 0
  26. mav.signing.timestamp = 0
  27. mav.signing.sign_outgoing = True
  28. # set the WP_RADIUS parameter on the MAV at the end of the link
  29. mav.param_set_send(7, 1, "WP_RADIUS", 101, mavlink.MAV_PARAM_TYPE_REAL32)
  30. # alternatively, produce a MAVLink_param_set object
  31. # this can be sent via your own transport if you like
  32. m = mav.param_set_encode(7, 1, "WP_RADIUS", 101, mavlink.MAV_PARAM_TYPE_REAL32)
  33. m.pack(mav)
  34. # get the encoded message as a buffer
  35. b = m.get_msgbuf()
  36. bi=[]
  37. for c in b:
  38. bi.append(int(c))
  39. print("Buffer containing the encoded message:")
  40. print(bi)
  41. print("Decoding message...")
  42. # decode an incoming message
  43. m2 = mav.decode(b)
  44. # show what fields it has
  45. print("Got a message with id %u and fields %s" % (m2.get_msgId(), m2.get_fieldnames()))
  46. # print out the fields
  47. print(m2)
  48. print("Testing mavlink1\n")
  49. test_protocol(mavlink1)
  50. print("\nTesting mavlink2\n")
  51. test_protocol(mavlink2)
  52. print("\nTesting mavlink2 with signing\n")
  53. test_protocol(mavlink2, True)