2017-08-26 08:53:53 -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/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <AP_HAL/AP_HAL.h>
|
2019-08-07 23:52:17 -03:00
|
|
|
#include <GCS_MAVLink/GCS.h>
|
2017-08-26 08:53:53 -03:00
|
|
|
#include "AP_MotorsHeli_Quad.h"
|
|
|
|
|
|
|
|
extern const AP_HAL::HAL& hal;
|
|
|
|
|
|
|
|
const AP_Param::GroupInfo AP_MotorsHeli_Quad::var_info[] = {
|
|
|
|
AP_NESTEDGROUPINFO(AP_MotorsHeli, 0),
|
|
|
|
|
2018-01-22 23:58:52 -04:00
|
|
|
// Indices 1-3 were used by RSC_PWM_MIN, RSC_PWM_MAX and RSC_PWM_REV and should not be used
|
|
|
|
|
2017-08-26 08:53:53 -03:00
|
|
|
AP_GROUPEND
|
|
|
|
};
|
|
|
|
|
2018-07-11 16:33:00 -03:00
|
|
|
#define QUAD_SERVO_MAX_ANGLE 4500
|
|
|
|
|
2017-08-26 08:53:53 -03:00
|
|
|
// set update rate to motors - a value in hertz
|
|
|
|
void AP_MotorsHeli_Quad::set_update_rate( uint16_t speed_hz )
|
|
|
|
{
|
|
|
|
// record requested speed
|
|
|
|
_speed_hz = speed_hz;
|
|
|
|
|
|
|
|
// setup fast channels
|
|
|
|
uint32_t mask = 0;
|
|
|
|
for (uint8_t i=0; i<AP_MOTORS_HELI_QUAD_NUM_MOTORS; i++) {
|
|
|
|
mask |= 1U << (AP_MOTORS_MOT_1+i);
|
|
|
|
}
|
|
|
|
|
|
|
|
rc_set_freq(mask, _speed_hz);
|
|
|
|
}
|
|
|
|
|
|
|
|
// init_outputs
|
|
|
|
bool AP_MotorsHeli_Quad::init_outputs()
|
|
|
|
{
|
2019-05-03 02:27:09 -03:00
|
|
|
if (initialised_ok()) {
|
2017-08-26 08:53:53 -03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (uint8_t i=0; i<AP_MOTORS_HELI_QUAD_NUM_MOTORS; i++) {
|
2018-07-11 16:33:00 -03:00
|
|
|
add_motor_num(CH_1+i);
|
|
|
|
SRV_Channels::set_angle(SRV_Channels::get_motor_function(i), QUAD_SERVO_MAX_ANGLE);
|
2017-08-26 08:53:53 -03:00
|
|
|
}
|
2018-03-23 01:09:14 -03:00
|
|
|
|
2017-08-26 08:53:53 -03:00
|
|
|
// set rotor servo range
|
2019-08-07 23:52:17 -03:00
|
|
|
_main_rotor.init_servo();
|
2017-08-26 08:53:53 -03:00
|
|
|
|
2020-04-01 11:40:53 -03:00
|
|
|
// set signal value for main rotor external governor to know when to use autorotation bailout ramp up
|
|
|
|
if (_main_rotor._rsc_mode.get() == ROTOR_CONTROL_MODE_SPEED_SETPOINT || _main_rotor._rsc_mode.get() == ROTOR_CONTROL_MODE_SPEED_PASSTHROUGH) {
|
|
|
|
_main_rotor.set_ext_gov_arot_bail(_main_rotor._ext_gov_arot_pct.get());
|
|
|
|
} else {
|
|
|
|
_main_rotor.set_ext_gov_arot_bail(0);
|
|
|
|
}
|
|
|
|
|
2019-05-03 02:27:09 -03:00
|
|
|
set_initialised_ok(true);
|
2017-08-26 08:53:53 -03:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-04-27 13:22:07 -03:00
|
|
|
// output_test_seq - spin a motor at the pwm value specified
|
2017-08-26 08:53:53 -03:00
|
|
|
// motor_seq is the motor's sequence number from 1 to the number of motors on the frame
|
|
|
|
// pwm value is an actual pwm value that will be output, normally in the range of 1000 ~ 2000
|
2018-04-27 13:22:07 -03:00
|
|
|
void AP_MotorsHeli_Quad::output_test_seq(uint8_t motor_seq, int16_t pwm)
|
2017-08-26 08:53:53 -03:00
|
|
|
{
|
|
|
|
// exit immediately if not armed
|
|
|
|
if (!armed()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// output to motors and servos
|
|
|
|
switch (motor_seq) {
|
|
|
|
case 1 ... AP_MOTORS_HELI_QUAD_NUM_MOTORS:
|
|
|
|
rc_write(AP_MOTORS_MOT_1 + (motor_seq-1), pwm);
|
|
|
|
break;
|
|
|
|
case AP_MOTORS_HELI_QUAD_NUM_MOTORS+1:
|
|
|
|
// main rotor
|
2019-08-07 23:52:17 -03:00
|
|
|
rc_write(AP_MOTORS_HELI_RSC, pwm);
|
2017-08-26 08:53:53 -03:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
// do nothing
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// set_desired_rotor_speed
|
|
|
|
void AP_MotorsHeli_Quad::set_desired_rotor_speed(float desired_speed)
|
|
|
|
{
|
2019-08-07 23:52:17 -03:00
|
|
|
_main_rotor.set_desired_speed(desired_speed);
|
2017-08-26 08:53:53 -03:00
|
|
|
}
|
|
|
|
|
2019-02-03 19:19:13 -04:00
|
|
|
// set_rotor_rpm - used for governor with speed sensor
|
2019-05-19 14:20:17 -03:00
|
|
|
void AP_MotorsHeli_Quad::set_rpm(float rotor_rpm)
|
2019-02-03 19:19:13 -04:00
|
|
|
{
|
2019-08-07 23:52:17 -03:00
|
|
|
_main_rotor.set_rotor_rpm(rotor_rpm);
|
2019-02-03 19:19:13 -04:00
|
|
|
}
|
|
|
|
|
2017-08-26 08:53:53 -03:00
|
|
|
// calculate_armed_scalars
|
|
|
|
void AP_MotorsHeli_Quad::calculate_armed_scalars()
|
|
|
|
{
|
2019-02-14 20:28:48 -04:00
|
|
|
// Set rsc mode specific parameters
|
2019-08-07 23:52:17 -03:00
|
|
|
if (_main_rotor._rsc_mode.get() == ROTOR_CONTROL_MODE_OPEN_LOOP_POWER_OUTPUT || _main_rotor._rsc_mode.get() == ROTOR_CONTROL_MODE_CLOSED_LOOP_POWER_OUTPUT) {
|
|
|
|
_main_rotor.set_throttle_curve();
|
|
|
|
}
|
|
|
|
// keeps user from changing RSC mode while armed
|
|
|
|
if (_main_rotor._rsc_mode.get() != _main_rotor.get_control_mode()) {
|
|
|
|
_main_rotor.reset_rsc_mode_param();
|
|
|
|
gcs().send_text(MAV_SEVERITY_CRITICAL, "RSC control mode change failed");
|
|
|
|
_heliflags.save_rsc_mode = true;
|
|
|
|
}
|
|
|
|
// saves rsc mode parameter when disarmed if it had been reset while armed
|
2019-05-03 02:27:09 -03:00
|
|
|
if (_heliflags.save_rsc_mode && !armed()) {
|
2019-08-07 23:52:17 -03:00
|
|
|
_main_rotor._rsc_mode.save();
|
|
|
|
_heliflags.save_rsc_mode = false;
|
2019-02-14 20:28:48 -04:00
|
|
|
}
|
2019-11-28 16:23:47 -04:00
|
|
|
|
|
|
|
// set bailout ramp time
|
|
|
|
_main_rotor.use_bailout_ramp_time(_heliflags.enable_bailout);
|
2020-04-01 11:40:53 -03:00
|
|
|
|
|
|
|
// allow use of external governor autorotation bailout window on main rotor
|
|
|
|
if (_main_rotor._ext_gov_arot_pct.get() > 0 && (_main_rotor._rsc_mode.get() == ROTOR_CONTROL_MODE_SPEED_SETPOINT || _main_rotor._rsc_mode.get() == ROTOR_CONTROL_MODE_SPEED_PASSTHROUGH)){
|
|
|
|
// RSC only needs to know that the vehicle is in an autorotation if using the bailout window on an external governor
|
2021-02-08 13:21:02 -04:00
|
|
|
_main_rotor.set_autorotation_flag(_heliflags.in_autorotation);
|
2020-04-01 11:40:53 -03:00
|
|
|
}
|
2017-08-26 08:53:53 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
// calculate_scalars
|
|
|
|
void AP_MotorsHeli_Quad::calculate_scalars()
|
|
|
|
{
|
|
|
|
// range check collective min, max and mid
|
|
|
|
if( _collective_min >= _collective_max ) {
|
|
|
|
_collective_min = AP_MOTORS_HELI_COLLECTIVE_MIN;
|
|
|
|
_collective_max = AP_MOTORS_HELI_COLLECTIVE_MAX;
|
|
|
|
}
|
|
|
|
|
|
|
|
_collective_mid = constrain_int16(_collective_mid, _collective_min, _collective_max);
|
|
|
|
|
|
|
|
// calculate collective mid point as a number from 0 to 1000
|
|
|
|
_collective_mid_pct = ((float)(_collective_mid-_collective_min))/((float)(_collective_max-_collective_min));
|
|
|
|
|
|
|
|
// calculate factors based on swash type and servo position
|
|
|
|
calculate_roll_pitch_collective_factors();
|
|
|
|
|
|
|
|
// set mode of main rotor controller and trigger recalculation of scalars
|
2019-08-07 23:52:17 -03:00
|
|
|
_main_rotor.set_control_mode(static_cast<RotorControlMode>(_main_rotor._rsc_mode.get()));
|
2017-08-26 08:53:53 -03:00
|
|
|
calculate_armed_scalars();
|
|
|
|
}
|
|
|
|
|
|
|
|
// calculate_swash_factors - calculate factors based on swash type and servo position
|
|
|
|
void AP_MotorsHeli_Quad::calculate_roll_pitch_collective_factors()
|
|
|
|
{
|
|
|
|
// assume X quad layout, with motors at 45, 135, 225 and 315 degrees
|
|
|
|
// order FrontRight, RearLeft, FrontLeft, RearLeft
|
|
|
|
const float angles[AP_MOTORS_HELI_QUAD_NUM_MOTORS] = { 45, 225, 315, 135 };
|
2017-09-30 03:30:02 -03:00
|
|
|
const bool x_clockwise[AP_MOTORS_HELI_QUAD_NUM_MOTORS] = { false, false, true, true };
|
2017-08-26 08:53:53 -03:00
|
|
|
const float cos45 = cosf(radians(45));
|
2018-03-23 01:09:14 -03:00
|
|
|
|
2017-08-26 08:53:53 -03:00
|
|
|
for (uint8_t i=0; i<AP_MOTORS_HELI_QUAD_NUM_MOTORS; i++) {
|
2017-09-30 03:30:02 -03:00
|
|
|
bool clockwise = x_clockwise[i];
|
|
|
|
if (_frame_type == MOTOR_FRAME_TYPE_H) {
|
|
|
|
// reverse yaw for H frame
|
|
|
|
clockwise = !clockwise;
|
|
|
|
}
|
2020-01-23 23:05:24 -04:00
|
|
|
_rollFactor[CH_1+i] = -0.25*sinf(radians(angles[i]))/cos45;
|
|
|
|
_pitchFactor[CH_1+i] = 0.25*cosf(radians(angles[i]))/cos45;
|
|
|
|
_yawFactor[CH_1+i] = clockwise?-0.25:0.25;
|
2017-08-26 08:53:53 -03:00
|
|
|
_collectiveFactor[CH_1+i] = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// get_motor_mask - returns a bitmask of which outputs are being used for motors or servos (1 means being used)
|
|
|
|
// this can be used to ensure other pwm outputs (i.e. for servos) do not conflict
|
|
|
|
uint16_t AP_MotorsHeli_Quad::get_motor_mask()
|
|
|
|
{
|
|
|
|
uint16_t mask = 0;
|
|
|
|
for (uint8_t i=0; i<AP_MOTORS_HELI_QUAD_NUM_MOTORS; i++) {
|
|
|
|
mask |= 1U << (AP_MOTORS_MOT_1+i);
|
|
|
|
}
|
2019-08-07 23:52:17 -03:00
|
|
|
mask |= 1U << AP_MOTORS_HELI_RSC;
|
2017-08-26 08:53:53 -03:00
|
|
|
return mask;
|
|
|
|
}
|
|
|
|
|
|
|
|
// update_motor_controls - sends commands to motor controllers
|
|
|
|
void AP_MotorsHeli_Quad::update_motor_control(RotorControlState state)
|
|
|
|
{
|
|
|
|
// Send state update to motors
|
2019-08-07 23:52:17 -03:00
|
|
|
_main_rotor.output(state);
|
2017-08-26 08:53:53 -03:00
|
|
|
|
|
|
|
if (state == ROTOR_CONTROL_STOP) {
|
|
|
|
// set engine run enable aux output to not run position to kill engine when disarmed
|
2019-11-24 21:52:18 -04:00
|
|
|
SRV_Channels::set_output_limit(SRV_Channel::k_engine_run_enable, SRV_Channel::Limit::MIN);
|
2017-08-26 08:53:53 -03:00
|
|
|
} else {
|
|
|
|
// else if armed, set engine run enable output to run position
|
2019-11-24 21:52:18 -04:00
|
|
|
SRV_Channels::set_output_limit(SRV_Channel::k_engine_run_enable, SRV_Channel::Limit::MAX);
|
2017-08-26 08:53:53 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check if rotors are run-up
|
2019-08-07 23:52:17 -03:00
|
|
|
_heliflags.rotor_runup_complete = _main_rotor.is_runup_complete();
|
2017-08-26 08:53:53 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// move_actuators - moves swash plate to attitude of parameters passed in
|
|
|
|
// - expected ranges:
|
|
|
|
// roll : -1 ~ +1
|
|
|
|
// pitch: -1 ~ +1
|
|
|
|
// collective: 0 ~ 1
|
|
|
|
// yaw: -1 ~ +1
|
|
|
|
//
|
|
|
|
void AP_MotorsHeli_Quad::move_actuators(float roll_out, float pitch_out, float collective_in, float yaw_out)
|
|
|
|
{
|
|
|
|
// initialize limits flag
|
|
|
|
limit.throttle_lower = false;
|
|
|
|
limit.throttle_upper = false;
|
|
|
|
|
|
|
|
// constrain collective input
|
|
|
|
float collective_out = collective_in;
|
|
|
|
if (collective_out <= 0.0f) {
|
|
|
|
collective_out = 0.0f;
|
|
|
|
limit.throttle_lower = true;
|
|
|
|
}
|
|
|
|
if (collective_out >= 1.0f) {
|
|
|
|
collective_out = 1.0f;
|
|
|
|
limit.throttle_upper = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// ensure not below landed/landing collective
|
2019-04-29 23:24:32 -03:00
|
|
|
if (_heliflags.landing_collective && collective_out < _collective_mid_pct) {
|
|
|
|
collective_out = _collective_mid_pct;
|
2017-08-26 08:53:53 -03:00
|
|
|
limit.throttle_lower = true;
|
|
|
|
}
|
|
|
|
|
2020-12-20 17:42:06 -04:00
|
|
|
// updates below mid collective flag
|
|
|
|
if (collective_out <= _collective_mid_pct) {
|
|
|
|
_heliflags.below_mid_collective = true;
|
|
|
|
} else {
|
|
|
|
_heliflags.below_mid_collective = false;
|
|
|
|
}
|
|
|
|
|
2020-12-01 17:35:45 -04:00
|
|
|
// updates takeoff collective flag based on 50% hover collective
|
|
|
|
update_takeoff_collective_flag(collective_out);
|
2020-11-15 19:32:31 -04:00
|
|
|
|
2020-01-23 23:05:24 -04:00
|
|
|
float collective_range = (_collective_max - _collective_min) * 0.001f;
|
2017-08-27 22:34:37 -03:00
|
|
|
|
2017-08-27 20:51:33 -03:00
|
|
|
if (_heliflags.inverted_flight) {
|
2020-01-23 23:05:24 -04:00
|
|
|
collective_out = 1.0f - collective_out;
|
2017-08-27 20:51:33 -03:00
|
|
|
}
|
2018-03-23 01:09:14 -03:00
|
|
|
|
2017-08-26 08:53:53 -03:00
|
|
|
// feed power estimate into main rotor controller
|
2019-08-07 23:52:17 -03:00
|
|
|
_main_rotor.set_collective(fabsf(collective_out));
|
2017-08-26 08:53:53 -03:00
|
|
|
|
2020-01-23 23:05:24 -04:00
|
|
|
// rescale collective for overhead calc
|
|
|
|
collective_out -= _collective_mid_pct;
|
2017-08-26 08:53:53 -03:00
|
|
|
|
2017-08-27 22:34:37 -03:00
|
|
|
// reserve some collective for attitude control
|
|
|
|
collective_out *= collective_range;
|
|
|
|
|
2017-08-26 08:53:53 -03:00
|
|
|
for (uint8_t i=0; i<AP_MOTORS_HELI_QUAD_NUM_MOTORS; i++) {
|
2018-12-28 02:32:05 -04:00
|
|
|
_out[i] =
|
2017-08-26 08:53:53 -03:00
|
|
|
_rollFactor[CH_1+i] * roll_out +
|
|
|
|
_pitchFactor[CH_1+i] * pitch_out +
|
|
|
|
_collectiveFactor[CH_1+i] * collective_out;
|
|
|
|
}
|
2017-09-15 22:11:12 -03:00
|
|
|
|
|
|
|
// see if we need to scale down yaw_out
|
|
|
|
for (uint8_t i=0; i<AP_MOTORS_HELI_QUAD_NUM_MOTORS; i++) {
|
|
|
|
float y = _yawFactor[CH_1+i] * yaw_out;
|
2020-01-23 23:05:24 -04:00
|
|
|
if (_out[i] < 0.0f) {
|
2017-09-15 22:11:12 -03:00
|
|
|
// the slope of the yaw effect changes at zero collective
|
|
|
|
y = -y;
|
|
|
|
}
|
2020-01-23 23:05:24 -04:00
|
|
|
if (_out[i] * (_out[i] + y) < 0.0f) {
|
2017-09-15 22:11:12 -03:00
|
|
|
// applying this yaw demand would change the sign of the
|
|
|
|
// collective, which means the yaw would not be applied
|
|
|
|
// evenly. We scale down the overall yaw demand to prevent
|
|
|
|
// it crossing over zero
|
2018-12-28 02:32:05 -04:00
|
|
|
float s = -(_out[i] / y);
|
2017-09-15 22:11:12 -03:00
|
|
|
yaw_out *= s;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// now apply the yaw correction
|
|
|
|
for (uint8_t i=0; i<AP_MOTORS_HELI_QUAD_NUM_MOTORS; i++) {
|
|
|
|
float y = _yawFactor[CH_1+i] * yaw_out;
|
2020-01-23 23:05:24 -04:00
|
|
|
if (_out[i] < 0.0f) {
|
2017-09-15 22:11:12 -03:00
|
|
|
// the slope of the yaw effect changes at zero collective
|
|
|
|
y = -y;
|
|
|
|
}
|
2018-12-28 02:32:05 -04:00
|
|
|
_out[i] += y;
|
|
|
|
}
|
|
|
|
|
2020-01-23 23:05:24 -04:00
|
|
|
for (uint8_t i=0; i<AP_MOTORS_HELI_QUAD_NUM_MOTORS; i++) {
|
|
|
|
// scale output to 0 to 1
|
|
|
|
_out[i] += _collective_mid_pct;
|
|
|
|
// scale output to -1 to 1 for servo output
|
|
|
|
_out[i] = _out[i] * 2.0f - 1.0f;
|
|
|
|
}
|
2018-12-28 02:32:05 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void AP_MotorsHeli_Quad::output_to_motors()
|
|
|
|
{
|
2019-05-03 02:27:09 -03:00
|
|
|
if (!initialised_ok()) {
|
2018-12-28 02:32:05 -04:00
|
|
|
return;
|
2017-09-15 22:11:12 -03:00
|
|
|
}
|
2018-03-23 01:09:14 -03:00
|
|
|
|
2017-08-26 08:53:53 -03:00
|
|
|
// move the servos
|
|
|
|
for (uint8_t i=0; i<AP_MOTORS_HELI_QUAD_NUM_MOTORS; i++) {
|
2018-12-28 02:32:05 -04:00
|
|
|
rc_write_angle(AP_MOTORS_MOT_1+i, _out[i] * QUAD_SERVO_MAX_ANGLE);
|
2017-08-26 08:53:53 -03:00
|
|
|
}
|
|
|
|
|
2019-04-09 09:15:45 -03:00
|
|
|
switch (_spool_state) {
|
|
|
|
case SpoolState::SHUT_DOWN:
|
2018-12-28 02:32:05 -04:00
|
|
|
// sends minimum values out to the motors
|
|
|
|
update_motor_control(ROTOR_CONTROL_STOP);
|
|
|
|
break;
|
2019-04-09 09:15:45 -03:00
|
|
|
case SpoolState::GROUND_IDLE:
|
2018-12-28 02:32:05 -04:00
|
|
|
// sends idle output to motors when armed. rotor could be static or turning (autorotation)
|
|
|
|
update_motor_control(ROTOR_CONTROL_IDLE);
|
|
|
|
break;
|
2019-04-09 09:15:45 -03:00
|
|
|
case SpoolState::SPOOLING_UP:
|
|
|
|
case SpoolState::THROTTLE_UNLIMITED:
|
2018-12-28 02:32:05 -04:00
|
|
|
// set motor output based on thrust requests
|
|
|
|
update_motor_control(ROTOR_CONTROL_ACTIVE);
|
|
|
|
break;
|
2019-04-09 09:15:45 -03:00
|
|
|
case SpoolState::SPOOLING_DOWN:
|
2018-12-28 02:32:05 -04:00
|
|
|
// sends idle output to motors and wait for rotor to stop
|
|
|
|
update_motor_control(ROTOR_CONTROL_IDLE);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2017-08-26 08:53:53 -03:00
|
|
|
|
|
|
|
// servo_test - move servos through full range of movement
|
|
|
|
void AP_MotorsHeli_Quad::servo_test()
|
|
|
|
{
|
|
|
|
// not implemented
|
|
|
|
}
|