Control Libraries 7.4.0
Loading...
Searching...
No Matches
AnalogIOState.cpp
1#include "state_representation/AnalogIOState.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::ANALOG_IO_STATE);
11}
12
13AnalogIOState::AnalogIOState(const std::string& name, unsigned int nb_ios) : IOState<double>(name, nb_ios) {
14 this->set_type(StateType::ANALOG_IO_STATE);
15 this->data_ = Eigen::VectorXd::Zero(nb_ios);
16}
17
18AnalogIOState::AnalogIOState(const std::string& name, const std::vector<std::string>& io_names) :
19 AnalogIOState(name, io_names.size()) {
20 this->set_names(io_names);
21}
22
23AnalogIOState::AnalogIOState(const AnalogIOState& state) : AnalogIOState(state.get_name(), state.get_names()) {
24 if (state) {
25 this->set_data(state.data());
26 }
27}
28
29AnalogIOState AnalogIOState::Zero(const std::string& name, unsigned int nb_ios) {
30 AnalogIOState zero = AnalogIOState(name, nb_ios);
31 // specify that the default constructed zero state is non-empty
32 zero.set_empty(false);
33 return zero;
34}
35
36AnalogIOState AnalogIOState::Zero(const std::string& name, const std::vector<std::string>& io_names) {
37 AnalogIOState zero = AnalogIOState(name, io_names);
38 // specify that the default constructed zero state is non-empty
39 zero.set_empty(false);
40 return zero;
41}
42
43AnalogIOState AnalogIOState::Random(const std::string& name, unsigned int nb_ios) {
44 AnalogIOState random = AnalogIOState(name, nb_ios);
45 // set all the state variables to random
46 random.set_data(Eigen::VectorXd::Random(random.get_size()));
47 return random;
48}
49
50AnalogIOState AnalogIOState::Random(const std::string& name, const std::vector<std::string>& io_names) {
51 AnalogIOState random = AnalogIOState(name, io_names);
52 // set all the state variables to random
53 random.set_data(Eigen::VectorXd::Random(random.get_size()));
54 return random;
55}
56
58 AnalogIOState tmp(state);
59 swap(*this, tmp);
60 return *this;
61}
62
64 AnalogIOState result(*this);
65 return result;
66}
67
69 this->set_zero();
70 this->State::reset();
71}
72
74 if (this->get_size() > 0) {
75 this->data_.setZero();
76 this->set_empty(false);
77 }
78}
79
80std::string AnalogIOState::to_string() const {
81 std::stringstream s;
82 s << this->State::to_string();
83 s << std::endl << "analog io names: [";
84 for (auto& n : this->get_names()) { s << n << ", "; }
85 s << "]";
86 if (this->is_empty()) {
87 return s.str();
88 }
89 s << std::endl << "values: [";
90 for (auto& p : this->data()) { s << p << ", "; }
91 s << "]";
92 return s.str();
93}
94
95std::ostream& operator<<(std::ostream& os, const AnalogIOState& state) {
96 os << state.to_string();
97 return os;
98}
99}// namespace state_representation
void reset() override
Reset the object to a post-construction state.
static AnalogIOState Zero(const std::string &name, unsigned int nb_ios)
Constructor for a zero analog IO state.
std::string to_string() const override
Convert the state to its string representation.
AnalogIOState()
Empty constructor for an analog IO state.
friend void swap(AnalogIOState &state1, AnalogIOState &state2)
Swap the values of the IO states.
void set_zero()
Set the analog IO state to zero data.
AnalogIOState copy() const
Return a copy of the analog IO state.
AnalogIOState & operator=(const AnalogIOState &state)
Copy assignment operator that has to be defined to the custom assignment operator.
static AnalogIOState Random(const std::string &name, unsigned int nb_ios)
Constructor for an analog IO state with random data.
Eigen::Vector< double, Eigen::Dynamic > data_
IO values.
Definition IOState.hpp:145
void set_data(const Eigen::Vector< double, 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
const std::vector< std::string > & get_names() const
Getter of the names.
Definition IOState.hpp:167
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
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)