2019-07-23 23:16:48 +02:00
|
|
|
/* -*- c++ -*- */
|
|
|
|
/*
|
|
|
|
* Copyright 2019 Thomas Kolb.
|
|
|
|
*
|
|
|
|
* This is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 3, or (at your option)
|
|
|
|
* any later version.
|
|
|
|
*
|
|
|
|
* This software is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this software; see the file COPYING. If not, write to
|
|
|
|
* the Free Software Foundation, Inc., 51 Franklin Street,
|
|
|
|
* Boston, MA 02110-1301, USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <gnuradio/io_signature.h>
|
|
|
|
#include "pid_controller_impl.h"
|
|
|
|
|
|
|
|
namespace gr {
|
|
|
|
namespace hamnet70 {
|
|
|
|
|
|
|
|
pid_controller::sptr
|
|
|
|
pid_controller::make(size_t interval, float p, float i, float d, float post_gain)
|
|
|
|
{
|
|
|
|
return gnuradio::get_initial_sptr
|
|
|
|
(new pid_controller_impl(interval, p, i, d, post_gain));
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The private constructor
|
|
|
|
*/
|
|
|
|
pid_controller_impl::pid_controller_impl(size_t interval, float p, float i, float d, float post_gain)
|
2019-08-04 01:42:58 +02:00
|
|
|
: gr::block("pid_controller",
|
2019-07-23 23:16:48 +02:00
|
|
|
gr::io_signature::make(1, 1, sizeof(float)),
|
|
|
|
gr::io_signature::make(0, 0, 0)),
|
|
|
|
d_interval(interval),
|
|
|
|
d_lastInput(0),
|
|
|
|
d_iVal(0),
|
2019-08-04 01:42:58 +02:00
|
|
|
d_postGain(post_gain),
|
|
|
|
d_nextSampleIdx(0)
|
2019-07-23 23:16:48 +02:00
|
|
|
{
|
|
|
|
// TODO: scale with interval?
|
|
|
|
d_kp = p;
|
|
|
|
d_ki = i;
|
|
|
|
d_kd = d;
|
|
|
|
|
|
|
|
message_port_register_out(pmt::mp("control_value"));
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Our virtual destructor.
|
|
|
|
*/
|
|
|
|
pid_controller_impl::~pid_controller_impl()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2019-08-04 01:42:58 +02:00
|
|
|
pid_controller_impl::general_work(int noutput_items,
|
|
|
|
gr_vector_int &ninput_items,
|
2019-07-23 23:16:48 +02:00
|
|
|
gr_vector_const_void_star &input_items,
|
|
|
|
gr_vector_void_star &output_items)
|
|
|
|
{
|
|
|
|
const float *in = (const float *) input_items[0];
|
|
|
|
|
2019-08-04 01:42:58 +02:00
|
|
|
int ninput_avail = ninput_items[0];
|
|
|
|
|
|
|
|
while(d_nextSampleIdx < ninput_avail) {
|
2019-07-23 23:16:48 +02:00
|
|
|
const float &inp = in[d_nextSampleIdx];
|
|
|
|
|
|
|
|
// proportional part
|
|
|
|
float control_value = d_kp * inp;
|
|
|
|
|
|
|
|
// integral part
|
|
|
|
d_iVal += d_ki * inp;
|
|
|
|
control_value += d_iVal;
|
|
|
|
|
|
|
|
// differential part
|
|
|
|
control_value += d_kd * (inp - d_lastInput);
|
|
|
|
d_lastInput = inp;
|
|
|
|
|
|
|
|
message_port_pub(pmt::intern("control_value"), pmt::from_double(d_postGain * control_value));
|
|
|
|
|
|
|
|
d_nextSampleIdx += d_interval;
|
|
|
|
}
|
|
|
|
|
2019-08-04 01:42:58 +02:00
|
|
|
d_nextSampleIdx -= ninput_avail;
|
|
|
|
|
|
|
|
consume(0, ninput_avail);
|
|
|
|
|
|
|
|
noutput_items = 0;
|
2019-07-23 23:16:48 +02:00
|
|
|
|
|
|
|
// Tell runtime system how many output items we produced.
|
|
|
|
return noutput_items;
|
|
|
|
}
|
|
|
|
|
|
|
|
} /* namespace hamnet70 */
|
|
|
|
} /* namespace gr */
|
|
|
|
|