ros2_control - rolling
Loading...
Searching...
No Matches
hardware_component.hpp
1// Copyright 2026 Open Source Robotics Foundation, 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
15#ifndef ZENBEDDED_RCL__HARDWARE_COMPONENT_HPP_
16#define ZENBEDDED_RCL__HARDWARE_COMPONENT_HPP_
17
18#include <zephyr/kernel.h>
19#include <cstdint>
20#include <string_view>
21
22enum class HwReturn : uint8_t
23{
24 OK = 0,
25 ERROR = 1,
26 BUSY = 2,
27 TIMEOUT = 3,
28};
29
30enum class HwState : uint8_t
31{
32 UNCONFIGURED = 0,
33 INACTIVE = 1,
34 ACTIVE = 2,
35 FINALIZED = 3,
36};
37
39{
40 std::string_view name;
41
42 double get()
43 {
44 k_mutex_lock(&mutex, K_FOREVER);
45 double v = value;
46 k_mutex_unlock(&mutex);
47 return v;
48 }
49
50 void set(double v)
51 {
52 k_mutex_lock(&mutex, K_FOREVER);
53 value = v;
54 k_mutex_unlock(&mutex);
55 }
56
57 explicit StateInterface(std::string_view n) : name(n) { k_mutex_init(&mutex); }
58
59private:
60 double value{0.0};
61 struct k_mutex mutex;
62};
63
65{
66 std::string_view name;
67
68 double get()
69 {
70 k_mutex_lock(&mutex, K_FOREVER);
71 double v = value;
72 k_mutex_unlock(&mutex);
73 return v;
74 }
75
76 void set(double v)
77 {
78 k_mutex_lock(&mutex, K_FOREVER);
79 value = v;
80 k_mutex_unlock(&mutex);
81 }
82
83 explicit CommandInterface(std::string_view n) : name(n) { k_mutex_init(&mutex); }
84
85private:
86 double value{0.0};
87 struct k_mutex mutex;
88};
89
91{
92public:
93 explicit HardwareComponent(std::string_view name) : name_(name)
94 {
95 k_mutex_init(&lifecycle_mutex_);
96 }
97
98 virtual ~HardwareComponent() = default;
99
100 HardwareComponent(const HardwareComponent &) = delete;
101 HardwareComponent & operator=(const HardwareComponent &) = delete;
102
103 HwReturn configure()
104 {
105 k_mutex_lock(&lifecycle_mutex_, K_FOREVER);
106 HwReturn ret = HwReturn::ERROR;
107 if (state_ == HwState::UNCONFIGURED)
108 {
109 ret = on_configure();
110 if (ret == HwReturn::OK)
111 {
112 state_ = HwState::INACTIVE;
113 }
114 }
115 k_mutex_unlock(&lifecycle_mutex_);
116 return ret;
117 }
118
119 HwReturn activate()
120 {
121 k_mutex_lock(&lifecycle_mutex_, K_FOREVER);
122 HwReturn ret = HwReturn::ERROR;
123 if (state_ == HwState::INACTIVE)
124 {
125 ret = on_activate();
126 if (ret == HwReturn::OK)
127 {
128 state_ = HwState::ACTIVE;
129 }
130 }
131 k_mutex_unlock(&lifecycle_mutex_);
132 return ret;
133 }
134
135 HwReturn deactivate()
136 {
137 k_mutex_lock(&lifecycle_mutex_, K_FOREVER);
138 HwReturn ret = HwReturn::ERROR;
139 if (state_ == HwState::ACTIVE)
140 {
141 ret = on_deactivate();
142 if (ret == HwReturn::OK)
143 {
144 state_ = HwState::INACTIVE;
145 }
146 }
147 k_mutex_unlock(&lifecycle_mutex_);
148 return ret;
149 }
150
151 HwReturn cleanup()
152 {
153 k_mutex_lock(&lifecycle_mutex_, K_FOREVER);
154 HwReturn ret = on_cleanup();
155 if (ret == HwReturn::OK)
156 {
157 state_ = HwState::FINALIZED;
158 }
159 k_mutex_unlock(&lifecycle_mutex_);
160 return ret;
161 }
162
163 HwReturn read()
164 {
165 if (state_ != HwState::ACTIVE)
166 {
167 return HwReturn::ERROR;
168 }
169 return on_read();
170 }
171
172 HwReturn write()
173 {
174 if (state_ != HwState::ACTIVE)
175 {
176 return HwReturn::ERROR;
177 }
178 return on_write();
179 }
180
181 HwState state() const { return state_; }
182 std::string_view name() const { return name_; }
183
184protected:
185 virtual HwReturn on_configure() { return HwReturn::OK; }
186 virtual HwReturn on_activate() { return HwReturn::OK; }
187 virtual HwReturn on_deactivate() { return HwReturn::OK; }
188 virtual HwReturn on_cleanup() { return HwReturn::OK; }
189 virtual HwReturn on_read() = 0;
190 virtual HwReturn on_write() = 0;
191
192 StateInterface * state_interfaces_{nullptr};
193 CommandInterface * command_interfaces_{nullptr};
194 uint8_t n_state_{0};
195 uint8_t n_command_{0};
196
197private:
198 std::string_view name_;
199 HwState state_{HwState::UNCONFIGURED};
200 struct k_mutex lifecycle_mutex_;
201};
202
203#endif // ZENBEDDED_RCL__HARDWARE_COMPONENT_HPP_
Definition hardware_component.hpp:91
Definition hardware_component.hpp:65
Definition hardware_component.hpp:39