Controlpp
Loading...
Searching...
No Matches
Generator.hpp
Go to the documentation of this file.
1#pragma once
12#include <Eigen/Dense>
13
14#include <controlpp/math.hpp>
15
16namespace controlpp
17{
30 template<class T>
32 private:
33 T frequency_;
34 T sample_time_;
35 Eigen::Vector<T, 2> x_;
36 Eigen::Matrix<T, 2, 2> A_;
37
38 public:
39
47 SineGenerator(const T& frequency, const T& sample_time)
48 : frequency_(frequency)
49 , sample_time_(sample_time)
50 , x_(static_cast<T>(0), static_cast<T>(1))
51 {
52 this->set_frequency_and_sample_time(frequency, sample_time);
53 }
54
55 void set_frequency(const T& frequency){
56 this->set_frequency_and_sample_time(frequency, this->sample_time_);
57 }
58
59 void set_sample_time(const T& sample_time){
60 this->set_frequency_and_sample_time(this->frequency_, sample_time);
61 }
62
63 void set_frequency_and_sample_time(const T& frequency, const T& sample_time){
64 // set members
65 this->frequency_ = frequency;
66 this->sample_time_ = sample_time;
67
68 // continuous dynamic matrix
69 Eigen::Matrix<T, 2, 2> A_continuous({
70 {0, frequency},
71 {-frequency, 0}
72 });
73
74 // discretised dynamic matrix
75 Eigen::Matrix<T, 2, 2> ATs = A_continuous * sample_time;
76 this->A_ = controlpp::expm(ATs);
77 }
78
84 T sin() const {return this->x_(0);}
85
91 T cos() const {return this->x_(1);}
92
98 Eigen::Vector<T, 2> next(){
99 Eigen::Vector<T, 2> new_x = this->A_ * this->x_;
100 this->x_ = new_x;
101 return new_x;
102 }
103
108 void reset(){
109 this->x_(0) = static_cast<T>(0);
110 this->x_(1) = static_cast<T>(1);
111 }
112
113 };
114} // namespace controlpp
115
A lite weight incremental sine generator.
Definition Generator.hpp:31
SineGenerator(const T &frequency, const T &sample_time)
Constructs a sine generator.
Definition Generator.hpp:47
T cos() const
Returns the current cosine output.
Definition Generator.hpp:91
void set_frequency_and_sample_time(const T &frequency, const T &sample_time)
Definition Generator.hpp:63
void set_frequency(const T &frequency)
Definition Generator.hpp:55
void reset()
Resets the Sine generator to the initial position.
Definition Generator.hpp:108
Eigen::Vector< T, 2 > next()
calculates the next output
Definition Generator.hpp:98
void set_sample_time(const T &sample_time)
Definition Generator.hpp:59
T sin() const
Returns the current sine output.
Definition Generator.hpp:84
The main namespace for the Control++ library.
Definition Bode.cpp:3
Eigen::Matrix< T, N, N > expm(const Eigen::Matrix< T, N, N, Options, MaxRows, MaxCols > &M)
Calculates the matrix exponent .
Definition expm.hpp:275