Skip to content

Commit a585815

Browse files
author
camilo
committed
fix to pid. bump to 1.2.2
1 parent e2116fb commit a585815

File tree

7 files changed

+64
-15
lines changed

7 files changed

+64
-15
lines changed

.vscode/settings.json

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,18 @@
5050
"iomanip": "cpp",
5151
"xloctime": "cpp",
5252
"string": "cpp",
53-
"numbers": "cpp"
53+
"numbers": "cpp",
54+
"thread": "cpp",
55+
"charconv": "cpp",
56+
"format": "cpp",
57+
"forward_list": "cpp",
58+
"locale": "cpp",
59+
"mutex": "cpp",
60+
"sstream": "cpp",
61+
"stop_token": "cpp",
62+
"vector": "cpp",
63+
"xlocbuf": "cpp",
64+
"xlocmes": "cpp"
5465
},
5566
"C_Cpp.errorSquiggles": "disabled"
5667
}

library.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
"maintainer": true
1717
}
1818
],
19-
"version": "1.2.1",
19+
"version": "1.2.2",
2020
"license": "MIT",
2121
"frameworks": "arduino",
2222
"platforms": "*"

library.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name=qlibs
2-
version=1.2.1
2+
version=1.2.2
33
license=MIT
44
author=J. Camilo Gomez C. <[email protected]>
55
maintainer=J. Camilo Gomez C. <[email protected]>

src/include/ltisys.hpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,13 @@ namespace qlibs {
2424
* @{
2525
*/
2626

27+
/** @cond **/
28+
/*only for continuousSystem*/
29+
#if !defined( LTISYS_EVAL_MODEL_CONTROLLABLE ) || !defined( LTISYS_EVAL_MODEL_OBSERVABLE )
30+
#define LTISYS_EVAL_MODEL_CONTROLLABLE
31+
#endif
32+
/** @endcond **/
33+
2734
/**
2835
* @brief All the possible natures of a LTI system.
2936
*/
@@ -55,6 +62,7 @@ namespace qlibs {
5562
real_t den[ order+1 ];
5663
continuousStates<order> states = {};
5764
continuousTF( const real_t ( &numerator )[ order + 1 ], const real_t ( &denominator )[ order + 1 ] ) {
65+
static_assert( order >= 1 , "Order should be greater than 0" );
5866
for ( size_t i = 0; i <= order; ++i ) {
5967
num[ i ] = numerator[ i ];
6068
den[ i ] = denominator[ i ];

src/ltisys.cpp

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -168,11 +168,11 @@ bool continuousSystem::setup( real_t *num,
168168
{
169169
bool retValue = false;
170170

171-
if ( ( nullptr != num ) && ( nullptr != den ) && ( nullptr != x ) ) {
171+
if ( ( nullptr != num ) && ( nullptr != den ) && ( nullptr != x ) && ( nD >= 1U ) ) {
172172
b = &num[ 1 ];
173173
n = nD;
174174
nb = n;
175-
na = nD + 1;
175+
na = nD + 1U;
176176
xc = x;
177177
dt = dT;
178178
a = &den[ 1 ];
@@ -206,6 +206,7 @@ bool continuousSystem::setInitStates( const real_t *xi )
206206
return retValue;
207207
}
208208
/*============================================================================*/
209+
#if defined( LTISYS_EVAL_MODEL_CONTROLLABLE )
209210
real_t continuousSystem::update( const real_t u )
210211
{
211212
real_t y = 0.0_re;
@@ -214,7 +215,6 @@ real_t continuousSystem::update( const real_t u )
214215
if ( 1U == n ) {
215216
dx0 = ( u - ( xc[ 0 ]*a[ 0 ] ) );
216217
(void)xc[ 0 ].integrate( dx0, dt );
217-
y = ( b[ 0 ] - ( a[ 0 ]*b0 ) )*xc[ 0 ];
218218
}
219219
else {
220220
/*compute states of the system by using the controllable canonical form*/
@@ -229,11 +229,42 @@ real_t continuousSystem::update( const real_t u )
229229
dx0 = u - ( dx0 + ( a[ 0 ]*xc[ 0 ] ) );
230230
(void)xc[ 0 ].integrate( dx0, dt ); /*integrate to get the first state*/
231231
/*compute the remaining part of the output*/
232-
y += ( b[ 0 ] - ( a[ 0 ]*b0 ) )*xc[ 0 ];
233232
}
233+
/*get the output of the system*/
234+
y += ( b[ 0 ] - ( a[ 0 ]*b0 ) )*xc[ 0 ];
234235

235236
return y;
236237
}
238+
#elif defined( LTISYS_EVAL_MODEL_OBSERVABLE )
239+
/*============================================================================*/
240+
real_t continuousSystem::update( const real_t u )
241+
{
242+
real_t y = 0.0_re;
243+
const real_t x0 = xc[ 0 ](); /*save first state for computation*/
244+
const size_t N = n - 1U;
245+
/*compute the last derivative dx_{n}*/
246+
const real_t dxn = ( -a[ N ]*x0 ) + ( ( b[ N ] - a[ N ]*b0 )*u );
247+
248+
if ( 1U == n ) {
249+
xc[ 0 ].integrate( dxn, dt );
250+
}
251+
else {
252+
/*compute states of the system by using the observable canonical form*/
253+
for ( size_t i = 0U; i < N ; ++i ) {
254+
const real_t dxi = ( -a[ i ]*x0 ) + xc[ i + 1U ] + ( ( b[ i ] - a[ i ]*b0 )*u );
255+
(void)xc[ i ].integrate( dxi , dt );
256+
}
257+
/*integrate the remaining last state*/
258+
(void)xc[ N ].integrate( dxn, dt );
259+
}
260+
/*get the output of the system*/
261+
y = xc[ 0 ] + ( b0*u );
262+
263+
return y;
264+
}
265+
#else
266+
#error "LTISYS evaluation mode not defined"
267+
#endif
237268
/*============================================================================*/
238269
bool continuousSystem::setIntegrationMethod( integrationMethod m )
239270
{

src/pid.cpp

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#include <include/pid.hpp>
22
#include <include/ffmath.hpp>
3-
3+
#include <iostream>
44
using namespace qlibs;
55

66

@@ -265,8 +265,8 @@ real_t pidController::control( const real_t w,
265265
if ( ffmath::absf( e ) <= epsilon ) {
266266
e = 0.0_re;
267267
}
268-
ie = c_state.integrate( e + u1, dt, false );
269-
de = c_state.derive( ( c*w ) - y , dt );
268+
de = c_state.derive( ( c*w ) - y , dt, false );
269+
ie = c_state.integrate( e + u1 , dt );
270270
D = de + beta*( D - de ); /*derivative filtering*/
271271
v = ( kc*( ( b*w ) - y ) ) + ( ki*ie ) + ( kd*D ); /*compute PID action*/
272272
if ( nullptr != yr ) {
@@ -285,8 +285,7 @@ real_t pidController::control( const real_t w,
285285
sw = ( pidMode::PID_AUTOMATIC == mode ) ? v : m;
286286
uSat = saturate( sw, sat_Min, sat_Max );
287287
u = uSat; /*output saturated*/
288-
/*anti-windup feedback*/
289-
u1 = kw*( u - v );
288+
u1 = kw*( u - v ); /*anti-windup feedback*/
290289
if ( nullptr != adapt ) {
291290
adaptGains( u, y );
292291
}

src/qlibs.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*!
22
* @file qlibs.h
33
* @author J. Camilo Gomez C.
4-
* @version 1.1.9
4+
* @version 1.2.2
55
* @note This file is part of the qlibs++ distribution.
66
* @brief Global inclusion header
77
**/
@@ -41,8 +41,8 @@ This file is part of the QuarkTS++ OS distribution.
4141
#ifndef QLIBS_CPP_H
4242
#define QLIBS_CPP_H
4343

44-
#define QLIBS_CPP_VERSION "1.2.1"
45-
#define QLIBS_CPP_VERNUM ( 121U )
44+
#define QLIBS_CPP_VERSION "1.2.2"
45+
#define QLIBS_CPP_VERNUM ( 122U )
4646
#define QLIBS_CPP_CAPTION "qLibs++" QLIBS_CPP_VERSION
4747

4848
#include <include/qlibs_types.hpp>

0 commit comments

Comments
 (0)