-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontroller.cpp
More file actions
203 lines (168 loc) · 4.23 KB
/
Copy pathcontroller.cpp
File metadata and controls
203 lines (168 loc) · 4.23 KB
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
/*
* @author Ricardo Garcia Rosas
*
* @section DESCRIPTION
*
* Basic proportional position controller. Tilt table as main application.
* This class should be extended to implement PID, linearized, etc. controllers.
*
*/
#include "controller.h"
#include <stdlib.h>
//Default constructor
Controller::Controller()
{
error = 0;
desPos_px = 0;
desPos_mm = 0.0;
curPos_px = 0;
curPos_mm = 0.0;
gainP = 0.0;
outputMin_deg = 0;
outputMax_deg = 0;
minimumPositionError = 0;
}
//Initializes controller limits and gain.
//@param controller output lower limit.
//@param controller output upper limit.
//@param controller proportional gain.
Controller::Controller(int outputMin_deg, int outputMax_deg, double gainP) : Controller()
{
this->gainP = gainP;
this->outputMin_deg = outputMin_deg;
this->outputMax_deg = outputMax_deg;
}
//Computes the error between the desired and current position.
void Controller::computeError()
{
error = desPos_px - curPos_px;
}
//Performs the proportional correction for the current position.
//If the correction is greater or lower than the controller's limits,
//the value is set to that of the limit.
//@return controller proportional correction as integer.
double Controller::proportionalCorrection()
{
double correctionP = gainP * error;
return correctionP;
}
//Performs proportional position control that sets the desired position.
//The output is normalized to work within the application's requirements.
//@return normalized control signal.
int Controller::positionControl(int desPos_px, int curPos_px)
{
setDesiredPos_px(desPos_px);
return positionControl(curPos_px);
}
//Performs proportional position control.
//The output is normalized to work within the application's requirements.
//@return normalized control signal.
int Controller::positionControl(int curPos_px)
{
setCurrentPos_px(curPos_px);
computeError();
double controlSignal = proportionalCorrection();
controlSignal = clampSaturation(controlSignal);
return normalizeData(controlSignal);
}
//Normalizes control data to the output range.
//@param data to be normalized.
//@return normalized control data.
int Controller::normalizeData(double data)
{
return (int)((data - LOWERLIMIT) * \
(outputMax_deg - outputMin_deg)/(UPPERLIMIT - LOWERLIMIT));
}
/**
Clamps our control signal within saturation limits, so we don't send a signal
that is out-of-bounds.
@param output Signal to clamp
@return Clamped signal if out-of-bounds, or given signal
*/
double Controller::clampSaturation(double output)
{
if (output > UPPERLIMIT)
output = UPPERLIMIT;
else if (output < LOWERLIMIT)
output = LOWERLIMIT;
return output;
}
/**
* Let's us know if this axis is at the desired position
*
* @return bool Whether we are within the minimum position error or not
*/
bool Controller::atDesiredPosition()
{
return abs(error) <= minimumPositionError;
}
//Setters
void Controller::setMinimumPositionError(double minimumPositionError)
{
this->minimumPositionError = minimumPositionError;
}
void Controller::setDesiredPos_px(int desPos_px)
{
this->desPos_px = desPos_px;
}
void Controller::setDesiredPos_mm(double desPos_mm)
{
this->desPos_mm = desPos_mm;
}
void Controller::setCurrentPos_px(int curPos_px)
{
this->curPos_px = curPos_px;
}
void Controller::SetCurrentPos_mm(double desPos_mm)
{
this->desPos_mm = desPos_mm;
}
void Controller::SetGainP(double gainP)
{
this->gainP = gainP;
}
void Controller::SetOutputMax(int outputMax_deg)
{
this->outputMax_deg = outputMax_deg;
}
void Controller::SetOutputMin(int outputMin_deg)
{
this->outputMin_deg = outputMin_deg;
}
//Getters
double Controller::getMinumumPositionError()
{
return this->minimumPositionError;
}
double Controller::GetGainP()
{
return gainP;
}
int Controller::GetOutputMax()
{
return outputMax_deg;
}
int Controller::GetOutputMin()
{
return outputMin_deg;
}
int Controller::GetDesiredPos_px()
{
return desPos_px;
}
double Controller::GetDesiredPos_mm()
{
return desPos_mm;
}
int Controller::GetCurrentPos_px()
{
return curPos_px;
}
double Controller::GetCurrentPos_mm()
{
return curPos_mm;
}
int Controller::GetCurrentError()
{
return error;
}