roo_control
API Documentation for roo_control
Loading...
Searching...
No Matches
bound_switch.h
Go to the documentation of this file.
1#pragma once
2
4#include "roo_transceivers/binding/binding.h"
5
6namespace roo_control {
7
8/// Switch backed by a bound sensing actuator from roo_transceivers.
9template <typename State>
10class BoundSwitch : public Switch<State> {
11 public:
12 BoundSwitch(roo_transceivers::Universe& universe,
13 const roo_transceivers::ActuatorBinding* binding)
14 : bound_sensing_actuator_(universe, binding) {}
15
16 bool getState(State& result) const override {
17 roo_transceivers::Measurement m = bound_sensing_actuator_.read();
18 if (m.isDefined()) {
19 CHECK(m.quantity() == roo_transceivers_Quantity_kBinaryState ||
20 m.quantity() == roo_transceivers_Quantity_kMultiState);
21 if ((int)m.value() != m.value()) {
22 LOG_EVERY_T(ERROR, 1)
23 << "Selector value is not an integer: " << m.value();
24 return false;
25 }
26 result = (State)m.value();
27 return true;
28 }
29 // LOG(ERROR) << "Unknown selector state " << bound_sensing_actuator_;
30 return false;
31 }
32
33 bool setState(State state) override {
34 return bound_sensing_actuator_.write((float)state);
35 }
36
37 private:
38 roo_transceivers::BoundSensingActuator bound_sensing_actuator_;
39};
40
41/// BinaryLogicalState specialization with stricter value checks.
42template <>
43class BoundSwitch<BinaryLogicalState> : public Switch<BinaryLogicalState> {
44 public:
45 BoundSwitch(roo_transceivers::Universe& universe,
46 const roo_transceivers::ActuatorBinding* binding)
47 : bound_sensing_actuator_(universe, binding) {}
48
49 bool getState(BinaryLogicalState& result) const override {
50 roo_transceivers::Measurement m = bound_sensing_actuator_.read();
51 if (m.isDefined()) {
52 if (m.quantity() != roo_transceivers_Quantity_kBinaryState &&
53 m.quantity() != roo_transceivers_Quantity_kMultiState) {
54 LOG_EVERY_T(ERROR, 1)
55 << "Unexpected quantity when reading bound actuator: "
56 << m.quantity() << " " << m.value();
57 return false;
58 }
59 if (m.value() != 0.0f && m.value() != 1.0f) {
60 LOG_EVERY_T(ERROR, 1) << "Selector value is invalid: " << m.value();
61 return false;
62 }
63 result = (BinaryLogicalState)((int)m.value());
64 return true;
65 }
66 // LOG(ERROR) << "Unknown selector state " << bound_sensing_actuator_;
67 return false;
68 }
69
70 bool setState(BinaryLogicalState state) override {
71 return bound_sensing_actuator_.write((float)state);
72 }
73
74 private:
75 roo_transceivers::BoundSensingActuator bound_sensing_actuator_;
76};
77
78/// Convenience alias for a bound binary switch.
80
81} // namespace roo_control
BinaryLogicalState specialization with stricter value checks.
bool setState(BinaryLogicalState state) override
Updates the state of the switch. Returns true on success.
bool getState(BinaryLogicalState &result) const override
BoundSwitch(roo_transceivers::Universe &universe, const roo_transceivers::ActuatorBinding *binding)
Switch backed by a bound sensing actuator from roo_transceivers.
bool setState(State state) override
Updates the state of the switch. Returns true on success.
BoundSwitch(roo_transceivers::Universe &universe, const roo_transceivers::ActuatorBinding *binding)
bool getState(State &result) const override
Retrieves the current state, or returns false when it cannot be read.
An abstraction of a multi-state settable switch.
Definition switch.h:24
BinaryLogicalState
Binary logical state used by selectors and switches.