disturbances.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 
26 
28 
29 namespace corbo {
30 
32 {
33  // first copy values
34  disturbed_values = values;
35 
36  if (values.size() != _mean.size() || values.size() != _std.size())
37  {
38  PRINT_ERROR_NAMED("Cannot disturb due to dimension mismatch");
39  return;
40  }
41 
42  // check if we already have already initialized our distributions
43  if (_distributions.size() != values.size()) initializeDistributions();
44 
45  // now add noise
46  for (int i = 0; i < values.size(); ++i)
47  {
48  if (approx_zero(_std[i]))
49  {
50  if (!approx_zero(_mean[i]))
51  disturbed_values[i] += _mean[i];
52  else
53  continue; // we skip this component
54  }
55  disturbed_values[i] += _distributions[i](_random_engine);
56  }
57 }
58 
60 {
61  reset();
62 
63  if (_mean.size() != _std.size())
64  {
65  PRINT_ERROR_NAMED("Cannot initialize distributions due to dimension mismatch between mean and std vector.");
66  return;
67  }
68 
69  _distributions.clear();
70  for (int i = 0; i < _mean.size(); ++i)
71  {
72  _distributions.emplace_back(_mean[i], _std[i]);
73  }
74 }
75 
77 {
78  _seed = seed;
79  reset(); // this should also reset the seed
80 }
81 
83 {
84  _random_engine.seed(_seed);
85  _distributions.clear();
86 }
87 
88 bool DisturbanceGaussianNoise::checkParameters(int values_dim, std::stringstream* issues) const
89 {
90  bool success = true;
91 
92  if (_mean.size() != values_dim)
93  {
94  if (issues)
95  *issues << "DisturbanceGaussianNoise: Mean vector dimension (" << _mean.size() << ") does not match required value dimension ("
96  << values_dim << ")." << std::endl;
97  success = false;
98  }
99 
100  if (_std.size() != values_dim)
101  {
102  if (issues)
103  *issues << "DisturbanceGaussianNoise: Std vector dimension (" << _std.size() << ") does not match required value dimension ("
104  << values_dim << ")." << std::endl;
105  success = false;
106  }
107  return success;
108 }
109 
110 #ifdef MESSAGE_SUPPORT
111 void DisturbanceGaussianNoise::toMessage(corbo::messages::DisturbanceGaussianNoise& message) const
112 {
113  // mean
114  message.mutable_mean_vec()->Resize(_mean.rows(), 0);
115  Eigen::Map<Eigen::VectorXd>(message.mutable_mean_vec()->mutable_data(), _mean.rows()) = _mean;
116 
117  // std
118  message.mutable_std_vec()->Resize(_std.rows(), 0);
119  Eigen::Map<Eigen::VectorXd>(message.mutable_std_vec()->mutable_data(), _std.rows()) = _std;
120 }
121 
122 void DisturbanceGaussianNoise::fromMessage(const corbo::messages::DisturbanceGaussianNoise& message, std::stringstream* issues)
123 {
124  // mean
125  if (message.mean_vec_size() > 0)
126  _mean = Eigen::Map<const Eigen::VectorXd>(message.mean_vec().data(), message.mean_vec_size());
127  else
128  _mean.resize(0);
129 
130  // std
131  if (message.std_vec_size() > 0)
132  _std = Eigen::Map<const Eigen::VectorXd>(message.std_vec().data(), message.mean_vec_size());
133  else
134  _std.resize(0);
135 
137 }
138 #endif
139 
140 } // namespace corbo
void reset() override
reset internal state
#define PRINT_ERROR_NAMED(msg)
Definition: console.h:260
A matrix or vector expression mapping an existing array of data.
Definition: Map.h:94
Representation of time stamps.
Definition: time.h:251
bool checkParameters(int values_dim, std::stringstream *issues) const override
Check the underlying parameter configuration for validity.
A matrix or vector expression mapping an existing expression.
Definition: Ref.h:192
std::vector< std::normal_distribution< double > > _distributions
Definition: disturbances.h:88
bool approx_zero(double val, double epsilon=1e-10)
Check if a double is appoximately zero.
void disturb(const Time &t, const Eigen::Ref< const Eigen::VectorXd > &values, Eigen::Ref< Eigen::VectorXd > disturbed_values) override
Modify values according to the underlying disturbance model.


control_box_rst
Author(s): Christoph Rösmann
autogenerated on Mon Feb 28 2022 22:06:51