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"
|
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"
|
2020-04-06 09:54:00 -03:00
|
|
|
#include "RPM_HarmonicNotch.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[] = {
|
2015-08-07 00:37:06 -03:00
|
|
|
// @Param: _TYPE
|
|
|
|
// @DisplayName: RPM type
|
|
|
|
// @Description: What type of RPM sensor is connected
|
2020-04-06 09:54:00 -03:00
|
|
|
// @Values: 0:None,1:PWM,2:AUXPIN,3:EFI,4:Harmonic Notch
|
2016-07-26 02:35:15 -03:00
|
|
|
// @User: Standard
|
2015-08-07 00:37:06 -03:00
|
|
|
AP_GROUPINFO("_TYPE", 0, AP_RPM, _type[0], 0),
|
|
|
|
|
|
|
|
// @Param: _SCALING
|
|
|
|
// @DisplayName: RPM scaling
|
|
|
|
// @Description: Scaling factor between sensor reading and RPM.
|
|
|
|
// @Increment: 0.001
|
2016-07-26 02:35:15 -03:00
|
|
|
// @User: Standard
|
2015-08-07 00:37:06 -03:00
|
|
|
AP_GROUPINFO("_SCALING", 1, AP_RPM, _scaling[0], 1.0f),
|
|
|
|
|
2015-08-08 07:00:36 -03:00
|
|
|
// @Param: _MAX
|
|
|
|
// @DisplayName: Maximum RPM
|
|
|
|
// @Description: Maximum RPM to report
|
|
|
|
// @Increment: 1
|
2016-07-26 02:35:15 -03:00
|
|
|
// @User: Standard
|
2016-05-14 05:05:13 -03:00
|
|
|
AP_GROUPINFO("_MAX", 2, AP_RPM, _maximum[0], 100000),
|
2015-08-08 07:00:36 -03:00
|
|
|
|
2015-11-20 18:07:41 -04:00
|
|
|
// @Param: _MIN
|
|
|
|
// @DisplayName: Minimum RPM
|
|
|
|
// @Description: Minimum RPM to report
|
|
|
|
// @Increment: 1
|
2016-07-26 02:35:15 -03:00
|
|
|
// @User: Standard
|
2016-05-14 05:05:13 -03:00
|
|
|
AP_GROUPINFO("_MIN", 3, AP_RPM, _minimum[0], 10),
|
2015-11-20 18:07:41 -04:00
|
|
|
|
2015-11-20 20:18:12 -04:00
|
|
|
// @Param: _MIN_QUAL
|
|
|
|
// @DisplayName: Minimum Quality
|
|
|
|
// @Description: Minimum data quality to be used
|
|
|
|
// @Increment: 0.1
|
2016-07-26 02:35:15 -03:00
|
|
|
// @User: Advanced
|
2015-11-20 20:18:12 -04:00
|
|
|
AP_GROUPINFO("_MIN_QUAL", 4, AP_RPM, _quality_min[0], 0.5),
|
|
|
|
|
2017-04-02 00:57:59 -03:00
|
|
|
// @Param: _PIN
|
|
|
|
// @DisplayName: Input pin number
|
|
|
|
// @Description: Which pin to use
|
|
|
|
// @Values: -1:Disabled,50:PixhawkAUX1,51:PixhawkAUX2,52:PixhawkAUX3,53:PixhawkAUX4,54:PixhawkAUX5,55:PixhawkAUX6
|
|
|
|
// @User: Standard
|
|
|
|
AP_GROUPINFO("_PIN", 5, AP_RPM, _pin[0], 54),
|
|
|
|
|
2015-08-07 00:37:06 -03:00
|
|
|
#if RPM_MAX_INSTANCES > 1
|
|
|
|
// @Param: 2_TYPE
|
|
|
|
// @DisplayName: Second RPM type
|
|
|
|
// @Description: What type of RPM sensor is connected
|
2020-04-06 09:54:00 -03:00
|
|
|
// @Values: 0:None,1:PWM,2:AUXPIN,3:EFI,4:Harmonic Notch
|
2016-07-26 02:35:15 -03:00
|
|
|
// @User: Advanced
|
2015-08-07 00:37:06 -03:00
|
|
|
AP_GROUPINFO("2_TYPE", 10, AP_RPM, _type[1], 0),
|
|
|
|
|
|
|
|
// @Param: 2_SCALING
|
|
|
|
// @DisplayName: RPM scaling
|
|
|
|
// @Description: Scaling factor between sensor reading and RPM.
|
|
|
|
// @Increment: 0.001
|
2016-07-26 02:35:15 -03:00
|
|
|
// @User: Advanced
|
2015-08-07 00:37:06 -03:00
|
|
|
AP_GROUPINFO("2_SCALING", 11, AP_RPM, _scaling[1], 1.0f),
|
|
|
|
|
2017-04-02 00:57:59 -03:00
|
|
|
// @Param: 2_PIN
|
|
|
|
// @DisplayName: RPM2 input pin number
|
|
|
|
// @Description: Which pin to use
|
|
|
|
// @Values: -1:Disabled,50:PixhawkAUX1,51:PixhawkAUX2,52:PixhawkAUX3,53:PixhawkAUX4,54:PixhawkAUX5,55:PixhawkAUX6
|
|
|
|
// @User: Standard
|
|
|
|
AP_GROUPINFO("2_PIN", 12, AP_RPM, _pin[1], -1),
|
2021-02-14 17:51:28 -04:00
|
|
|
#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
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
initialise the AP_RPM class.
|
|
|
|
*/
|
|
|
|
void AP_RPM::init(void)
|
|
|
|
{
|
|
|
|
if (num_instances != 0) {
|
|
|
|
// init called a 2nd time?
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
for (uint8_t i=0; i<RPM_MAX_INSTANCES; i++) {
|
2018-10-16 23:12:12 -03:00
|
|
|
uint8_t type = _type[i];
|
2019-12-08 17:37:48 -04:00
|
|
|
#if CONFIG_HAL_BOARD != HAL_BOARD_SITL
|
2019-10-17 03:04:59 -03:00
|
|
|
if (type == RPM_TYPE_PWM) {
|
|
|
|
// PWM option same as PIN option, for upgrade
|
2018-10-16 23:12:12 -03:00
|
|
|
type = RPM_TYPE_PIN;
|
|
|
|
}
|
2018-07-04 02:38:36 -03:00
|
|
|
if (type == RPM_TYPE_PIN) {
|
|
|
|
drivers[i] = new AP_RPM_Pin(*this, i, state[i]);
|
2018-03-26 03:19:13 -03:00
|
|
|
}
|
2019-12-08 17:37:48 -04:00
|
|
|
#endif
|
2021-06-08 01:17:00 -03:00
|
|
|
#if HAL_EFI_ENABLED
|
2019-11-11 00:37:44 -04:00
|
|
|
if (type == RPM_TYPE_EFI) {
|
|
|
|
drivers[i] = new AP_RPM_EFI(*this, i, state[i]);
|
|
|
|
}
|
|
|
|
#endif
|
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
|
|
|
|
if (type == RPM_TYPE_HNTCH) {
|
|
|
|
drivers[i] = new AP_RPM_HarmonicNotch(*this, i, state[i]);
|
|
|
|
}
|
2015-11-22 23:28:17 -04:00
|
|
|
#if CONFIG_HAL_BOARD == HAL_BOARD_SITL
|
2021-02-25 02:21:52 -04:00
|
|
|
if (type == RPM_TYPE_SITL) {
|
2019-11-11 00:37:44 -04:00
|
|
|
drivers[i] = new AP_RPM_SITL(*this, i, state[i]);
|
|
|
|
}
|
2015-08-07 00:37:06 -03:00
|
|
|
#endif
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
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) {
|
2015-08-07 00:37:06 -03:00
|
|
|
if (_type[i] == 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();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
check if an instance is healthy
|
|
|
|
*/
|
|
|
|
bool AP_RPM::healthy(uint8_t instance) const
|
|
|
|
{
|
2019-01-31 11:02:54 -04:00
|
|
|
if (instance >= num_instances || _type[instance] == 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
|
|
|
|
if (state[instance].signal_quality < _quality_min[0]) {
|
|
|
|
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.
|
|
|
|
if (_type[instance] == RPM_TYPE_NONE) {
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|