From 3a3f04c0f43c9f777b8257a0a5b37ce5e3c0ad48 Mon Sep 17 00:00:00 2001 From: Daniel Agar Date: Wed, 17 Jul 2024 13:14:06 -0400 Subject: [PATCH] drivers/rc: new standalone ghst_rc driver - extracted from monolithic drivers/rc_input which will eventually be dropped once all drivers migrated and boards updated --- src/drivers/rc/ghst_rc/CMakeLists.txt | 45 ++++ src/drivers/rc/ghst_rc/GhstRc.cpp | 304 ++++++++++++++++++++++ src/drivers/rc/ghst_rc/GhstRc.hpp | 104 ++++++++ src/drivers/rc/ghst_rc/Kconfig | 5 + src/drivers/rc/ghst_rc/ghst_telemetry.cpp | 139 ++++++++++ src/drivers/rc/ghst_rc/ghst_telemetry.hpp | 91 +++++++ src/drivers/rc/ghst_rc/module.yaml | 22 ++ 7 files changed, 710 insertions(+) create mode 100644 src/drivers/rc/ghst_rc/CMakeLists.txt create mode 100644 src/drivers/rc/ghst_rc/GhstRc.cpp create mode 100644 src/drivers/rc/ghst_rc/GhstRc.hpp create mode 100644 src/drivers/rc/ghst_rc/Kconfig create mode 100644 src/drivers/rc/ghst_rc/ghst_telemetry.cpp create mode 100644 src/drivers/rc/ghst_rc/ghst_telemetry.hpp create mode 100644 src/drivers/rc/ghst_rc/module.yaml diff --git a/src/drivers/rc/ghst_rc/CMakeLists.txt b/src/drivers/rc/ghst_rc/CMakeLists.txt new file mode 100644 index 0000000000..2a0342584f --- /dev/null +++ b/src/drivers/rc/ghst_rc/CMakeLists.txt @@ -0,0 +1,45 @@ +############################################################################ +# +# Copyright (c) 2015-2024 PX4 Development Team. All rights reserved. +# +# Redistribution and use in source and binary forms, with or without +# modification, are permitted provided that the following conditions +# are met: +# +# 1. Redistributions of source code must retain the above copyright +# notice, this list of conditions and the following disclaimer. +# 2. Redistributions in binary form must reproduce the above copyright +# notice, this list of conditions and the following disclaimer in +# the documentation and/or other materials provided with the +# distribution. +# 3. Neither the name PX4 nor the names of its contributors may be +# used to endorse or promote products derived from this software +# without specific prior written permission. +# +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS +# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +# COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, +# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS +# OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED +# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT +# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN +# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +# POSSIBILITY OF SUCH DAMAGE. +# +############################################################################ +px4_add_module( + MODULE drivers__rc__ghst_rc + MAIN ghst_rc + COMPILE_FLAGS + SRCS + GhstRc.cpp + GhstRc.hpp + ghst_telemetry.cpp + MODULE_CONFIG + module.yaml + DEPENDS + rc + ) diff --git a/src/drivers/rc/ghst_rc/GhstRc.cpp b/src/drivers/rc/ghst_rc/GhstRc.cpp new file mode 100644 index 0000000000..1960c84ce6 --- /dev/null +++ b/src/drivers/rc/ghst_rc/GhstRc.cpp @@ -0,0 +1,304 @@ +/**************************************************************************** + * + * Copyright (c) 2012-2024 PX4 Development Team. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * 3. Neither the name PX4 nor the names of its contributors may be + * used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN + * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * + ****************************************************************************/ + +#include "GhstRc.hpp" + +#include + +GhstRc::GhstRc(const char *device) : + ModuleParams(nullptr), + ScheduledWorkItem(MODULE_NAME, px4::serial_port_to_wq(device)), + _cycle_perf(perf_alloc(PC_ELAPSED, MODULE_NAME": cycle time")), + _publish_interval_perf(perf_alloc(PC_INTERVAL, MODULE_NAME": publish interval")) +{ + if (device) { + strncpy(_device, device, sizeof(_device) - 1); + _device[sizeof(_device) - 1] = '\0'; + } +} + +GhstRc::~GhstRc() +{ + delete _ghst_telemetry; + + perf_free(_cycle_perf); + perf_free(_publish_interval_perf); +} + +int GhstRc::task_spawn(int argc, char *argv[]) +{ + bool error_flag = false; + + int myoptind = 1; + int ch; + const char *myoptarg = nullptr; + const char *device_name = nullptr; + bool silent = false; + + while ((ch = px4_getopt(argc, argv, "d:", &myoptind, &myoptarg)) != EOF) { + switch (ch) { + case 'd': + device_name = myoptarg; + silent = false; + break; + + case '?': + error_flag = true; + break; + + default: + PX4_WARN("unrecognized flag"); + error_flag = true; + break; + } + } + + if (error_flag) { + return -1; + } + + if (device_name && (access(device_name, R_OK | W_OK) == 0)) { + GhstRc *instance = new GhstRc(device_name); + + if (instance == nullptr) { + PX4_ERR("alloc failed"); + return PX4_ERROR; + } + + _object.store(instance); + _task_id = task_id_is_work_queue; + + instance->ScheduleOnInterval(_current_update_interval); + + return PX4_OK; + + } else if (silent) { + return PX4_OK; + + } else { + if (device_name) { + PX4_ERR("invalid device (-d) %s", device_name); + + } else { + PX4_ERR("valid device required"); + } + } + + return PX4_ERROR; +} + +void GhstRc::Run() +{ + if (should_exit()) { + + close(_rcs_fd); + + exit_and_cleanup(); + return; + } + + perf_begin(_cycle_perf); + + // Check if parameters have changed + if (_parameter_update_sub.updated()) { + // clear update + parameter_update_s param_update; + _parameter_update_sub.copy(¶m_update); + + updateParams(); + } + + const hrt_abstime cycle_timestamp = hrt_absolute_time(); + + bool rc_updated = false; + + constexpr hrt_abstime rc_scan_max = 3_s; + + // read all available data from the serial RC input UART + static constexpr size_t RC_MAX_BUFFER_SIZE{64}; + uint8_t rcs_buf[RC_MAX_BUFFER_SIZE] {}; + int newBytes = ::read(_rcs_fd, &rcs_buf[0], RC_MAX_BUFFER_SIZE); + + if (newBytes > 0) { + _bytes_rx += newBytes; + } + + const bool rc_scan_locked = _rc_scan_locked; + + if (_rc_scan_begin == 0) { + _rc_scan_begin = cycle_timestamp; + + // Configure serial port + if (_rcs_fd < 0) { + _rcs_fd = open(_device, O_RDWR | O_NONBLOCK); + } + + ghst_config(_rcs_fd); + + // flush serial buffer and any existing buffered data + tcflush(_rcs_fd, TCIOFLUSH); + + } else if (_rc_scan_locked + || cycle_timestamp - _rc_scan_begin < rc_scan_max) { + + if (newBytes > 0) { + uint16_t raw_rc_values[input_rc_s::RC_INPUT_MAX_CHANNELS] {}; + uint16_t raw_rc_count = 0; + int8_t ghst_rssi = -1; + + if (ghst_parse(cycle_timestamp, &rcs_buf[0], newBytes, &raw_rc_values[0], &ghst_rssi, + &raw_rc_count, input_rc_s::RC_INPUT_MAX_CHANNELS) + ) { + // we have a new GHST frame. Publish it. + input_rc_s input_rc{}; + input_rc.timestamp_last_signal = cycle_timestamp; + input_rc.channel_count = math::constrain(raw_rc_count, (uint16_t)0, (uint16_t)input_rc_s::RC_INPUT_MAX_CHANNELS); + input_rc.rssi = ghst_rssi; + input_rc.input_source = input_rc_s::RC_INPUT_SOURCE_PX4FMU_GHST; + + unsigned valid_chans = 0; + + for (unsigned i = 0; i < input_rc.channel_count; i++) { + input_rc.values[i] = raw_rc_values[i]; + + if (raw_rc_values[i] != UINT16_MAX) { + valid_chans++; + } + } + + input_rc.channel_count = valid_chans; + + if (valid_chans == 0) { + input_rc.rssi = 0; + } + + input_rc.rc_lost = (valid_chans == 0); + + input_rc.link_quality = -1; + input_rc.rssi_dbm = NAN; + + input_rc.timestamp = hrt_absolute_time(); + _input_rc_pub.publish(input_rc); + perf_count(_publish_interval_perf); + + _timestamp_last_signal = cycle_timestamp; + rc_updated = true; + + if (valid_chans > 0) { + _rc_scan_locked = true; + } + + if (!_rc_scan_locked && !_ghst_telemetry && _param_rc_ghst_tel_en.get()) { + _ghst_telemetry = new GHSTTelemetry(_rcs_fd); + } + + if (_ghst_telemetry) { + _ghst_telemetry->update(cycle_timestamp); + } + } + } + + } else { + _rc_scan_begin = 0; + _rc_scan_locked = false; + + close(_rcs_fd); + _rcs_fd = -1; + } + + if (!rc_updated && (hrt_elapsed_time(&_timestamp_last_signal) > 1_s)) { + _rc_scan_locked = false; + } + + if (!rc_scan_locked && _rc_scan_locked) { + PX4_INFO("RC input locked"); + } + + perf_end(_cycle_perf); +} + +int GhstRc::custom_command(int argc, char *argv[]) +{ + if (!is_running()) { + int ret = GhstRc::task_spawn(argc, argv); + + if (ret) { + return ret; + } + } + + return print_usage("unknown command"); +} + +int GhstRc::print_status() +{ + PX4_INFO("Max update rate: %u Hz", 1000000 / _current_update_interval); + + if (_device[0] != '\0') { + PX4_INFO("UART device: %s", _device); + PX4_INFO("UART RX bytes: %" PRIu32, _bytes_rx); + } + + PX4_INFO("RC state: %s", _rc_scan_locked ? "found" : "searching for signal"); + + perf_print_counter(_cycle_perf); + perf_print_counter(_publish_interval_perf); + + return 0; +} + +int GhstRc::print_usage(const char *reason) +{ + if (reason) { + PX4_WARN("%s\n", reason); + } + + PRINT_MODULE_DESCRIPTION( + R"DESCR_STR( +### Description +This module does Ghost (GHST) RC input parsing. + +)DESCR_STR"); + + PRINT_MODULE_USAGE_NAME("ghst_rc", "driver"); + PRINT_MODULE_USAGE_COMMAND("start"); + PRINT_MODULE_USAGE_PARAM_STRING('d', "/dev/ttyS3", "", "RC device", true); + PRINT_MODULE_USAGE_DEFAULT_COMMANDS(); + + return 0; +} + +extern "C" __EXPORT int ghst_rc_main(int argc, char *argv[]) +{ + return GhstRc::main(argc, argv); +} diff --git a/src/drivers/rc/ghst_rc/GhstRc.hpp b/src/drivers/rc/ghst_rc/GhstRc.hpp new file mode 100644 index 0000000000..0090b216dc --- /dev/null +++ b/src/drivers/rc/ghst_rc/GhstRc.hpp @@ -0,0 +1,104 @@ +/**************************************************************************** + * + * Copyright (c) 2012-2024 PX4 Development Team. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * 3. Neither the name PX4 nor the names of its contributors may be + * used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN + * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * + ****************************************************************************/ + +#pragma once + +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "ghst_telemetry.hpp" + +using namespace time_literals; + +class GhstRc : public ModuleBase, public ModuleParams, public px4::ScheduledWorkItem +{ +public: + + GhstRc(const char *device); + virtual ~GhstRc(); + + /** @see ModuleBase */ + static int task_spawn(int argc, char *argv[]); + + /** @see ModuleBase */ + static int custom_command(int argc, char *argv[]); + + /** @see ModuleBase */ + static int print_usage(const char *reason = nullptr); + + /** @see ModuleBase::print_status() */ + int print_status() override; + +private: + + void Run() override; + + hrt_abstime _rc_scan_begin{0}; + hrt_abstime _timestamp_last_signal{0}; + + bool _rc_scan_locked{false}; + + static constexpr unsigned _current_update_interval{4000}; // 250 Hz + + uORB::SubscriptionInterval _parameter_update_sub{ORB_ID(parameter_update), 1_s}; + + uORB::PublicationMulti _input_rc_pub{ORB_ID(input_rc)}; + + int _rcs_fd{-1}; + char _device[20] {}; ///< device / serial port path + + GHSTTelemetry *_ghst_telemetry{nullptr}; + + perf_counter_t _cycle_perf; + perf_counter_t _publish_interval_perf; + uint32_t _bytes_rx{0}; + + DEFINE_PARAMETERS( + (ParamBool) _param_rc_ghst_tel_en + ) +}; diff --git a/src/drivers/rc/ghst_rc/Kconfig b/src/drivers/rc/ghst_rc/Kconfig new file mode 100644 index 0000000000..fbff9b26d4 --- /dev/null +++ b/src/drivers/rc/ghst_rc/Kconfig @@ -0,0 +1,5 @@ +menuconfig DRIVERS_RC_GHST_RC + bool "ghst_rc" + default n + ---help--- + Enable support for ghst_rc diff --git a/src/drivers/rc/ghst_rc/ghst_telemetry.cpp b/src/drivers/rc/ghst_rc/ghst_telemetry.cpp new file mode 100644 index 0000000000..47c1c21fa5 --- /dev/null +++ b/src/drivers/rc/ghst_rc/ghst_telemetry.cpp @@ -0,0 +1,139 @@ +/**************************************************************************** + * + * Copyright (c) 2021 PX4 Development Team. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * 3. Neither the name PX4 nor the names of its contributors may be + * used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN + * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * + ****************************************************************************/ + +/** + * @file ghst_telemetry.cpp + * + * IRC Ghost (Immersion RC Ghost) telemetry. + * + * @author Igor Misic + * @author Juraj Ciberlin + */ + +#include "ghst_telemetry.hpp" +#include + +using time_literals::operator ""_s; + +GHSTTelemetry::GHSTTelemetry(int uart_fd) : + _uart_fd(uart_fd) +{ +} + +bool GHSTTelemetry::update(const hrt_abstime &now) +{ + bool success = false; + + if ((now - _last_update) > (1_s / (UPDATE_RATE_HZ * NUM_DATA_TYPES))) { + + switch (_next_type) { + case 0U: + success = send_battery_status(); + break; + + case 1U: + success = send_gps1_status(); + break; + + case 2U: + success = send_gps2_status(); + break; + + default: + success = false; + break; + } + + _last_update = now; + _next_type = (_next_type + 1U) % NUM_DATA_TYPES; + } + + return success; +} + +bool GHSTTelemetry::send_battery_status() +{ + bool success = false; + float voltage_in_10mV; + float current_in_10mA; + float fuel_in_10mAh; + battery_status_s battery_status; + + if (_battery_status_sub.update(&battery_status)) { + voltage_in_10mV = battery_status.voltage_v * FACTOR_VOLTS_TO_10MV; + current_in_10mA = battery_status.current_a * FACTOR_AMPS_TO_10MA; + fuel_in_10mAh = battery_status.discharged_mah * FACTOR_MAH_TO_10MAH; + success = ghst_send_telemetry_battery_status(_uart_fd, + static_cast(voltage_in_10mV), + static_cast(current_in_10mA), + static_cast(fuel_in_10mAh)); + } + + return success; +} + +bool GHSTTelemetry::send_gps1_status() +{ + sensor_gps_s vehicle_gps_position; + + if (!_vehicle_gps_position_sub.update(&vehicle_gps_position)) { + return false; + } + + int32_t latitude = static_cast(round(vehicle_gps_position.latitude_deg * 1e7)); // 1e-7 degrees + int32_t longitude = static_cast(round(vehicle_gps_position.longitude_deg * 1e7)); // 1e-7 degrees + uint16_t altitude = static_cast(round(vehicle_gps_position.altitude_msl_m)); // meters + + return ghst_send_telemetry_gps1_status(_uart_fd, latitude, longitude, altitude); +} + +bool GHSTTelemetry::send_gps2_status() +{ + sensor_gps_s vehicle_gps_position; + + if (!_vehicle_gps_position_sub.update(&vehicle_gps_position)) { + return false; + } + + uint16_t ground_speed = (uint16_t)(vehicle_gps_position.vel_d_m_s / 3.6f * 10.f); + uint16_t ground_course = (uint16_t)(math::degrees(vehicle_gps_position.cog_rad) * 100.f); + uint8_t num_sats = vehicle_gps_position.satellites_used; + + // TBD: Can these be computed in a RC telemetry driver? + uint16_t home_dist = 0; + uint16_t home_dir = 0; + uint8_t flags = 0; + + return ghst_send_telemetry_gps2_status(_uart_fd, ground_speed, ground_course, num_sats, home_dist, home_dir, flags); +} + diff --git a/src/drivers/rc/ghst_rc/ghst_telemetry.hpp b/src/drivers/rc/ghst_rc/ghst_telemetry.hpp new file mode 100644 index 0000000000..faa61f69b2 --- /dev/null +++ b/src/drivers/rc/ghst_rc/ghst_telemetry.hpp @@ -0,0 +1,91 @@ +/**************************************************************************** + * + * Copyright (c) 2021 PX4 Development Team. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * 3. Neither the name PX4 nor the names of its contributors may be + * used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN + * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * + ****************************************************************************/ + +/** + * @file ghst_telemetry.cpp + * + * IRC Ghost (Immersion RC Ghost) telemetry. + * + * @author Igor Misic + * @author Juraj Ciberlin + */ + +#pragma once + +#include +#include +#include +#include + +/** + * High-level class that handles sending of GHST telemetry data + */ +class GHSTTelemetry +{ +public: + /** + * @param uart_fd file descriptor for the UART to use. It is expected to be configured + * already. + */ + explicit GHSTTelemetry(int uart_fd); + + ~GHSTTelemetry() = default; + + /** + * Send telemetry data. Call this regularly (i.e. at 100Hz), it will automatically + * limit the sending rate. + * @return true if new data sent + */ + bool update(const hrt_abstime &now); + +private: + bool send_battery_status(); + bool send_gps1_status(); + bool send_gps2_status(); + + uORB::Subscription _vehicle_gps_position_sub{ORB_ID(vehicle_gps_position)}; + uORB::Subscription _battery_status_sub{ORB_ID(battery_status)}; + + int _uart_fd; + hrt_abstime _last_update {0U}; + uint32_t _next_type {0U}; + + static constexpr uint32_t NUM_DATA_TYPES {3U}; // number of different telemetry data types + static constexpr uint32_t UPDATE_RATE_HZ {10U}; // update rate [Hz] + + // Factors that should be applied to get correct values + static constexpr float FACTOR_VOLTS_TO_10MV {100.0F}; + static constexpr float FACTOR_AMPS_TO_10MA {100.0F}; + static constexpr float FACTOR_MAH_TO_10MAH {0.1F}; + +}; diff --git a/src/drivers/rc/ghst_rc/module.yaml b/src/drivers/rc/ghst_rc/module.yaml new file mode 100644 index 0000000000..2e15a42d97 --- /dev/null +++ b/src/drivers/rc/ghst_rc/module.yaml @@ -0,0 +1,22 @@ +module_name: GHST RC Input Driver +serial_config: + - command: "ghst_rc start -d ${SERIAL_DEV}" + port_config_param: + name: RC_GHST_PRT_CFG + group: Serial + #default: RC + #depends_on_port: RC + description_extended: | + Ghost (GHST) RC driver. + +parameters: + - group: RC + definitions: + RC_GHST_TEL_EN: + description: + short: Ghost RC telemetry enable + long: | + Ghost telemetry enable + + type: boolean + default: [0]