/*
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 .
*/
/*
* AP_Motors6DOF.cpp - ArduSub motors library
*/
#include
#include
#include "AP_Motors6DOF.h"
#include
#include
#include "../../ArduSub/Sub.h"
extern const AP_HAL::HAL& hal;
// parameters for the motor class
const AP_Param::GroupInfo AP_Motors6DOF::var_info[] = {
AP_NESTEDGROUPINFO(AP_MotorsMulticopter, 0),
// @Param: 1_DIRECTION
// @DisplayName: Motor normal or reverse
// @Description: Used to change motor rotation directions without changing wires
// @Values: 1:normal,-1:reverse
// @User: Standard
AP_GROUPINFO("1_DIRECTION", 1, AP_Motors6DOF, _motor_reverse[0], 1),
// @Param: 2_DIRECTION
// @DisplayName: Motor normal or reverse
// @Description: Used to change motor rotation directions without changing wires
// @Values: 1:normal,-1:reverse
// @User: Standard
AP_GROUPINFO("2_DIRECTION", 2, AP_Motors6DOF, _motor_reverse[1], 1),
// @Param: 3_DIRECTION
// @DisplayName: Motor normal or reverse
// @Description: Used to change motor rotation directions without changing wires
// @Values: 1:normal,-1:reverse
// @User: Standard
AP_GROUPINFO("3_DIRECTION", 3, AP_Motors6DOF, _motor_reverse[2], 1),
// @Param: 4_DIRECTION
// @DisplayName: Motor normal or reverse
// @Description: Used to change motor rotation directions without changing wires
// @Values: 1:normal,-1:reverse
// @User: Standard
AP_GROUPINFO("4_DIRECTION", 4, AP_Motors6DOF, _motor_reverse[3], 1),
// @Param: 5_DIRECTION
// @DisplayName: Motor normal or reverse
// @Description: Used to change motor rotation directions without changing wires
// @Values: 1:normal,-1:reverse
// @User: Standard
AP_GROUPINFO("5_DIRECTION", 5, AP_Motors6DOF, _motor_reverse[4], 1),
// @Param: 6_DIRECTION
// @DisplayName: Motor normal or reverse
// @Description: Used to change motor rotation directions without changing wires
// @Values: 1:normal,-1:reverse
// @User: Standard
AP_GROUPINFO("6_DIRECTION", 6, AP_Motors6DOF, _motor_reverse[5], 1),
// @Param: 7_DIRECTION
// @DisplayName: Motor normal or reverse
// @Description: Used to change motor rotation directions without changing wires
// @Values: 1:normal,-1:reverse
// @User: Standard
AP_GROUPINFO("7_DIRECTION", 7, AP_Motors6DOF, _motor_reverse[6], 1),
// @Param: 8_DIRECTION
// @DisplayName: Motor normal or reverse
// @Description: Used to change motor rotation directions without changing wires
// @Values: 1:normal,-1:reverse
// @User: Standard
AP_GROUPINFO("8_DIRECTION", 8, AP_Motors6DOF, _motor_reverse[7], 1),
// @Param: FV_CPLNG_K
// @DisplayName: Forward/vertical to pitch decoupling factor
// @Description: Used to decouple pitch from forward/vertical motion. 0 to disable, 1.2 normal
// @Range: 0.0 1.5
// @Increment: 0.1
// @User: Standard
AP_GROUPINFO("FV_CPLNG_K", 9, AP_Motors6DOF, _forwardVerticalCouplingFactor, 1.0),
// @Param: 9_DIRECTION
// @DisplayName: Motor normal or reverse
// @Description: Used to change motor rotation directions without changing wires
// @Values: 1:normal,-1:reverse
// @User: Standard
AP_GROUPINFO("9_DIRECTION", 10, AP_Motors6DOF, _motor_reverse[8], 1),
// @Param: 10_DIRECTION
// @DisplayName: Motor normal or reverse
// @Description: Used to change motor rotation directions without changing wires
// @Values: 1:normal,-1:reverse
// @User: Standard
AP_GROUPINFO("10_DIRECTION", 11, AP_Motors6DOF, _motor_reverse[9], 1),
// @Param: 11_DIRECTION
// @DisplayName: Motor normal or reverse
// @Description: Used to change motor rotation directions without changing wires
// @Values: 1:normal,-1:reverse
// @User: Standard
AP_GROUPINFO("11_DIRECTION", 12, AP_Motors6DOF, _motor_reverse[10], 1),
// @Param: 12_DIRECTION
// @DisplayName: Motor normal or reverse
// @Description: Used to change motor rotation directions without changing wires
// @Values: 1:normal,-1:reverse
// @User: Standard
AP_GROUPINFO("12_DIRECTION", 13, AP_Motors6DOF, _motor_reverse[11], 1),
AP_GROUPEND
};
void AP_Motors6DOF::setup_motors(motor_frame_class frame_class, motor_frame_type frame_type)
{
// remove existing motors
for (int8_t i=0; iration
/* if (fabsf(thrust_in - last_thrust_in[i]) > ratio)
{//缓加缓减
if (thrust >= last_thrust_Dhot[i] + step)
{//新的Dshot >当前的Dshot + 步长
last_thrust_Dhot[i] += step;
}
else if(thrust <= last_thrust_Dhot[i] -step)
{//新的Dshot < 当前的Dshot -步长
last_thrust_Dhot[i] -= step;
}
if (thrust>=last_thrust_Dhot[i] - step_thrust && thrust<=last_thrust_Dhot[i] + step_thrust){
last_thrust_in[i] = thrust_in;
last_thrust_Dhot[i] = thrust;
}
if ((last_thrust_Dhot[i]< step && last_thrust_Dhot[i]> -step) && thrust == 0)
{
last_thrust_Dhot[i] =0;
}
}
else{
//比例之差=last_thrust_Dhot[i] - stephv && thrust<=last_thrust_Dhot[i] + stephv){
last_thrust_Dhot[i] = thrust;
}else if (thrust >= last_thrust_Dhot[i] + stephv)
{//新的Dshot >当前的Dshot + 步长
last_thrust_Dhot[i] += stephv;
}
else if(thrust <= last_thrust_Dhot[i] -stephv)
{//新的Dshot < 当前的Dshot -步长
last_thrust_Dhot[i] -= stephv;
}
if (last_thrust_Dhot[i]< stephv && last_thrust_Dhot[i]> -stephv)
{
last_thrust_Dhot[i] =0;
}
static int k = 0;
k++;
if(k>400)
{
//gcs().send_text(MAV_SEVERITY_INFO, "_thrust_rpyt_out %d %d ,%d \n",(int)last_thrust_Dhot[2],(int)last_thrust_Dhot[3],(int)last_thrust_Dhot[4]);
}
int16_t speedref = (int16_t)((int32_t)last_thrust_Dhot[i]*hv_max/1000);
return constrain_int16(speedref,-hv_max, hv_max);
}
}
int16_t AP_Motors6DOF::calc_thrust_to_Dshot(float thrust_in,int8_t i)
{
int16_t thrust = 0;
float ratio= 0.02;//阶跃百分比
int16_t step = 8;//4;//步长 0加到1024需要320毫秒
int16_t step_thrust = 8;//4;//退出缓启动的条件
thrust = (int16_t)(thrust_in* ThrustScale);
//输入比例与上次比例差 >ration
if (fabsf(thrust_in - last_thrust_in[i]) > ratio)
{//缓加缓减
if (thrust >= last_thrust_Dhot[i] + step)
{//新的Dshot >当前的Dshot + 步长
last_thrust_Dhot[i] += step;
}
else if(thrust <= last_thrust_Dhot[i] -step)
{//新的Dshot < 当前的Dshot -步长
last_thrust_Dhot[i] -= step;
}
if (thrust>=last_thrust_Dhot[i] - step_thrust && thrust<=last_thrust_Dhot[i] + step_thrust){
last_thrust_in[i] = thrust_in;
last_thrust_Dhot[i] = thrust;
}
if ((last_thrust_Dhot[i]< step && last_thrust_Dhot[i]> -step) && thrust == 0)
{
last_thrust_Dhot[i] =0;
}
}
else{
//比例之差= _throttle_thrust_max) {
throttle_thrust = _throttle_thrust_max;
limit.throttle_upper = true;
}
// calculate roll, pitch and yaw for each motor
for (i=0; i= _throttle_thrust_max) {
throttle_thrust = _throttle_thrust_max;
limit.throttle_upper = true;
}
// calculate roll, pitch and yaw for each motor
for (i=0; i _batt_current_max * 1.5f) {
batt_current_ratio = 2.5f;
} else if (_batt_current < _batt_current_max && predicted_current > _batt_current_max) {
batt_current_ratio = predicted_current_ratio;
}
_output_limited += (loop_interval/(loop_interval+_batt_current_time_constant)) * (1 - batt_current_ratio);
_output_limited = constrain_float(_output_limited, 0.0f, 1.0f);
for (uint8_t i = 0; i < AP_MOTORS_MAX_NUM_MOTORS; i++) {
if (motor_enabled[i]) {
_thrust_rpyt_out[i] *= _output_limited;
}
}
}
// output_armed - sends commands to the motors
// includes new scaling stability patch
// TODO pull code that is common to output_armed_not_stabilizing into helper functions
// ToDo calculate headroom for rpy to be added for stabilization during full throttle/forward/lateral commands
void AP_Motors6DOF::output_armed_stabilizing_vectored()
{
uint8_t i; // general purpose counter
float roll_thrust; // roll thrust input value, +/- 1.0
float pitch_thrust; // pitch thrust input value, +/- 1.0
float yaw_thrust; // yaw thrust input value, +/- 1.0
float throttle_thrust; // throttle thrust input value, +/- 1.0
float forward_thrust; // forward thrust input value, +/- 1.0
float lateral_thrust; // lateral thrust input value, +/- 1.0
roll_thrust = (_roll_in + _roll_in_ff);
pitch_thrust = (_pitch_in + _pitch_in_ff);
yaw_thrust = (_yaw_in + _yaw_in_ff);
throttle_thrust = get_throttle_bidirectional();
forward_thrust = _forward_in;
lateral_thrust = _lateral_in;
float rpy_out[AP_MOTORS_MAX_NUM_MOTORS]; // buffer so we don't have to multiply coefficients multiple times.
float linear_out[AP_MOTORS_MAX_NUM_MOTORS]; // 3 linear DOF mix for each motor
// initialize limits flags
limit.roll= false;
limit.pitch = false;
limit.yaw = false;
limit.throttle_lower = false;
limit.throttle_upper = false;
// sanity check throttle is above zero and below current limited throttle
if (throttle_thrust <= -_throttle_thrust_max) {
throttle_thrust = -_throttle_thrust_max;
limit.throttle_lower = true;
}
if (throttle_thrust >= _throttle_thrust_max) {
throttle_thrust = _throttle_thrust_max;
limit.throttle_upper = true;
}
// calculate roll, pitch and yaw for each motor
for (i=0; i= _throttle_thrust_max) {
throttle_thrust = _throttle_thrust_max;
limit.throttle_upper = true;
}
// calculate roll, pitch and Throttle for each motor (only used by vertical thrusters)
rpt_max = 1; //Initialized to 1 so that normalization will only occur if value is saturated
for (i=0; i rpt_max) {
rpt_max = fabsf(rpt_out[i]);
}
}
}
// calculate linear/yaw command for each motor (only used for translational thrusters)
// linear factors should be 0.0 or 1.0 for now
yfl_max = 1; //Initialized to 1 so that normalization will only occur if value is saturated
for (i=0; i yfl_max) {
yfl_max = fabsf(yfl_out[i]);
}
}
}
// Calculate final output for each motor and normalize if necessary
for (i=0; i= AP_MOTORS_MAX_NUM_MOTORS) {
return Vector3f(0,0,0);
}
return Vector3f(_roll_factor[motor_number], _pitch_factor[motor_number], _yaw_factor[motor_number]);
}
bool AP_Motors6DOF::motor_is_enabled(int motor_number) {
if (motor_number < 0 || motor_number >= AP_MOTORS_MAX_NUM_MOTORS) {
return false;
}
return motor_enabled[motor_number];
}
bool AP_Motors6DOF::set_reversed(int motor_number, bool reversed) {
if (motor_number < 0 || motor_number >= AP_MOTORS_MAX_NUM_MOTORS) {
return false;
}
if (reversed) {
_motor_reverse[motor_number].set_and_save(-1);
} else {
_motor_reverse[motor_number].set_and_save(1);
}
return true;
}
//--------------wangdan--------
AP_Motors6DOF *AP_Motors6DOF::_singleton;
namespace AP {
AP_Motors6DOF &motors6dof()
{
return *AP_Motors6DOF::get_singleton();
}
};