SIM_Tracker.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /*
  2. This program is free software: you can redistribute it and/or modify
  3. it under the terms of the GNU General Public License as published by
  4. the Free Software Foundation, either version 3 of the License, or
  5. (at your option) any later version.
  6. This program is distributed in the hope that it will be useful,
  7. but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  9. GNU General Public License for more details.
  10. You should have received a copy of the GNU General Public License
  11. along with this program. If not, see <http://www.gnu.org/licenses/>.
  12. */
  13. /*
  14. antenna-tracker simulator class
  15. */
  16. #include "SIM_Tracker.h"
  17. #include <stdio.h>
  18. namespace SITL {
  19. Tracker::Tracker(const char *frame_str) :
  20. Aircraft(frame_str)
  21. {}
  22. /*
  23. update function for position (normal) servos.
  24. */
  25. void Tracker::update_position_servos(float delta_time, float &yaw_rate, float &pitch_rate)
  26. {
  27. float pitch_target = pitch_input*pitch_range;
  28. float yaw_target = yaw_input*yaw_range;
  29. pitch_rate = constrain_float(pitch_target - pitch_current_relative, -pitchrate, pitchrate);
  30. yaw_rate = constrain_float(yaw_target - yaw_current_relative, -yawrate, yawrate);
  31. }
  32. /*
  33. update function for onoff servos.
  34. These servos either move at a constant rate or are still
  35. Returns (yaw_rate,pitch_rate) tuple
  36. */
  37. void Tracker::update_onoff_servos(float &yaw_rate, float &pitch_rate)
  38. {
  39. if (fabsf(yaw_input) < 0.1) {
  40. yaw_rate = 0;
  41. } else if (yaw_input >= 0.1) {
  42. yaw_rate = yawrate;
  43. } else {
  44. yaw_rate = -yawrate;
  45. }
  46. if (fabsf(pitch_input) < 0.1) {
  47. pitch_rate = 0;
  48. } else if (pitch_input >= 0.1) {
  49. pitch_rate = pitchrate;
  50. } else {
  51. pitch_rate = -pitchrate;
  52. }
  53. }
  54. /*
  55. update state of tracker
  56. */
  57. void Tracker::update(const struct sitl_input &input)
  58. {
  59. // how much time has passed?
  60. float delta_time = frame_time_us * 1.0e-6f;
  61. float yaw_rate = 0.0f, pitch_rate = 0.0f;
  62. yaw_input = (input.servos[0]-1500)/500.0f;
  63. pitch_input = (input.servos[1]-1500)/500.0f;
  64. // implement yaw and pitch limits
  65. float r, p, y;
  66. dcm.to_euler(&r, &p, &y);
  67. pitch_current_relative = degrees(p) - zero_pitch;
  68. yaw_current_relative = degrees(y) - zero_yaw;
  69. float roll_current = degrees(r);
  70. if (yaw_current_relative > 180) {
  71. yaw_current_relative -= 360;
  72. }
  73. if (yaw_current_relative < -180) {
  74. yaw_current_relative += 360;
  75. }
  76. if (yaw_rate > 0 && yaw_current_relative >= yaw_range) {
  77. yaw_rate = 0;
  78. }
  79. if (yaw_rate < 0 && yaw_current_relative <= -yaw_range) {
  80. yaw_rate = 0;
  81. }
  82. if (pitch_rate > 0 && pitch_current_relative >= pitch_range) {
  83. pitch_rate = 0;
  84. }
  85. if (pitch_rate < 0 && pitch_current_relative <= -pitch_range) {
  86. pitch_rate = 0;
  87. }
  88. if (onoff) {
  89. update_onoff_servos(yaw_rate, pitch_rate);
  90. } else {
  91. update_position_servos(delta_time, yaw_rate, pitch_rate);
  92. }
  93. // keep it level
  94. float roll_rate = 0 - roll_current;
  95. if (time_now_us - last_debug_us > 2e6f && !onoff) {
  96. last_debug_us = time_now_us;
  97. printf("roll=%.1f pitch=%.1f yaw=%.1f rates=%.1f/%.1f/%.1f in=%.3f,%.3f\n",
  98. roll_current,
  99. pitch_current_relative,
  100. yaw_current_relative,
  101. roll_rate, pitch_rate, yaw_rate,
  102. yaw_input, pitch_input);
  103. }
  104. gyro = Vector3f(radians(roll_rate),radians(pitch_rate),radians(yaw_rate));
  105. // update attitude
  106. dcm.rotate(gyro * delta_time);
  107. dcm.normalize();
  108. Vector3f accel_earth = Vector3f(0, 0, -GRAVITY_MSS);
  109. accel_body = dcm.transposed() * accel_earth;
  110. // new velocity vector
  111. velocity_ef.zero();
  112. update_position();
  113. time_advance();
  114. // update magnetic field
  115. update_mag_field_bf();
  116. }
  117. } // namespace SITL