|
| 1 | +// Copyright (c) 2018 - for information on the respective copyright owner |
| 2 | +// see the NOTICE file and/or the repository https://github.com/microros/system_modes |
| 3 | +// |
| 4 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +// you may not use this file except in compliance with the License. |
| 6 | +// You may obtain a copy of the License at |
| 7 | +// |
| 8 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +// |
| 10 | +// Unless required by applicable law or agreed to in writing, software |
| 11 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +// See the License for the specific language governing permissions and |
| 14 | +// limitations under the License. |
| 15 | +#include "system_modes/mode_observer.hpp" |
| 16 | + |
| 17 | +#include <lifecycle_msgs/msg/state.hpp> |
| 18 | +#include <lifecycle_msgs/msg/transition_event.hpp> |
| 19 | + |
| 20 | +#include <functional> |
| 21 | +#include <map> |
| 22 | +#include <string> |
| 23 | +#include <vector> |
| 24 | +#include <memory> |
| 25 | + |
| 26 | +#include "system_modes/msg/mode_event.hpp" |
| 27 | + |
| 28 | +using std::function; |
| 29 | +using std::map; |
| 30 | +using std::mutex; |
| 31 | +using std::placeholders::_1; |
| 32 | +using std::string; |
| 33 | +using std::vector; |
| 34 | + |
| 35 | +using lifecycle_msgs::msg::State; |
| 36 | +using lifecycle_msgs::msg::Transition; |
| 37 | +using lifecycle_msgs::msg::TransitionEvent; |
| 38 | +using lifecycle_msgs::srv::GetState; |
| 39 | + |
| 40 | +using system_modes::msg::ModeEvent; |
| 41 | +using system_modes::srv::GetMode; |
| 42 | + |
| 43 | +using namespace std::chrono_literals; |
| 44 | +using shared_mutex = std::shared_timed_mutex; |
| 45 | + |
| 46 | +namespace system_modes |
| 47 | +{ |
| 48 | + |
| 49 | +ModeObserver::ModeObserver(std::weak_ptr<rclcpp::Node> handle) |
| 50 | +: node_handle_(handle) |
| 51 | +{ |
| 52 | +} |
| 53 | + |
| 54 | +StateAndMode |
| 55 | +ModeObserver::get(const std::string & part_name) |
| 56 | +{ |
| 57 | + std::shared_lock<shared_mutex> lock(this->mutex_); |
| 58 | + try { |
| 59 | + return cache_.at(part_name); |
| 60 | + } catch (const std::exception & e) { |
| 61 | + std::cerr << e.what() << '\n'; |
| 62 | + return StateAndMode(); |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +void |
| 67 | +ModeObserver::observe(const std::string & part_name) |
| 68 | +{ |
| 69 | + std::unique_lock<shared_mutex> lock(mutex_); |
| 70 | + |
| 71 | + cache_[part_name] = StateAndMode(); |
| 72 | + |
| 73 | + // Initial try to get current state via service request |
| 74 | + std::string topic = part_name + "/get_state"; |
| 75 | + auto gsrequest = std::make_shared<GetState::Request>(); |
| 76 | + auto stateclient = node_handle_.lock()->create_client<GetState>(topic); |
| 77 | + if (stateclient->wait_for_service(std::chrono::microseconds(500))) { |
| 78 | + auto state = stateclient->async_send_request(gsrequest); |
| 79 | + if (rclcpp::spin_until_future_complete(node_handle_.lock(), state) == |
| 80 | + rclcpp::FutureReturnCode::SUCCESS) |
| 81 | + { |
| 82 | + auto result = state.get(); |
| 83 | + cache_[part_name].state = result->current_state.id; |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + // Initial try to get current mode via service request |
| 88 | + topic = part_name + "/get_mode"; |
| 89 | + auto gmrequest = std::make_shared<GetMode::Request>(); |
| 90 | + auto modeclient = node_handle_.lock()->create_client<GetMode>(topic); |
| 91 | + if (modeclient->wait_for_service(std::chrono::microseconds(500))) { |
| 92 | + auto mode = modeclient->async_send_request(gmrequest); |
| 93 | + if (rclcpp::spin_until_future_complete(node_handle_.lock(), mode) == |
| 94 | + rclcpp::FutureReturnCode::SUCCESS) |
| 95 | + { |
| 96 | + auto result = mode.get(); |
| 97 | + cache_[part_name].mode = result->current_mode; |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + // Set up transition subscriber and mode event subscriber for continuous observation |
| 102 | + topic = part_name + "/transition_event"; |
| 103 | + function<void(TransitionEvent::SharedPtr)> transition_cb = |
| 104 | + bind(&ModeObserver::transition_callback, this, _1, part_name); |
| 105 | + auto state_sub = node_handle_.lock()->create_subscription<TransitionEvent>( |
| 106 | + topic, |
| 107 | + rclcpp::SystemDefaultsQoS(), |
| 108 | + transition_cb); |
| 109 | + state_subs_.emplace(part_name, state_sub); |
| 110 | + topic = part_name + "/mode_event"; |
| 111 | + function<void(ModeEvent::SharedPtr)> mode_cb = |
| 112 | + bind(&ModeObserver::mode_event_callback, this, _1, part_name); |
| 113 | + auto mode_sub = node_handle_.lock()->create_subscription<ModeEvent>( |
| 114 | + topic, |
| 115 | + rclcpp::SystemDefaultsQoS(), |
| 116 | + mode_cb); |
| 117 | + mode_subs_.emplace(part_name, mode_sub); |
| 118 | +} |
| 119 | + |
| 120 | +void |
| 121 | +ModeObserver::stop_observing(const std::string & part_name) |
| 122 | +{ |
| 123 | + std::unique_lock<shared_mutex> lock(mutex_); |
| 124 | + state_subs_.erase(part_name); |
| 125 | + mode_subs_.erase(part_name); |
| 126 | + cache_.erase(part_name); |
| 127 | +} |
| 128 | + |
| 129 | +void |
| 130 | +ModeObserver::transition_callback( |
| 131 | + const TransitionEvent::SharedPtr msg, |
| 132 | + const string & part_name) |
| 133 | +{ |
| 134 | + std::unique_lock<shared_mutex> lock(mutex_); |
| 135 | + cache_[part_name].state = msg->goal_state.id; |
| 136 | +} |
| 137 | + |
| 138 | +void |
| 139 | +ModeObserver::mode_event_callback( |
| 140 | + const ModeEvent::SharedPtr msg, |
| 141 | + const string & part_name) |
| 142 | +{ |
| 143 | + std::unique_lock<shared_mutex> lock(mutex_); |
| 144 | + cache_[part_name].mode = msg->goal_mode.label; |
| 145 | +} |
| 146 | + |
| 147 | +} // namespace system_modes |
0 commit comments