magtest.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. #!/usr/bin/env python
  2. '''
  3. rotate APMs on bench to test magnetometers
  4. '''
  5. from __future__ import print_function
  6. import time
  7. from math import radians
  8. from pymavlink import mavutil
  9. from argparse import ArgumentParser
  10. parser = ArgumentParser(description=__doc__)
  11. parser.add_argument("--device1", required=True, help="mavlink device1")
  12. parser.add_argument("--device2", required=True, help="mavlink device2")
  13. parser.add_argument("--baudrate", type=int,
  14. help="master port baud rate", default=115200)
  15. args = parser.parse_args()
  16. def set_attitude(rc3, rc4):
  17. global mav1, mav2
  18. values = [ 65535 ] * 8
  19. values[2] = rc3
  20. values[3] = rc4
  21. mav1.mav.rc_channels_override_send(mav1.target_system, mav1.target_component, *values)
  22. mav2.mav.rc_channels_override_send(mav2.target_system, mav2.target_component, *values)
  23. # create a mavlink instance
  24. mav1 = mavutil.mavlink_connection(args.device1, baud=args.baudrate)
  25. # create a mavlink instance
  26. mav2 = mavutil.mavlink_connection(args.device2, baud=args.baudrate)
  27. print("Waiting for HEARTBEAT")
  28. mav1.wait_heartbeat()
  29. mav2.wait_heartbeat()
  30. print("Heartbeat from APM (system %u component %u)" % (mav1.target_system, mav1.target_component))
  31. print("Heartbeat from APM (system %u component %u)" % (mav2.target_system, mav2.target_component))
  32. print("Waiting for MANUAL mode")
  33. mav1.recv_match(type='SYS_STATUS', condition='SYS_STATUS.mode==2 and SYS_STATUS.nav_mode==4', blocking=True)
  34. mav2.recv_match(type='SYS_STATUS', condition='SYS_STATUS.mode==2 and SYS_STATUS.nav_mode==4', blocking=True)
  35. print("Setting declination")
  36. mav1.mav.param_set_send(mav1.target_system, mav1.target_component,
  37. 'COMPASS_DEC', radians(12.33))
  38. mav2.mav.param_set_send(mav2.target_system, mav2.target_component,
  39. 'COMPASS_DEC', radians(12.33))
  40. set_attitude(1060, 1160)
  41. event = mavutil.periodic_event(30)
  42. pevent = mavutil.periodic_event(0.3)
  43. rc3_min = 1060
  44. rc3_max = 1850
  45. rc4_min = 1080
  46. rc4_max = 1500
  47. rc3 = rc3_min
  48. rc4 = 1160
  49. delta3 = 2
  50. delta4 = 1
  51. use_pitch = 1
  52. MAV_ACTION_CALIBRATE_GYRO = 17
  53. mav1.mav.action_send(mav1.target_system, mav1.target_component, MAV_ACTION_CALIBRATE_GYRO)
  54. mav2.mav.action_send(mav2.target_system, mav2.target_component, MAV_ACTION_CALIBRATE_GYRO)
  55. print("Waiting for gyro calibration")
  56. mav1.recv_match(type='ACTION_ACK')
  57. mav2.recv_match(type='ACTION_ACK')
  58. print("Resetting mag offsets")
  59. mav1.mav.set_mag_offsets_send(mav1.target_system, mav1.target_component, 0, 0, 0)
  60. mav2.mav.set_mag_offsets_send(mav2.target_system, mav2.target_component, 0, 0, 0)
  61. def TrueHeading(SERVO_OUTPUT_RAW):
  62. p = float(SERVO_OUTPUT_RAW.servo3_raw - rc3_min) / (rc3_max - rc3_min)
  63. return 172 + p*(326 - 172)
  64. while True:
  65. mav1.recv_msg()
  66. mav2.recv_msg()
  67. if event.trigger():
  68. if not use_pitch:
  69. rc4 = 1160
  70. set_attitude(rc3, rc4)
  71. rc3 += delta3
  72. if rc3 > rc3_max or rc3 < rc3_min:
  73. delta3 = -delta3
  74. use_pitch ^= 1
  75. rc4 += delta4
  76. if rc4 > rc4_max or rc4 < rc4_min:
  77. delta4 = -delta4
  78. if pevent.trigger():
  79. print("hdg1: %3u hdg2: %3u ofs1: %4u, %4u, %4u ofs2: %4u, %4u, %4u" % (
  80. mav1.messages['VFR_HUD'].heading,
  81. mav2.messages['VFR_HUD'].heading,
  82. mav1.messages['SENSOR_OFFSETS'].mag_ofs_x,
  83. mav1.messages['SENSOR_OFFSETS'].mag_ofs_y,
  84. mav1.messages['SENSOR_OFFSETS'].mag_ofs_z,
  85. mav2.messages['SENSOR_OFFSETS'].mag_ofs_x,
  86. mav2.messages['SENSOR_OFFSETS'].mag_ofs_y,
  87. mav2.messages['SENSOR_OFFSETS'].mag_ofs_z,
  88. ))
  89. time.sleep(0.01)
  90. # 314M 326G
  91. # 160M 172G