SMACC2
Loading...
Searching...
No Matches
cp_lifecycle_state_tracker.cpp
Go to the documentation of this file.
1// Copyright 2024 RobosoftAI Inc.
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
16
17namespace cl_lifecycle_node
18{
20{
21 // Initialize with empty state
22 RCLCPP_INFO(getLogger(), "[CpLifecycleStateTracker] Component initialized");
23}
24
25void CpLifecycleStateTracker::updateState(const lifecycle_msgs::msg::State & state)
26{
27 std::lock_guard<std::mutex> lock(stateMutex_);
28 currentState_ = state;
29 RCLCPP_DEBUG(
30 getLogger(), "[CpLifecycleStateTracker] State updated to: %s (id: %d)", state.label.c_str(),
31 state.id);
32}
33
34std::optional<lifecycle_msgs::msg::State> CpLifecycleStateTracker::getCurrentState() const
35{
36 std::lock_guard<std::mutex> lock(stateMutex_);
37 return currentState_;
38}
39
40std::optional<std::string> CpLifecycleStateTracker::getCurrentStateLabel() const
41{
42 std::lock_guard<std::mutex> lock(stateMutex_);
43 if (currentState_)
44 {
45 return currentState_->label;
46 }
47 return std::nullopt;
48}
49
51{
52 std::lock_guard<std::mutex> lock(stateMutex_);
53 if (currentState_)
54 {
55 return currentState_->id;
56 }
57 return 0;
58}
59
60} // namespace cl_lifecycle_node
std::optional< lifecycle_msgs::msg::State > currentState_
void onInitialize() override
Component initialization.
void updateState(const lifecycle_msgs::msg::State &state)
Update the current state.
std::optional< lifecycle_msgs::msg::State > getCurrentState() const
Get the current lifecycle state.
uint8_t getCurrentStateId() const
Get the current state ID.
std::optional< std::string > getCurrentStateLabel() const
Get the current state label as string.
rclcpp::Logger getLogger() const