5 * Created by Andreas Lyder on 31/03/08.
6 * Copyright 2008 __MyCompanyName__. All rights reserved.
12 #include <cyg/kernel/kapi.h> /* All the kernel specific stuff */
13 #include <cyg/hal/hal_arch.h>
14 #include "pidcontrol.h"
20 /* GLOBAL VARIABLES */
22 pidcontrol_t
*create_pidcontrol(double pGain
, double iGain
, double dGain
, double iMax
, double iMin
) {
23 pidcontrol_t
*pid
= malloc(sizeof(pidcontrol_t
));
36 void release_pidcontrol(pidcontrol_t
*pid
) {
40 double update_pidcontrol(pidcontrol_t
* pid
, double error
, double position
) {
41 double pTerm
,dTerm
=0,iTerm
=0;
43 pTerm
= pid
->pGain
* error
;
45 if (pid
->iGain
!= 0) {
47 if (pid
->iState
> pid
->iMax
) pid
->iState
= pid
->iMax
;
48 else if (pid
->iState
< pid
->iMin
) pid
->iState
= pid
->iMin
;
49 iTerm
= pid
->iGain
* pid
->iState
;
52 if (pid
->dGain
!= 0) {
53 dTerm
= pid
->dGain
* (position
- pid
->dState
);
54 pid
->dState
= position
;
57 return pTerm
+ iTerm
+ dTerm
;