|
| 1 | +// this is for emacs file handling -*- mode: c++; indent-tabs-mode: nil -*- |
| 2 | + |
| 3 | +// -- BEGIN LICENSE BLOCK ---------------------------------------------- |
| 4 | +// Copyright 2019 FZI Forschungszentrum Informatik |
| 5 | +// |
| 6 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | +// you may not use this file except in compliance with the License. |
| 8 | +// You may obtain a copy of the License at |
| 9 | +// |
| 10 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +// |
| 12 | +// Unless required by applicable law or agreed to in writing, software |
| 13 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | +// See the License for the specific language governing permissions and |
| 16 | +// limitations under the License. |
| 17 | +// -- END LICENSE BLOCK ------------------------------------------------ |
| 18 | + |
| 19 | +//---------------------------------------------------------------------- |
| 20 | +/*!\file |
| 21 | + * |
| 22 | + * \author Tristan Schnell [email protected] |
| 23 | + * \date 2019-10-30 |
| 24 | + * |
| 25 | + */ |
| 26 | +//---------------------------------------------------------------------- |
| 27 | +#ifndef UR_DRIVER_DATA_FIELD_PUBLISHER_H_INCLUDED |
| 28 | +#define UR_DRIVER_DATA_FIELD_PUBLISHER_H_INCLUDED |
| 29 | + |
| 30 | +#include <realtime_tools/realtime_publisher.h> |
| 31 | +#include <ur_robot_driver/rtde/data_package.h> |
| 32 | + |
| 33 | +namespace ur_driver |
| 34 | +{ |
| 35 | +namespace rtde_interface |
| 36 | +{ |
| 37 | +/*! |
| 38 | + * \brief The DataFieldPublisher class implements an abstract wrapper for various ROS publishers |
| 39 | + * that publish fields of the RTDE data package. In addition, it contains a static factory to |
| 40 | + * create correct publishers for a data field. |
| 41 | + */ |
| 42 | +class DataFieldPublisher |
| 43 | +{ |
| 44 | +public: |
| 45 | + DataFieldPublisher() = default; |
| 46 | + |
| 47 | + /*! |
| 48 | + * \brief Publishes partial information from a data package. |
| 49 | + * |
| 50 | + * \param data_package The given data package to publish from |
| 51 | + * |
| 52 | + * \returns True if the realtime publisher could publish the data. |
| 53 | + */ |
| 54 | + virtual bool publish(const std::unique_ptr<DataPackage>& data_package) = 0; |
| 55 | + |
| 56 | + /*! |
| 57 | + * \brief Creates a DataFieldPublisher object based on a given data field. |
| 58 | + * |
| 59 | + * \param data_field_identifier The name of the data field to publish |
| 60 | + * \param nh The used ROS node handle |
| 61 | + * |
| 62 | + * \returns A unique pointer to the created Publisher object |
| 63 | + */ |
| 64 | + static std::unique_ptr<DataFieldPublisher> createFromString(const std::string& data_field_identifier, |
| 65 | + ros::NodeHandle& nh); |
| 66 | +}; |
| 67 | + |
| 68 | +/*! |
| 69 | + * \brief Implements a publisher that directly publishes a datafield of a given type to a ROS topic |
| 70 | + * of a given message type. |
| 71 | + * |
| 72 | + */ |
| 73 | +template <typename DataT, typename MsgT> |
| 74 | +class DirectDataPublisher : public DataFieldPublisher |
| 75 | +{ |
| 76 | +public: |
| 77 | + /*! |
| 78 | + * \brief Creates a DirectDataPublisher object. |
| 79 | + * |
| 80 | + * \param data_field_identifier The string identifier of the data field to publish |
| 81 | + * \param nh The used ROS node handle |
| 82 | + */ |
| 83 | + DirectDataPublisher(const std::string& data_field_identifier, ros::NodeHandle& nh) |
| 84 | + : data_field_identifier_(data_field_identifier) |
| 85 | + { |
| 86 | + pub_.reset(new realtime_tools::RealtimePublisher<MsgT>(nh, "rtde_data/" + data_field_identifier_, 1)); |
| 87 | + } |
| 88 | + |
| 89 | + /*! |
| 90 | + * \brief Publishes the relevant data field from a data package. |
| 91 | + * |
| 92 | + * \param data_package The given data package to publish from |
| 93 | + * |
| 94 | + * \returns True if the realtime publisher could publish the data. |
| 95 | + */ |
| 96 | + virtual bool publish(const std::unique_ptr<DataPackage>& data_package) |
| 97 | + { |
| 98 | + if (data_package->getData(data_field_identifier_, data_)) |
| 99 | + { |
| 100 | + if (pub_) |
| 101 | + { |
| 102 | + if (pub_->trylock()) |
| 103 | + { |
| 104 | + pub_->msg_.data = data_; |
| 105 | + pub_->unlockAndPublish(); |
| 106 | + return true; |
| 107 | + } |
| 108 | + } |
| 109 | + } |
| 110 | + return false; |
| 111 | + } |
| 112 | + |
| 113 | +private: |
| 114 | + DataT data_; |
| 115 | + std::string data_field_identifier_; |
| 116 | + std::unique_ptr<realtime_tools::RealtimePublisher<MsgT>> pub_; |
| 117 | +}; |
| 118 | + |
| 119 | +/*! |
| 120 | + * \brief Implements a publisher that publishes a datafield containing an array of a given type and size to a ROS topic |
| 121 | + * of a given message type. |
| 122 | + * |
| 123 | + */ |
| 124 | +template <typename DataT, typename MsgT, size_t N> |
| 125 | +class ArrayDataPublisher : public DataFieldPublisher |
| 126 | +{ |
| 127 | +public: |
| 128 | + /*! |
| 129 | + * \brief Creates a DirectDataPublisher object. |
| 130 | + * |
| 131 | + * \param data_field_identifier The string identifier of the data field to publish |
| 132 | + * \param nh The used ROS node handle |
| 133 | + */ |
| 134 | + ArrayDataPublisher(const std::string& data_field_identifier, ros::NodeHandle& nh) |
| 135 | + : data_field_identifier_(data_field_identifier) |
| 136 | + { |
| 137 | + pub_.reset(new realtime_tools::RealtimePublisher<MsgT>(nh, "rtde_data/" + data_field_identifier_, 1)); |
| 138 | + pub_->msg_.data.resize(N); |
| 139 | + } |
| 140 | + |
| 141 | + /*! |
| 142 | + * \brief Publishes the relevant data field from a data package. |
| 143 | + * |
| 144 | + * \param data_package The given data package to publish from |
| 145 | + * |
| 146 | + * \returns True if the realtime publisher could publish the data. |
| 147 | + */ |
| 148 | + virtual bool publish(const std::unique_ptr<DataPackage>& data_package) |
| 149 | + { |
| 150 | + if (data_package->getData(data_field_identifier_, data_)) |
| 151 | + { |
| 152 | + if (pub_) |
| 153 | + { |
| 154 | + if (pub_->trylock()) |
| 155 | + { |
| 156 | + for (size_t i = 0; i < N; i++) |
| 157 | + { |
| 158 | + pub_->msg_.data[i] = data_[i]; |
| 159 | + } |
| 160 | + pub_->unlockAndPublish(); |
| 161 | + return true; |
| 162 | + } |
| 163 | + } |
| 164 | + } |
| 165 | + return false; |
| 166 | + } |
| 167 | + |
| 168 | +private: |
| 169 | + std::array<DataT, N> data_; |
| 170 | + std::string data_field_identifier_; |
| 171 | + std::unique_ptr<realtime_tools::RealtimePublisher<MsgT>> pub_; |
| 172 | +}; |
| 173 | +} // namespace rtde_interface |
| 174 | +} // namespace ur_driver |
| 175 | + |
| 176 | +#endif // ifndef UR_DRIVER_DATA_FIELD_PUBLISHER_H_INCLUDED |
0 commit comments