2015-08-07 00:37:06 -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 "AP_RPM.h"
|
2022-07-15 08:53:41 -03:00
|
|
|
|
|
|
|
#if AP_RPM_ENABLED
|
|
|
|
|
2023-08-18 01:58:40 -03:00
|
|
|
#include "RPM_Backend.h"
|
2017-04-02 00:57:59 -03:00
|
|
|
#include "RPM_Pin.h"
|
2015-11-22 23:28:17 -04:00
|
|
|
#include "RPM_SITL.h"
|
2019-11-11 00:37:44 -04:00
|
|
|
#include "RPM_EFI.h"
|
2022-08-29 06:34:58 -03:00
|
|
|
#include "RPM_Generator.h"
|
2020-04-06 09:54:00 -03:00
|
|
|
#include "RPM_HarmonicNotch.h"
|
2021-06-03 04:27:54 -03:00
|
|
|
#include "RPM_ESC_Telem.h"
|
2024-03-13 21:58:46 -03:00
|
|
|
#include "RPM_DroneCAN.h"
|
2015-08-07 00:37:06 -03:00
|
|
|
|
2022-01-05 07:23:44 -04:00
|
|
|
#include <AP_Logger/AP_Logger.h>
|
|
|
|
|
2015-08-07 00:37:06 -03:00
|
|
|
extern const AP_HAL::HAL& hal;
|
|
|
|
|
|
|
|
// table of user settable parameters
|
2015-10-25 14:03:46 -03:00
|
|
|
const AP_Param::GroupInfo AP_RPM::var_info[] = {
|
2021-08-09 08:21:52 -03:00
|
|
|
// 0-13 used by old param indexes before being moved to AP_RPM_Params
|
|
|
|
|
|
|
|
// @Group: 1_
|
|
|
|
// @Path: AP_RPM_Params.cpp
|
|
|
|
AP_SUBGROUPINFO(_params[0], "1_", 14, AP_RPM, AP_RPM_Params),
|
2021-06-03 04:27:54 -03:00
|
|
|
|
2015-08-07 00:37:06 -03:00
|
|
|
#if RPM_MAX_INSTANCES > 1
|
2021-08-09 08:21:52 -03:00
|
|
|
// @Group: 2_
|
|
|
|
// @Path: AP_RPM_Params.cpp
|
|
|
|
AP_SUBGROUPINFO(_params[1], "2_", 15, AP_RPM, AP_RPM_Params),
|
2021-02-14 17:51:28 -04:00
|
|
|
#endif
|
|
|
|
|
2023-08-14 23:19:22 -03:00
|
|
|
#if RPM_MAX_INSTANCES > 2
|
|
|
|
// @Group: 3_
|
|
|
|
// @Path: AP_RPM_Params.cpp
|
|
|
|
AP_SUBGROUPINFO(_params[2], "3_", 16, AP_RPM, AP_RPM_Params),
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if RPM_MAX_INSTANCES > 3
|
|
|
|
// @Group: 4_
|
|
|
|
// @Path: AP_RPM_Params.cpp
|
|
|
|
AP_SUBGROUPINFO(_params[3], "4_", 17, AP_RPM, AP_RPM_Params),
|
|
|
|
#endif
|
|
|
|
|
2015-08-07 00:37:06 -03:00
|
|
|
AP_GROUPEND
|
|
|
|
};
|
|
|
|
|
2018-07-23 01:06:55 -03:00
|
|
|
AP_RPM::AP_RPM(void)
|
2015-08-07 00:37:06 -03:00
|
|
|
{
|
|
|
|
AP_Param::setup_object_defaults(this, var_info);
|
2019-02-25 12:48:19 -04:00
|
|
|
|
|
|
|
if (_singleton != nullptr) {
|
|
|
|
AP_HAL::panic("AP_RPM must be singleton");
|
|
|
|
}
|
|
|
|
_singleton = this;
|
2015-08-07 00:37:06 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2021-06-03 04:27:54 -03:00
|
|
|
initialise the AP_RPM class.
|
2015-08-07 00:37:06 -03:00
|
|
|
*/
|
|
|
|
void AP_RPM::init(void)
|
|
|
|
{
|
|
|
|
if (num_instances != 0) {
|
|
|
|
// init called a 2nd time?
|
|
|
|
return;
|
|
|
|
}
|
2021-08-09 08:21:52 -03:00
|
|
|
|
|
|
|
convert_params();
|
|
|
|
|
2015-08-07 00:37:06 -03:00
|
|
|
for (uint8_t i=0; i<RPM_MAX_INSTANCES; i++) {
|
2021-08-09 08:21:52 -03:00
|
|
|
switch (_params[i].type) {
|
2022-07-15 08:53:41 -03:00
|
|
|
#if AP_RPM_PIN_ENABLED
|
2021-06-03 04:27:54 -03:00
|
|
|
case RPM_TYPE_PWM:
|
|
|
|
case RPM_TYPE_PIN:
|
2019-10-17 03:04:59 -03:00
|
|
|
// PWM option same as PIN option, for upgrade
|
2024-05-26 22:24:14 -03:00
|
|
|
drivers[i] = NEW_NOTHROW AP_RPM_Pin(*this, i, state[i]);
|
2021-06-03 04:27:54 -03:00
|
|
|
break;
|
2022-07-15 08:53:41 -03:00
|
|
|
#endif // AP_RPM_PIN_ENABLED
|
|
|
|
#if AP_RPM_ESC_TELEM_ENABLED
|
2021-06-03 04:27:54 -03:00
|
|
|
case RPM_TYPE_ESC_TELEM:
|
2024-05-26 22:24:14 -03:00
|
|
|
drivers[i] = NEW_NOTHROW AP_RPM_ESC_Telem(*this, i, state[i]);
|
2021-06-03 04:27:54 -03:00
|
|
|
break;
|
2022-07-15 08:53:41 -03:00
|
|
|
#endif // AP_RPM_ESC_TELEM_ENABLED
|
|
|
|
#if AP_RPM_EFI_ENABLED
|
2021-06-03 04:27:54 -03:00
|
|
|
case RPM_TYPE_EFI:
|
2024-05-26 22:24:14 -03:00
|
|
|
drivers[i] = NEW_NOTHROW AP_RPM_EFI(*this, i, state[i]);
|
2021-06-03 04:27:54 -03:00
|
|
|
break;
|
2022-07-15 08:53:41 -03:00
|
|
|
#endif // AP_RPM_EFI_ENABLED
|
|
|
|
#if AP_RPM_GENERATOR_ENABLED
|
2022-08-29 06:34:58 -03:00
|
|
|
case RPM_TYPE_GENERATOR:
|
2024-05-26 22:24:14 -03:00
|
|
|
drivers[i] = NEW_NOTHROW AP_RPM_Generator(*this, i, state[i]);
|
2022-08-29 06:34:58 -03:00
|
|
|
break;
|
2022-07-15 08:53:41 -03:00
|
|
|
#endif // AP_RPM_GENERATOR_ENABLED
|
|
|
|
#if AP_RPM_HARMONICNOTCH_ENABLED
|
2020-04-06 09:54:00 -03:00
|
|
|
// include harmonic notch last
|
|
|
|
// this makes whatever process is driving the dynamic notch appear as an RPM value
|
2021-06-03 04:27:54 -03:00
|
|
|
case RPM_TYPE_HNTCH:
|
2024-05-26 22:24:14 -03:00
|
|
|
drivers[i] = NEW_NOTHROW AP_RPM_HarmonicNotch(*this, i, state[i]);
|
2021-06-03 04:27:54 -03:00
|
|
|
break;
|
2022-07-15 08:53:41 -03:00
|
|
|
#endif // AP_RPM_HARMONICNOTCH_ENABLED
|
2024-03-13 21:58:46 -03:00
|
|
|
#if AP_RPM_DRONECAN_ENABLED
|
|
|
|
case RPM_TYPE_DRONECAN:
|
2024-05-26 22:24:14 -03:00
|
|
|
drivers[i] = NEW_NOTHROW AP_RPM_DroneCAN(*this, i, state[i]);
|
2024-03-13 21:58:46 -03:00
|
|
|
break;
|
|
|
|
#endif // AP_RPM_DRONECAN_ENABLED
|
2022-07-15 08:53:41 -03:00
|
|
|
#if AP_RPM_SIM_ENABLED
|
2021-06-03 04:27:54 -03:00
|
|
|
case RPM_TYPE_SITL:
|
2024-05-26 22:24:14 -03:00
|
|
|
drivers[i] = NEW_NOTHROW AP_RPM_SITL(*this, i, state[i]);
|
2021-06-03 04:27:54 -03:00
|
|
|
break;
|
2022-07-15 08:53:41 -03:00
|
|
|
#endif // AP_RPM_SIM_ENABLED
|
2021-06-03 04:27:54 -03:00
|
|
|
}
|
2016-10-30 02:24:21 -03:00
|
|
|
if (drivers[i] != nullptr) {
|
2015-08-07 00:37:06 -03:00
|
|
|
// we loaded a driver for this instance, so it must be
|
|
|
|
// present (although it may not be healthy)
|
2018-07-04 02:38:36 -03:00
|
|
|
num_instances = i+1; // num_instances is a high-water-mark
|
2015-08-07 00:37:06 -03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-09 08:21:52 -03:00
|
|
|
/*
|
|
|
|
PARAMETER_CONVERSION - Added: Aug-2021
|
|
|
|
*/
|
|
|
|
void AP_RPM::convert_params(void)
|
|
|
|
{
|
2022-05-11 19:04:37 -03:00
|
|
|
if (_params[0].type.configured()) {
|
|
|
|
// _params[0].type will always be configured after conversion is done the first time
|
2021-08-09 08:21:52 -03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// don't do conversion if neither RPM types were set
|
|
|
|
bool type_set;
|
|
|
|
uint8_t rpm_type = 0;
|
|
|
|
uint8_t rpm2_type = 0;
|
|
|
|
type_set = AP_Param::get_param_by_index(this, 0, AP_PARAM_INT8, &rpm_type);
|
|
|
|
type_set |= AP_Param::get_param_by_index(this, 10, AP_PARAM_INT8, &rpm2_type);
|
|
|
|
|
|
|
|
if (!type_set || (rpm_type == 0 && rpm2_type == 0)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct ConversionTable {
|
|
|
|
uint8_t old_element;
|
|
|
|
uint8_t new_index;
|
|
|
|
uint8_t instance;
|
|
|
|
};
|
|
|
|
|
|
|
|
const struct ConversionTable conversionTable[] = {
|
|
|
|
// RPM 1
|
|
|
|
{0, 0, 0}, // TYPE
|
|
|
|
{1, 1, 0}, // SCALING
|
|
|
|
{2, 2, 0}, // MAX
|
|
|
|
{3, 3, 0}, // MIN
|
|
|
|
{4, 4, 0}, // MIN_QUAL
|
|
|
|
{5, 5, 0}, // PIN
|
|
|
|
{6, 6, 0}, // ESC_MASK
|
|
|
|
|
|
|
|
// RPM 2
|
|
|
|
{10, 0, 1}, // TYPE
|
|
|
|
{11, 1, 1}, // SCALING
|
|
|
|
// MAX (Previous bug meant RPM2_MAX param was never accesible to users. No conversion required.)
|
|
|
|
// MIN (Previous bug meant RPM2_MIN param was never accesible to users. No conversion required.)
|
|
|
|
{4, 4, 1}, // MIN_QUAL (Previously the min quality of the 1st RPM instance was used for all RPM instances.)
|
|
|
|
{12, 5, 1}, // PIN
|
|
|
|
{13, 6, 1}, // ESC_MASK
|
|
|
|
};
|
|
|
|
|
|
|
|
char param_name[17] = {0};
|
|
|
|
AP_Param::ConversionInfo info;
|
|
|
|
info.new_name = param_name;
|
|
|
|
|
|
|
|
if (!AP_Param::find_top_level_key_by_pointer(this, info.old_key)) {
|
|
|
|
_params[0].type.save(true);
|
|
|
|
return; // no conversion is supported on this platform
|
|
|
|
}
|
|
|
|
|
|
|
|
for (uint8_t i = 0; i < ARRAY_SIZE(conversionTable); i++) {
|
|
|
|
uint8_t param_instance = conversionTable[i].instance + 1;
|
|
|
|
uint8_t destination_index = conversionTable[i].new_index;
|
|
|
|
info.old_group_element = conversionTable[i].old_element;
|
|
|
|
|
|
|
|
// The var type of the params has not changed in the conversion so this is ok:
|
|
|
|
info.type = (ap_var_type)AP_RPM_Params::var_info[destination_index].type;
|
|
|
|
hal.util->snprintf(param_name, sizeof(param_name), "RPM%X_%s", param_instance, AP_RPM_Params::var_info[destination_index].name);
|
|
|
|
param_name[sizeof(param_name)-1] = '\0';
|
|
|
|
|
|
|
|
AP_Param::convert_old_parameter(&info, 1.0f, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
// force _params[0].type into storage to flag that conversion has been done
|
|
|
|
_params[0].type.save(true);
|
|
|
|
}
|
|
|
|
|
2015-08-07 00:37:06 -03:00
|
|
|
/*
|
|
|
|
update RPM state for all instances. This should be called by main loop
|
|
|
|
*/
|
|
|
|
void AP_RPM::update(void)
|
|
|
|
{
|
|
|
|
for (uint8_t i=0; i<num_instances; i++) {
|
2016-10-30 02:24:21 -03:00
|
|
|
if (drivers[i] != nullptr) {
|
2021-08-09 08:21:52 -03:00
|
|
|
if (_params[i].type == RPM_TYPE_NONE) {
|
2019-01-31 11:02:54 -04:00
|
|
|
// allow user to disable an RPM sensor at runtime and force it to re-learn the quality if re-enabled.
|
|
|
|
state[i].signal_quality = 0;
|
2015-08-07 00:37:06 -03:00
|
|
|
continue;
|
|
|
|
}
|
2019-01-31 11:02:54 -04:00
|
|
|
|
2015-08-07 00:37:06 -03:00
|
|
|
drivers[i]->update();
|
2023-08-14 23:16:48 -03:00
|
|
|
|
|
|
|
#if AP_RPM_ESC_TELEM_OUTBOUND_ENABLED
|
|
|
|
drivers[i]->update_esc_telem_outbound();
|
|
|
|
#endif
|
2015-08-07 00:37:06 -03:00
|
|
|
}
|
|
|
|
}
|
2022-01-05 07:23:44 -04:00
|
|
|
|
2022-03-03 20:36:14 -04:00
|
|
|
#if HAL_LOGGING_ENABLED
|
2024-06-14 13:56:14 -03:00
|
|
|
Log_RPM();
|
2022-03-03 20:36:14 -04:00
|
|
|
#endif
|
2015-08-07 00:37:06 -03:00
|
|
|
}
|
2021-06-03 04:27:54 -03:00
|
|
|
|
2015-08-07 00:37:06 -03:00
|
|
|
/*
|
|
|
|
check if an instance is healthy
|
|
|
|
*/
|
|
|
|
bool AP_RPM::healthy(uint8_t instance) const
|
|
|
|
{
|
2021-08-09 08:21:52 -03:00
|
|
|
if (instance >= num_instances || _params[instance].type == RPM_TYPE_NONE) {
|
2015-08-07 00:37:06 -03:00
|
|
|
return false;
|
|
|
|
}
|
2015-11-20 20:18:12 -04:00
|
|
|
|
|
|
|
// check that data quality is above minimum required
|
2021-08-09 08:21:52 -03:00
|
|
|
if (state[instance].signal_quality < _params[instance].quality_min) {
|
2015-11-20 20:18:12 -04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-08-07 00:37:06 -03:00
|
|
|
return true;
|
|
|
|
}
|
2015-11-18 19:59:10 -04:00
|
|
|
|
|
|
|
/*
|
|
|
|
check if an instance is activated
|
|
|
|
*/
|
|
|
|
bool AP_RPM::enabled(uint8_t instance) const
|
|
|
|
{
|
|
|
|
if (instance >= num_instances) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
// if no sensor type is selected, the sensor is not activated.
|
2021-08-09 08:21:52 -03:00
|
|
|
if (_params[instance].type == RPM_TYPE_NONE) {
|
2015-11-18 19:59:10 -04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2019-02-25 12:48:19 -04:00
|
|
|
|
2020-03-05 19:46:27 -04:00
|
|
|
/*
|
|
|
|
get RPM value, return true on success
|
|
|
|
*/
|
|
|
|
bool AP_RPM::get_rpm(uint8_t instance, float &rpm_value) const
|
|
|
|
{
|
|
|
|
if (!healthy(instance)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
rpm_value = state[instance].rate_rpm;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-07-20 01:20:16 -03:00
|
|
|
// check settings are valid
|
|
|
|
bool AP_RPM::arming_checks(size_t buflen, char *buffer) const
|
|
|
|
{
|
|
|
|
for (uint8_t i=0; i<RPM_MAX_INSTANCES; i++) {
|
2021-08-09 08:21:52 -03:00
|
|
|
switch (_params[i].type) {
|
2022-09-30 09:15:16 -03:00
|
|
|
#if AP_RPM_PIN_ENABLED
|
2021-07-20 01:20:16 -03:00
|
|
|
case RPM_TYPE_PWM:
|
|
|
|
case RPM_TYPE_PIN:
|
2021-08-09 08:21:52 -03:00
|
|
|
if (_params[i].pin == -1) {
|
2022-04-18 23:18:44 -03:00
|
|
|
hal.util->snprintf(buffer, buflen, "RPM%u_PIN not set", unsigned(i + 1));
|
2021-07-20 01:20:16 -03:00
|
|
|
return false;
|
|
|
|
}
|
2021-08-09 08:21:52 -03:00
|
|
|
if (!hal.gpio->valid_pin(_params[i].pin)) {
|
2022-04-18 23:18:44 -03:00
|
|
|
uint8_t servo_ch;
|
|
|
|
if (hal.gpio->pin_to_servo_channel(_params[i].pin, servo_ch)) {
|
|
|
|
hal.util->snprintf(buffer, buflen, "RPM%u_PIN=%d, set SERVO%u_FUNCTION=-1", unsigned(i + 1), int(_params[i].pin.get()), unsigned(servo_ch+1));
|
|
|
|
} else {
|
|
|
|
hal.util->snprintf(buffer, buflen, "RPM%u_PIN=%d invalid", unsigned(i + 1), int(_params[i].pin.get()));
|
|
|
|
}
|
2021-07-20 01:20:16 -03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
break;
|
2022-09-30 09:15:16 -03:00
|
|
|
#endif
|
2021-07-20 01:20:16 -03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-03-03 20:36:14 -04:00
|
|
|
#if HAL_LOGGING_ENABLED
|
2023-06-09 11:14:40 -03:00
|
|
|
void AP_RPM::Log_RPM() const
|
2022-03-03 20:36:14 -04:00
|
|
|
{
|
2024-06-14 13:56:14 -03:00
|
|
|
// update logging for each instance
|
|
|
|
for (uint8_t i=0; i<num_instances; i++) {
|
|
|
|
if (drivers[i] == nullptr || !enabled(i)) {
|
|
|
|
// don't log unused instances
|
|
|
|
continue;
|
|
|
|
}
|
2022-03-03 20:36:14 -04:00
|
|
|
|
2024-06-14 13:56:14 -03:00
|
|
|
const struct log_RPM pkt{
|
|
|
|
LOG_PACKET_HEADER_INIT(LOG_RPM_MSG),
|
|
|
|
time_us : AP_HAL::micros64(),
|
|
|
|
inst : i,
|
|
|
|
rpm : state[i].rate_rpm,
|
|
|
|
quality : get_signal_quality(i),
|
|
|
|
health : uint8_t(healthy(i))
|
|
|
|
};
|
|
|
|
AP::logger().WriteBlock(&pkt, sizeof(pkt));
|
|
|
|
}
|
2022-03-03 20:36:14 -04:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2024-03-13 21:09:20 -03:00
|
|
|
#ifdef HAL_PERIPH_ENABLE_RPM_STREAM
|
|
|
|
// Return the sensor id to use for streaming over DroneCAN, negative number disables
|
|
|
|
int8_t AP_RPM::get_dronecan_sensor_id(uint8_t instance) const
|
|
|
|
{
|
|
|
|
if (!enabled(instance)) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return _params[instance].dronecan_sensor_id;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2019-02-25 12:48:19 -04:00
|
|
|
// singleton instance
|
|
|
|
AP_RPM *AP_RPM::_singleton;
|
|
|
|
|
2019-03-15 08:15:44 -03:00
|
|
|
namespace AP {
|
|
|
|
|
|
|
|
AP_RPM *rpm()
|
|
|
|
{
|
|
|
|
return AP_RPM::get_singleton();
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2022-07-15 08:53:41 -03:00
|
|
|
|
|
|
|
#endif // AP_RPM_ENABLED
|