2016-10-30 12:34:41 -03:00
|
|
|
/*
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
/*
|
|
|
|
Submarine simulator class
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "SIM_Submarine.h"
|
|
|
|
#include <AP_Motors/AP_Motors.h>
|
|
|
|
|
2017-03-06 13:52:47 -04:00
|
|
|
#include <stdio.h>
|
|
|
|
|
2016-10-30 12:34:41 -03:00
|
|
|
using namespace SITL;
|
|
|
|
|
|
|
|
static Thruster vectored_thrusters[] =
|
2019-08-05 13:42:48 -03:00
|
|
|
{ // Motor # Roll Factor Pitch Factor Yaw Factor Throttle Factor Forward Factor Lateral Factor
|
|
|
|
Thruster(0, 0, 0, 1.0f, 0, -1.0f, 1.0f),
|
|
|
|
Thruster(1, 0, 0, -1.0f, 0, -1.0f, -1.0f),
|
|
|
|
Thruster(2, 0, 0, -1.0f, 0, 1.0f, 1.0f),
|
|
|
|
Thruster(3, 0, 0, 1.0f, 0, 1.0f, -1.0f),
|
|
|
|
Thruster(4, 1.0f, 0, 0, -1.0f, 0, 0),
|
|
|
|
Thruster(5, -1.0f, 0, 0, -1.0f, 0, 0)
|
|
|
|
};
|
2016-10-30 12:34:41 -03:00
|
|
|
|
2019-08-05 13:44:46 -03:00
|
|
|
|
|
|
|
static Thruster vectored_6dof_thrusters[] =
|
|
|
|
{
|
|
|
|
// Motor # Roll Factor Pitch Factor Yaw Factor Throttle Factor Forward Factor Lateral Factor
|
|
|
|
Thruster(0, 0, 0, 1.0f, 0, -1.0f, 1.0f),
|
|
|
|
Thruster(1, 0, 0, -1.0f, 0, -1.0f, -1.0f),
|
|
|
|
Thruster(2, 0, 0, -1.0f, 0, 1.0f, 1.0f),
|
|
|
|
Thruster(3, 0, 0, 1.0f, 0, 1.0f, -1.0f),
|
|
|
|
Thruster(4, 1.0f, -1.0f, 0, -1.0f, 0, 0),
|
|
|
|
Thruster(5, -1.0f, -1.0f, 0, -1.0f, 0, 0),
|
|
|
|
Thruster(6, 1.0f, 1.0f, 0, -1.0f, 0, 0),
|
|
|
|
Thruster(7, -1.0f, 1.0f, 0, -1.0f, 0, 0)
|
2016-10-30 12:34:41 -03:00
|
|
|
};
|
|
|
|
|
2019-08-15 01:01:24 -03:00
|
|
|
Submarine::Submarine(const char *frame_str) :
|
|
|
|
Aircraft(frame_str),
|
2016-10-30 12:34:41 -03:00
|
|
|
frame(NULL)
|
|
|
|
{
|
2017-03-06 13:52:47 -04:00
|
|
|
frame_height = 0.0;
|
2016-10-30 12:34:41 -03:00
|
|
|
ground_behavior = GROUND_BEHAVIOR_NONE;
|
2019-08-05 13:44:46 -03:00
|
|
|
|
|
|
|
// default to vectored frame
|
|
|
|
thrusters = vectored_thrusters;
|
|
|
|
n_thrusters = 6;
|
|
|
|
|
|
|
|
if (strstr(frame_str, "vectored_6dof")) {
|
|
|
|
thrusters = vectored_6dof_thrusters;
|
|
|
|
n_thrusters = 8;
|
|
|
|
}
|
2021-05-17 21:25:12 -03:00
|
|
|
lock_step_scheduled = true;
|
2016-10-30 12:34:41 -03:00
|
|
|
}
|
|
|
|
|
2021-12-11 00:18:13 -04:00
|
|
|
float Submarine::perpendicular_distance_to_rangefinder_surface() const
|
|
|
|
{
|
|
|
|
const float floor_depth = calculate_sea_floor_depth(position);
|
|
|
|
return floor_depth - position.z;
|
|
|
|
}
|
|
|
|
|
2016-10-30 12:34:41 -03:00
|
|
|
// calculate rotational and linear accelerations
|
|
|
|
void Submarine::calculate_forces(const struct sitl_input &input, Vector3f &rot_accel, Vector3f &body_accel)
|
|
|
|
{
|
|
|
|
rot_accel = Vector3f(0,0,0);
|
2017-03-06 13:52:47 -04:00
|
|
|
|
|
|
|
// slight positive buoyancy
|
2019-08-05 13:49:28 -03:00
|
|
|
body_accel = dcm.transposed() * Vector3f(0, 0, -calculate_buoyancy_acceleration());
|
2017-03-06 13:52:47 -04:00
|
|
|
|
2019-08-05 13:44:46 -03:00
|
|
|
for (int i = 0; i < n_thrusters; i++) {
|
|
|
|
Thruster t = thrusters[i];
|
2016-10-30 12:34:41 -03:00
|
|
|
int16_t pwm = input.servos[t.servo];
|
|
|
|
float output = 0;
|
2019-08-05 14:14:45 -03:00
|
|
|
// if valid pwm and not in the esc deadzone
|
|
|
|
// TODO: extract deadzone from parameters/vehicle code
|
|
|
|
if (pwm < 2000 && pwm > 1000 && (pwm < 1475 || pwm > 1525)) {
|
|
|
|
output = (pwm - 1500) / 400.0; // range -1~1
|
2016-10-30 12:34:41 -03:00
|
|
|
}
|
|
|
|
|
2019-11-28 11:40:10 -04:00
|
|
|
float thrust = output * fabs(output) * frame_property.thrust; // approximate pwm to thrust function using a quadratic curve
|
|
|
|
body_accel += t.linear * thrust / frame_property.weight;
|
|
|
|
rot_accel += t.rotational * thrust * frame_property.thruster_mount_radius / frame_property.moment_of_inertia;
|
2016-10-30 12:34:41 -03:00
|
|
|
}
|
|
|
|
|
2017-03-06 13:52:47 -04:00
|
|
|
// Limit movement at the sea floor
|
2021-12-11 00:18:13 -04:00
|
|
|
const float floor_depth = calculate_sea_floor_depth(position);
|
2019-10-29 13:07:10 -03:00
|
|
|
if (position.z > floor_depth && body_accel.z > -GRAVITY_MSS) {
|
2017-03-06 13:52:47 -04:00
|
|
|
body_accel.z = -GRAVITY_MSS;
|
|
|
|
}
|
|
|
|
|
2019-03-04 17:18:28 -04:00
|
|
|
// Calculate linear drag forces
|
|
|
|
Vector3f linear_drag_forces;
|
|
|
|
calculate_drag_force(velocity_air_bf, frame_property.linear_drag_coefficient, linear_drag_forces);
|
|
|
|
// Add forces in body frame accel
|
|
|
|
body_accel -= linear_drag_forces / frame_property.weight;
|
|
|
|
|
|
|
|
// Calculate angular drag forces
|
2019-09-17 17:20:32 -03:00
|
|
|
// TODO: This results in the wrong units. Fix the math.
|
2019-09-18 14:15:23 -03:00
|
|
|
Vector3f angular_drag_torque;
|
|
|
|
calculate_angular_drag_torque(gyro, frame_property.angular_drag_coefficient, angular_drag_torque);
|
2019-09-17 17:20:32 -03:00
|
|
|
|
|
|
|
// Calculate torque induced by buoyancy foams on the frame
|
|
|
|
Vector3f buoyancy_torque;
|
|
|
|
calculate_buoyancy_torque(buoyancy_torque);
|
2019-03-04 17:18:28 -04:00
|
|
|
// Add forces in body frame accel
|
2019-09-18 14:15:23 -03:00
|
|
|
rot_accel -= angular_drag_torque / frame_property.moment_of_inertia;
|
2019-09-17 17:20:32 -03:00
|
|
|
rot_accel += buoyancy_torque / frame_property.moment_of_inertia;
|
2019-10-28 16:17:08 -03:00
|
|
|
add_shove_forces(rot_accel, body_accel);
|
2019-03-04 17:18:28 -04:00
|
|
|
}
|
2016-10-30 12:34:41 -03:00
|
|
|
|
2019-09-17 17:20:32 -03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Calculate the torque induced by buoyancy foam
|
|
|
|
*
|
|
|
|
* @param torque Output torques
|
|
|
|
*/
|
|
|
|
void Submarine::calculate_buoyancy_torque(Vector3f &torque)
|
|
|
|
{
|
|
|
|
// Let's assume 2 Liters water displacement at the top, and ~ 2kg of weight at the bottom.
|
|
|
|
const Vector3f force_up(0,0,-40); // 40 N upwards
|
|
|
|
const Vector3f force_position = dcm.transposed() * Vector3f(0, 0, 0.15); // offset in meters
|
|
|
|
torque = force_position % force_up;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-10-29 13:07:10 -03:00
|
|
|
/**
|
|
|
|
* @brief Calculate sea floor depth from submarine position
|
|
|
|
* This creates a non planar floor for rangefinder sensor test
|
|
|
|
* TODO: Create a better sea floor with procedural generatation
|
|
|
|
*
|
|
|
|
* @param position
|
|
|
|
* @return float
|
|
|
|
*/
|
2021-12-11 00:18:13 -04:00
|
|
|
float Submarine::calculate_sea_floor_depth(const Vector3d &/*position*/) const
|
2019-10-29 13:07:10 -03:00
|
|
|
{
|
|
|
|
return 50;
|
|
|
|
}
|
|
|
|
|
2019-03-04 17:18:28 -04:00
|
|
|
/**
|
|
|
|
* @brief Calculate drag force against body
|
|
|
|
*
|
|
|
|
* @param velocity Body frame velocity of fluid
|
|
|
|
* @param drag_coefficient Drag coefficient of body
|
|
|
|
* @param force Output forces
|
|
|
|
* $ F_D = rho * v^2 * A * C_D / 2 $
|
|
|
|
* rho = water density (kg/m^3), V = velocity (m/s), A = area (m^2), C_D = drag_coefficient
|
|
|
|
*/
|
2021-02-01 12:37:57 -04:00
|
|
|
void Submarine::calculate_drag_force(const Vector3f &velocity, const Vector3f &drag_coefficient, Vector3f &force) const
|
2019-03-04 17:18:28 -04:00
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @brief It's necessary to keep the velocity orientation from the body frame.
|
|
|
|
* To do so, a mathematical artifice is used to do velocity square but without loosing the direction.
|
|
|
|
* $(|V|/V)*V^2$ = $|V|*V$
|
|
|
|
*/
|
|
|
|
const Vector3f velocity_2(
|
|
|
|
fabsf(velocity.x) * velocity.x,
|
|
|
|
fabsf(velocity.y) * velocity.y,
|
|
|
|
fabsf(velocity.z) * velocity.z
|
|
|
|
);
|
|
|
|
|
|
|
|
force = (velocity_2 * water_density) * frame_property.equivalent_sphere_area / 2.0f;
|
|
|
|
force *= drag_coefficient;
|
2016-10-30 12:34:41 -03:00
|
|
|
}
|
|
|
|
|
2019-09-18 14:15:23 -03:00
|
|
|
/**
|
2019-09-23 12:49:00 -03:00
|
|
|
* @brief Calculate angular drag torque using the equivalente sphere area and assuming a laminar external flow.
|
|
|
|
*
|
|
|
|
* $F_D = C_D*A*\rho*V^2/2$
|
|
|
|
* where:
|
|
|
|
* $F_D$ is the drag force
|
|
|
|
* $C_D$ is the drag coefficient
|
|
|
|
* $A$ is the surface area in contact with the fluid
|
|
|
|
* $/rho$ is the fluid density (1000kg/m³ for water)
|
|
|
|
* $V$ is the fluid velocity velocity relative to the surface
|
2019-09-18 14:15:23 -03:00
|
|
|
*
|
|
|
|
* @param angular_velocity Body frame velocity of fluid
|
|
|
|
* @param drag_coefficient Rotational drag coefficient of body
|
|
|
|
*/
|
2021-02-01 12:37:57 -04:00
|
|
|
void Submarine::calculate_angular_drag_torque(const Vector3f &angular_velocity, const Vector3f &drag_coefficient, Vector3f &torque) const
|
2019-09-18 14:15:23 -03:00
|
|
|
{
|
2019-09-23 12:49:00 -03:00
|
|
|
/**
|
|
|
|
* @brief It's necessary to keep the velocity orientation from the body frame.
|
|
|
|
* To do so, a mathematical artifice is used to do velocity square but without loosing the direction.
|
|
|
|
* $(|V|/V)*V^2$ = $|V|*V$
|
|
|
|
*/
|
|
|
|
Vector3f v_2(
|
|
|
|
fabsf(angular_velocity.x) * angular_velocity.x,
|
|
|
|
fabsf(angular_velocity.y) * angular_velocity.y,
|
|
|
|
fabsf(angular_velocity.z) * angular_velocity.z
|
|
|
|
);
|
|
|
|
Vector3f f_d = v_2 *= drag_coefficient * frame_property.equivalent_sphere_area * 1000 / 2;
|
|
|
|
torque = f_d * frame_property.equivalent_sphere_radius;
|
2019-09-18 14:15:23 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-11-22 12:09:43 -04:00
|
|
|
/**
|
|
|
|
* @brief Calculate buoyancy force of the frame
|
|
|
|
*
|
|
|
|
* @return float
|
|
|
|
*/
|
|
|
|
float Submarine::calculate_buoyancy_acceleration()
|
|
|
|
{
|
2018-10-14 01:36:33 -03:00
|
|
|
float below_water_level = position.z - frame_property.height/2;
|
2017-11-22 12:09:43 -04:00
|
|
|
|
|
|
|
// Completely above water level
|
|
|
|
if (below_water_level < 0) {
|
|
|
|
return 0.0f;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Completely below water level
|
2018-10-14 01:36:33 -03:00
|
|
|
if (below_water_level > frame_property.height/2) {
|
2020-04-13 12:39:27 -03:00
|
|
|
return GRAVITY_MSS + sitl->buoyancy / frame_property.mass;
|
2017-11-22 12:09:43 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// bouyant force is proportional to fraction of height in water
|
2020-04-13 12:39:27 -03:00
|
|
|
return GRAVITY_MSS + (sitl->buoyancy * below_water_level/frame_property.height) / frame_property.mass;
|
2017-11-22 12:09:43 -04:00
|
|
|
};
|
|
|
|
|
2016-10-30 12:34:41 -03:00
|
|
|
/*
|
|
|
|
update the Submarine simulation by one time step
|
|
|
|
*/
|
|
|
|
void Submarine::update(const struct sitl_input &input)
|
|
|
|
{
|
|
|
|
// get wind vector setup
|
|
|
|
update_wind(input);
|
|
|
|
|
|
|
|
Vector3f rot_accel;
|
|
|
|
|
|
|
|
calculate_forces(input, rot_accel, accel_body);
|
|
|
|
|
|
|
|
update_dynamics(rot_accel);
|
2021-01-03 20:51:04 -04:00
|
|
|
update_external_payload(input);
|
2016-10-30 12:34:41 -03:00
|
|
|
|
|
|
|
// update lat/lon/altitude
|
|
|
|
update_position();
|
2017-03-03 06:23:40 -04:00
|
|
|
time_advance();
|
2016-10-30 12:34:41 -03:00
|
|
|
|
|
|
|
// update magnetic field
|
|
|
|
update_mag_field_bf();
|
|
|
|
}
|
2017-03-06 13:52:47 -04:00
|
|
|
|
|
|
|
/*
|
|
|
|
return true if we are on the ground
|
|
|
|
*/
|
|
|
|
bool Submarine::on_ground() const
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|