GNU Radio 3.7.2 C++ API
control_loop.h
Go to the documentation of this file.
1 /* -*- c++ -*- */
2 /*
3  * Copyright 2011,2013 Free Software Foundation, Inc.
4  *
5  * This file is part of GNU Radio
6  *
7  * GNU Radio is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 3, or (at your option)
10  * any later version.
11  *
12  * GNU Radio is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with GNU Radio; see the file COPYING. If not, write to
19  * the Free Software Foundation, Inc., 51 Franklin Street,
20  * Boston, MA 02110-1301, USA.
21  */
22 
23 #ifndef GR_BLOCKS_CONTROL_LOOP
24 #define GR_BLOCKS_CONTROL_LOOP
25 
26 #include <gnuradio/blocks/api.h>
27 
28 namespace gr {
29  namespace blocks {
30 
32  {
33  protected:
34  float d_phase, d_freq;
35  float d_max_freq, d_min_freq;
36  float d_damping, d_loop_bw;
37  float d_alpha, d_beta;
38 
39  public:
40  control_loop(void) {}
41  control_loop(float loop_bw, float max_freq, float min_freq);
42  virtual ~control_loop();
43 
44  /*! \brief update the system gains from the loop bandwidth and damping factor
45  *
46  * This function updates the system gains based on the loop
47  * bandwidth and damping factor of the system. These two
48  * factors can be set separately through their own set
49  * functions.
50  */
51  void update_gains();
52 
53  /*! \brief Advance the control loop based on the current gain
54  * settings and the inputted error signal.
55  */
56  void advance_loop(float error);
57 
58  /*! \brief Keep the phase between -2pi and 2pi
59  *
60  * This function keeps the phase between -2pi and 2pi. If the
61  * phase is greater than 2pi by d, it wraps around to be -2pi+d;
62  * similarly if it is less than -2pi by d, it wraps around to
63  * 2pi-d.
64  *
65  * This function should be called after advance_loop to keep the
66  * phase in a good operating region. It is set as a separate
67  * method in case another way is desired as this is fairly
68  * heavy-handed.
69  */
70  void phase_wrap();
71 
72  /*! \brief Keep the frequency between d_min_freq and d_max_freq
73  *
74  * This function keeps the frequency between d_min_freq and
75  * d_max_freq. If the frequency is greater than d_max_freq, it
76  * is set to d_max_freq. If the frequency is less than
77  * d_min_freq, it is set to d_min_freq.
78  *
79  * This function should be called after advance_loop to keep the
80  * frequency in the specified region. It is set as a separate
81  * method in case another way is desired as this is fairly
82  * heavy-handed.
83  */
84  void frequency_limit();
85 
86  /*******************************************************************
87  * SET FUNCTIONS
88  *******************************************************************/
89 
90  /*!
91  * \brief Set the loop bandwidth
92  *
93  * Set the loop filter's bandwidth to \p bw. This should be
94  * between 2*pi/200 and 2*pi/100 (in rads/samp). It must also be
95  * a positive number.
96  *
97  * When a new damping factor is set, the gains, alpha and beta,
98  * of the loop are recalculated by a call to update_gains().
99  *
100  * \param bw (float) new bandwidth
101  */
102  virtual void set_loop_bandwidth(float bw);
103 
104  /*!
105  * \brief Set the loop damping factor
106  *
107  * Set the loop filter's damping factor to \p df. The damping
108  * factor should be sqrt(2)/2.0 for critically damped systems.
109  * Set it to anything else only if you know what you are
110  * doing. It must be a number between 0 and 1.
111  *
112  * When a new damping factor is set, the gains, alpha and beta,
113  * of the loop are recalculated by a call to update_gains().
114  *
115  * \param df (float) new damping factor
116  */
117  void set_damping_factor(float df);
118 
119  /*!
120  * \brief Set the loop gain alpha
121  *
122  * Set's the loop filter's alpha gain parameter.
123  *
124  * This value should really only be set by adjusting the loop
125  * bandwidth and damping factor.
126  *
127  * \param alpha (float) new alpha gain
128  *
129  */
130  void set_alpha(float alpha);
131 
132  /*!
133  * \brief Set the loop gain beta
134  *
135  * Set's the loop filter's beta gain parameter.
136  *
137  * This value should really only be set by adjusting the loop
138  * bandwidth and damping factor.
139  *
140  * \param beta (float) new beta gain
141  */
142  void set_beta(float beta);
143 
144  /*!
145  * \brief Set the control loop's frequency.
146  *
147  * Set's the control loop's frequency. While this is normally
148  * updated by the inner loop of the algorithm, it could be
149  * useful to manually initialize, set, or reset this under
150  * certain circumstances.
151  *
152  * \param freq (float) new frequency
153  */
154  void set_frequency(float freq);
155 
156  /*!
157  * \brief Set the control loop's phase.
158  *
159  * Set's the control loop's phase. While this is normally
160  * updated by the inner loop of the algorithm, it could be
161  * useful to manually initialize, set, or reset this under
162  * certain circumstances.
163  *
164  * \param phase (float) new phase
165  */
166  void set_phase(float phase);
167 
168  /*!
169  * \brief Set the control loop's maximum frequency.
170  *
171  * Set the maximum frequency the control loop can track.
172  *
173  * \param freq (float) new max frequency
174  */
175  void set_max_freq(float freq);
176 
177  /*!
178  * \brief Set the control loop's minimum frequency.
179  *
180  * Set the minimum frequency the control loop can track.
181  *
182  * \param freq (float) new min frequency
183  */
184  void set_min_freq(float freq);
185 
186  /*******************************************************************
187  * GET FUNCTIONS
188  *******************************************************************/
189 
190  /*!
191  * \brief Returns the loop bandwidth
192  */
193  float get_loop_bandwidth() const;
194 
195  /*!
196  * \brief Returns the loop damping factor
197  */
198  float get_damping_factor() const;
199 
200  /*!
201  * \brief Returns the loop gain alpha
202  */
203  float get_alpha() const;
204 
205  /*!
206  * \brief Returns the loop gain beta
207  */
208  float get_beta() const;
209 
210  /*!
211  * \brief Get the control loop's frequency estimate
212  */
213  float get_frequency() const;
214 
215  /*!
216  * \brief Get the control loop's phase estimate
217  */
218  float get_phase() const;
219 
220  /*!
221  * \brief Get the control loop's maximum frequency.
222  */
223  float get_max_freq() const;
224 
225  /*!
226  * \brief Get the control loop's minimum frequency.
227  */
228  float get_min_freq() const;
229  };
230 
231  } /* namespace blocks */
232 } /* namespace gr */
233 
234 #endif /* GR_BLOCKS_CONTROL_LOOP */
float d_loop_bw
Definition: control_loop.h:36
float d_beta
Definition: control_loop.h:37
#define BLOCKS_API
Definition: gr-blocks/include/gnuradio/blocks/api.h:30
float d_phase
Definition: control_loop.h:34
Definition: control_loop.h:31
float d_min_freq
Definition: control_loop.h:35
control_loop(void)
Definition: control_loop.h:40