SIM_CRRCSim.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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. simulator connector for ardupilot version of CRRCSim
  15. */
  16. #include "SIM_CRRCSim.h"
  17. #include <stdio.h>
  18. #include <AP_HAL/AP_HAL.h>
  19. extern const AP_HAL::HAL& hal;
  20. namespace SITL {
  21. CRRCSim::CRRCSim(const char *frame_str) :
  22. Aircraft(frame_str),
  23. last_timestamp(0),
  24. sock(true)
  25. {
  26. // try to bind to a specific port so that if we restart ArduPilot
  27. // CRRCSim keeps sending us packets. Not strictly necessary but
  28. // useful for debugging
  29. sock.bind("127.0.0.1", 9003);
  30. sock.reuseaddress();
  31. sock.set_blocking(false);
  32. heli_servos = (strstr(frame_str,"heli") != nullptr);
  33. }
  34. /*
  35. decode and send servos for heli
  36. */
  37. void CRRCSim::send_servos_heli(const struct sitl_input &input)
  38. {
  39. float swash1 = (input.servos[0]-1000) / 1000.0f;
  40. float swash2 = (input.servos[1]-1000) / 1000.0f;
  41. float swash3 = (input.servos[2]-1000) / 1000.0f;
  42. float tail_rotor = (input.servos[3]-1000) / 1000.0f;
  43. float rsc = (input.servos[7]-1000) / 1000.0f;
  44. float col_pitch = (swash1+swash2+swash3)/3.0 - 0.5f;
  45. float roll_rate = (swash1 - swash2)/2;
  46. float pitch_rate = -((swash1 + swash2)/2.0 - swash3)/2;
  47. float yaw_rate = -(tail_rotor - 0.5);
  48. servo_packet pkt;
  49. pkt.roll_rate = constrain_float(roll_rate, -0.5, 0.5);
  50. pkt.pitch_rate = constrain_float(pitch_rate, -0.5, 0.5);
  51. pkt.throttle = constrain_float(rsc, 0, 1);
  52. pkt.yaw_rate = constrain_float(yaw_rate, -0.5, 0.5);
  53. pkt.col_pitch = constrain_float(col_pitch, -0.5, 0.5);
  54. sock.sendto(&pkt, sizeof(pkt), "127.0.0.1", 9002);
  55. }
  56. /*
  57. decode and send servos for fixed wing
  58. */
  59. void CRRCSim::send_servos_fixed_wing(const struct sitl_input &input)
  60. {
  61. float roll_rate = ((input.servos[0]-1000)/1000.0) - 0.5;
  62. float pitch_rate = ((input.servos[1]-1000)/1000.0) - 0.5;
  63. float yaw_rate = ((input.servos[3]-1000)/1000.0) - 0.5;
  64. float throttle = ((input.servos[2]-1000)/1000.0);
  65. servo_packet pkt;
  66. pkt.roll_rate = constrain_float(roll_rate, -0.5, 0.5);
  67. pkt.pitch_rate = constrain_float(pitch_rate, -0.5, 0.5);
  68. pkt.throttle = constrain_float(throttle, 0, 1);
  69. pkt.yaw_rate = constrain_float(yaw_rate, -0.5, 0.5);
  70. pkt.col_pitch = 0;
  71. sock.sendto(&pkt, sizeof(pkt), "127.0.0.1", 9002);
  72. }
  73. /*
  74. decode and send servos
  75. */
  76. void CRRCSim::send_servos(const struct sitl_input &input)
  77. {
  78. if (heli_servos) {
  79. send_servos_heli(input);
  80. } else {
  81. send_servos_fixed_wing(input);
  82. }
  83. }
  84. /*
  85. receive an update from the FDM
  86. This is a blocking function
  87. */
  88. void CRRCSim::recv_fdm(const struct sitl_input &input)
  89. {
  90. fdm_packet pkt;
  91. /*
  92. we re-send the servo packet every 0.1 seconds until we get a
  93. reply. This allows us to cope with some packet loss to the FDM
  94. */
  95. while (sock.recv(&pkt, sizeof(pkt), 100) != sizeof(pkt)) {
  96. send_servos(input);
  97. }
  98. accel_body = Vector3f(pkt.xAccel, pkt.yAccel, pkt.zAccel);
  99. gyro = Vector3f(pkt.rollRate, pkt.pitchRate, pkt.yawRate);
  100. velocity_ef = Vector3f(pkt.speedN, pkt.speedE, pkt.speedD);
  101. Location loc1, loc2;
  102. loc2.lat = pkt.latitude * 1.0e7;
  103. loc2.lng = pkt.longitude * 1.0e7;
  104. const Vector2f posdelta = loc1.get_distance_NE(loc2);
  105. position.x = posdelta.x;
  106. position.y = posdelta.y;
  107. position.z = -pkt.altitude;
  108. airspeed = pkt.airspeed;
  109. airspeed_pitot = pkt.airspeed;
  110. dcm.from_euler(pkt.roll, pkt.pitch, pkt.yaw);
  111. // auto-adjust to crrcsim frame rate
  112. double deltat = pkt.timestamp - last_timestamp;
  113. time_now_us += deltat * 1.0e6;
  114. if (deltat < 0.01 && deltat > 0) {
  115. adjust_frame_time(1.0/deltat);
  116. }
  117. last_timestamp = pkt.timestamp;
  118. }
  119. /*
  120. update the CRRCSim simulation by one time step
  121. */
  122. void CRRCSim::update(const struct sitl_input &input)
  123. {
  124. send_servos(input);
  125. recv_fdm(input);
  126. update_position();
  127. time_advance();
  128. // update magnetic field
  129. update_mag_field_bf();
  130. }
  131. } // namespace SITL