GNU Radio 3.7.1 C++ API
control_loop.h
Go to the documentation of this file.
00001 /* -*- c++ -*- */
00002 /*
00003  * Copyright 2011,2013 Free Software Foundation, Inc.
00004  *
00005  * This file is part of GNU Radio
00006  *
00007  * GNU Radio is free software; you can redistribute it and/or modify
00008  * it under the terms of the GNU General Public License as published by
00009  * the Free Software Foundation; either version 3, or (at your option)
00010  * any later version.
00011  *
00012  * GNU Radio is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  * GNU General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU General Public License
00018  * along with GNU Radio; see the file COPYING.  If not, write to
00019  * the Free Software Foundation, Inc., 51 Franklin Street,
00020  * Boston, MA 02110-1301, USA.
00021  */
00022 
00023 #ifndef GR_BLOCKS_CONTROL_LOOP
00024 #define GR_BLOCKS_CONTROL_LOOP
00025 
00026 #include <gnuradio/blocks/api.h>
00027 
00028 namespace gr {
00029   namespace blocks {
00030 
00031     class BLOCKS_API control_loop
00032     {
00033     protected:
00034       float d_phase, d_freq;
00035       float d_max_freq, d_min_freq;
00036       float d_damping, d_loop_bw;
00037       float d_alpha, d_beta;
00038 
00039     public:
00040       control_loop(void) {}
00041       control_loop(float loop_bw, float max_freq, float min_freq);
00042       virtual ~control_loop();
00043 
00044       /*! \brief update the system gains from the loop bandwidth and damping factor
00045        *
00046        *  This function updates the system gains based on the loop
00047        *  bandwidth and damping factor of the system. These two
00048        *  factors can be set separately through their own set
00049        *  functions.
00050        */
00051       void update_gains();
00052 
00053       /*! \brief Advance the control loop based on the current gain
00054        *  settings and the inputted error signal.
00055        */
00056       void advance_loop(float error);
00057 
00058       /*! \brief Keep the phase between -2pi and 2pi
00059        *
00060        * This function keeps the phase between -2pi and 2pi. If the
00061        * phase is greater than 2pi by d, it wraps around to be -2pi+d;
00062        * similarly if it is less than -2pi by d, it wraps around to
00063        * 2pi-d.
00064        *
00065        * This function should be called after advance_loop to keep the
00066        * phase in a good operating region. It is set as a separate
00067        * method in case another way is desired as this is fairly
00068        * heavy-handed.
00069        */
00070       void phase_wrap();
00071 
00072       /*! \brief Keep the frequency between d_min_freq and d_max_freq
00073        *
00074        * This function keeps the frequency between d_min_freq and
00075        * d_max_freq. If the frequency is greater than d_max_freq, it
00076        * is set to d_max_freq.  If the frequency is less than
00077        * d_min_freq, it is set to d_min_freq.
00078        *
00079        * This function should be called after advance_loop to keep the
00080        * frequency in the specified region. It is set as a separate
00081        * method in case another way is desired as this is fairly
00082        * heavy-handed.
00083        */
00084       void frequency_limit();
00085 
00086       /*******************************************************************
00087        * SET FUNCTIONS
00088        *******************************************************************/
00089 
00090       /*!
00091        * \brief Set the loop bandwidth
00092        *
00093        * Set the loop filter's bandwidth to \p bw. This should be
00094        * between 2*pi/200 and 2*pi/100 (in rads/samp). It must also be
00095        * a positive number.
00096        *
00097        * When a new damping factor is set, the gains, alpha and beta,
00098        * of the loop are recalculated by a call to update_gains().
00099        *
00100        * \param bw    (float) new bandwidth
00101        */
00102       virtual void set_loop_bandwidth(float bw);
00103 
00104       /*!
00105        * \brief Set the loop damping factor
00106        *
00107        * Set the loop filter's damping factor to \p df. The damping
00108        * factor should be sqrt(2)/2.0 for critically damped systems.
00109        * Set it to anything else only if you know what you are
00110        * doing. It must be a number between 0 and 1.
00111        *
00112        * When a new damping factor is set, the gains, alpha and beta,
00113        * of the loop are recalculated by a call to update_gains().
00114        *
00115        * \param df    (float) new damping factor
00116        */
00117       void set_damping_factor(float df);
00118 
00119       /*!
00120        * \brief Set the loop gain alpha
00121        *
00122        * Set's the loop filter's alpha gain parameter.
00123        *
00124        * This value should really only be set by adjusting the loop
00125        * bandwidth and damping factor.
00126        *
00127        * \param alpha    (float) new alpha gain
00128        *
00129        */
00130       void set_alpha(float alpha);
00131 
00132       /*!
00133        * \brief Set the loop gain beta
00134        *
00135        * Set's the loop filter's beta gain parameter.
00136        *
00137        * This value should really only be set by adjusting the loop
00138        * bandwidth and damping factor.
00139        *
00140        * \param beta    (float) new beta gain
00141        */
00142       void set_beta(float beta);
00143 
00144       /*!
00145        * \brief Set the control loop's frequency.
00146        *
00147        * Set's the control loop's frequency. While this is normally
00148        * updated by the inner loop of the algorithm, it could be
00149        * useful to manually initialize, set, or reset this under
00150        * certain circumstances.
00151        *
00152        * \param freq    (float) new frequency
00153        */
00154       void set_frequency(float freq);
00155 
00156       /*!
00157        * \brief Set the control loop's phase.
00158        *
00159        * Set's the control loop's phase. While this is normally
00160        * updated by the inner loop of the algorithm, it could be
00161        * useful to manually initialize, set, or reset this under
00162        * certain circumstances.
00163        *
00164        * \param phase    (float) new phase
00165        */
00166       void set_phase(float phase);
00167 
00168       /*!
00169        * \brief Set the control loop's maximum frequency.
00170        *
00171        * Set the maximum frequency the control loop can track. 
00172        *
00173        * \param freq    (float) new max frequency
00174        */
00175       void set_max_freq(float freq);
00176 
00177       /*!
00178        * \brief Set the control loop's minimum frequency.
00179        *
00180        * Set the minimum frequency the control loop can track. 
00181        *
00182        * \param freq    (float) new min frequency
00183        */
00184       void set_min_freq(float freq);
00185 
00186       /*******************************************************************
00187        * GET FUNCTIONS
00188        *******************************************************************/
00189 
00190       /*!
00191        * \brief Returns the loop bandwidth
00192        */
00193       float get_loop_bandwidth() const;
00194 
00195       /*!
00196        * \brief Returns the loop damping factor
00197        */
00198       float get_damping_factor() const;
00199 
00200       /*!
00201        * \brief Returns the loop gain alpha
00202        */
00203       float get_alpha() const;
00204 
00205       /*!
00206        * \brief Returns the loop gain beta
00207        */
00208       float get_beta() const;
00209 
00210       /*!
00211        * \brief Get the control loop's frequency estimate
00212        */
00213       float get_frequency() const;
00214 
00215       /*!
00216        * \brief Get the control loop's phase estimate
00217        */
00218       float get_phase() const;
00219 
00220       /*!
00221        * \brief Get the control loop's maximum frequency.
00222        */
00223       float get_max_freq() const;
00224 
00225       /*!
00226        * \brief Get the control loop's minimum frequency.
00227        */
00228       float get_min_freq() const;
00229     };
00230 
00231   } /* namespace blocks */
00232 } /* namespace gr */
00233 
00234 #endif /* GR_BLOCKS_CONTROL_LOOP */