signals.cpp
Go to the documentation of this file.
1 /*********************************************************************
2  *
3  * Software License Agreement
4  *
5  * Copyright (c) 2020,
6  * TU Dortmund - Institute of Control Theory and Systems Engineering.
7  * All rights reserved.
8  *
9  * This program is free software: you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation, either version 3 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program. If not, see <https://www.gnu.org/licenses/>.
21  *
22  * Authors: Christoph Rösmann
23  *********************************************************************/
24 
25 #include <corbo-core/signals.h>
26 
27 namespace corbo {
28 
29 // void ValuesSignal::toMessage(corbo::messages::Signal& message) const
30 //{
31 // header.toMessage(*message.mutable_header());
32 // google::protobuf::RepeatedField<double> values(_values.begin(), _values.end());
33 // message.mutable_values()->mutable_values()->Swap(&values);
34 //}
35 // void ValuesSignal::fromMessage(const corbo::messages::Signal& message, std::stringstream* issues)
36 //{
37 // header.fromMessage(message.header(), issues);
38 // _values.assign(message.values().values().begin(), message.values().values().end());
39 //}
40 
41 #ifdef MESSAGE_SUPPORT
42 void Measurement::toMessage(corbo::messages::Signal& message) const
43 {
44  header.toMessage(*message.mutable_header());
45  message.mutable_measurement()->set_time(_time);
46  google::protobuf::RepeatedField<double> values(_values.begin(), _values.end());
47  message.mutable_measurement()->mutable_values()->Swap(&values);
48  // labels
49  message.mutable_measurement()->clear_value_labels();
50  for (const std::string& label : _value_labels) message.mutable_measurement()->add_value_labels(label);
51 }
52 void Measurement::fromMessage(const corbo::messages::Signal& message, std::stringstream* issues)
53 {
54  header.fromMessage(message.header(), issues);
55  _time = message.measurement().time();
56  _values.assign(message.measurement().values().begin(), message.measurement().values().end());
57  // labels
58  _value_labels.clear();
59  for (int i = 0; i < message.measurement().value_labels_size(); ++i) _value_labels.push_back(message.measurement().value_labels(i));
60 }
61 #endif
62 
63 void TimeSeriesSignal::add(const Measurement& measurement)
64 {
65  if (!_time_series) _time_series = std::make_shared<TimeSeries>();
66  _time_series->add(measurement.getTime(), measurement.getValues());
67 
68  if (!measurement.getValueLabels().empty() && _time_series->getValueLabels().empty())
69  {
70  _time_series->getValueLabelsRef() = measurement.getValueLabels();
71  }
72 }
73 
74 void TimeSeriesSignal::add(double time, const Eigen::Ref<const Eigen::VectorXd>& values)
75 {
76  if (!_time_series) _time_series = std::make_shared<TimeSeries>();
77  _time_series->add(time, values);
78 }
79 
80 void TimeSeriesSignal::add(double time, const std::vector<double>& values)
81 {
82  if (!_time_series) _time_series = std::make_shared<TimeSeries>();
83  _time_series->add(time, values);
84 }
85 
87  double time_from_start)
88 {
89  if (!_time_series) _time_series = std::make_shared<TimeSeries>();
90  _time_series->set(time, values_matrix, time_from_start);
91 }
92 
93 void TimeSeriesSignal::setTimeFromStart(double time_from_start)
94 {
95  if (!_time_series) _time_series = std::make_shared<TimeSeries>();
96  _time_series->setTimeFromStart(time_from_start);
97 }
98 
99 #ifdef MESSAGE_SUPPORT
100 void TimeSeriesSignal::toMessage(corbo::messages::Signal& message) const
101 {
102  header.toMessage(*message.mutable_header());
103  if (_time_series) _time_series->toMessage(*message.mutable_time_series());
104 }
105 
106 void TimeSeriesSignal::fromMessage(const corbo::messages::Signal& message, std::stringstream* issues)
107 {
108  header.fromMessage(message.header());
109  if (!_time_series) _time_series = std::make_shared<TimeSeries>();
110  _time_series->fromMessage(message.time_series(), issues);
111 }
112 #endif
113 
115 {
116  if (!_ts_sequence) _ts_sequence = std::make_shared<TimeSeriesSequence>();
117  _ts_sequence->add(ts);
118 }
119 
120 #ifdef MESSAGE_SUPPORT
121 void TimeSeriesSequenceSignal::toMessage(corbo::messages::Signal& message) const
122 {
123  header.toMessage(*message.mutable_header());
124  if (_ts_sequence) _ts_sequence->toMessage(*message.mutable_ts_sequence());
125 }
126 void TimeSeriesSequenceSignal::fromMessage(const corbo::messages::Signal& message, std::stringstream* issues)
127 {
128  header.fromMessage(message.header());
129  if (!_ts_sequence) _ts_sequence = std::make_shared<TimeSeriesSequence>();
130  _ts_sequence->fromMessage(message.ts_sequence(), issues);
131 }
132 #endif
133 
135 {
136  std::copy(values.data(), values.data() + values.size(), std::back_inserter(_values));
137 }
138 void IndexedValuesSignal::add(const std::vector<double>& values) { std::copy(values.begin(), values.end(), std::back_inserter(_values)); }
139 
140 void IndexedValuesSignal::set(int index, double value)
141 {
142  clear();
143  setIndex(index);
144  add(value);
145 }
146 void IndexedValuesSignal::set(int index, const Eigen::Ref<const Eigen::VectorXd>& values)
147 {
148  clear();
149  setIndex(index);
150  add(values);
151 }
152 void IndexedValuesSignal::set(int index, const std::vector<double>& values)
153 {
154  clear();
155  setIndex(index);
156  add(values);
157 }
158 
159 #ifdef MESSAGE_SUPPORT
160 void IndexedValuesSignal::toMessage(messages::IndexedValues& message) const
161 {
162  message.set_index(_index);
163  google::protobuf::RepeatedField<double> values(_values.begin(), _values.end());
164  message.mutable_values()->Swap(&values);
165 }
166 
167 void IndexedValuesSignal::fromMessage(const messages::IndexedValues& message, std::stringstream* issues)
168 {
169  _index = message.index();
170  _values.assign(message.values().begin(), message.values().end());
171 }
172 #endif
173 
174 void IndexedValuesSetSignal::add(int index, double value) { _values_map[index].push_back(value); }
175 
176 void IndexedValuesSetSignal::add(const IndexedValuesSignal& indexed_values)
177 {
178  std::copy(indexed_values.getValues().begin(), indexed_values.getValues().end(), std::back_inserter(_values_map[indexed_values.getIndex()]));
179 }
180 
182 {
183  std::copy(values.data(), values.data() + values.size(), std::back_inserter(_values_map[index]));
184 }
185 
186 void IndexedValuesSetSignal::add(int index, const std::vector<double>& values)
187 {
188  std::copy(values.begin(), values.end(), std::back_inserter(_values_map[index]));
189 }
190 
192 {
193  int max_dim = 0;
194  for (const auto& elem : _values_map)
195  {
196  if ((int)elem.second.size() > max_dim) max_dim = (int)elem.second.size();
197  }
198  return max_dim;
199 }
200 
201 #ifdef MESSAGE_SUPPORT
202 void IndexedValuesSetSignal::toMessage(messages::IndexedValuesSet& message) const
203 {
204  for (auto& item : _values_map)
205  {
206  messages::Vector& value_vec = (*message.mutable_indexed_values())[item.first];
207  for (const double& value : item.second) value_vec.add_values(value);
208  }
209 }
210 
211 void IndexedValuesSetSignal::fromMessage(const messages::IndexedValuesSet& message, std::stringstream* issues)
212 {
213  clear();
214 
215  for (const auto& item : message.indexed_values())
216  {
217  std::vector<double>& value_vec = _values_map[item.first];
218  value_vec.assign(item.second.values().begin(), item.second.values().end());
219  }
220 }
221 #endif
222 
223 #ifdef MESSAGE_SUPPORT
224 void MatrixSignal::toMessage(messages::Matrix& message) const
225 {
226  message.set_rows(_matrix.rows());
227  message.set_cols(_matrix.cols());
228  message.set_row_major(true);
229 
230  message.mutable_data()->Resize(_matrix.rows() * _matrix.cols(), 0); // TODO(roesmann): inefficient, to first fill with zeros!
231  Eigen::Map<Eigen::Matrix<double, -1, -1, Eigen::RowMajor>>(message.mutable_data()->mutable_data(), _matrix.rows(), _matrix.cols()) = _matrix;
232 
233  message.set_label(_label);
234 }
235 
236 void MatrixSignal::fromMessage(const messages::Matrix& message, std::stringstream* isses)
237 {
238  if (message.row_major())
239  {
240  _matrix = Eigen::Map<const Eigen::Matrix<double, -1, -1, Eigen::RowMajor>>(message.data().data(), message.rows(), message.cols());
241  }
242  else
243  {
244  _matrix = Eigen::Map<const Eigen::Matrix<double, -1, -1, Eigen::ColMajor>>(message.data().data(), message.rows(), message.cols());
245  }
246 
247  _label = message.label();
248 }
249 
250 #endif
251 
252 void MatrixSetSignal::add(MatrixSignal::Ptr& matrix_signal) { _matrix_set.push_back(matrix_signal); }
253 
254 void MatrixSetSignal::add(const Eigen::Ref<const Eigen::MatrixXd>& matrix, const std::string& label)
255 {
256  MatrixSignal::Ptr matrix_signal = std::make_shared<MatrixSignal>(matrix, label);
257  _matrix_set.push_back(matrix_signal);
258 }
259 
260 #ifdef MESSAGE_SUPPORT
261 void MatrixSetSignal::toMessage(messages::MatrixSet& message) const
262 {
263  for (const MatrixSignal::Ptr& mat : _matrix_set)
264  {
265  messages::Matrix* mat_msg = message.add_matrices();
266  mat->toMessage(*mat_msg);
267  }
268 }
269 
270 void MatrixSetSignal::fromMessage(const messages::MatrixSet& message, std::stringstream* issues)
271 {
272  clear();
273  for (int i = 0; i < message.matrices_size(); ++i)
274  {
275  MatrixSignal::Ptr matrix_signal = std::make_shared<MatrixSignal>();
276  matrix_signal->fromMessage(message.matrices(i));
277  }
278 }
279 
280 #endif
281 
282 } // namespace corbo
corbo::MatrixSignal::Ptr
std::shared_ptr< MatrixSignal > Ptr
Definition: signals.h:564
corbo::IndexedValuesSetSignal::clear
void clear()
Definition: signals.h:529
corbo::IndexedValuesSignal::setIndex
void setIndex(int index)
Set desired index.
Definition: signals.h:439
corbo::IndexedValuesSignal
Signal containing values indexed by a single integer.
Definition: signals.h:412
corbo::IndexedValuesSetSignal::_values_map
Map _values_map
Definition: signals.h:549
corbo
Definition: communication/include/corbo-communication/utilities.h:37
corbo::IndexedValuesSignal::clear
void clear()
Definition: signals.h:463
corbo::TimeSeriesSignal::setTimeFromStart
void setTimeFromStart(double time_from_start)
Set time from start (offset to all time stamps in time())
Definition: signals.cpp:115
corbo::MatrixSetSignal::clear
void clear()
Definition: signals.h:655
copy
int EIGEN_BLAS_FUNC() copy(int *n, RealScalar *px, int *incx, RealScalar *py, int *incy)
Definition: level1_impl.h:29
Eigen::RowMajor
@ RowMajor
Definition: Constants.h:322
corbo::TimeSeriesSignal::add
void add(const Measurement &measurement)
Add measurement (time and value pair)
Definition: signals.cpp:85
matrix
Map< Matrix< T, Dynamic, Dynamic, ColMajor >, 0, OuterStride<> > matrix(T *data, int rows, int cols, int stride)
Definition: common.h:102
corbo::SignalInterface::header
SignalHeader header
The header of the signal.
Definition: signals.h:171
corbo::MatrixSignal::_label
std::string _label
Definition: signals.h:617
corbo::TimeSeriesSequenceSignal::add
void add(TimeSeries::Ptr ts)
Add a new time serie to the sequence.
Definition: signals.cpp:136
signals.h
corbo::Measurement::_time
double _time
measurement time
Definition: signals.h:247
Eigen::Map
A matrix or vector expression mapping an existing array of data.
Definition: Map.h:94
int
return int(ret)+1
corbo::TimeSeriesSequenceSignal::_ts_sequence
TimeSeriesSequence::Ptr _ts_sequence
Definition: signals.h:400
corbo::MatrixSetSignal::_matrix_set
std::vector< MatrixSignal::Ptr > _matrix_set
Definition: signals.h:675
corbo::Measurement::_values
std::vector< double > _values
corresponding value vector
Definition: signals.h:248
corbo::IndexedValuesSignal::_index
int _index
Definition: signals.h:483
corbo::MatrixSignal::_matrix
Eigen::MatrixXd _matrix
Definition: signals.h:616
Eigen::Ref
A matrix or vector expression mapping an existing expression.
Definition: Ref.h:192
corbo::TimeSeriesSignal::_time_series
TimeSeries::Ptr _time_series
Definition: signals.h:335
for
for(int i=0;i< std::min(*m, *n);++i) ipiv[i]++
corbo::IndexedValuesSetSignal::getMaxValueDimension
int getMaxValueDimension() const
Iterate internal map to find the largest value vector dimension.
Definition: signals.cpp:213
corbo::IndexedValuesSignal::_values
std::vector< double > _values
Definition: signals.h:484
corbo::IndexedValuesSignal::getValues
const std::vector< double > & getValues() const
Read access to the underlying values object.
Definition: signals.h:458
corbo::IndexedValuesSetSignal::add
void add(int index, double value)
Add index value pair.
Definition: signals.cpp:196
corbo::Measurement::_value_labels
std::vector< std::string > _value_labels
labels for value vector components (optional)
Definition: signals.h:249
Eigen::Matrix
The matrix class, also used for vectors and row-vectors.
Definition: Matrix.h:178
corbo::IndexedValuesSignal::set
void set(int index, double value)
Set index value pair (wipes off previous values)
Definition: signals.cpp:162
Eigen::ColMajor
@ ColMajor
Definition: Constants.h:320
corbo::IndexedValuesSignal::add
void add(double value)
Add value.
Definition: signals.h:442
corbo::MatrixSetSignal::add
void add(MatrixSignal::Ptr &matrix_signal)
Add matrix signal.
Definition: signals.cpp:274
corbo::TimeSeries::Ptr
std::shared_ptr< TimeSeries > Ptr
Definition: time_series.h:108
corbo::IndexedValuesSignal::getIndex
int getIndex() const
Return current index.
Definition: signals.h:456
mat
else mat
Definition: eigenvalues.cpp:43
corbo::TimeSeriesSignal::set
void set(TimeSeries::Ptr time_series)
Set time series (and override any existing)
Definition: signals.h:301


control_box_rst
Author(s): Christoph Rösmann
autogenerated on Wed Mar 2 2022 00:06:12