ros2_control - rolling
dither.hpp
1 // Copyright (c) 2009, Willow Garage, Inc.
2 // All rights reserved.
3 //
4 // Software License Agreement (BSD License 2.0)
5 //
6 // Redistribution and use in source and binary forms, with or without
7 // modification, are permitted provided that the following conditions
8 // are met:
9 //
10 // * Redistributions of source code must retain the above copyright
11 // notice, this list of conditions and the following disclaimer.
12 // * Redistributions in binary form must reproduce the above
13 // copyright notice, this list of conditions and the following
14 // disclaimer in the documentation and/or other materials provided
15 // with the distribution.
16 // * Neither the name of the Willow Garage nor the names of its
17 // contributors may be used to endorse or promote products derived
18 // from this software without specific prior written permission.
19 //
20 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23 // FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24 // COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25 // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
26 // BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
27 // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
28 // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 // LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
30 // ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 // POSSIBILITY OF SUCH DAMAGE.
32 
33 // \author Kevin Watts
34 
35 #ifndef CONTROL_TOOLBOX__DITHER_HPP_
36 #define CONTROL_TOOLBOX__DITHER_HPP_
37 
38 #include <math.h>
39 #include <cstdlib>
40 #include <ctime>
41 #include <random>
42 
43 #include "rcutils/logging_macros.h"
44 
45 namespace control_toolbox
46 {
47 /***************************************************/
57 class Dither
58 {
59 public:
60  Dither();
61 
66  double update();
67 
68  /*
69  *\brief Dither gets an amplitude, must be >0 to initialize
70  *
71  *\param amplitude Amplitude of white noise output
72  *\param seed Random seed for white noise
73  */
74  bool init(const double & amplitude, const double & seed)
75  {
76  if (amplitude < 0.0) {
77  RCUTILS_LOG_ERROR("Dither amplitude not set properly. Amplitude must be >0.");
78  return false;
79  }
80 
81  amplitude_ = amplitude;
82 
83  // seed generator for reproducible sequence of random numbers
84  generator_.seed(static_cast<unsigned int>(seed));
85 
86  return true;
87  }
88 
89  /*
90  *\brief Generate a random number with random_device for non-deterministic random numbers
91  */
92  static double generateRandomSeed()
93  {
94  std::random_device rdev{};
95  return static_cast<double>(rdev());
96  }
97 
98 private:
99  double amplitude_;
100  double saved_value_;
101  bool has_saved_value_;
102  std::mt19937 generator_;
103 };
104 } // namespace control_toolbox
105 
106 #endif // CONTROL_TOOLBOX__DITHER_HPP_
Gives white noise at specified amplitude.
Definition: dither.hpp:58
double update()
Get next Gaussian white noise point. Called in RT loop.
Definition: dither.cpp:45
Definition: dither.hpp:46