SMACC2
Loading...
Searching...
No Matches
cb_relay_status.hpp
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
15/*****************************************************************************************************************
16 *
17 * Authors: Pablo Inigo Blasco, Brett Aldrich
18 *
19 ******************************************************************************************************************/
20
21#pragma once
22
25
26#include <optional>
27
28namespace cl_modbus_tcp_relay
29{
30
38{
39public:
43 CbRelayStatus() : channel_(std::nullopt) {}
44
49 explicit CbRelayStatus(int channel) : channel_(channel) {}
50
51 virtual ~CbRelayStatus() {}
52
53 template <typename TOrthogonal, typename TSourceObject>
61
62 virtual void onEntry() override
63 {
64 if (channel_.has_value())
65 {
67 }
68 else
69 {
71 }
72 }
73
78 uint8_t getChannelStates() const { return channelStates_; }
79
85 virtual void onStatusRead(uint8_t channelStates)
86 {
87 RCLCPP_INFO(getLogger(), "[CbRelayStatus] Status read callback: 0x%02X", channelStates);
88 }
89
90private:
91 std::optional<int> channel_;
94 uint8_t channelStates_ = 0;
95
96 void readSingleChannel(int channel)
97 {
98 RCLCPP_INFO(getLogger(), "[CbRelayStatus] Reading channel %d status", channel);
99
100 if (!relayComponent_)
101 {
102 RCLCPP_ERROR(getLogger(), "[CbRelayStatus] Relay component not available");
103 this->postFailureEvent();
104 return;
105 }
106
107 bool state;
108 bool success = relayComponent_->readCoil(channel, state);
109
110 if (success)
111 {
112 channelStates_ = state ? (1 << (channel - 1)) : 0;
113 RCLCPP_INFO(getLogger(), "[CbRelayStatus] Channel %d is %s", channel, state ? "ON" : "OFF");
115 this->postSuccessEvent();
116 }
117 else
118 {
119 RCLCPP_ERROR(getLogger(), "[CbRelayStatus] Failed to read channel %d", channel);
120 this->postFailureEvent();
121 }
122 }
123
125 {
126 RCLCPP_INFO(getLogger(), "[CbRelayStatus] Reading all channel statuses");
127
128 if (!relayComponent_)
129 {
130 RCLCPP_ERROR(getLogger(), "[CbRelayStatus] Relay component not available");
131 this->postFailureEvent();
132 return;
133 }
134
136
137 if (success)
138 {
139 RCLCPP_INFO(getLogger(), "[CbRelayStatus] All channel statuses: 0x%02X", channelStates_);
140
141 // Log individual channel states
142 for (int i = 0; i < 8; i++)
143 {
144 bool state = (channelStates_ & (1 << i)) != 0;
145 RCLCPP_INFO(getLogger(), "[CbRelayStatus] Channel %d: %s", i + 1, state ? "ON" : "OFF");
146 }
147
149 this->postSuccessEvent();
150 }
151 else
152 {
153 RCLCPP_ERROR(getLogger(), "[CbRelayStatus] Failed to read channel statuses");
154 this->postFailureEvent();
155 }
156 }
157};
158
159} // namespace cl_modbus_tcp_relay
Client behavior to read relay channel status.
virtual void onStatusRead(uint8_t channelStates)
Virtual callback for status read completion Override in derived classes for custom handling.
uint8_t getChannelStates() const
Get the last read channel states (bitmask)
CbRelayStatus(int channel)
Construct behavior to read a specific channel status.
CbRelayStatus()
Construct behavior to read all channel statuses.
SMACC2 Client for controlling Modbus TCP relays.
Component that handles Modbus coil read/write operations for 8-channel relay.
bool readCoil(int channel, bool &state)
virtual rclcpp::Logger getLogger() const
void requiresClient(SmaccClientType *&storage)
void requiresComponent(SmaccComponentType *&storage, ComponentRequirement requirementType=ComponentRequirement::SOFT)