2017-04-02 00:57:59 -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/>.
|
|
|
|
*/
|
|
|
|
|
2018-03-26 03:19:13 -03:00
|
|
|
#include "RPM_Pin.h"
|
2017-04-02 00:57:59 -03:00
|
|
|
|
2022-07-15 08:53:41 -03:00
|
|
|
#if AP_RPM_PIN_ENABLED
|
|
|
|
|
|
|
|
#include <AP_HAL/AP_HAL.h>
|
|
|
|
|
2018-07-04 02:38:36 -03:00
|
|
|
#include <AP_HAL/GPIO.h>
|
|
|
|
#include <GCS_MAVLink/GCS.h>
|
2017-04-02 00:57:59 -03:00
|
|
|
|
|
|
|
extern const AP_HAL::HAL& hal;
|
|
|
|
AP_RPM_Pin::IrqState AP_RPM_Pin::irq_state[RPM_MAX_INSTANCES];
|
|
|
|
|
|
|
|
/*
|
|
|
|
open the sensor in constructor
|
|
|
|
*/
|
|
|
|
AP_RPM_Pin::AP_RPM_Pin(AP_RPM &_ap_rpm, uint8_t instance, AP_RPM::RPM_State &_state) :
|
|
|
|
AP_RPM_Backend(_ap_rpm, instance, _state)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
handle interrupt on an instance
|
|
|
|
*/
|
2018-07-04 02:38:36 -03:00
|
|
|
void AP_RPM_Pin::irq_handler(uint8_t pin, bool pin_state, uint32_t timestamp)
|
2017-04-02 00:57:59 -03:00
|
|
|
{
|
2018-07-04 02:38:36 -03:00
|
|
|
const uint32_t dt = timestamp - irq_state[state.instance].last_pulse_us;
|
|
|
|
irq_state[state.instance].last_pulse_us = timestamp;
|
2017-04-02 00:57:59 -03:00
|
|
|
// we don't accept pulses less than 100us. Using an irq for such
|
|
|
|
// high RPM is too inaccurate, and it is probably just bounce of
|
|
|
|
// the signal which we should ignore
|
|
|
|
if (dt > 100 && dt < 1000*1000) {
|
2018-07-04 02:38:36 -03:00
|
|
|
irq_state[state.instance].dt_sum += dt;
|
|
|
|
irq_state[state.instance].dt_count++;
|
2017-04-02 00:57:59 -03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void AP_RPM_Pin::update(void)
|
|
|
|
{
|
|
|
|
if (last_pin != get_pin()) {
|
2018-07-04 02:38:36 -03:00
|
|
|
// detach from last pin
|
2022-04-19 05:14:36 -03:00
|
|
|
if (interrupt_attached) {
|
|
|
|
// ignore this failure of the user may be stuck
|
|
|
|
IGNORE_RETURN(hal.gpio->detach_interrupt(last_pin));
|
|
|
|
interrupt_attached = false;
|
2017-04-02 00:57:59 -03:00
|
|
|
}
|
|
|
|
irq_state[state.instance].dt_count = 0;
|
|
|
|
irq_state[state.instance].dt_sum = 0;
|
2018-07-04 02:38:36 -03:00
|
|
|
// attach to new pin
|
|
|
|
last_pin = get_pin();
|
2022-04-19 05:14:36 -03:00
|
|
|
if (last_pin > 0) {
|
2018-11-08 00:20:17 -04:00
|
|
|
hal.gpio->pinMode(last_pin, HAL_GPIO_INPUT);
|
2022-04-19 05:14:36 -03:00
|
|
|
if (hal.gpio->attach_interrupt(
|
2018-07-04 02:38:36 -03:00
|
|
|
last_pin,
|
|
|
|
FUNCTOR_BIND_MEMBER(&AP_RPM_Pin::irq_handler, void, uint8_t, bool, uint32_t),
|
|
|
|
AP_HAL::GPIO::INTERRUPT_RISING)) {
|
2022-04-19 05:14:36 -03:00
|
|
|
interrupt_attached = true;
|
|
|
|
} else {
|
|
|
|
gcs().send_text(MAV_SEVERITY_WARNING, "RPM: Failed to attach to pin %d", last_pin);
|
2018-07-04 02:38:36 -03:00
|
|
|
}
|
2017-04-02 00:57:59 -03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (irq_state[state.instance].dt_count > 0) {
|
|
|
|
|
|
|
|
// disable interrupts to prevent race with irq_handler
|
2018-03-26 03:19:13 -03:00
|
|
|
void *irqstate = hal.scheduler->disable_interrupts_save();
|
2021-08-13 04:57:10 -03:00
|
|
|
const float dt_avg = static_cast<float>(irq_state[state.instance].dt_sum) / irq_state[state.instance].dt_count;
|
2017-04-02 00:57:59 -03:00
|
|
|
irq_state[state.instance].dt_count = 0;
|
|
|
|
irq_state[state.instance].dt_sum = 0;
|
2018-03-26 03:19:13 -03:00
|
|
|
hal.scheduler->restore_interrupts(irqstate);
|
2017-04-02 00:57:59 -03:00
|
|
|
|
2021-08-09 08:21:52 -03:00
|
|
|
const float scaling = ap_rpm._params[state.instance].scaling;
|
|
|
|
float maximum = ap_rpm._params[state.instance].maximum;
|
|
|
|
float minimum = ap_rpm._params[state.instance].minimum;
|
2017-04-02 00:57:59 -03:00
|
|
|
float quality = 0;
|
|
|
|
float rpm = scaling * (1.0e6 / dt_avg) * 60;
|
|
|
|
float filter_value = signal_quality_filter.get();
|
|
|
|
|
|
|
|
state.rate_rpm = signal_quality_filter.apply(rpm);
|
2021-08-09 18:34:28 -03:00
|
|
|
|
2017-04-02 00:57:59 -03:00
|
|
|
if ((maximum <= 0 || rpm <= maximum) && (rpm >= minimum)) {
|
|
|
|
if (is_zero(filter_value)){
|
|
|
|
quality = 0;
|
|
|
|
} else {
|
|
|
|
quality = 1 - constrain_float((fabsf(rpm-filter_value))/filter_value, 0.0, 1.0);
|
|
|
|
quality = powf(quality, 2.0);
|
|
|
|
}
|
|
|
|
state.last_reading_ms = AP_HAL::millis();
|
|
|
|
} else {
|
|
|
|
quality = 0;
|
|
|
|
}
|
|
|
|
state.signal_quality = (0.1 * quality) + (0.9 * state.signal_quality);
|
|
|
|
}
|
2021-08-09 18:34:28 -03:00
|
|
|
|
2017-04-02 00:57:59 -03:00
|
|
|
// assume we get readings at at least 1Hz, otherwise reset quality to zero
|
|
|
|
if (AP_HAL::millis() - state.last_reading_ms > 1000) {
|
|
|
|
state.signal_quality = 0;
|
|
|
|
state.rate_rpm = 0;
|
|
|
|
}
|
|
|
|
}
|
2022-07-15 08:53:41 -03:00
|
|
|
|
|
|
|
#endif // AP_RPM_PIN_ENABLED
|