2021-10-27 05:43:28 -03:00
|
|
|
/*
|
|
|
|
* This file 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 file 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/>.
|
|
|
|
*/
|
2024-11-16 09:10:16 -04:00
|
|
|
|
2021-10-27 05:43:28 -03:00
|
|
|
#include <GCS_MAVLink/GCS.h>
|
|
|
|
|
|
|
|
#include "AP_HAL_ESP32.h"
|
|
|
|
#include "RCInput.h"
|
|
|
|
|
2024-11-16 09:10:16 -04:00
|
|
|
#include <AP_RCProtocol/AP_RCProtocol_config.h>
|
|
|
|
|
|
|
|
#include <AP_Math/AP_Math.h>
|
|
|
|
|
|
|
|
#ifndef HAL_NO_UARTDRIVER
|
|
|
|
#include <GCS_MAVLink/GCS.h>
|
|
|
|
#endif
|
|
|
|
|
2021-10-27 05:43:28 -03:00
|
|
|
using namespace ESP32;
|
|
|
|
|
2024-11-16 09:10:16 -04:00
|
|
|
extern const AP_HAL::HAL& hal;
|
|
|
|
|
2021-10-27 05:43:28 -03:00
|
|
|
void RCInput::init()
|
|
|
|
{
|
|
|
|
if (_init) {
|
|
|
|
return;
|
|
|
|
}
|
2023-05-03 10:08:00 -03:00
|
|
|
#if AP_RCPROTOCOL_ENABLED
|
2021-10-27 05:43:28 -03:00
|
|
|
AP::RC().init();
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef HAL_ESP32_RCIN
|
|
|
|
sig_reader.init();
|
2024-11-16 09:10:16 -04:00
|
|
|
pulse_input_enabled = true;
|
2021-10-27 05:43:28 -03:00
|
|
|
#endif
|
2024-11-16 09:10:16 -04:00
|
|
|
|
2021-10-27 05:43:28 -03:00
|
|
|
_init = true;
|
|
|
|
}
|
|
|
|
|
2024-11-16 09:10:16 -04:00
|
|
|
/*
|
|
|
|
enable or disable pulse input for RC input. This is used to reduce
|
|
|
|
load when we are decoding R/C via a UART
|
|
|
|
*/
|
|
|
|
void RCInput::pulse_input_enable(bool enable)
|
|
|
|
{
|
|
|
|
pulse_input_enabled = enable;
|
|
|
|
#if HAL_ESP32_RCIN
|
|
|
|
if (!enable) {
|
|
|
|
sig_reader.disable();
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2021-10-27 05:43:28 -03:00
|
|
|
bool RCInput::new_input()
|
|
|
|
{
|
|
|
|
if (!_init) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
bool valid;
|
|
|
|
{
|
|
|
|
WITH_SEMAPHORE(rcin_mutex);
|
|
|
|
valid = _rcin_timestamp_last_signal != _last_read;
|
|
|
|
_last_read = _rcin_timestamp_last_signal;
|
|
|
|
}
|
|
|
|
|
|
|
|
return valid;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t RCInput::num_channels()
|
|
|
|
{
|
|
|
|
if (!_init) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return _num_channels;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint16_t RCInput::read(uint8_t channel)
|
|
|
|
{
|
|
|
|
if (!_init || (channel >= MIN(RC_INPUT_MAX_CHANNELS, _num_channels))) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
uint16_t v;
|
|
|
|
{
|
|
|
|
WITH_SEMAPHORE(rcin_mutex);
|
|
|
|
v = _rc_values[channel];
|
|
|
|
}
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t RCInput::read(uint16_t* periods, uint8_t len)
|
|
|
|
{
|
|
|
|
if (!_init) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (len > RC_INPUT_MAX_CHANNELS) {
|
|
|
|
len = RC_INPUT_MAX_CHANNELS;
|
|
|
|
}
|
|
|
|
{
|
|
|
|
WITH_SEMAPHORE(rcin_mutex);
|
|
|
|
memcpy(periods, _rc_values, len*sizeof(periods[0]));
|
|
|
|
}
|
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
|
|
|
void RCInput::_timer_tick(void)
|
|
|
|
{
|
|
|
|
if (!_init) {
|
|
|
|
return;
|
|
|
|
}
|
2024-11-16 09:10:16 -04:00
|
|
|
#ifndef HAL_NO_UARTDRIVER
|
|
|
|
const char *rc_protocol = nullptr;
|
|
|
|
RCSource source = last_source;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if AP_RCPROTOCOL_ENABLED
|
2021-10-27 05:43:28 -03:00
|
|
|
AP_RCProtocol &rcprot = AP::RC();
|
|
|
|
|
|
|
|
#ifdef HAL_ESP32_RCIN
|
2024-11-16 09:10:16 -04:00
|
|
|
if (pulse_input_enabled) {
|
|
|
|
uint32_t width_s0, width_s1;
|
|
|
|
while (sig_reader.read(width_s0, width_s1)) {
|
|
|
|
rcprot.process_pulse(width_s0, width_s1);
|
|
|
|
}
|
2021-10-27 05:43:28 -03:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2024-11-16 09:10:16 -04:00
|
|
|
#endif // AP_RCPROTOCOL_ENABLED
|
|
|
|
|
|
|
|
#if AP_RCPROTOCOL_ENABLED
|
2021-10-27 05:43:28 -03:00
|
|
|
if (rcprot.new_input()) {
|
|
|
|
WITH_SEMAPHORE(rcin_mutex);
|
|
|
|
_rcin_timestamp_last_signal = AP_HAL::micros();
|
|
|
|
_num_channels = rcprot.num_channels();
|
|
|
|
_num_channels = MIN(_num_channels, RC_INPUT_MAX_CHANNELS);
|
|
|
|
rcprot.read(_rc_values, _num_channels);
|
|
|
|
_rssi = rcprot.get_RSSI();
|
2024-11-16 09:10:16 -04:00
|
|
|
_rx_link_quality = rcprot.get_rx_link_quality();
|
2021-10-27 05:43:28 -03:00
|
|
|
#ifndef HAL_NO_UARTDRIVER
|
|
|
|
rc_protocol = rcprot.protocol_name();
|
2024-11-16 09:10:16 -04:00
|
|
|
source = rcprot.using_uart() ? RCSource::RCPROT_BYTES : RCSource::RCPROT_PULSES;
|
|
|
|
// printf("RCInput: decoding %s", last_protocol);
|
2021-10-27 05:43:28 -03:00
|
|
|
#endif
|
|
|
|
}
|
2024-11-16 09:10:16 -04:00
|
|
|
#endif // AP_RCPROTOCOL_ENABLED
|
2021-10-27 05:43:28 -03:00
|
|
|
|
|
|
|
#ifndef HAL_NO_UARTDRIVER
|
2024-11-16 09:10:16 -04:00
|
|
|
if (rc_protocol && (rc_protocol != last_protocol || source != last_source)) {
|
2021-10-27 05:43:28 -03:00
|
|
|
last_protocol = rc_protocol;
|
2024-11-16 09:10:16 -04:00
|
|
|
last_source = source;
|
|
|
|
GCS_SEND_TEXT(MAV_SEVERITY_DEBUG, "RCInput: decoding %s(%u)", last_protocol, unsigned(source));
|
2021-10-27 05:43:28 -03:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2024-11-16 09:10:16 -04:00
|
|
|
// note, we rely on the vehicle code checking new_input()
|
|
|
|
// and a timeout for the last valid input to handle failsafe
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
start a bind operation, if supported
|
|
|
|
*/
|
|
|
|
bool RCInput::rc_bind(int dsmMode)
|
|
|
|
{
|
|
|
|
#if AP_RCPROTOCOL_ENABLED
|
|
|
|
// ask AP_RCProtocol to start a bind
|
|
|
|
AP::RC().start_bind();
|
2021-10-27 05:43:28 -03:00
|
|
|
#endif
|
2024-11-16 09:10:16 -04:00
|
|
|
|
|
|
|
return true;
|
2021-10-27 05:43:28 -03:00
|
|
|
}
|