SIM_Submarine.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. ROV/AUV/Submarine simulator class
  15. */
  16. #pragma once
  17. #include "SIM_Aircraft.h"
  18. #include "SIM_Motor.h"
  19. #include "SIM_Frame.h"
  20. namespace SITL {
  21. /*
  22. a submarine simulator
  23. */
  24. class Thruster {
  25. public:
  26. Thruster(int8_t _servo, float roll_fac, float pitch_fac, float yaw_fac, float throttle_fac, float forward_fac, float lat_fac) :
  27. servo(_servo)
  28. {
  29. linear = Vector3f(forward_fac, lat_fac, -throttle_fac);
  30. rotational = Vector3f(roll_fac, pitch_fac, yaw_fac);
  31. };
  32. int8_t servo;
  33. Vector3f linear;
  34. Vector3f rotational;
  35. };
  36. class Submarine : public Aircraft {
  37. public:
  38. Submarine(const char *frame_str);
  39. /* update model by one time step */
  40. void update(const struct sitl_input &input) override;
  41. /* static object creator */
  42. static Aircraft *create(const char *frame_str) {
  43. return new Submarine(frame_str);
  44. }
  45. protected:
  46. const float water_density = 1023.6; // (kg/m^3) At a temperature of 25 °C, salinity of 35 g/kg and 1 atm pressure
  47. const struct {
  48. float length = 0.457; // x direction (meters)
  49. float width = 0.338; // y direction (meters)
  50. float height = 0.254; // z direction (meters)
  51. float weight = 10.5; // (kg)
  52. float thrust = 51.48; // (N)
  53. float thruster_mount_radius = 0.25; // distance in meters from thrusters to center of mass. Used to calculate torque.
  54. float equivalent_sphere_radius = 0.25;
  55. // Moment of Inertia (I)(kg.m²) approximated with a sphere with a 25 cm radius (r) and same density as water
  56. // I = 2.m.r²/5
  57. // mass = volume* density
  58. // volume = 4.pi.r³/3
  59. // ,-----------------------------------Mass--------------------.
  60. // ||------------------------Volume-----------------| |density||
  61. float moment_of_inertia = 2 * (4 * M_PI * pow(equivalent_sphere_radius, 3) / 3) * 1000 * pow(equivalent_sphere_radius, 2) / 5;
  62. float net_buoyancy = 2.0; // (N)
  63. float buoyancy_acceleration = GRAVITY_MSS + net_buoyancy/weight;
  64. // Frame drag coefficient
  65. const Vector3f linear_drag_coefficient = Vector3f(0.2, 0.3, 0.4);
  66. // Angular drag coefficient CD for a cube is 1.05. This is subject to change based on experimentation.
  67. const Vector3f angular_drag_coefficient = Vector3f(1.05, 1.05, 1.05);
  68. // Calculate total volume from water buoyancy
  69. // $ V = F_b / (rho * g) $
  70. // V = volume (m^3), rho = water density (kg/m^3), g = gravity (m/s^2), F_b = force (N)
  71. float volume = buoyancy_acceleration * weight / (GRAVITY_MSS * 1023.6f);
  72. // Calculate equivalent sphere area for drag force
  73. // $ A = pi * r^2 / 4 $
  74. // $ V = 4 * pi * r^3 / 3 $
  75. // $ r ^2 = (V * 3 / 4) ^ (2/3) $
  76. // A = area (m^3), r = sphere radius (m)
  77. float equivalent_sphere_area = M_PI_4 * pow(volume * 3.0f / 4.0f, 2.0f / 3.0f);
  78. } frame_property;
  79. bool on_ground() const override;
  80. // calculate rotational and linear accelerations
  81. void calculate_forces(const struct sitl_input &input, Vector3f &rot_accel, Vector3f &body_accel);
  82. // calculate buoyancy
  83. float calculate_buoyancy_acceleration();
  84. // calculate drag from velocity and drag coefficient
  85. void calculate_drag_force(const Vector3f &velocity, const Vector3f &drag_coefficient, Vector3f &force);
  86. // calculate torque water resistance
  87. void calculate_angular_drag_torque(const Vector3f &angular_velocity, const Vector3f &drag_coefficient, Vector3f &torque);
  88. // calculate torque induced by buoyancy foams
  89. void calculate_buoyancy_torque(Vector3f &torque);
  90. Frame *frame;
  91. Thruster* thrusters;
  92. uint8_t n_thrusters;
  93. };
  94. }