SIM_SingleCopter.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. singlecopter simulator class
  15. */
  16. #include "SIM_SingleCopter.h"
  17. #include <stdio.h>
  18. using namespace SITL;
  19. SingleCopter::SingleCopter(const char *frame_str) :
  20. Aircraft(frame_str)
  21. {
  22. mass = 2.0f;
  23. if (strstr(frame_str, "coax")) {
  24. frame_type = FRAME_COAX;
  25. } else {
  26. frame_type = FRAME_SINGLE;
  27. }
  28. /*
  29. scaling from motor power to Newtons. Allows the copter
  30. to hover against gravity when the motor is at hover_throttle
  31. */
  32. thrust_scale = (mass * GRAVITY_MSS) / hover_throttle;
  33. frame_height = 0.1;
  34. }
  35. /*
  36. update the copter simulation by one time step
  37. */
  38. void SingleCopter::update(const struct sitl_input &input)
  39. {
  40. // get wind vector setup
  41. update_wind(input);
  42. float actuator[4];
  43. for (uint8_t i=0; i<4; i++) {
  44. actuator[i] = constrain_float((input.servos[i]-1500) / 500.0f, -1, 1);
  45. }
  46. float thrust;
  47. float yaw_thrust;
  48. float roll_thrust;
  49. float pitch_thrust;
  50. switch (frame_type) {
  51. case FRAME_SINGLE:
  52. thrust = constrain_float((input.servos[4]-1000) / 1000.0f, 0, 1);
  53. yaw_thrust = -(actuator[0] + actuator[1] + actuator[2] + actuator[3]) * 0.25f * thrust + thrust * rotor_rot_accel;
  54. roll_thrust = (actuator[0] - actuator[2]) * 0.5f * thrust;
  55. pitch_thrust = (actuator[1] - actuator[3]) * 0.5f * thrust;
  56. break;
  57. case FRAME_COAX:
  58. default: {
  59. float motor1 = constrain_float((input.servos[4]-1000) / 1000.0f, 0, 1);
  60. float motor2 = constrain_float((input.servos[5]-1000) / 1000.0f, 0, 1);
  61. thrust = 0.5f*(motor1 + motor2);
  62. yaw_thrust = -(actuator[0] + actuator[1] + actuator[2] + actuator[3]) * 0.25f * thrust + (motor2 - motor1) * rotor_rot_accel;
  63. roll_thrust = (actuator[0] - actuator[2]) * 0.5f * thrust;
  64. pitch_thrust = (actuator[1] - actuator[3]) * 0.5f * thrust;
  65. break;
  66. }
  67. }
  68. // rotational acceleration, in rad/s/s, in body frame
  69. Vector3f rot_accel(roll_thrust * roll_rate_max,
  70. pitch_thrust * pitch_rate_max,
  71. yaw_thrust * yaw_rate_max);
  72. // rotational air resistance
  73. rot_accel.x -= gyro.x * radians(5000.0) / terminal_rotation_rate;
  74. rot_accel.y -= gyro.y * radians(5000.0) / terminal_rotation_rate;
  75. rot_accel.z -= gyro.z * radians(400.0) / terminal_rotation_rate;
  76. // air resistance
  77. Vector3f air_resistance = -velocity_air_ef * (GRAVITY_MSS/terminal_velocity);
  78. // scale thrust to newtons
  79. thrust *= thrust_scale;
  80. accel_body = Vector3f(0, 0, -thrust / mass);
  81. accel_body += dcm.transposed() * air_resistance;
  82. update_dynamics(rot_accel);
  83. // update lat/lon/altitude
  84. update_position();
  85. time_advance();
  86. // update magnetic field
  87. update_mag_field_bf();
  88. }