PID controller: converted to general block; fixed initialization

This commit is contained in:
Thomas Kolb 2019-08-04 01:42:58 +02:00
parent 014fd28f22
commit 53a06afd69
3 changed files with 17 additions and 8 deletions

View file

@ -23,7 +23,7 @@
#define INCLUDED_HAMNET70_PID_CONTROLLER_H
#include <hamnet70/api.h>
#include <gnuradio/sync_block.h>
#include <gnuradio/block.h>
namespace gr {
namespace hamnet70 {
@ -33,7 +33,7 @@ namespace gr {
* \ingroup hamnet70
*
*/
class HAMNET70_API pid_controller : virtual public gr::sync_block
class HAMNET70_API pid_controller : virtual public gr::block
{
public:
typedef boost::shared_ptr<pid_controller> sptr;

View file

@ -39,13 +39,14 @@ namespace gr {
* The private constructor
*/
pid_controller_impl::pid_controller_impl(size_t interval, float p, float i, float d, float post_gain)
: gr::sync_block("pid_controller",
: gr::block("pid_controller",
gr::io_signature::make(1, 1, sizeof(float)),
gr::io_signature::make(0, 0, 0)),
d_interval(interval),
d_lastInput(0),
d_iVal(0),
d_postGain(post_gain)
d_postGain(post_gain),
d_nextSampleIdx(0)
{
// TODO: scale with interval?
d_kp = p;
@ -63,13 +64,16 @@ namespace gr {
}
int
pid_controller_impl::work(int noutput_items,
pid_controller_impl::general_work(int noutput_items,
gr_vector_int &ninput_items,
gr_vector_const_void_star &input_items,
gr_vector_void_star &output_items)
{
const float *in = (const float *) input_items[0];
while(d_nextSampleIdx < noutput_items) {
int ninput_avail = ninput_items[0];
while(d_nextSampleIdx < ninput_avail) {
const float &inp = in[d_nextSampleIdx];
// proportional part
@ -88,7 +92,11 @@ namespace gr {
d_nextSampleIdx += d_interval;
}
d_nextSampleIdx -= noutput_items;
d_nextSampleIdx -= ninput_avail;
consume(0, ninput_avail);
noutput_items = 0;
// Tell runtime system how many output items we produced.
return noutput_items;

View file

@ -46,7 +46,8 @@ namespace gr {
~pid_controller_impl();
// Where all the action really happens
int work(int noutput_items,
int general_work(int noutput_items,
gr_vector_int &ninput_items,
gr_vector_const_void_star &input_items,
gr_vector_void_star &output_items);
};