2015-08-11 03:28:43 -03:00
|
|
|
#include <AP_HAL/AP_HAL.h>
|
2020-09-12 16:04:38 -03:00
|
|
|
|
2015-05-04 03:15:12 -03:00
|
|
|
#if CONFIG_HAL_BOARD == HAL_BOARD_SITL
|
2012-12-17 23:56:21 -04:00
|
|
|
|
2023-07-07 00:27:26 -03:00
|
|
|
#include <AP_BoardConfig/AP_BoardConfig.h>
|
|
|
|
#include <SITL/SITL.h>
|
|
|
|
|
2012-12-17 23:56:21 -04:00
|
|
|
#include "RCOutput.h"
|
2016-01-04 16:19:47 -04:00
|
|
|
|
|
|
|
#define ENABLE_DEBUG 0
|
|
|
|
|
|
|
|
#if ENABLE_DEBUG
|
2017-01-09 08:31:44 -04:00
|
|
|
# include <stdio.h>
|
|
|
|
# define Debug(fmt, args ...) do {::printf("%s:%d: " fmt "\n", __FUNCTION__, __LINE__, ## args); } while (0)
|
2016-01-04 16:19:47 -04:00
|
|
|
#else
|
|
|
|
# define Debug(fmt, args ...)
|
|
|
|
#endif
|
2012-12-17 23:56:21 -04:00
|
|
|
|
2015-05-04 03:15:12 -03:00
|
|
|
using namespace HALSITL;
|
2012-12-17 23:56:21 -04:00
|
|
|
|
2016-01-10 02:23:32 -04:00
|
|
|
void RCOutput::init() {}
|
2012-12-17 23:56:21 -04:00
|
|
|
|
2016-01-10 02:23:32 -04:00
|
|
|
void RCOutput::set_freq(uint32_t chmask, uint16_t freq_hz)
|
2016-01-04 16:19:47 -04:00
|
|
|
{
|
2017-01-09 08:31:44 -04:00
|
|
|
Debug("set_freq(0x%04x, %u)\n", static_cast<uint32_t>(chmask), static_cast<uint32_t>(freq_hz));
|
2012-12-17 23:56:21 -04:00
|
|
|
_freq_hz = freq_hz;
|
|
|
|
}
|
|
|
|
|
2016-01-10 02:23:32 -04:00
|
|
|
uint16_t RCOutput::get_freq(uint8_t ch)
|
2016-01-04 16:19:47 -04:00
|
|
|
{
|
2012-12-17 23:56:21 -04:00
|
|
|
return _freq_hz;
|
|
|
|
}
|
|
|
|
|
2016-01-10 02:23:32 -04:00
|
|
|
void RCOutput::enable_ch(uint8_t ch)
|
2016-01-04 16:19:47 -04:00
|
|
|
{
|
2017-01-09 08:31:44 -04:00
|
|
|
if (!(_enable_mask & (1U << ch))) {
|
2016-01-04 16:19:47 -04:00
|
|
|
Debug("enable_ch(%u)\n", ch);
|
|
|
|
}
|
2022-03-25 14:52:53 -03:00
|
|
|
_enable_mask |= (1U << ch);
|
2016-01-04 16:19:47 -04:00
|
|
|
}
|
2012-12-17 23:56:21 -04:00
|
|
|
|
2016-01-10 02:23:32 -04:00
|
|
|
void RCOutput::disable_ch(uint8_t ch)
|
2016-01-04 16:19:47 -04:00
|
|
|
{
|
2017-01-09 08:31:44 -04:00
|
|
|
if (_enable_mask & (1U << ch)) {
|
2016-01-04 16:19:47 -04:00
|
|
|
Debug("disable_ch(%u)\n", ch);
|
|
|
|
}
|
2022-03-25 14:52:53 -03:00
|
|
|
_enable_mask &= ~(1U << ch);
|
2016-01-04 16:19:47 -04:00
|
|
|
}
|
2012-12-17 23:56:21 -04:00
|
|
|
|
2016-01-10 02:23:32 -04:00
|
|
|
void RCOutput::write(uint8_t ch, uint16_t period_us)
|
2012-12-17 23:56:21 -04:00
|
|
|
{
|
2023-07-07 00:27:26 -03:00
|
|
|
if (safety_state == AP_HAL::Util::SAFETY_DISARMED) {
|
2023-08-19 19:49:28 -03:00
|
|
|
const auto *board_config = AP_BoardConfig::get_singleton();
|
|
|
|
const uint32_t safety_mask = board_config != nullptr? board_config->get_safety_mask() : 0;
|
2023-07-07 00:27:26 -03:00
|
|
|
if (!(safety_mask & (1U<<ch))) {
|
|
|
|
// implement safety pwm value
|
|
|
|
period_us = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-07 09:05:52 -03:00
|
|
|
_sitlState->output_ready = true;
|
2022-03-25 14:52:53 -03:00
|
|
|
// FIXME: something in sitl is expecting to be able to read and write disabled channels
|
|
|
|
if (ch < SITL_NUM_CHANNELS /*&& (_enable_mask & (1U<<ch))*/) {
|
2016-10-11 08:02:04 -03:00
|
|
|
if (_corked) {
|
|
|
|
_pending[ch] = period_us;
|
|
|
|
} else {
|
|
|
|
_sitlState->pwm_output[ch] = period_us;
|
|
|
|
}
|
2014-11-11 00:15:28 -04:00
|
|
|
}
|
2012-12-17 23:56:21 -04:00
|
|
|
}
|
|
|
|
|
2016-01-10 02:23:32 -04:00
|
|
|
uint16_t RCOutput::read(uint8_t ch)
|
2014-11-11 00:15:28 -04:00
|
|
|
{
|
2022-03-25 14:52:53 -03:00
|
|
|
// FIXME: something in sitl is expecting to be able to read and write disabled channels
|
|
|
|
if (ch < SITL_NUM_CHANNELS /*&& (_enable_mask & (1U<<ch))*/) {
|
2015-05-04 21:59:07 -03:00
|
|
|
return _sitlState->pwm_output[ch];
|
2014-11-11 00:15:28 -04:00
|
|
|
}
|
|
|
|
return 0;
|
2012-12-17 23:56:21 -04:00
|
|
|
}
|
|
|
|
|
2016-01-10 02:23:32 -04:00
|
|
|
void RCOutput::read(uint16_t* period_us, uint8_t len)
|
2012-12-17 23:56:21 -04:00
|
|
|
{
|
2017-01-09 08:31:44 -04:00
|
|
|
memcpy(period_us, _sitlState->pwm_output, len * sizeof(uint16_t));
|
2012-12-17 23:56:21 -04:00
|
|
|
}
|
2012-12-18 05:04:47 -04:00
|
|
|
|
2016-10-11 08:02:04 -03:00
|
|
|
void RCOutput::cork(void)
|
|
|
|
{
|
2017-04-17 20:40:27 -03:00
|
|
|
if (!_corked) {
|
|
|
|
memcpy(_pending, _sitlState->pwm_output, SITL_NUM_CHANNELS * sizeof(uint16_t));
|
|
|
|
_corked = true;
|
|
|
|
}
|
2016-10-11 08:02:04 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
void RCOutput::push(void)
|
|
|
|
{
|
2017-04-17 20:40:27 -03:00
|
|
|
if (_corked) {
|
|
|
|
memcpy(_sitlState->pwm_output, _pending, SITL_NUM_CHANNELS * sizeof(uint16_t));
|
|
|
|
_corked = false;
|
|
|
|
}
|
2021-08-31 20:04:17 -03:00
|
|
|
|
|
|
|
SITL::SIM *sitl = AP::sitl();
|
2023-01-18 00:19:29 -04:00
|
|
|
if (sitl && sitl->esc_telem) {
|
|
|
|
if (esc_telem == nullptr) {
|
2024-05-26 22:24:12 -03:00
|
|
|
esc_telem = NEW_NOTHROW AP_ESC_Telem_SITL;
|
2023-01-18 00:19:29 -04:00
|
|
|
}
|
|
|
|
if (esc_telem != nullptr) {
|
|
|
|
esc_telem->update();
|
|
|
|
}
|
2021-05-12 21:10:42 -03:00
|
|
|
}
|
2016-10-11 08:02:04 -03:00
|
|
|
}
|
|
|
|
|
2019-10-30 07:12:17 -03:00
|
|
|
/*
|
|
|
|
Serial LED emulation
|
|
|
|
*/
|
2021-01-11 22:02:11 -04:00
|
|
|
bool RCOutput::set_serial_led_num_LEDs(const uint16_t chan, uint8_t num_leds, output_mode mode, uint32_t clock_mask)
|
2019-10-30 07:12:17 -03:00
|
|
|
{
|
2020-02-09 06:35:07 -04:00
|
|
|
if (chan > 15 || num_leds > 64) {
|
2019-10-30 07:12:17 -03:00
|
|
|
return false;
|
|
|
|
}
|
2021-07-30 07:13:59 -03:00
|
|
|
SITL::SIM *sitl = AP::sitl();
|
2019-10-30 07:12:17 -03:00
|
|
|
if (sitl) {
|
|
|
|
sitl->led.num_leds[chan] = num_leds;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-11-17 14:04:48 -04:00
|
|
|
bool RCOutput::set_serial_led_rgb_data(const uint16_t chan, int8_t led, uint8_t red, uint8_t green, uint8_t blue)
|
2019-10-30 07:12:17 -03:00
|
|
|
{
|
|
|
|
if (chan > 15) {
|
2023-11-17 14:04:48 -04:00
|
|
|
return false;
|
2019-10-30 07:12:17 -03:00
|
|
|
}
|
2021-07-30 07:13:59 -03:00
|
|
|
SITL::SIM *sitl = AP::sitl();
|
2020-02-09 06:35:07 -04:00
|
|
|
if (led == -1) {
|
|
|
|
for (uint8_t i=0; i < sitl->led.num_leds[chan]; i++) {
|
2020-02-24 20:54:04 -04:00
|
|
|
set_serial_led_rgb_data(chan, i, red, green, blue);
|
2019-10-30 07:12:17 -03:00
|
|
|
}
|
2023-11-17 14:04:48 -04:00
|
|
|
return true;
|
2020-02-09 06:35:07 -04:00
|
|
|
}
|
|
|
|
if (led < -1 || led >= sitl->led.num_leds[chan]) {
|
2023-11-17 14:04:48 -04:00
|
|
|
return false;
|
2020-02-09 06:35:07 -04:00
|
|
|
}
|
|
|
|
if (sitl) {
|
|
|
|
sitl->led.rgb[chan][led].rgb[0] = red;
|
|
|
|
sitl->led.rgb[chan][led].rgb[1] = green;
|
|
|
|
sitl->led.rgb[chan][led].rgb[2] = blue;
|
2019-10-30 07:12:17 -03:00
|
|
|
}
|
2023-11-17 14:04:48 -04:00
|
|
|
return true;
|
2019-10-30 07:12:17 -03:00
|
|
|
}
|
|
|
|
|
2023-11-17 14:04:48 -04:00
|
|
|
bool RCOutput::serial_led_send(const uint16_t chan)
|
2019-10-30 07:12:17 -03:00
|
|
|
{
|
2021-07-30 07:13:59 -03:00
|
|
|
SITL::SIM *sitl = AP::sitl();
|
2019-10-30 07:12:17 -03:00
|
|
|
if (sitl) {
|
|
|
|
sitl->led.send_counter++;
|
|
|
|
}
|
2023-11-17 14:04:48 -04:00
|
|
|
return true;
|
2019-10-30 07:12:17 -03:00
|
|
|
}
|
|
|
|
|
2020-09-12 16:04:38 -03:00
|
|
|
#endif //CONFIG_HAL_BOARD == HAL_BOARD_SITL
|