From 1ddfd61d34bdf3d8e8ea164c1a9d62b1bf8266ad Mon Sep 17 00:00:00 2001 From: Daniel Agar Date: Wed, 7 Oct 2020 15:49:09 -0400 Subject: [PATCH] mavlink: move ESC_STATUS stream to separate file --- src/modules/mavlink/mavlink_messages.cpp | 89 +------------------- src/modules/mavlink/streams/ESC_STATUS.hpp | 94 ++++++++++++++++++++++ 2 files changed, 97 insertions(+), 86 deletions(-) create mode 100644 src/modules/mavlink/streams/ESC_STATUS.hpp diff --git a/src/modules/mavlink/mavlink_messages.cpp b/src/modules/mavlink/mavlink_messages.cpp index 2a03d4a53e..9767899790 100644 --- a/src/modules/mavlink/mavlink_messages.cpp +++ b/src/modules/mavlink/mavlink_messages.cpp @@ -119,6 +119,7 @@ using matrix::Vector3f; using matrix::wrap_2pi; #include "streams/AUTOPILOT_VERSION.hpp" +#include "streams/ESC_STATUS.hpp" #include "streams/EXTENDED_SYS_STATE.hpp" #include "streams/FLIGHT_INFORMATION.hpp" #include "streams/PROTOCOL_VERSION.hpp" @@ -5381,92 +5382,6 @@ protected: } }; -class MavlinkStreamESCStatus : public MavlinkStream -{ -public: - const char *get_name() const override - { - return MavlinkStreamESCStatus::get_name_static(); - } - - static constexpr const char *get_name_static() - { - return "ESC_STATUS"; - } - - static constexpr uint16_t get_id_static() - { - return MAVLINK_MSG_ID_ESC_STATUS; - } - - uint16_t get_id() override - { - return get_id_static(); - } - - static MavlinkStream *new_instance(Mavlink *mavlink) - { - return new MavlinkStreamESCStatus(mavlink); - } - - unsigned get_size() override - { - static constexpr unsigned size_per_batch = MAVLINK_MSG_ID_ESC_STATUS_LEN + MAVLINK_NUM_NON_PAYLOAD_BYTES; - return _esc_status_sub.advertised() ? size_per_batch * _number_of_batches : 0; - } - -private: - uORB::Subscription _esc_status_sub{ORB_ID(esc_status)}; - - /* do not allow top copying this class */ - MavlinkStreamESCStatus(MavlinkStreamESCStatus &) = delete; - MavlinkStreamESCStatus &operator = (const MavlinkStreamESCStatus &) = delete; - -protected: - - uint8_t _number_of_batches{0}; - - explicit MavlinkStreamESCStatus(Mavlink *mavlink) : MavlinkStream(mavlink) - {} - - bool send(const hrt_abstime t) override - { - - esc_status_s esc_status; - uint8_t batch_size = MAVLINK_MSG_ESC_STATUS_FIELD_RPM_LEN; - - if (_esc_status_sub.update(&esc_status)) { - - mavlink_esc_status_t msg = {}; - - msg.time_usec = esc_status.timestamp; - - // Ceil value of integer division. For 1-4 esc => 1 batch, 5-8 esc => 2 batches etc - _number_of_batches = ceilf((float)esc_status.esc_count / batch_size); - - for (int batch_number = 0; batch_number < _number_of_batches; batch_number++) { - - msg.index = batch_number * batch_size; - - for (int esc_index = 0; esc_index < batch_size ; esc_index++) { - - msg.rpm[esc_index] = esc_status.esc[esc_index].esc_rpm; - msg.voltage[esc_index] = esc_status.esc[esc_index].esc_voltage; - msg.current[esc_index] = esc_status.esc[esc_index].esc_current; - } - - mavlink_msg_esc_status_send_struct(_mavlink->get_channel(), &msg); - - } - - return true; - } - - return false; - } -}; - - static const StreamListItem streams_list[] = { create_stream_list_item(), create_stream_list_item(), @@ -5531,7 +5446,9 @@ static const StreamListItem streams_list[] = { create_stream_list_item(), create_stream_list_item(), create_stream_list_item(), +#if defined(ESC_STATUS_HPP) create_stream_list_item(), +#endif // ESC_STATUS_HPP #if defined(AUTOPILOT_VERSION_HPP) create_stream_list_item(), #endif // AUTOPILOT_VERSION_HPP diff --git a/src/modules/mavlink/streams/ESC_STATUS.hpp b/src/modules/mavlink/streams/ESC_STATUS.hpp new file mode 100644 index 0000000000..94016f375f --- /dev/null +++ b/src/modules/mavlink/streams/ESC_STATUS.hpp @@ -0,0 +1,94 @@ +/**************************************************************************** + * + * Copyright (c) 2020 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. + * + ****************************************************************************/ + +#ifndef ESC_STATUS_HPP +#define ESC_STATUS_HPP + +#include + +class MavlinkStreamESCStatus : public MavlinkStream +{ +public: + static MavlinkStream *new_instance(Mavlink *mavlink) { return new MavlinkStreamESCStatus(mavlink); } + + static constexpr const char *get_name_static() { return "ESC_STATUS"; } + static constexpr uint16_t get_id_static() { return MAVLINK_MSG_ID_ESC_STATUS; } + + const char *get_name() const override { return get_name_static(); } + uint16_t get_id() override { return get_id_static(); } + + unsigned get_size() override + { + static constexpr unsigned size_per_batch = MAVLINK_MSG_ID_ESC_STATUS_LEN + MAVLINK_NUM_NON_PAYLOAD_BYTES; + return _esc_status_sub.advertised() ? size_per_batch * _number_of_batches : 0; + } + +private: + explicit MavlinkStreamESCStatus(Mavlink *mavlink) : MavlinkStream(mavlink) {} + + uORB::Subscription _esc_status_sub{ORB_ID(esc_status)}; + uint8_t _number_of_batches{0}; + + bool send(const hrt_abstime t) override + { + static constexpr uint8_t batch_size = MAVLINK_MSG_ESC_STATUS_FIELD_RPM_LEN; + esc_status_s esc_status; + + if (_esc_status_sub.update(&esc_status)) { + mavlink_esc_status_t msg{}; + + msg.time_usec = esc_status.timestamp; + + // Ceil value of integer division. For 1-4 esc => 1 batch, 5-8 esc => 2 batches etc + _number_of_batches = ceilf((float)esc_status.esc_count / batch_size); + + for (int batch_number = 0; batch_number < _number_of_batches; batch_number++) { + msg.index = batch_number * batch_size; + + for (int esc_index = 0; esc_index < batch_size ; esc_index++) { + msg.rpm[esc_index] = esc_status.esc[esc_index].esc_rpm; + msg.voltage[esc_index] = esc_status.esc[esc_index].esc_voltage; + msg.current[esc_index] = esc_status.esc[esc_index].esc_current; + } + + mavlink_msg_esc_status_send_struct(_mavlink->get_channel(), &msg); + } + + return true; + } + + return false; + } +}; + +#endif // ESC_STATUS_HPP