SIM_last_letter.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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 last_letter
  15. */
  16. #include "SIM_last_letter.h"
  17. #include <fcntl.h>
  18. #include <stdio.h>
  19. #include <sys/stat.h>
  20. #include <sys/types.h>
  21. #include <AP_HAL/AP_HAL.h>
  22. extern const AP_HAL::HAL& hal;
  23. namespace SITL {
  24. last_letter::last_letter(const char *_frame_str) :
  25. Aircraft(_frame_str),
  26. last_timestamp_us(0),
  27. sock(true)
  28. {
  29. // try to bind to a specific port so that if we restart ArduPilot
  30. // last_letter keeps sending us packets. Not strictly necessary but
  31. // useful for debugging
  32. sock.bind("127.0.0.1", fdm_port+1);
  33. sock.reuseaddress();
  34. sock.set_blocking(false);
  35. start_last_letter();
  36. }
  37. /*
  38. start last_letter child
  39. */
  40. void last_letter::start_last_letter(void)
  41. {
  42. pid_t child_pid = fork();
  43. if (child_pid == 0) {
  44. // in child
  45. close(0);
  46. open("/dev/null", O_RDONLY|O_CLOEXEC);
  47. for (uint8_t i=3; i<100; i++) {
  48. close(i);
  49. }
  50. int ret = execlp("roslaunch",
  51. "roslaunch",
  52. "last_letter",
  53. "gazebo.launch",
  54. "ArduPlane:=true",
  55. nullptr);
  56. if (ret != 0) {
  57. perror("roslaunch");
  58. }
  59. exit(1);
  60. }
  61. }
  62. /*
  63. send servos
  64. */
  65. void last_letter::send_servos(const struct sitl_input &input)
  66. {
  67. servo_packet pkt;
  68. memcpy(pkt.servos, input.servos, sizeof(pkt.servos));
  69. sock.sendto(&pkt, sizeof(pkt), "127.0.0.1", fdm_port);
  70. }
  71. /*
  72. receive an update from the FDM
  73. This is a blocking function
  74. */
  75. void last_letter::recv_fdm(const struct sitl_input &input)
  76. {
  77. fdm_packet pkt;
  78. /*
  79. we re-send the servo packet every 0.1 seconds until we get a
  80. reply. This allows us to cope with some packet loss to the FDM
  81. */
  82. while (sock.recv(&pkt, sizeof(pkt), 100) != sizeof(pkt)) {
  83. send_servos(input);
  84. }
  85. accel_body = Vector3f(pkt.xAccel, pkt.yAccel, pkt.zAccel);
  86. gyro = Vector3f(pkt.rollRate, pkt.pitchRate, pkt.yawRate);
  87. velocity_ef = Vector3f(pkt.speedN, pkt.speedE, pkt.speedD);
  88. location.lat = pkt.latitude * 1.0e7;
  89. location.lng = pkt.longitude * 1.0e7;
  90. location.alt = pkt.altitude*1.0e2;
  91. dcm.from_euler(pkt.roll, pkt.pitch, pkt.yaw);
  92. airspeed = pkt.airspeed;
  93. airspeed_pitot = pkt.airspeed;
  94. // auto-adjust to last_letter frame rate
  95. uint64_t deltat_us = pkt.timestamp_us - last_timestamp_us;
  96. time_now_us += deltat_us;
  97. if (deltat_us < 1.0e4 && deltat_us > 0) {
  98. adjust_frame_time(1.0e6/deltat_us);
  99. }
  100. last_timestamp_us = pkt.timestamp_us;
  101. }
  102. /*
  103. update the last_letter simulation by one time step
  104. */
  105. void last_letter::update(const struct sitl_input &input)
  106. {
  107. send_servos(input);
  108. recv_fdm(input);
  109. sync_frame_time();
  110. update_position();
  111. time_advance();
  112. // update magnetic field
  113. update_mag_field_bf();
  114. }
  115. } // namespace SITL