apmsetrate.py 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. #!/usr/bin/env python
  2. '''
  3. set stream rate on an APM
  4. '''
  5. from __future__ import print_function
  6. from builtins import range
  7. import sys
  8. from argparse import ArgumentParser
  9. parser = ArgumentParser(description=__doc__)
  10. parser.add_argument("--baudrate", type=int,
  11. help="master port baud rate", default=115200)
  12. parser.add_argument("--device", required=True, help="serial device")
  13. parser.add_argument("--rate", default=4, type=int, help="requested stream rate")
  14. parser.add_argument("--source-system", dest='SOURCE_SYSTEM', type=int,
  15. default=255, help='MAVLink source system for this GCS')
  16. parser.add_argument("--showmessages", action='store_true',
  17. help="show incoming messages", default=False)
  18. args = parser.parse_args()
  19. from pymavlink import mavutil
  20. def wait_heartbeat(m):
  21. '''wait for a heartbeat so we know the target system IDs'''
  22. print("Waiting for APM heartbeat")
  23. m.wait_heartbeat()
  24. print("Heartbeat from APM (system %u component %u)" % (m.target_system, m.target_system))
  25. def show_messages(m):
  26. '''show incoming mavlink messages'''
  27. while True:
  28. msg = m.recv_match(blocking=True)
  29. if not msg:
  30. return
  31. if msg.get_type() == "BAD_DATA":
  32. if mavutil.all_printable(msg.data):
  33. sys.stdout.write(msg.data)
  34. sys.stdout.flush()
  35. else:
  36. print(msg)
  37. # create a mavlink serial instance
  38. master = mavutil.mavlink_connection(args.device, baud=args.baudrate)
  39. # wait for the heartbeat msg to find the system ID
  40. wait_heartbeat(master)
  41. print("Sending all stream request for rate %u" % args.rate)
  42. for i in range(0, 3):
  43. master.mav.request_data_stream_send(master.target_system, master.target_component,
  44. mavutil.mavlink.MAV_DATA_STREAM_ALL, args.rate, 1)
  45. if args.showmessages:
  46. show_messages(master)