From f7ee6b3c5e8a47e36f64eb869bd81b8ddedbe3ee Mon Sep 17 00:00:00 2001 From: Daniel Agar Date: Sat, 20 Feb 2021 12:24:15 -0500 Subject: [PATCH] mavlink: move SERVO_OUTPUT_RAW to separate stream header --- src/modules/mavlink/mavlink_messages.cpp | 92 +-------------- .../mavlink/streams/SERVO_OUTPUT_RAW.hpp | 108 ++++++++++++++++++ 2 files changed, 111 insertions(+), 89 deletions(-) create mode 100644 src/modules/mavlink/streams/SERVO_OUTPUT_RAW.hpp diff --git a/src/modules/mavlink/mavlink_messages.cpp b/src/modules/mavlink/mavlink_messages.cpp index 1de240d0f6..69ffce1208 100644 --- a/src/modules/mavlink/mavlink_messages.cpp +++ b/src/modules/mavlink/mavlink_messages.cpp @@ -133,6 +133,7 @@ using matrix::wrap_2pi; #include "streams/RAW_RPM.hpp" #include "streams/RC_CHANNELS.hpp" #include "streams/SCALED_IMU.hpp" +#include "streams/SERVO_OUTPUT_RAW.hpp" #include "streams/STATUSTEXT.hpp" #include "streams/STORAGE_INFORMATION.hpp" #include "streams/TRAJECTORY_REPRESENTATION_WAYPOINTS.hpp" @@ -3138,95 +3139,6 @@ protected: } }; - -template -class MavlinkStreamServoOutputRaw : public MavlinkStream -{ -public: - const char *get_name() const override - { - return MavlinkStreamServoOutputRaw::get_name_static(); - } - - static constexpr uint16_t get_id_static() - { - return MAVLINK_MSG_ID_SERVO_OUTPUT_RAW; - } - - uint16_t get_id() override - { - return get_id_static(); - } - - static constexpr const char *get_name_static() - { - switch (N) { - case 0: - return "SERVO_OUTPUT_RAW_0"; - - case 1: - return "SERVO_OUTPUT_RAW_1"; - } - } - - static MavlinkStream *new_instance(Mavlink *mavlink) - { - return new MavlinkStreamServoOutputRaw(mavlink); - } - - unsigned get_size() override - { - return _act_sub.advertised() ? MAVLINK_MSG_ID_SERVO_OUTPUT_RAW_LEN + MAVLINK_NUM_NON_PAYLOAD_BYTES : 0; - } - -private: - uORB::Subscription _act_sub{ORB_ID(actuator_outputs), N}; - - /* do not allow top copying this class */ - MavlinkStreamServoOutputRaw(MavlinkStreamServoOutputRaw &) = delete; - MavlinkStreamServoOutputRaw &operator = (const MavlinkStreamServoOutputRaw &) = delete; - -protected: - explicit MavlinkStreamServoOutputRaw(Mavlink *mavlink) : MavlinkStream(mavlink) - {} - - bool send() override - { - actuator_outputs_s act; - - if (_act_sub.update(&act)) { - mavlink_servo_output_raw_t msg{}; - - static_assert(sizeof(act.output) / sizeof(act.output[0]) >= 16, "mavlink message requires at least 16 outputs"); - - msg.time_usec = act.timestamp; - msg.port = N; - msg.servo1_raw = act.output[0]; - msg.servo2_raw = act.output[1]; - msg.servo3_raw = act.output[2]; - msg.servo4_raw = act.output[3]; - msg.servo5_raw = act.output[4]; - msg.servo6_raw = act.output[5]; - msg.servo7_raw = act.output[6]; - msg.servo8_raw = act.output[7]; - msg.servo9_raw = act.output[8]; - msg.servo10_raw = act.output[9]; - msg.servo11_raw = act.output[10]; - msg.servo12_raw = act.output[11]; - msg.servo13_raw = act.output[12]; - msg.servo14_raw = act.output[13]; - msg.servo15_raw = act.output[14]; - msg.servo16_raw = act.output[15]; - - mavlink_msg_servo_output_raw_send_struct(_mavlink->get_channel(), &msg); - - return true; - } - - return false; - } -}; - class MavlinkStreamCameraCapture : public MavlinkStream { public: @@ -3347,8 +3259,10 @@ static const StreamListItem streams_list[] = { create_stream_list_item(), #endif create_stream_list_item(), +#if defined(SERVO_OUTPUT_RAW_HPP) create_stream_list_item >(), create_stream_list_item >(), +#endif // SERVO_OUTPUT_RAW_HPP #if defined(HIL_ACTUATOR_CONTROLS_HPP) create_stream_list_item(), #endif // HIL_ACTUATOR_CONTROLS_HPP diff --git a/src/modules/mavlink/streams/SERVO_OUTPUT_RAW.hpp b/src/modules/mavlink/streams/SERVO_OUTPUT_RAW.hpp new file mode 100644 index 0000000000..89b8918acc --- /dev/null +++ b/src/modules/mavlink/streams/SERVO_OUTPUT_RAW.hpp @@ -0,0 +1,108 @@ +/**************************************************************************** + * + * 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. + * + ****************************************************************************/ + +#ifndef SERVO_OUTPUT_RAW_HPP +#define SERVO_OUTPUT_RAW_HPP + +#include + +template +class MavlinkStreamServoOutputRaw : public MavlinkStream +{ +public: + static MavlinkStream *new_instance(Mavlink *mavlink) { return new MavlinkStreamServoOutputRaw(mavlink); } + + static constexpr const char *get_name_static() + { + switch (N) { + case 0: + return "SERVO_OUTPUT_RAW_0"; + + case 1: + return "SERVO_OUTPUT_RAW_1"; + } + } + + static constexpr uint16_t get_id_static() { return MAVLINK_MSG_ID_SERVO_OUTPUT_RAW; } + + const char *get_name() const override { return get_name_static(); } + uint16_t get_id() override { return get_id_static(); } + + unsigned get_size() override + { + return _act_sub.advertised() ? MAVLINK_MSG_ID_SERVO_OUTPUT_RAW_LEN + MAVLINK_NUM_NON_PAYLOAD_BYTES : 0; + } + +private: + explicit MavlinkStreamServoOutputRaw(Mavlink *mavlink) : MavlinkStream(mavlink) {} + + uORB::Subscription _act_sub{ORB_ID(actuator_outputs), N}; + + bool send() override + { + actuator_outputs_s act; + + if (_act_sub.update(&act)) { + mavlink_servo_output_raw_t msg{}; + + static_assert(sizeof(act.output) / sizeof(act.output[0]) >= 16, "mavlink message requires at least 16 outputs"); + + msg.time_usec = act.timestamp; + msg.port = N; + msg.servo1_raw = act.output[0]; + msg.servo2_raw = act.output[1]; + msg.servo3_raw = act.output[2]; + msg.servo4_raw = act.output[3]; + msg.servo5_raw = act.output[4]; + msg.servo6_raw = act.output[5]; + msg.servo7_raw = act.output[6]; + msg.servo8_raw = act.output[7]; + msg.servo9_raw = act.output[8]; + msg.servo10_raw = act.output[9]; + msg.servo11_raw = act.output[10]; + msg.servo12_raw = act.output[11]; + msg.servo13_raw = act.output[12]; + msg.servo14_raw = act.output[13]; + msg.servo15_raw = act.output[14]; + msg.servo16_raw = act.output[15]; + + mavlink_msg_servo_output_raw_send_struct(_mavlink->get_channel(), &msg); + + return true; + } + + return false; + } +}; + +#endif // SERVO_OUTPUT_RAW_HPP