-
Notifications
You must be signed in to change notification settings - Fork 0
/
PlantController.cpp
72 lines (59 loc) · 1.54 KB
/
PlantController.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
/*
* PlantController.cpp
*
* Created on: Apr 1, 2013
* Author: dam7633
*/
#include "PlantController.h"
#include "PlantContext.h"
PlantController::PlantController(Sensor *s, Actuator *a) :
sensor(s),
actuator(a),
proportionalGain(0.0),
integralGain(0.0),
derivativeGain(0.0),
lastOutput(0.0) {
pthread_mutex_init(¶metersMutex, NULL);
}
PlantController::~PlantController() {
pthread_mutex_destroy(¶metersMutex);
}
void PlantController::initialize(PlantContext *c) {
context = c;
integral = 0;
derivative = 0;
}
void PlantController::step() {
double currentReading = sensor->getValue();
double setPoint = context->getRef();
double error = (setPoint - currentReading);
context->setError(error);
integral += error; //integral + error;
if(integral > MAX_INTEGRAL) {
integral = MAX_INTEGRAL;
} else if (integral < MIN_INTEGRAL) {
integral = MIN_INTEGRAL;
}
derivative = (error - context->getLastError());
double output = lastOutput + proportionalGain*error + integralGain*integral + derivativeGain*derivative;
actuator->setValue( output );
lastOutput = output;
}
void PlantController::shutdown() {
}
double PlantController::getProportionalGain() {
return proportionalGain;
}
double PlantController::getIntegralGain() {
return integralGain;
}
double PlantController::getDerivateGain() {
return derivativeGain;
}
void PlantController::setParameters(double kp, double ki, double kd) {
pthread_mutex_lock(¶metersMutex);
proportionalGain = kp;
integralGain = ki;
derivativeGain = kd;
pthread_mutex_unlock(¶metersMutex);
}