rllib  1
Functions
rlcontroller.cpp File Reference
#include "rlcontroller.h"
Include dependency graph for rlcontroller.cpp:

Go to the source code of this file.

Functions

static void * control (void *arg)
 

Function Documentation

◆ control()

static void* control ( void *  arg)
static

Definition at line 18 of file rlcontroller.cpp.

19 {
20  THREAD_PARAM *p = (THREAD_PARAM *) arg;
21  rlController *c = (rlController *) p->user;
22  while(c->running == 1)
23  {
24  if(c->sleepLocally) rlsleep(c->dt);
25  c->measurement = c->getMeasurement();
26  c->lock(); // lock mutex because user might set another controller type
27  c->ydk_1 = c->ydk;
28  c->y1k_1 = c->y1k;
29  c->yk_2 = c->yk_1;
30  c->yk_1 = c->yk;
31  c->ek_2 = c->ek_1;
32  c->ek_1 = c->ek;
33  c->ek = c->reference - c->measurement;
34  switch(c->type)
35  {
36  case rlController::P:
37  c->yk = c->d0*c->ek;
38  break;
39  case rlController::I:
40  case rlController::D_T1:
41  case rlController::PI:
43  c->yk = c->d0*c->ek + c->d1*c->ek_1 + c->c1*c->yk_1;
44  break;
46  c->yk = c->d0*c->ek + c->d1*c->ek_1 + c->d2*c->ek_2 + c->c1*c->yk_1 + c->c2*c->yk_2;
47  break;
49  c->yk = c->Kp*c->ek + c->y1k;
50  c->y1k = c->y1k_1 + c->d1*(c->ek+c->ek_1);
51  break;
53  c->yk = c->Kp*c->ek + c->ydk;
54  c->ydk = c->cD*c->ydk_1 + c->dD*(c->ek-c->ek_1);
55  break;
57  c->yk = c->Kp*c->ek + c->y1k + c->ydk;
58  c->y1k = c->y1k_1 + c->d1*(c->ek+c->ek_1);
59  c->ydk = c->cD*c->ydk_1 + c->dD*(c->ek-c->ek_1);
60  break;
61  default:
62  break;
63  }
64  if(c->limited)
65  {
66  if(c->yk > c->yk_max) c->yk = c->yk_max;
67  if(c->yk < c->yk_min) c->yk = c->yk_min;
68  if(c->y1k > c->yk_max) c->y1k = c->yk_max;
69  if(c->y1k < c->yk_min) c->y1k = c->yk_min;
70  if(c->ydk > c->yk_max) c->ydk = c->yk_max;
71  if(c->ydk < c->yk_min) c->ydk = c->yk_min;
72  }
73  c->unlock();
74  c->writeOutput(c->yk);
75  }
76  return arg;
77 }
void * user
Definition: rlthread.h:30
void(* writeOutput)(double output)
Definition: rlcontroller.h:140
int unlock()
Definition: rlthread.cpp:52
double measurement
Definition: rlcontroller.h:154
void rlsleep(long msec)
Definition: rlwthread.cpp:396
double(* getMeasurement)()
Definition: rlcontroller.h:136
int lock()
Definition: rlthread.cpp:47
double reference
Definition: rlcontroller.h:123