2013-08-29 02:34:34 -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/>.
|
|
|
|
*/
|
|
|
|
|
2012-10-26 20:59:07 -03:00
|
|
|
#include "AP_Motors_Class.h"
|
2015-08-11 03:28:44 -03:00
|
|
|
#include <AP_HAL/AP_HAL.h>
|
2017-01-03 05:56:57 -04:00
|
|
|
#include <SRV_Channel/SRV_Channel.h>
|
2017-04-17 05:15:55 -03:00
|
|
|
#include <GCS_MAVLink/GCS.h>
|
2022-02-28 21:19:11 -04:00
|
|
|
#include <AP_Notify/AP_Notify.h>
|
2017-01-03 05:56:57 -04:00
|
|
|
|
2022-05-11 06:14:51 -03:00
|
|
|
#define AP_MOTORS_SLEW_FILTER_CUTOFF 50.0f
|
|
|
|
|
2012-10-26 20:59:07 -03:00
|
|
|
extern const AP_HAL::HAL& hal;
|
2012-04-02 05:26:37 -03:00
|
|
|
|
2018-03-21 21:45:52 -03:00
|
|
|
// singleton instance
|
2019-02-10 14:04:50 -04:00
|
|
|
AP_Motors *AP_Motors::_singleton;
|
2018-03-21 21:45:52 -03:00
|
|
|
|
2012-08-17 03:20:26 -03:00
|
|
|
// Constructor
|
2022-12-06 21:03:36 -04:00
|
|
|
AP_Motors::AP_Motors(uint16_t speed_hz) :
|
2012-08-17 03:20:26 -03:00
|
|
|
_speed_hz(speed_hz),
|
2015-07-13 01:39:57 -03:00
|
|
|
_throttle_filter(),
|
2022-05-11 06:14:51 -03:00
|
|
|
_throttle_slew(),
|
|
|
|
_throttle_slew_filter(),
|
2019-04-09 09:15:45 -03:00
|
|
|
_spool_desired(DesiredSpoolState::SHUT_DOWN),
|
2023-01-30 21:54:00 -04:00
|
|
|
_spool_state(SpoolState::SHUT_DOWN)
|
2012-04-02 05:26:37 -03:00
|
|
|
{
|
2019-02-10 14:04:50 -04:00
|
|
|
_singleton = this;
|
2015-06-08 00:18:40 -03:00
|
|
|
|
2015-04-16 01:55:12 -03:00
|
|
|
// setup throttle filtering
|
2015-04-16 16:36:02 -03:00
|
|
|
_throttle_filter.set_cutoff_frequency(0.0f);
|
2015-04-01 00:16:43 -03:00
|
|
|
_throttle_filter.reset(0.0f);
|
2015-06-08 00:18:40 -03:00
|
|
|
|
2022-05-11 06:14:51 -03:00
|
|
|
_throttle_slew_filter.set_cutoff_frequency(AP_MOTORS_SLEW_FILTER_CUTOFF);
|
|
|
|
_throttle_slew_filter.reset(0.0f);
|
|
|
|
|
|
|
|
// setup throttle slew detector
|
|
|
|
_throttle_slew.reset();
|
|
|
|
|
2015-06-08 00:18:40 -03:00
|
|
|
// init limit flags
|
2019-07-27 02:37:31 -03:00
|
|
|
limit.roll = true;
|
|
|
|
limit.pitch = true;
|
2015-06-08 00:18:40 -03:00
|
|
|
limit.yaw = true;
|
|
|
|
limit.throttle_lower = true;
|
|
|
|
limit.throttle_upper = true;
|
2018-08-12 11:19:20 -03:00
|
|
|
_thrust_boost = false;
|
|
|
|
_thrust_balanced = true;
|
2012-04-02 05:26:37 -03:00
|
|
|
};
|
|
|
|
|
2021-06-19 20:54:22 -03:00
|
|
|
void AP_Motors::get_frame_and_type_string(char *buffer, uint8_t buflen) const
|
|
|
|
{
|
|
|
|
const char *frame_str = get_frame_string();
|
|
|
|
const char *type_str = get_type_string();
|
2022-01-01 11:55:34 -04:00
|
|
|
if (type_str != nullptr && strlen(type_str)
|
|
|
|
#if AP_SCRIPTING_ENABLED
|
|
|
|
&& custom_frame_string == nullptr
|
|
|
|
#endif
|
|
|
|
) {
|
2021-06-19 20:54:22 -03:00
|
|
|
hal.util->snprintf(buffer, buflen, "Frame: %s/%s", frame_str, type_str);
|
|
|
|
} else {
|
|
|
|
hal.util->snprintf(buffer, buflen, "Frame: %s", frame_str);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-08 10:15:04 -03:00
|
|
|
void AP_Motors::armed(bool arm)
|
|
|
|
{
|
2019-05-03 02:27:09 -03:00
|
|
|
if (_armed != arm) {
|
|
|
|
_armed = arm;
|
2016-06-09 02:21:04 -03:00
|
|
|
AP_Notify::flags.armed = arm;
|
2016-06-16 00:07:37 -03:00
|
|
|
if (!arm) {
|
|
|
|
save_params_on_disarm();
|
|
|
|
}
|
2016-06-09 02:21:04 -03:00
|
|
|
}
|
2015-07-13 01:39:57 -03:00
|
|
|
};
|
2016-01-04 01:56:54 -04:00
|
|
|
|
2019-04-09 09:15:45 -03:00
|
|
|
void AP_Motors::set_desired_spool_state(DesiredSpoolState spool)
|
2018-12-28 02:31:38 -04:00
|
|
|
{
|
2019-05-03 02:27:09 -03:00
|
|
|
if (_armed || (spool == DesiredSpoolState::SHUT_DOWN)) {
|
2018-12-28 02:31:38 -04:00
|
|
|
_spool_desired = spool;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-02-03 07:55:55 -04:00
|
|
|
// pilot input in the -1 ~ +1 range for roll, pitch and yaw. 0~1 range for throttle
|
|
|
|
void AP_Motors::set_radio_passthrough(float roll_input, float pitch_input, float throttle_input, float yaw_input)
|
|
|
|
{
|
|
|
|
_roll_radio_passthrough = roll_input;
|
|
|
|
_pitch_radio_passthrough = pitch_input;
|
|
|
|
_throttle_radio_passthrough = throttle_input;
|
|
|
|
_yaw_radio_passthrough = yaw_input;
|
|
|
|
}
|
|
|
|
|
2016-01-04 01:56:54 -04:00
|
|
|
/*
|
|
|
|
write to an output channel
|
|
|
|
*/
|
|
|
|
void AP_Motors::rc_write(uint8_t chan, uint16_t pwm)
|
|
|
|
{
|
2017-10-23 00:16:13 -03:00
|
|
|
SRV_Channel::Aux_servo_function_t function = SRV_Channels::get_motor_function(chan);
|
2024-03-05 21:03:28 -04:00
|
|
|
if ((1U<<chan) & _motor_pwm_scaled.mask) {
|
2019-10-12 06:28:24 -03:00
|
|
|
// note that PWM_MIN/MAX has been forced to 1000/2000
|
2024-03-05 21:03:28 -04:00
|
|
|
SRV_Channels::set_output_scaled(function, float(pwm) - _motor_pwm_scaled.offset);
|
2019-10-12 06:28:24 -03:00
|
|
|
} else {
|
|
|
|
SRV_Channels::set_output_pwm(function, pwm);
|
|
|
|
}
|
2016-01-04 01:56:54 -04:00
|
|
|
}
|
2016-01-04 06:24:06 -04:00
|
|
|
|
2018-05-20 22:37:38 -03:00
|
|
|
/*
|
|
|
|
write to an output channel for an angle actuator
|
|
|
|
*/
|
|
|
|
void AP_Motors::rc_write_angle(uint8_t chan, int16_t angle_cd)
|
|
|
|
{
|
|
|
|
SRV_Channel::Aux_servo_function_t function = SRV_Channels::get_motor_function(chan);
|
|
|
|
SRV_Channels::set_output_scaled(function, angle_cd);
|
|
|
|
}
|
|
|
|
|
2016-01-04 06:24:06 -04:00
|
|
|
/*
|
|
|
|
set frequency of a set of channels
|
|
|
|
*/
|
2021-12-28 23:40:20 -04:00
|
|
|
void AP_Motors::rc_set_freq(uint32_t motor_mask, uint16_t freq_hz)
|
2016-01-04 16:35:24 -04:00
|
|
|
{
|
2016-04-22 00:24:04 -03:00
|
|
|
if (freq_hz > 50) {
|
2021-12-28 23:40:20 -04:00
|
|
|
_motor_fast_mask |= motor_mask;
|
2016-04-22 00:24:04 -03:00
|
|
|
}
|
2018-03-14 03:04:20 -03:00
|
|
|
|
2021-12-28 23:40:20 -04:00
|
|
|
const uint32_t mask = motor_mask_to_srv_channel_mask(motor_mask);
|
2016-04-22 00:24:04 -03:00
|
|
|
hal.rcout->set_freq(mask, freq_hz);
|
2022-05-23 09:59:22 -03:00
|
|
|
hal.rcout->set_dshot_esc_type(SRV_Channels::get_dshot_esc_type());
|
2018-03-14 03:04:20 -03:00
|
|
|
|
2024-07-25 22:06:14 -03:00
|
|
|
const PWMType type = _pwm_type;
|
2024-03-05 21:03:28 -04:00
|
|
|
switch (type) {
|
2024-07-25 22:06:14 -03:00
|
|
|
case PWMType::ONESHOT:
|
2018-03-14 03:04:20 -03:00
|
|
|
if (freq_hz > 50 && mask != 0) {
|
|
|
|
hal.rcout->set_output_mode(mask, AP_HAL::RCOutput::MODE_PWM_ONESHOT);
|
|
|
|
}
|
|
|
|
break;
|
2024-07-25 22:06:14 -03:00
|
|
|
case PWMType::ONESHOT125:
|
2018-04-03 05:11:31 -03:00
|
|
|
if (freq_hz > 50 && mask != 0) {
|
|
|
|
hal.rcout->set_output_mode(mask, AP_HAL::RCOutput::MODE_PWM_ONESHOT125);
|
|
|
|
}
|
|
|
|
break;
|
2024-07-25 22:06:14 -03:00
|
|
|
case PWMType::BRUSHED:
|
2018-03-14 03:04:20 -03:00
|
|
|
hal.rcout->set_output_mode(mask, AP_HAL::RCOutput::MODE_PWM_BRUSHED);
|
|
|
|
break;
|
2024-07-25 22:06:14 -03:00
|
|
|
case PWMType::DSHOT150:
|
2018-03-14 03:04:20 -03:00
|
|
|
hal.rcout->set_output_mode(mask, AP_HAL::RCOutput::MODE_PWM_DSHOT150);
|
|
|
|
break;
|
2024-07-25 22:06:14 -03:00
|
|
|
case PWMType::DSHOT300:
|
2018-03-14 03:04:20 -03:00
|
|
|
hal.rcout->set_output_mode(mask, AP_HAL::RCOutput::MODE_PWM_DSHOT300);
|
|
|
|
break;
|
2024-07-25 22:06:14 -03:00
|
|
|
case PWMType::DSHOT600:
|
2018-03-14 03:04:20 -03:00
|
|
|
hal.rcout->set_output_mode(mask, AP_HAL::RCOutput::MODE_PWM_DSHOT600);
|
|
|
|
break;
|
2024-07-25 22:06:14 -03:00
|
|
|
case PWMType::DSHOT1200:
|
2018-03-14 03:04:20 -03:00
|
|
|
hal.rcout->set_output_mode(mask, AP_HAL::RCOutput::MODE_PWM_DSHOT1200);
|
|
|
|
break;
|
2024-07-25 22:06:14 -03:00
|
|
|
case PWMType::PWM_RANGE:
|
|
|
|
case PWMType::PWM_ANGLE:
|
2019-10-12 06:28:24 -03:00
|
|
|
/*
|
|
|
|
this is a motor output type for multirotors which honours
|
2024-03-05 21:03:28 -04:00
|
|
|
the SERVOn_MIN/MAX (and TRIM for angle) values per channel
|
|
|
|
|
|
|
|
Motor PWM min and max are hard coded to 1000 to 2000.
|
|
|
|
Range type offsets by 1000 to get 0 to 1000.
|
|
|
|
Angle type offsets by 1500 to get -500 to 500.
|
2019-10-12 06:28:24 -03:00
|
|
|
*/
|
2024-03-05 21:03:28 -04:00
|
|
|
|
2024-07-25 22:06:14 -03:00
|
|
|
if (type == PWMType::PWM_RANGE) {
|
2024-03-05 21:03:28 -04:00
|
|
|
_motor_pwm_scaled.offset = 1000.0;
|
|
|
|
} else {
|
2024-07-25 22:06:14 -03:00
|
|
|
// PWMType::PWM_ANGLE
|
2024-03-05 21:03:28 -04:00
|
|
|
_motor_pwm_scaled.offset = 1500.0;
|
|
|
|
}
|
|
|
|
|
|
|
|
_motor_pwm_scaled.mask |= motor_mask;
|
2019-10-12 06:28:24 -03:00
|
|
|
for (uint8_t i=0; i<16; i++) {
|
2021-12-28 23:40:20 -04:00
|
|
|
if ((1U<<i) & motor_mask) {
|
2024-07-25 22:06:14 -03:00
|
|
|
if (type == PWMType::PWM_RANGE) {
|
2024-03-05 21:03:28 -04:00
|
|
|
SRV_Channels::set_range(SRV_Channels::get_motor_function(i), 1000);
|
|
|
|
} else {
|
2024-07-25 22:06:14 -03:00
|
|
|
// PWMType::PWM_ANGLE
|
2024-03-05 21:03:28 -04:00
|
|
|
SRV_Channels::set_angle(SRV_Channels::get_motor_function(i), 500);
|
|
|
|
}
|
2019-10-12 06:28:24 -03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
hal.rcout->set_output_mode(mask, AP_HAL::RCOutput::MODE_PWM_NORMAL);
|
|
|
|
break;
|
2018-03-14 03:04:20 -03:00
|
|
|
default:
|
|
|
|
hal.rcout->set_output_mode(mask, AP_HAL::RCOutput::MODE_PWM_NORMAL);
|
|
|
|
break;
|
2016-04-13 03:33:20 -03:00
|
|
|
}
|
2016-01-04 16:35:24 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2017-04-17 05:15:55 -03:00
|
|
|
map an internal motor mask to real motor mask, accounting for
|
|
|
|
SERVOn_FUNCTION mappings, and allowing for multiple outputs per
|
|
|
|
motor number
|
2016-01-04 16:35:24 -04:00
|
|
|
*/
|
2021-01-02 08:09:56 -04:00
|
|
|
uint32_t AP_Motors::motor_mask_to_srv_channel_mask(uint32_t mask) const
|
2016-01-04 06:24:06 -04:00
|
|
|
{
|
|
|
|
uint32_t mask2 = 0;
|
2019-04-19 21:59:40 -03:00
|
|
|
for (uint8_t i = 0; i < 32; i++) {
|
|
|
|
uint32_t bit = 1UL << i;
|
2016-01-04 06:24:06 -04:00
|
|
|
if (mask & bit) {
|
2017-10-23 00:16:13 -03:00
|
|
|
SRV_Channel::Aux_servo_function_t function = SRV_Channels::get_motor_function(i);
|
2017-04-17 05:15:55 -03:00
|
|
|
mask2 |= SRV_Channels::get_output_channel_mask(function);
|
2016-01-04 06:24:06 -04:00
|
|
|
}
|
|
|
|
}
|
2016-01-04 16:35:24 -04:00
|
|
|
return mask2;
|
2016-01-04 06:24:06 -04:00
|
|
|
}
|
2016-02-02 08:22:53 -04:00
|
|
|
|
2016-04-21 04:32:25 -03:00
|
|
|
/*
|
2017-04-17 05:15:55 -03:00
|
|
|
add a motor, setting up default output function as needed
|
2016-04-21 04:32:25 -03:00
|
|
|
*/
|
|
|
|
void AP_Motors::add_motor_num(int8_t motor_num)
|
|
|
|
{
|
|
|
|
// ensure valid motor number is provided
|
2019-04-19 21:59:40 -03:00
|
|
|
if (motor_num >= 0 && motor_num < AP_MOTORS_MAX_NUM_MOTORS) {
|
2017-10-23 00:16:13 -03:00
|
|
|
SRV_Channel::Aux_servo_function_t function = SRV_Channels::get_motor_function(motor_num);
|
2017-01-03 05:56:57 -04:00
|
|
|
SRV_Channels::set_aux_channel_default(function, motor_num);
|
2016-04-21 04:32:25 -03:00
|
|
|
}
|
|
|
|
}
|
2019-11-28 16:23:47 -04:00
|
|
|
|
2020-12-01 17:35:45 -04:00
|
|
|
// set limit flag for pitch, roll and yaw
|
|
|
|
void AP_Motors::set_limit_flag_pitch_roll_yaw(bool flag)
|
|
|
|
{
|
|
|
|
limit.roll = flag;
|
|
|
|
limit.pitch = flag;
|
|
|
|
limit.yaw = flag;
|
|
|
|
}
|
|
|
|
|
2022-10-21 20:45:43 -03:00
|
|
|
#if AP_SCRIPTING_ENABLED
|
|
|
|
void AP_Motors::set_external_limits(bool roll, bool pitch, bool yaw, bool throttle_lower, bool throttle_upper)
|
|
|
|
{
|
|
|
|
external_limits.roll = roll;
|
|
|
|
external_limits.pitch = pitch;
|
|
|
|
external_limits.yaw = yaw;
|
|
|
|
external_limits.throttle_lower = throttle_lower;
|
|
|
|
external_limits.throttle_upper = throttle_upper;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2021-09-22 12:18:05 -03:00
|
|
|
// returns true if the configured PWM type is digital and should have fixed endpoints
|
|
|
|
bool AP_Motors::is_digital_pwm_type() const
|
|
|
|
{
|
2024-07-25 22:06:14 -03:00
|
|
|
switch ((PWMType)_pwm_type) {
|
|
|
|
case PWMType::DSHOT150:
|
|
|
|
case PWMType::DSHOT300:
|
|
|
|
case PWMType::DSHOT600:
|
|
|
|
case PWMType::DSHOT1200:
|
|
|
|
return true;
|
|
|
|
case PWMType::NORMAL:
|
|
|
|
case PWMType::ONESHOT:
|
|
|
|
case PWMType::ONESHOT125:
|
|
|
|
case PWMType::BRUSHED:
|
|
|
|
case PWMType::PWM_RANGE:
|
|
|
|
case PWMType::PWM_ANGLE:
|
|
|
|
break;
|
2021-09-22 12:18:05 -03:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-01-01 11:55:34 -04:00
|
|
|
// return string corresponding to frame_class
|
|
|
|
const char* AP_Motors::get_frame_string() const
|
|
|
|
{
|
|
|
|
#if AP_SCRIPTING_ENABLED
|
|
|
|
if (custom_frame_string != nullptr) {
|
|
|
|
return custom_frame_string;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
return _get_frame_string();
|
|
|
|
}
|
|
|
|
|
|
|
|
#if AP_SCRIPTING_ENABLED
|
|
|
|
// set custom frame string
|
|
|
|
void AP_Motors::set_frame_string(const char * str) {
|
|
|
|
if (custom_frame_string != nullptr) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const size_t len = strlen(str)+1;
|
2024-05-26 22:24:13 -03:00
|
|
|
custom_frame_string = NEW_NOTHROW char[len];
|
2022-01-01 11:55:34 -04:00
|
|
|
if (custom_frame_string != nullptr) {
|
|
|
|
strncpy(custom_frame_string, str, len);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2022-01-27 11:37:06 -04:00
|
|
|
// output_test_seq - spin a motor at the pwm value specified
|
|
|
|
// 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
|
|
|
|
void AP_Motors::output_test_seq(uint8_t motor_seq, int16_t pwm)
|
|
|
|
{
|
|
|
|
if (armed() && _interlock) {
|
|
|
|
_output_test_seq(motor_seq, pwm);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-03 22:58:37 -03:00
|
|
|
bool AP_Motors::arming_checks(size_t buflen, char *buffer) const
|
|
|
|
{
|
|
|
|
if (!initialised_ok()) {
|
|
|
|
hal.util->snprintf(buffer, buflen, "Check frame class and type");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2023-06-18 15:56:32 -03:00
|
|
|
bool AP_Motors::motor_test_checks(size_t buflen, char *buffer) const
|
|
|
|
{
|
|
|
|
// Must pass base class arming checks (the function above)
|
|
|
|
// Do not run frame specific arming checks as motor test is less strict
|
|
|
|
// For example not all the outputs have to be assigned
|
|
|
|
return AP_Motors::arming_checks(buflen, buffer);
|
|
|
|
}
|
|
|
|
|
2019-11-28 16:23:47 -04:00
|
|
|
namespace AP {
|
|
|
|
AP_Motors *motors()
|
|
|
|
{
|
|
|
|
return AP_Motors::get_singleton();
|
|
|
|
}
|
2020-12-01 17:35:45 -04:00
|
|
|
}
|