Output.ino 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /* This file is part of the Razor AHRS Firmware */
  2. // Output angles: yaw, pitch, roll
  3. void output_angles()
  4. {
  5. if (output_format == OUTPUT__FORMAT_BINARY)
  6. {
  7. float ypr[3];
  8. ypr[0] = TO_DEG(yaw);
  9. ypr[1] = TO_DEG(pitch);
  10. ypr[2] = TO_DEG(roll);
  11. Serial.write((byte*) ypr, 12); // No new-line
  12. }
  13. else if (output_format == OUTPUT__FORMAT_TEXT)
  14. {
  15. Serial.print("#YPR=");
  16. Serial.print(TO_DEG(yaw)); Serial.print(",");
  17. Serial.print(TO_DEG(pitch)); Serial.print(",");
  18. Serial.print(TO_DEG(roll)); Serial.println();
  19. }
  20. }
  21. void output_calibration(int calibration_sensor)
  22. {
  23. if (calibration_sensor == 0) // Accelerometer
  24. {
  25. // Output MIN/MAX values
  26. Serial.print("accel x,y,z (min/max) = ");
  27. for (int i = 0; i < 3; i++) {
  28. if (accel[i] < accel_min[i]) accel_min[i] = accel[i];
  29. if (accel[i] > accel_max[i]) accel_max[i] = accel[i];
  30. Serial.print(accel_min[i]);
  31. Serial.print("/");
  32. Serial.print(accel_max[i]);
  33. if (i < 2) Serial.print(" ");
  34. else Serial.println();
  35. }
  36. }
  37. else if (calibration_sensor == 1) // Magnetometer
  38. {
  39. // Output MIN/MAX values
  40. Serial.print("magn x,y,z (min/max) = ");
  41. for (int i = 0; i < 3; i++) {
  42. if (magnetom[i] < magnetom_min[i]) magnetom_min[i] = magnetom[i];
  43. if (magnetom[i] > magnetom_max[i]) magnetom_max[i] = magnetom[i];
  44. Serial.print(magnetom_min[i]);
  45. Serial.print("/");
  46. Serial.print(magnetom_max[i]);
  47. if (i < 2) Serial.print(" ");
  48. else Serial.println();
  49. }
  50. }
  51. else if (calibration_sensor == 2) // Gyroscope
  52. {
  53. // Average gyro values
  54. for (int i = 0; i < 3; i++)
  55. gyro_average[i] += gyro[i];
  56. gyro_num_samples++;
  57. // Output current and averaged gyroscope values
  58. Serial.print("gyro x,y,z (current/average) = ");
  59. for (int i = 0; i < 3; i++) {
  60. Serial.print(gyro[i]);
  61. Serial.print("/");
  62. Serial.print(gyro_average[i] / (float) gyro_num_samples);
  63. if (i < 2) Serial.print(" ");
  64. else Serial.println();
  65. }
  66. }
  67. }
  68. void output_sensors_text(char raw_or_calibrated)
  69. {
  70. Serial.print("#A-"); Serial.print(raw_or_calibrated); Serial.print('=');
  71. Serial.print(accel[0]); Serial.print(",");
  72. Serial.print(accel[1]); Serial.print(",");
  73. Serial.print(accel[2]); Serial.println();
  74. Serial.print("#M-"); Serial.print(raw_or_calibrated); Serial.print('=');
  75. Serial.print(magnetom[0]); Serial.print(",");
  76. Serial.print(magnetom[1]); Serial.print(",");
  77. Serial.print(magnetom[2]); Serial.println();
  78. Serial.print("#G-"); Serial.print(raw_or_calibrated); Serial.print('=');
  79. Serial.print(gyro[0]); Serial.print(",");
  80. Serial.print(gyro[1]); Serial.print(",");
  81. Serial.print(gyro[2]); Serial.println();
  82. }
  83. void output_both_angles_and_sensors_text()
  84. {
  85. Serial.print("#YPRAG=");
  86. Serial.print(TO_DEG(yaw)); Serial.print(",");
  87. Serial.print(TO_DEG(pitch)); Serial.print(",");
  88. Serial.print(TO_DEG(roll)); Serial.print(",");
  89. Serial.print(Accel_Vector[0]); Serial.print(",");
  90. Serial.print(Accel_Vector[1]); Serial.print(",");
  91. Serial.print(Accel_Vector[2]); Serial.print(",");
  92. Serial.print(Gyro_Vector[0]); Serial.print(",");
  93. Serial.print(Gyro_Vector[1]); Serial.print(",");
  94. Serial.print(Gyro_Vector[2]); Serial.println();
  95. }
  96. void output_sensors_binary()
  97. {
  98. Serial.write((byte*) accel, 12);
  99. Serial.write((byte*) magnetom, 12);
  100. Serial.write((byte*) gyro, 12);
  101. }
  102. void output_sensors()
  103. {
  104. if (output_mode == OUTPUT__MODE_SENSORS_RAW)
  105. {
  106. if (output_format == OUTPUT__FORMAT_BINARY)
  107. output_sensors_binary();
  108. else if (output_format == OUTPUT__FORMAT_TEXT)
  109. output_sensors_text('R');
  110. }
  111. else if (output_mode == OUTPUT__MODE_SENSORS_CALIB)
  112. {
  113. // Apply sensor calibration
  114. compensate_sensor_errors();
  115. if (output_format == OUTPUT__FORMAT_BINARY)
  116. output_sensors_binary();
  117. else if (output_format == OUTPUT__FORMAT_TEXT)
  118. output_sensors_text('C');
  119. }
  120. else if (output_mode == OUTPUT__MODE_SENSORS_BOTH)
  121. {
  122. if (output_format == OUTPUT__FORMAT_BINARY)
  123. {
  124. output_sensors_binary();
  125. compensate_sensor_errors();
  126. output_sensors_binary();
  127. }
  128. else if (output_format == OUTPUT__FORMAT_TEXT)
  129. {
  130. output_sensors_text('R');
  131. compensate_sensor_errors();
  132. output_sensors_text('C');
  133. }
  134. }
  135. }