Control Libraries 7.4.0
Loading...
Searching...
No Matches
DigitalIOState.cpp
1#include "state_representation/DigitalIOState.hpp"
2
3#include "state_representation/exceptions/IONotFoundException.hpp"
4
5using namespace state_representation::exceptions;
6
7namespace state_representation {
8
10 this->set_type(StateType::DIGITAL_IO_STATE);
11}
12
13DigitalIOState::DigitalIOState(const std::string& name, unsigned int nb_ios) : IOState<bool>(name, nb_ios) {
14 this->set_type(StateType::DIGITAL_IO_STATE);
15 this->data_ = Eigen::Vector<bool, Eigen::Dynamic>::Zero(nb_ios);
16}
17
18DigitalIOState::DigitalIOState(const std::string& name, const std::vector<std::string>& io_names) :
19 DigitalIOState(name, io_names.size()) {
20 this->set_names(io_names);
21}
22
23DigitalIOState::DigitalIOState(const DigitalIOState& state) : DigitalIOState(state.get_name(), state.get_names()) {
24 if (state) {
25 this->set_data(state.data());
26 }
27}
28
29DigitalIOState DigitalIOState::Zero(const std::string& name, unsigned int nb_ios) {
30 DigitalIOState zero = DigitalIOState(name, nb_ios);
31 // specify that the default constructed zero state is non-empty
32 zero.set_empty(false);
33 return zero;
34}
35
36DigitalIOState DigitalIOState::Zero(const std::string& name, const std::vector<std::string>& io_names) {
37 DigitalIOState zero = DigitalIOState(name, io_names);
38 // specify that the default constructed zero state is non-empty
39 zero.set_empty(false);
40 return zero;
41}
42
43DigitalIOState DigitalIOState::Random(const std::string& name, unsigned int nb_ios) {
44 DigitalIOState random = DigitalIOState(name, nb_ios);
45 // set all the state variables to random
46 random.set_data(Eigen::Vector<bool, Eigen::Dynamic>::Random(random.get_size()));
47 return random;
48}
49
50DigitalIOState DigitalIOState::Random(const std::string& name, const std::vector<std::string>& io_names) {
51 DigitalIOState random = DigitalIOState(name, io_names);
52 // set all the state variables to random
53 random.set_data(Eigen::Vector<bool, Eigen::Dynamic>::Random(random.get_size()));
54 return random;
55}
56
58 DigitalIOState tmp(state);
59 swap(*this, tmp);
60 return *this;
61}
62
63bool DigitalIOState::is_true(const std::string& io_name) const {
64 return this->is_true(this->get_io_index(io_name));
65}
66
67bool DigitalIOState::is_true(unsigned int io_index) const {
68 this->assert_not_empty();
70 return this->data_(io_index) == true;
71}
72
73bool DigitalIOState::is_false(const std::string& io_name) const {
74 return !this->is_true(io_name);
75}
76
77bool DigitalIOState::is_false(unsigned int io_index) const {
78 return !this->is_true(io_index);
79}
80
81void DigitalIOState::set_true(const std::string& io_name) {
82 this->set_true(this->get_io_index(io_name));
83}
84
85void DigitalIOState::set_true(unsigned int io_index) {
86 this->set_value(true, io_index);
87}
88
89void DigitalIOState::set_false(const std::string& io_name) {
90 this->set_false(this->get_io_index(io_name));
91}
92
93void DigitalIOState::set_false(unsigned int io_index) {
94 this->set_value(false, io_index);
95}
96
98 DigitalIOState result(*this);
99 return result;
100}
101
103 this->set_false();
104 this->State::reset();
105}
106
108 if (this->get_size() > 0) {
109 this->data_.setZero();
110 this->set_empty(false);
111 }
112}
113
114std::string DigitalIOState::to_string() const {
115 std::stringstream s;
116 s << this->State::to_string();
117 s << std::endl << "digital io names: [";
118 for (auto& n : this->get_names()) { s << n << ", "; }
119 s << "]";
120 if (this->is_empty()) {
121 return s.str();
122 }
123 s << std::endl << "values: [";
124 for (auto& p : this->data()) { s << p << ", "; }
125 s << "]";
126 return s.str();
127}
128
129std::ostream& operator<<(std::ostream& os, const DigitalIOState& state) {
130 os << state.to_string();
131 return os;
132}
133}// namespace state_representation
std::string to_string() const override
Convert the state to its string representation.
void set_false()
Set all digital IOs false.
friend void swap(DigitalIOState &state1, DigitalIOState &state2)
Swap the values of the IO states.
static DigitalIOState Zero(const std::string &name, unsigned int nb_ios)
Constructor for a zero digital IO state.
bool is_true(const std::string &name) const
Check if a digital IO is true by its name, if it exists.
void reset() override
Reset the object to a post-construction state.
DigitalIOState()
Empty constructor for a digital IO state.
DigitalIOState & operator=(const DigitalIOState &state)
Copy assignment operator that has to be defined to the custom assignment operator.
DigitalIOState copy() const
Return a copy of the digital IO state.
bool is_false(const std::string &name) const
Check if a digital IO is false by its name, if it exists.
void set_true(const std::string &name)
Set the a digital IO to true by its name.
static DigitalIOState Random(const std::string &name, unsigned int nb_ios)
Constructor for a digital IO state with random data.
Eigen::Vector< bool, Eigen::Dynamic > data_
IO values.
Definition IOState.hpp:145
void set_data(const Eigen::Vector< bool, Eigen::Dynamic > &data)
Set the values of the IO state from a single Eigen vector.
Definition IOState.hpp:240
Eigen::Vector< T, Eigen::Dynamic > data() const
Returns the values of the IO state as an Eigen vector.
Definition IOState.hpp:193
void set_names(unsigned int nb_ios)
Setter of the names from the number of IOs.
Definition IOState.hpp:204
unsigned int get_size() const
Getter of the size.
Definition IOState.hpp:162
void set_value(bool value, const std::string &name)
Set the value of an IO by its name.
Definition IOState.hpp:228
const std::vector< std::string > & get_names() const
Getter of the names.
Definition IOState.hpp:167
unsigned int get_io_index(const std::string &io_name) const
Get IO index by the name of the IO, if it exists.
Definition IOState.hpp:172
void set_type(const StateType &type)
Setter of the state type attribute.
Definition State.cpp:41
virtual std::string to_string() const
Convert the state to its string representation.
Definition State.cpp:99
virtual void reset()
Reset the object to a post-construction state.
Definition State.cpp:77
void assert_not_empty() const
Throw an exception if the state is empty.
Definition State.cpp:55
bool is_empty() const
Getter of the empty attribute.
Definition State.cpp:33
void set_empty(bool empty=true)
Setter of the empty attribute.
Definition State.cpp:50
Core state variables and objects.
std::ostream & operator<<(std::ostream &os, const AnalogIOState &state)