SIM_Multicopter.cpp 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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. multicopter simulator class
  15. */
  16. #include "SIM_Multicopter.h"
  17. #include <AP_Motors/AP_Motors.h>
  18. #include <stdio.h>
  19. using namespace SITL;
  20. MultiCopter::MultiCopter(const char *frame_str) :
  21. Aircraft(frame_str),
  22. frame(nullptr)
  23. {
  24. mass = 1.5f;
  25. frame = Frame::find_frame(frame_str);
  26. if (frame == nullptr) {
  27. printf("Frame '%s' not found", frame_str);
  28. exit(1);
  29. }
  30. // initial mass is passed through to Frame for it to calculate a
  31. // hover thrust requirement.
  32. if (strstr(frame_str, "-fast")) {
  33. frame->init(gross_mass(), 0.5, 85, 4*radians(360));
  34. } else {
  35. frame->init(gross_mass(), 0.51, 15, 4*radians(360));
  36. }
  37. frame_height = 0.1;
  38. ground_behavior = GROUND_BEHAVIOR_NO_MOVEMENT;
  39. }
  40. // calculate rotational and linear accelerations
  41. void MultiCopter::calculate_forces(const struct sitl_input &input, Vector3f &rot_accel, Vector3f &body_accel)
  42. {
  43. frame->calculate_forces(*this, input, rot_accel, body_accel);
  44. add_shove_forces(rot_accel, body_accel);
  45. add_twist_forces(rot_accel);
  46. }
  47. /*
  48. update the multicopter simulation by one time step
  49. */
  50. void MultiCopter::update(const struct sitl_input &input)
  51. {
  52. // get wind vector setup
  53. update_wind(input);
  54. Vector3f rot_accel;
  55. calculate_forces(input, rot_accel, accel_body);
  56. // estimate voltage and current
  57. frame->current_and_voltage(input, battery_voltage, battery_current);
  58. update_dynamics(rot_accel);
  59. update_external_payload(input);
  60. // update lat/lon/altitude
  61. update_position();
  62. time_advance();
  63. // update magnetic field
  64. update_mag_field_bf();
  65. }