Skip to content

Commit 86ad138

Browse files
chore: optimize PID (#78)
* optimize PID * fix pid impl * fix coverage * increase coverage * fix coverage
1 parent fac565a commit 86ad138

4 files changed

Lines changed: 409 additions & 8 deletions

File tree

‎numerical/controllers/implementations/CMakeLists.txt‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
1-
add_library(numerical.controllers.implementations INTERFACE ${EMIL_EXCLUDE_FROM_ALL})
1+
add_library(numerical.controllers.implementations ${EMIL_EXCLUDE_FROM_ALL})
22

3-
target_include_directories(numerical.controllers.implementations INTERFACE
3+
target_include_directories(numerical.controllers.implementations PUBLIC
44
"$<BUILD_INTERFACE:${CMAKE_CURRENT_LIST_DIR}/../../../>"
55
"$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>"
66
)
77

8-
target_link_libraries(numerical.controllers.implementations INTERFACE
8+
target_link_libraries(numerical.controllers.implementations PUBLIC
99
numerical.controllers.interfaces
1010
)
1111

1212
target_sources(numerical.controllers.implementations PRIVATE
13+
PidIncremental.cpp
1314
PidIncremental.hpp
1415
)
1516

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#include "numerical/controllers/implementations/PidIncremental.hpp"
2+
#include "numerical/math/QNumber.hpp"
3+
4+
namespace controllers
5+
{
6+
template class PidIncrementalBase<float>;
7+
template class PidIncrementalBase<math::Q15>;
8+
template class PidIncrementalBase<math::Q31>;
9+
10+
template class PidIncrementalAsynchronous<float>;
11+
template class PidIncrementalAsynchronous<math::Q15>;
12+
template class PidIncrementalAsynchronous<math::Q31>;
13+
14+
template class PidIncrementalSynchronous<float>;
15+
template class PidIncrementalSynchronous<math::Q15>;
16+
template class PidIncrementalSynchronous<math::Q31>;
17+
}

‎numerical/controllers/implementations/PidIncremental.hpp‎

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@
33
#include "numerical/controllers/interfaces/PidController.hpp"
44
#include "numerical/controllers/interfaces/PidDriver.hpp"
55
#include "numerical/math/CompilerOptimizations.hpp"
6-
#include <optional>
6+
7+
#if defined(__GNUC__) || defined(__clang__)
8+
#pragma GCC optimize("O3", "fast-math")
9+
#endif
710

811
namespace controllers
912
{
@@ -15,6 +18,7 @@ namespace controllers
1518
void SetLimits(PidLimits<QNumberType> limits);
1619
void SetTunings(PidTunings<QNumberType> tunnings);
1720
void Enable();
21+
void Disable();
1822
QNumberType Process(QNumberType processVariable);
1923

2024
protected:
@@ -24,7 +28,8 @@ namespace controllers
2428
QNumberType Clamp(QNumberType input);
2529

2630
private:
27-
std::optional<QNumberType> setPoint;
31+
QNumberType setPointValue{};
32+
bool hasSetPoint = false;
2833
PidLimits<QNumberType> limits;
2934
QNumberType a0 = 0;
3035
QNumberType a1 = 0;
@@ -90,7 +95,8 @@ namespace controllers
9095
template<class QNumberType>
9196
void PidIncrementalBase<QNumberType>::SetPoint(QNumberType _setPoint)
9297
{
93-
this->setPoint.emplace(_setPoint);
98+
this->setPointValue = _setPoint;
99+
this->hasSetPoint = true;
94100
}
95101

96102
template<class QNumberType>
@@ -104,6 +110,12 @@ namespace controllers
104110
e_2 = QNumberType(0.0f);
105111
}
106112

113+
template<class QNumberType>
114+
void PidIncrementalBase<QNumberType>::Disable()
115+
{
116+
hasSetPoint = false;
117+
}
118+
107119
template<class QNumberType>
108120
void PidIncrementalBase<QNumberType>::SetLimits(PidLimits<QNumberType> _limits)
109121
{
@@ -139,13 +151,13 @@ namespace controllers
139151
QNumberType
140152
PidIncrementalBase<QNumberType>::Process(QNumberType processVariable)
141153
{
142-
if (!setPoint.has_value())
154+
if (!hasSetPoint) [[unlikely]]
143155
return processVariable;
144156

145157
u_1 = u;
146158
e_2 = e_1;
147159
e_1 = e;
148-
e = *setPoint - processVariable;
160+
e = setPointValue - processVariable;
149161
u = Clamp(u_1 + a0 * e + a1 * e_1 + a2 * e_2);
150162

151163
return u;
@@ -227,6 +239,7 @@ namespace controllers
227239
template<class QNumberType>
228240
void PidIncrementalSynchronous<QNumberType>::Disable()
229241
{
242+
PidIncrementalBase<QNumberType>::Disable();
230243
}
231244

232245
template<class QNumberType>
@@ -236,4 +249,16 @@ namespace controllers
236249
{
237250
return PidIncrementalBase<QNumberType>::Process(processVariable);
238251
}
252+
253+
extern template class PidIncrementalBase<float>;
254+
extern template class PidIncrementalBase<math::Q15>;
255+
extern template class PidIncrementalBase<math::Q31>;
256+
257+
extern template class PidIncrementalAsynchronous<float>;
258+
extern template class PidIncrementalAsynchronous<math::Q15>;
259+
extern template class PidIncrementalAsynchronous<math::Q31>;
260+
261+
extern template class PidIncrementalSynchronous<float>;
262+
extern template class PidIncrementalSynchronous<math::Q15>;
263+
extern template class PidIncrementalSynchronous<math::Q31>;
239264
}

0 commit comments

Comments
 (0)