roo_control
API Documentation for roo_control
Loading...
Searching...
No Matches
bound_selector.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/// Selector backed by a bound sensor from roo_transceivers.
9template <typename State>
10class BoundSelector : public Selector<State> {
11 public:
12 BoundSelector(roo_transceivers::Universe& universe,
13 const roo_transceivers::SensorBinding& binding)
14 : bound_sensor_(universe, binding), state_() {}
15
16 bool getState(State& result) const override {
17 Measurement m = bound_sensor_.read();
18 if (m.isDefined()) {
19 CHECK(m.quantity() == roo_control_Quantity_kBinaryState ||
20 m.quantity() == roo_control_Quantity_kMultiState);
21 if ((int)m.value()) != m.value() {
22 LOG(ERROR) << "Selector value is not an integer: " << m.value();
23 return false;
24 }
25 result = (State)m.value();
26 return true;
27 }
28 LOG(ERROR) << "Unknown selector state " << bound_sensor_;
29 return false;
30 }
31
32 private:
33 roo_transceivers::BoundSensor bound_sensor_;
34};
35
36/// BinaryLogicalState specialization with stricter value checks.
37template <>
38class BoundSelector<BinaryLogicalState> : public Selector<BinaryLogicalState> {
39 public:
41 roo_transceivers::Universe& universe,
42 const roo_transceivers::SensorBinding& binding)
43 : bound_sensor_(universe, binding) {}
44
45 bool getState(BinaryLogicalState& result) const override {
46 roo_transceivers::Measurement m = bound_sensor_.read();
47 if (m.isDefined()) {
48 CHECK(m.quantity() == roo_control_Quantity_kBinaryState ||
49 m.quantity() == roo_control_Quantity_kMultiState);
50 if (m.value() != 0.0f && m.value() != 1.0f) {
51 LOG(ERROR) << "Selector value is invalid: " << m.value();
52 return false;
53 }
54 result = (BinaryLogicalState)((int)m.value());
55 return true;
56 }
57 LOG(ERROR) << "Unknown selector state " << bound_sensor_;
58 return false;
59 }
60
61 private:
62 roo_transceivers::BoundSensor bound_sensor_;
63};
64
65/// Convenience alias for a bound binary selector.
67
68} // namespace roo_control
BinaryLogicalState specialization with stricter value checks.
bool getState(BinaryLogicalState &result) const override
Retrieves the current state, or returns false when it cannot be read.
Selector backed by a bound sensor from roo_transceivers.
BoundSelector(roo_transceivers::Universe &universe, const roo_transceivers::SensorBinding &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 device.
Definition selector.h:23
BinaryLogicalState
Binary logical state used by selectors and switches.