2015-05-02 05:09:30 -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/>.
|
|
|
|
*/
|
|
|
|
/*
|
2015-05-03 04:47:58 -03:00
|
|
|
multicopter simulator class
|
2015-05-02 05:09:30 -03:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "SIM_Multicopter.h"
|
2016-02-07 19:58:56 -04:00
|
|
|
#include <AP_Motors/AP_Motors.h>
|
2015-10-22 10:58:33 -03:00
|
|
|
|
2015-05-02 05:09:30 -03:00
|
|
|
#include <stdio.h>
|
|
|
|
|
2015-12-31 23:15:06 -04:00
|
|
|
using namespace SITL;
|
2015-05-02 05:09:30 -03:00
|
|
|
|
2019-08-15 01:01:24 -03:00
|
|
|
MultiCopter::MultiCopter(const char *frame_str) :
|
2020-09-22 19:44:18 -03:00
|
|
|
Aircraft(frame_str)
|
2016-02-01 20:34:29 -04:00
|
|
|
{
|
|
|
|
frame = Frame::find_frame(frame_str);
|
2016-10-30 02:24:21 -03:00
|
|
|
if (frame == nullptr) {
|
2015-05-02 05:09:30 -03:00
|
|
|
printf("Frame '%s' not found", frame_str);
|
|
|
|
exit(1);
|
|
|
|
}
|
2020-10-20 04:19:54 -03:00
|
|
|
|
2020-10-24 17:38:02 -03:00
|
|
|
frame->init(frame_str, &battery);
|
2020-10-20 04:19:54 -03:00
|
|
|
|
|
|
|
mass = frame->get_mass();
|
2015-05-02 05:09:30 -03:00
|
|
|
frame_height = 0.1;
|
2016-07-19 01:42:31 -03:00
|
|
|
ground_behavior = GROUND_BEHAVIOR_NO_MOVEMENT;
|
2021-05-17 21:25:12 -03:00
|
|
|
lock_step_scheduled = true;
|
2015-05-02 05:09:30 -03:00
|
|
|
}
|
|
|
|
|
2015-12-31 23:15:06 -04:00
|
|
|
// calculate rotational and linear accelerations
|
|
|
|
void MultiCopter::calculate_forces(const struct sitl_input &input, Vector3f &rot_accel, Vector3f &body_accel)
|
|
|
|
{
|
2022-08-18 04:18:32 -03:00
|
|
|
motor_mask |= ((1U<<frame->num_motors)-1U) << frame->motor_offset;
|
2019-09-27 16:56:15 -03:00
|
|
|
frame->calculate_forces(*this, input, rot_accel, body_accel, rpm);
|
|
|
|
|
2017-11-27 06:45:23 -04:00
|
|
|
add_shove_forces(rot_accel, body_accel);
|
2019-04-10 22:55:04 -03:00
|
|
|
add_twist_forces(rot_accel);
|
2015-12-31 23:15:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
update the multicopter simulation by one time step
|
|
|
|
*/
|
|
|
|
void MultiCopter::update(const struct sitl_input &input)
|
|
|
|
{
|
2016-04-19 22:48:37 -03:00
|
|
|
// get wind vector setup
|
|
|
|
update_wind(input);
|
|
|
|
|
2015-12-31 23:15:06 -04:00
|
|
|
Vector3f rot_accel;
|
|
|
|
|
|
|
|
calculate_forces(input, rot_accel, accel_body);
|
2015-05-02 05:09:30 -03:00
|
|
|
|
2019-01-12 23:07:36 -04:00
|
|
|
// estimate voltage and current
|
2020-10-20 04:19:54 -03:00
|
|
|
frame->current_and_voltage(battery_voltage, battery_current);
|
2019-01-12 23:07:36 -04:00
|
|
|
|
2020-10-24 17:38:02 -03:00
|
|
|
battery.set_current(battery_current);
|
|
|
|
|
2016-01-01 00:11:55 -04:00
|
|
|
update_dynamics(rot_accel);
|
2018-07-31 09:33:23 -03:00
|
|
|
update_external_payload(input);
|
2015-05-02 05:09:30 -03:00
|
|
|
|
|
|
|
// update lat/lon/altitude
|
|
|
|
update_position();
|
2017-03-03 06:23:40 -04:00
|
|
|
time_advance();
|
2016-06-17 00:46:12 -03:00
|
|
|
|
|
|
|
// update magnetic field
|
|
|
|
update_mag_field_bf();
|
2016-10-27 03:12:09 -03:00
|
|
|
}
|
2019-09-27 16:56:15 -03:00
|
|
|
|