|
| 1 | +// Copyright 2021 ros2_control Development Team |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +#include "ros2_control_demo_hardware/diffbot_system.hpp" |
| 16 | + |
| 17 | +#include <chrono> |
| 18 | +#include <cmath> |
| 19 | +#include <limits> |
| 20 | +#include <memory> |
| 21 | +#include <vector> |
| 22 | + |
| 23 | +#include "hardware_interface/types/hardware_interface_type_values.hpp" |
| 24 | +#include "rclcpp/rclcpp.hpp" |
| 25 | + |
| 26 | +namespace ros2_control_demo_hardware |
| 27 | +{ |
| 28 | +hardware_interface::return_type DiffBotSystemHardware::configure( |
| 29 | + const hardware_interface::HardwareInfo & info) |
| 30 | +{ |
| 31 | + if (configure_default(info) != hardware_interface::return_type::OK) |
| 32 | + { |
| 33 | + return hardware_interface::return_type::ERROR; |
| 34 | + } |
| 35 | + |
| 36 | + hw_start_sec_ = stod(info_.hardware_parameters["example_param_hw_start_duration_sec"]); |
| 37 | + hw_stop_sec_ = stod(info_.hardware_parameters["example_param_hw_stop_duration_sec"]); |
| 38 | + hw_states_.resize(info_.joints.size(), std::numeric_limits<double>::quiet_NaN()); |
| 39 | + hw_commands_.resize(info_.joints.size(), std::numeric_limits<double>::quiet_NaN()); |
| 40 | + |
| 41 | + for (const hardware_interface::ComponentInfo & joint : info_.joints) |
| 42 | + { |
| 43 | + // DiffBotSystem has exactly two states and one command interface on each joint |
| 44 | + if (joint.command_interfaces.size() != 1) |
| 45 | + { |
| 46 | + RCLCPP_FATAL( |
| 47 | + rclcpp::get_logger("DiffBotSystemHardware"), |
| 48 | + "Joint '%s' has %d command interfaces found. 1 expected.", joint.name.c_str(), |
| 49 | + joint.command_interfaces.size()); |
| 50 | + return hardware_interface::return_type::ERROR; |
| 51 | + } |
| 52 | + |
| 53 | + if (joint.command_interfaces[0].name != hardware_interface::HW_IF_VELOCITY) |
| 54 | + { |
| 55 | + RCLCPP_FATAL( |
| 56 | + rclcpp::get_logger("DiffBotSystemHardware"), |
| 57 | + "Joint '%s' have %s command interfaces found. '%s' expected.", joint.name.c_str(), |
| 58 | + joint.command_interfaces[0].name.c_str(), hardware_interface::HW_IF_VELOCITY); |
| 59 | + return hardware_interface::return_type::ERROR; |
| 60 | + } |
| 61 | + |
| 62 | + if (joint.state_interfaces.size() != 2) |
| 63 | + { |
| 64 | + RCLCPP_FATAL( |
| 65 | + rclcpp::get_logger("DiffBotSystemHardware"), |
| 66 | + "Joint '%s' has %d state interface. 2 expected.", joint.name.c_str(), |
| 67 | + joint.state_interfaces.size()); |
| 68 | + return hardware_interface::return_type::ERROR; |
| 69 | + } |
| 70 | + |
| 71 | + if (joint.state_interfaces[0].name != hardware_interface::HW_IF_POSITION) |
| 72 | + { |
| 73 | + RCLCPP_FATAL( |
| 74 | + rclcpp::get_logger("DiffBotSystemHardware"), |
| 75 | + "Joint '%s' have '%s' as first state interface. '%s' and '%s' expected.", |
| 76 | + joint.name.c_str(), joint.state_interfaces[0].name.c_str(), |
| 77 | + hardware_interface::HW_IF_POSITION); |
| 78 | + return hardware_interface::return_type::ERROR; |
| 79 | + } |
| 80 | + |
| 81 | + if (joint.state_interfaces[1].name != hardware_interface::HW_IF_VELOCITY) |
| 82 | + { |
| 83 | + RCLCPP_FATAL( |
| 84 | + rclcpp::get_logger("DiffBotSystemHardware"), |
| 85 | + "Joint '%s' have '%s' as second state interface. '%s' expected.", joint.name.c_str(), |
| 86 | + joint.state_interfaces[1].name.c_str(), hardware_interface::HW_IF_VELOCITY); |
| 87 | + return hardware_interface::return_type::ERROR; |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + status_ = hardware_interface::status::CONFIGURED; |
| 92 | + return hardware_interface::return_type::OK; |
| 93 | +} |
| 94 | + |
| 95 | +std::vector<hardware_interface::StateInterface> DiffBotSystemHardware::export_state_interfaces() |
| 96 | +{ |
| 97 | + std::vector<hardware_interface::StateInterface> state_interfaces; |
| 98 | + for (uint i = 0; i < info_.joints.size(); i++) |
| 99 | + { |
| 100 | + state_interfaces.emplace_back(hardware_interface::StateInterface( |
| 101 | + info_.joints[i].name, hardware_interface::HW_IF_POSITION, &hw_states_[i])); |
| 102 | + state_interfaces.emplace_back(hardware_interface::StateInterface( |
| 103 | + info_.joints[i].name, hardware_interface::HW_IF_VELOCITY, &hw_states_[i])); |
| 104 | + } |
| 105 | + |
| 106 | + return state_interfaces; |
| 107 | +} |
| 108 | + |
| 109 | +std::vector<hardware_interface::CommandInterface> DiffBotSystemHardware::export_command_interfaces() |
| 110 | +{ |
| 111 | + std::vector<hardware_interface::CommandInterface> command_interfaces; |
| 112 | + for (uint i = 0; i < info_.joints.size(); i++) |
| 113 | + { |
| 114 | + command_interfaces.emplace_back(hardware_interface::CommandInterface( |
| 115 | + info_.joints[i].name, hardware_interface::HW_IF_VELOCITY, &hw_commands_[i])); |
| 116 | + } |
| 117 | + |
| 118 | + return command_interfaces; |
| 119 | +} |
| 120 | + |
| 121 | +hardware_interface::return_type DiffBotSystemHardware::start() |
| 122 | +{ |
| 123 | + RCLCPP_INFO(rclcpp::get_logger("DiffBotSystemHardware"), "Starting ...please wait..."); |
| 124 | + |
| 125 | + for (int i = 0; i <= hw_start_sec_; i++) |
| 126 | + { |
| 127 | + rclcpp::sleep_for(std::chrono::seconds(1)); |
| 128 | + RCLCPP_INFO( |
| 129 | + rclcpp::get_logger("DiffBotSystemHardware"), "%.1f seconds left...", hw_start_sec_ - i); |
| 130 | + } |
| 131 | + |
| 132 | + // set some default values |
| 133 | + for (uint i = 0; i < hw_states_.size(); i++) |
| 134 | + { |
| 135 | + if (std::isnan(hw_states_[i])) |
| 136 | + { |
| 137 | + hw_states_[i] = 0; |
| 138 | + hw_commands_[i] = 0; |
| 139 | + } |
| 140 | + } |
| 141 | + |
| 142 | + status_ = hardware_interface::status::STARTED; |
| 143 | + |
| 144 | + RCLCPP_INFO(rclcpp::get_logger("DiffBotSystemHardware"), "System Successfully started!"); |
| 145 | + |
| 146 | + return hardware_interface::return_type::OK; |
| 147 | +} |
| 148 | + |
| 149 | +hardware_interface::return_type DiffBotSystemHardware::stop() |
| 150 | +{ |
| 151 | + RCLCPP_INFO(rclcpp::get_logger("DiffBotSystemHardware"), "Stopping ...please wait..."); |
| 152 | + |
| 153 | + for (int i = 0; i <= hw_stop_sec_; i++) |
| 154 | + { |
| 155 | + rclcpp::sleep_for(std::chrono::seconds(1)); |
| 156 | + RCLCPP_INFO( |
| 157 | + rclcpp::get_logger("DiffBotSystemHardware"), "%.1f seconds left...", hw_stop_sec_ - i); |
| 158 | + } |
| 159 | + |
| 160 | + status_ = hardware_interface::status::STOPPED; |
| 161 | + |
| 162 | + RCLCPP_INFO(rclcpp::get_logger("DiffBotSystemHardware"), "System successfully stopped!"); |
| 163 | + |
| 164 | + return hardware_interface::return_type::OK; |
| 165 | +} |
| 166 | + |
| 167 | +hardware_interface::return_type DiffBotSystemHardware::read() |
| 168 | +{ |
| 169 | + RCLCPP_INFO(rclcpp::get_logger("DiffBotSystemHardware"), "Reading..."); |
| 170 | + |
| 171 | + double tau = 0.2; |
| 172 | + double a = tau / (tau + 1); |
| 173 | + double b = 1 / (tau + 1); |
| 174 | + for (uint i = 0; i < hw_commands_.size(); i++) |
| 175 | + { |
| 176 | + // Simulate DiffBot wheels's movement as a first-order system |
| 177 | + hw_states_[1] = a * hw_states_[1] + b * hw_commands_[i]; |
| 178 | + hw_states_[0] += hw_states_[1] / 2; |
| 179 | + RCLCPP_INFO( |
| 180 | + rclcpp::get_logger("DiffBotSystemHardware"), |
| 181 | + "Got position state %.5f and velocity state %.5f for '%s'!", hw_states_[0], hw_states_[1], |
| 182 | + info_.joints[i].name.c_str()); |
| 183 | + } |
| 184 | + RCLCPP_INFO(rclcpp::get_logger("DiffBotSystemHardware"), "Joints successfully read!"); |
| 185 | + |
| 186 | + return hardware_interface::return_type::OK; |
| 187 | +} |
| 188 | + |
| 189 | +hardware_interface::return_type ros2_control_demo_hardware::DiffBotSystemHardware::write() |
| 190 | +{ |
| 191 | + RCLCPP_INFO(rclcpp::get_logger("DiffBotSystemHardware"), "Writing..."); |
| 192 | + |
| 193 | + for (uint i = 0; i < hw_commands_.size(); i++) |
| 194 | + { |
| 195 | + // Simulate sending commands to the hardware |
| 196 | + RCLCPP_INFO( |
| 197 | + rclcpp::get_logger("DiffBotSystemHardware"), "Got command %.5f for '%s'!", hw_commands_[i], |
| 198 | + info_.joints[i].name.c_str()); |
| 199 | + } |
| 200 | + RCLCPP_INFO(rclcpp::get_logger("DiffBotSystemHardware"), "Joints successfully written!"); |
| 201 | + |
| 202 | + return hardware_interface::return_type::OK; |
| 203 | +} |
| 204 | + |
| 205 | +} // namespace ros2_control_demo_hardware |
| 206 | + |
| 207 | +#include "pluginlib/class_list_macros.hpp" |
| 208 | +PLUGINLIB_EXPORT_CLASS( |
| 209 | + ros2_control_demo_hardware::DiffBotSystemHardware, hardware_interface::SystemInterface) |
0 commit comments