|
| 1 | +/**************************************************************************** |
| 2 | + * |
| 3 | + * Copyright (c) 2026 PX4 Development Team. All rights reserved. |
| 4 | + * |
| 5 | + * Redistribution and use in source and binary forms, with or without |
| 6 | + * modification, are permitted provided that the following conditions |
| 7 | + * are met: |
| 8 | + * |
| 9 | + * 1. Redistributions of source code must retain the above copyright |
| 10 | + * notice, this list of conditions and the following disclaimer. |
| 11 | + * 2. Redistributions in binary form must reproduce the above copyright |
| 12 | + * notice, this list of conditions and the following disclaimer in |
| 13 | + * the documentation and/or other materials provided with the |
| 14 | + * distribution. |
| 15 | + * 3. Neither the name PX4 nor the names of its contributors may be |
| 16 | + * used to endorse or promote products derived from this software |
| 17 | + * without specific prior written permission. |
| 18 | + * |
| 19 | + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 20 | + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 21 | + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
| 22 | + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
| 23 | + * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 24 | + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
| 25 | + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS |
| 26 | + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED |
| 27 | + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 28 | + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN |
| 29 | + * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 30 | + * POSSIBILITY OF SUCH DAMAGE. |
| 31 | + * |
| 32 | + ****************************************************************************/ |
| 33 | + |
| 34 | +#pragma once |
| 35 | + |
| 36 | +#include "UavcanSubscriberBase.hpp" |
| 37 | + |
| 38 | +#include <uavcan/equipment/hardpoint/Command.hpp> |
| 39 | + |
| 40 | +#include <uORB/Publication.hpp> |
| 41 | +#include <uORB/topics/actuator_servos.h> |
| 42 | + |
| 43 | +namespace uavcannode |
| 44 | +{ |
| 45 | + |
| 46 | +class HardpointCommand; |
| 47 | + |
| 48 | +typedef uavcan::MethodBinder<HardpointCommand *, |
| 49 | + void (HardpointCommand::*)(const uavcan::ReceivedDataStructure<uavcan::equipment::hardpoint::Command>&)> |
| 50 | + HardpointCommandBinder; |
| 51 | + |
| 52 | +class HardpointCommand : |
| 53 | + public UavcanSubscriberBase, |
| 54 | + private uavcan::Subscriber<uavcan::equipment::hardpoint::Command, HardpointCommandBinder> |
| 55 | +{ |
| 56 | +public: |
| 57 | + HardpointCommand(uavcan::INode &node) : |
| 58 | + UavcanSubscriberBase(uavcan::equipment::hardpoint::Command::DefaultDataTypeID), |
| 59 | + uavcan::Subscriber<uavcan::equipment::hardpoint::Command, HardpointCommandBinder>(node) |
| 60 | + {} |
| 61 | + |
| 62 | + bool init() |
| 63 | + { |
| 64 | + if (start(HardpointCommandBinder(this, &HardpointCommand::callback)) < 0) { |
| 65 | + PX4_ERR("uavcan::equipment::hardpoint::Command subscription failed"); |
| 66 | + return false; |
| 67 | + } |
| 68 | + |
| 69 | + return true; |
| 70 | + } |
| 71 | + |
| 72 | + void PrintInfo() const override |
| 73 | + { |
| 74 | + printf("\t%s:%d -> %s\n", |
| 75 | + uavcan::equipment::hardpoint::Command::getDataTypeFullName(), |
| 76 | + uavcan::equipment::hardpoint::Command::DefaultDataTypeID, |
| 77 | + _actuator_servos_pub.get_topic()->o_name); |
| 78 | + } |
| 79 | + |
| 80 | +private: |
| 81 | + void callback(const uavcan::ReceivedDataStructure<uavcan::equipment::hardpoint::Command> &msg) |
| 82 | + { |
| 83 | + |
| 84 | + uint8_t servo_id = msg.hardpoint_id; |
| 85 | + actuator_servos_s actuator_servos {}; |
| 86 | + |
| 87 | + if (servo_id >= actuator_servos_s::NUM_CONTROLS) { |
| 88 | + return; |
| 89 | + } |
| 90 | + |
| 91 | + for (uint8_t i = servo_id; i < actuator_servos_s::NUM_CONTROLS; ++i) { |
| 92 | + |
| 93 | + actuator_servos.timestamp = hrt_absolute_time(); |
| 94 | + actuator_servos.timestamp_sample = actuator_servos.timestamp; |
| 95 | + |
| 96 | + if (msg.command == 1) { |
| 97 | + actuator_servos.control[i] = 1; // grip |
| 98 | + |
| 99 | + } else if (msg.command == 0) { |
| 100 | + actuator_servos.control[i] = -1; // release |
| 101 | + |
| 102 | + } else { |
| 103 | + actuator_servos.control[i] = 0; // do nothing |
| 104 | + } |
| 105 | + |
| 106 | + actuator_servos.timestamp = hrt_absolute_time(); |
| 107 | + actuator_servos.timestamp_sample = actuator_servos.timestamp; |
| 108 | + |
| 109 | + // If ID is not 0 (broadcast), do not iterate |
| 110 | + if (servo_id != 0) { |
| 111 | + break; |
| 112 | + } |
| 113 | + |
| 114 | + } |
| 115 | + |
| 116 | + _actuator_servos_pub.publish(actuator_servos); |
| 117 | + |
| 118 | + } |
| 119 | + |
| 120 | + uORB::Publication<actuator_servos_s> _actuator_servos_pub{ORB_ID(actuator_servos)}; |
| 121 | + |
| 122 | +}; |
| 123 | +} // namespace uavcannode |
0 commit comments