SIM_Rover.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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. rover simulator class
  15. */
  16. #include "SIM_Rover.h"
  17. #include <string.h>
  18. #include <stdio.h>
  19. namespace SITL {
  20. SimRover::SimRover(const char *frame_str) :
  21. Aircraft(frame_str),
  22. max_speed(20),
  23. max_accel(10),
  24. max_wheel_turn(35),
  25. turning_circle(1.8),
  26. skid_turn_rate(140), // degrees/sec
  27. skid_steering(false)
  28. {
  29. skid_steering = strstr(frame_str, "skid") != nullptr;
  30. if (skid_steering) {
  31. printf("SKID Steering Rover Simulation Started\n");
  32. // these are taken from a 6V wild thumper with skid steering,
  33. // with a sabertooth controller
  34. max_accel = 14;
  35. max_speed = 4;
  36. }
  37. }
  38. /*
  39. return turning circle (diameter) in meters for steering angle proportion in degrees
  40. */
  41. float SimRover::turn_circle(float steering)
  42. {
  43. if (fabsf(steering) < 1.0e-6) {
  44. return 0;
  45. }
  46. return turning_circle * sinf(radians(max_wheel_turn)) / sinf(radians(steering*max_wheel_turn));
  47. }
  48. /*
  49. return yaw rate in degrees/second given steering_angle and speed
  50. */
  51. float SimRover::calc_yaw_rate(float steering, float speed)
  52. {
  53. if (skid_steering) {
  54. return steering * skid_turn_rate;
  55. }
  56. if (fabsf(steering) < 1.0e-6 or fabsf(speed) < 1.0e-6) {
  57. return 0;
  58. }
  59. float d = turn_circle(steering);
  60. float c = M_PI * d;
  61. float t = c / speed;
  62. float rate = 360.0f / t;
  63. return rate;
  64. }
  65. /*
  66. return lateral acceleration in m/s/s
  67. */
  68. float SimRover::calc_lat_accel(float steering_angle, float speed)
  69. {
  70. float yaw_rate = calc_yaw_rate(steering_angle, speed);
  71. float accel = radians(yaw_rate) * speed;
  72. return accel;
  73. }
  74. /*
  75. update the rover simulation by one time step
  76. */
  77. void SimRover::update(const struct sitl_input &input)
  78. {
  79. float steering, throttle;
  80. // if in skid steering mode the steering and throttle values are used for motor1 and motor2
  81. if (skid_steering) {
  82. float motor1 = 2*((input.servos[0]-1000)/1000.0f - 0.5f);
  83. float motor2 = 2*((input.servos[2]-1000)/1000.0f - 0.5f);
  84. steering = motor1 - motor2;
  85. throttle = 0.5*(motor1 + motor2);
  86. } else {
  87. steering = 2*((input.servos[0]-1000)/1000.0f - 0.5f);
  88. throttle = 2*((input.servos[2]-1000)/1000.0f - 0.5f);
  89. }
  90. // how much time has passed?
  91. float delta_time = frame_time_us * 1.0e-6f;
  92. // speed in m/s in body frame
  93. Vector3f velocity_body = dcm.transposed() * velocity_ef;
  94. // speed along x axis, +ve is forward
  95. float speed = velocity_body.x;
  96. // yaw rate in degrees/s
  97. float yaw_rate = calc_yaw_rate(steering, speed);
  98. // target speed with current throttle
  99. float target_speed = throttle * max_speed;
  100. // linear acceleration in m/s/s - very crude model
  101. float accel = max_accel * (target_speed - speed) / max_speed;
  102. gyro = Vector3f(0,0,radians(yaw_rate));
  103. // update attitude
  104. dcm.rotate(gyro * delta_time);
  105. dcm.normalize();
  106. // accel in body frame due to motor
  107. accel_body = Vector3f(accel, 0, 0);
  108. // add in accel due to direction change
  109. accel_body.y += radians(yaw_rate) * speed;
  110. // now in earth frame
  111. Vector3f accel_earth = dcm * accel_body;
  112. accel_earth += Vector3f(0, 0, GRAVITY_MSS);
  113. // we are on the ground, so our vertical accel is zero
  114. accel_earth.z = 0;
  115. // work out acceleration as seen by the accelerometers. It sees the kinematic
  116. // acceleration (ie. real movement), plus gravity
  117. accel_body = dcm.transposed() * (accel_earth + Vector3f(0, 0, -GRAVITY_MSS));
  118. // new velocity vector
  119. velocity_ef += accel_earth * delta_time;
  120. // new position vector
  121. position += velocity_ef * delta_time;
  122. update_external_payload(input);
  123. // update lat/lon/altitude
  124. update_position();
  125. time_advance();
  126. // update magnetic field
  127. update_mag_field_bf();
  128. }
  129. } // namespace SITL