2010-12-31 02:20:28 -04:00
|
|
|
/*
|
|
|
|
* AP_Controller.h
|
|
|
|
* Copyright (C) James Goppert 2010 <james.goppert@gmail.com>
|
|
|
|
*
|
|
|
|
* This file 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 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This file 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 program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef AP_Controller_H
|
|
|
|
#define AP_Controller_H
|
|
|
|
|
2011-02-07 21:58:51 -04:00
|
|
|
#include <AP_Common.h>
|
2010-12-31 02:20:28 -04:00
|
|
|
#include <AP_Vector.h>
|
|
|
|
#include <AP_Var.h>
|
|
|
|
#include <AP_RcChannel.h>
|
|
|
|
#include <APM_RC.h>
|
|
|
|
|
2011-02-07 21:58:51 -04:00
|
|
|
|
|
|
|
#define APVarPtr2Float(var) (*(AP_Meta_class::meta_cast<AP_Float>((AP_Var *)var)))
|
|
|
|
|
2010-12-31 02:20:28 -04:00
|
|
|
/// Block
|
|
|
|
class Block
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
Block() :
|
|
|
|
_input(), _output()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
virtual void update(const float & dt = 0) = 0;
|
|
|
|
virtual void connect( Block * block)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
const char * getName() { return _name; }
|
2011-01-26 03:05:22 -04:00
|
|
|
const Vector < AP_Var * > & getOutput() const { return _output; }
|
2010-12-31 02:20:28 -04:00
|
|
|
protected:
|
|
|
|
const char * _name;
|
2011-02-07 19:33:12 -04:00
|
|
|
Vector< const AP_Var * > _input;
|
2011-02-07 21:58:51 -04:00
|
|
|
Vector< AP_Var * > _output;
|
2010-12-31 02:20:28 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
/// Servo Block
|
|
|
|
class ToServo: public Block
|
|
|
|
{
|
|
|
|
public:
|
2011-02-07 19:33:12 -04:00
|
|
|
ToServo(AP_RcChannel & ch) : _ch(ch)
|
2010-12-31 02:20:28 -04:00
|
|
|
{
|
|
|
|
}
|
|
|
|
virtual void connect(Block * block)
|
|
|
|
{
|
|
|
|
if (block->getOutput().getSize() > 0)
|
|
|
|
_input.push_back(block->getOutput()[0]);
|
|
|
|
}
|
|
|
|
virtual void update(const float & dt = 0)
|
|
|
|
{
|
|
|
|
if (_input.getSize() > 0)
|
2011-02-07 21:58:51 -04:00
|
|
|
_ch.setPosition(APVarPtr2Float(_output[0]));
|
2010-12-31 02:20:28 -04:00
|
|
|
}
|
|
|
|
private:
|
|
|
|
float _position;
|
2011-02-07 19:33:12 -04:00
|
|
|
AP_RcChannel & _ch;
|
2010-12-31 02:20:28 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
/// SumGain
|
|
|
|
class SumGain : public Block
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
/// Constructor that allows 1-8 sum gain pairs, more
|
|
|
|
/// can be added if necessary
|
|
|
|
SumGain(
|
2011-02-07 19:33:12 -04:00
|
|
|
AP_Var & var1 = AP_Float_zero, AP_Var & gain1 = AP_Float_zero,
|
|
|
|
AP_Var & var2 = AP_Float_zero, AP_Var & gain2 = AP_Float_zero,
|
|
|
|
AP_Var & var3 = AP_Float_zero, AP_Var & gain3 = AP_Float_zero,
|
|
|
|
AP_Var & var4 = AP_Float_zero, AP_Var & gain4 = AP_Float_zero,
|
|
|
|
AP_Var & var5 = AP_Float_zero, AP_Var & gain5 = AP_Float_zero,
|
|
|
|
AP_Var & var6 = AP_Float_zero, AP_Var & gain6 = AP_Float_zero,
|
|
|
|
AP_Var & var7 = AP_Float_zero, AP_Var & gain7 = AP_Float_zero,
|
2011-02-07 21:58:51 -04:00
|
|
|
AP_Var & var8 = AP_Float_zero, AP_Var & gain8 = AP_Float_zero)
|
2010-12-31 02:20:28 -04:00
|
|
|
{
|
2011-02-07 19:33:12 -04:00
|
|
|
if ( (&var1 == &AP_Float_zero) || (&gain1 == &AP_Float_zero) ) add(var1,gain1);
|
|
|
|
if ( (&var2 == &AP_Float_zero) || (&gain2 == &AP_Float_zero) ) add(var2,gain2);
|
|
|
|
if ( (&var3 == &AP_Float_zero) || (&gain3 == &AP_Float_zero) ) add(var3,gain3);
|
|
|
|
if ( (&var4 == &AP_Float_zero) || (&gain4 == &AP_Float_zero) ) add(var4,gain4);
|
|
|
|
if ( (&var5 == &AP_Float_zero) || (&gain5 == &AP_Float_zero) ) add(var5,gain5);
|
|
|
|
if ( (&var6 == &AP_Float_zero) || (&gain6 == &AP_Float_zero) ) add(var6,gain6);
|
|
|
|
if ( (&var7 == &AP_Float_zero) || (&gain7 == &AP_Float_zero) ) add(var7,gain7);
|
|
|
|
if ( (&var8 == &AP_Float_zero) || (&gain8 == &AP_Float_zero) ) add(var8,gain8);
|
2010-12-31 02:20:28 -04:00
|
|
|
}
|
2011-02-07 19:33:12 -04:00
|
|
|
void add(AP_Var & var, AP_Var & gain)
|
2010-12-31 02:20:28 -04:00
|
|
|
{
|
2011-02-07 19:33:12 -04:00
|
|
|
_input.push_back(&var);
|
|
|
|
_gain.push_back(&gain);
|
2010-12-31 02:20:28 -04:00
|
|
|
}
|
|
|
|
virtual void connect(Block * block)
|
|
|
|
{
|
|
|
|
if (block->getOutput().getSize() > 0)
|
|
|
|
_input.push_back(block->getOutput()[0]);
|
|
|
|
}
|
|
|
|
virtual void update(const float & dt = 0)
|
|
|
|
{
|
|
|
|
if (_output.getSize() < 1) return;
|
2011-02-07 21:58:51 -04:00
|
|
|
_output[0]=0;
|
2010-12-31 02:20:28 -04:00
|
|
|
for (int i=0;i<_input.getSize();i++)
|
|
|
|
{
|
2011-02-07 21:58:51 -04:00
|
|
|
*_output[0] = APVarPtr2Float(_output[i]) * APVarPtr2Float(_gain[i]) ;
|
2010-12-31 02:20:28 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
private:
|
2011-01-26 03:05:22 -04:00
|
|
|
Vector< AP_Var * > _gain;
|
2010-12-31 02:20:28 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
/// PID block
|
2011-02-07 19:33:12 -04:00
|
|
|
class Pid : public Block, public AP_Var_group
|
2010-12-31 02:20:28 -04:00
|
|
|
{
|
|
|
|
public:
|
2011-01-26 03:05:22 -04:00
|
|
|
Pid(AP_Var::Key key, const prog_char * name,
|
|
|
|
float kP = 0.0,
|
|
|
|
float kI = 0.0,
|
|
|
|
float kD = 0.0,
|
|
|
|
float iMax = 0.0,
|
2011-02-07 21:58:51 -04:00
|
|
|
uint8_t dFcut = 20.0
|
2010-12-31 02:20:28 -04:00
|
|
|
) :
|
2011-02-07 21:58:51 -04:00
|
|
|
AP_Var_group(key,name),
|
|
|
|
_kP(this,1,kP,PSTR("P")),
|
|
|
|
_kI(this,2,kI,PSTR("I")),
|
|
|
|
_kD(this,3,kD,PSTR("D")),
|
|
|
|
_iMax(this,4,iMax,PSTR("IMAX")),
|
|
|
|
_fCut(this,5,dFcut,PSTR("FCUT"))
|
2010-12-31 02:20:28 -04:00
|
|
|
{
|
2011-01-26 03:05:22 -04:00
|
|
|
_output.push_back(new AP_Float(0));
|
2010-12-31 02:20:28 -04:00
|
|
|
}
|
|
|
|
virtual void connect(Block * block)
|
|
|
|
{
|
|
|
|
if (block->getOutput().getSize() > 0)
|
|
|
|
_input.push_back(block->getOutput()[0]);
|
|
|
|
}
|
|
|
|
virtual void update(const float & dt = 0)
|
|
|
|
{
|
|
|
|
if (_output.getSize() < 1) return;
|
|
|
|
|
|
|
|
// derivative
|
2011-02-07 21:58:51 -04:00
|
|
|
float RC = 1/(2*M_PI*_fCut); // low pass filter
|
|
|
|
_eD = _eD + ( ((_e - APVarPtr2Float(_input[0])))/dt - _eD ) * (dt / (dt + RC));
|
2010-12-31 02:20:28 -04:00
|
|
|
|
|
|
|
// proportional, note must come after derivative
|
|
|
|
// because derivatve uses _e as previous value
|
2011-02-07 21:58:51 -04:00
|
|
|
_e = APVarPtr2Float(_input[0]);
|
2010-12-31 02:20:28 -04:00
|
|
|
|
|
|
|
// integral
|
|
|
|
_eI += _e*dt;
|
|
|
|
|
|
|
|
// pid sum
|
2011-02-07 21:58:51 -04:00
|
|
|
*_output[0] = _kP*_e + _kI*_eI + _kD*_eD;
|
2010-12-31 02:20:28 -04:00
|
|
|
}
|
|
|
|
private:
|
|
|
|
float _e; /// input
|
|
|
|
float _eI; /// integral of input
|
|
|
|
float _eD; /// derivative of input
|
2011-02-07 21:58:51 -04:00
|
|
|
AP_Float _kP; /// proportional gain
|
|
|
|
AP_Float _kI; /// integral gain
|
|
|
|
AP_Float _kD; /// derivative gain
|
|
|
|
AP_Float _iMax; /// integrator saturation
|
|
|
|
AP_Uint8 _fCut; /// derivative low-pass cut freq (Hz)
|
2010-12-31 02:20:28 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
/// Controller class
|
|
|
|
class AP_Controller
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
void addBlock(Block * block)
|
|
|
|
{
|
|
|
|
if (_blocks.getSize() > 0)
|
|
|
|
_blocks[_blocks.getSize()]->connect(block);
|
|
|
|
_blocks.push_back(block);
|
|
|
|
}
|
|
|
|
void addCh(AP_RcChannel * ch)
|
|
|
|
{
|
|
|
|
_rc.push_back(ch);
|
|
|
|
}
|
2011-02-07 19:33:12 -04:00
|
|
|
AP_RcChannel & getRc(int i)
|
2010-12-31 02:20:28 -04:00
|
|
|
{
|
2011-02-07 19:33:12 -04:00
|
|
|
return *(_rc[i]);
|
2010-12-31 02:20:28 -04:00
|
|
|
}
|
|
|
|
void update()
|
|
|
|
{
|
|
|
|
for (int i=0;i<_blocks.getSize();i++)
|
|
|
|
_blocks[i]->update();
|
|
|
|
}
|
|
|
|
private:
|
|
|
|
Vector<Block * > _blocks;
|
|
|
|
Vector<AP_RcChannel * > _rc;
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif // AP_Controller_H
|
|
|
|
|
|
|
|
// vim:ts=4:sw=4:expandtab
|