/*
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 .
*/
#include "AP_RPM.h"
#if AP_RPM_ENABLED
#include "RPM_Pin.h"
#include "RPM_SITL.h"
#include "RPM_EFI.h"
#include "RPM_Generator.h"
#include "RPM_HarmonicNotch.h"
#include "RPM_ESC_Telem.h"
#include
extern const AP_HAL::HAL& hal;
// table of user settable parameters
const AP_Param::GroupInfo AP_RPM::var_info[] = {
// 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),
#if RPM_MAX_INSTANCES > 1
// @Group: 2_
// @Path: AP_RPM_Params.cpp
AP_SUBGROUPINFO(_params[1], "2_", 15, AP_RPM, AP_RPM_Params),
#endif
AP_GROUPEND
};
AP_RPM::AP_RPM(void)
{
AP_Param::setup_object_defaults(this, var_info);
if (_singleton != nullptr) {
AP_HAL::panic("AP_RPM must be singleton");
}
_singleton = this;
}
/*
initialise the AP_RPM class.
*/
void AP_RPM::init(void)
{
if (num_instances != 0) {
// init called a 2nd time?
return;
}
convert_params();
for (uint8_t i=0; isnprintf(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);
}
/*
update RPM state for all instances. This should be called by main loop
*/
void AP_RPM::update(void)
{
for (uint8_t i=0; iupdate();
}
}
#if HAL_LOGGING_ENABLED
if (enabled(0) || enabled(1)) {
Log_RPM();
}
#endif
}
/*
check if an instance is healthy
*/
bool AP_RPM::healthy(uint8_t instance) const
{
if (instance >= num_instances || _params[instance].type == RPM_TYPE_NONE) {
return false;
}
// check that data quality is above minimum required
if (state[instance].signal_quality < _params[instance].quality_min) {
return false;
}
return true;
}
/*
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 (_params[instance].type == RPM_TYPE_NONE) {
return false;
}
return true;
}
/*
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;
}
// check settings are valid
bool AP_RPM::arming_checks(size_t buflen, char *buffer) const
{
for (uint8_t i=0; isnprintf(buffer, buflen, "RPM%u_PIN not set", unsigned(i + 1));
return false;
}
if (!hal.gpio->valid_pin(_params[i].pin)) {
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()));
}
return false;
}
break;
}
}
return true;
}
#if HAL_LOGGING_ENABLED
void AP_RPM::Log_RPM()
{
float rpm1 = -1, rpm2 = -1;
get_rpm(0, rpm1);
get_rpm(1, rpm2);
const struct log_RPM pkt{
LOG_PACKET_HEADER_INIT(LOG_RPM_MSG),
time_us : AP_HAL::micros64(),
rpm1 : rpm1,
rpm2 : rpm2
};
AP::logger().WriteBlock(&pkt, sizeof(pkt));
}
#endif
// singleton instance
AP_RPM *AP_RPM::_singleton;
namespace AP {
AP_RPM *rpm()
{
return AP_RPM::get_singleton();
}
}
#endif // AP_RPM_ENABLED