Skip to content

Commit

Permalink
Use callable instead of c-style callbacks
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael Rasmussen committed Feb 21, 2022
1 parent f13095a commit d754a1c
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 11 deletions.
8 changes: 4 additions & 4 deletions cpp/PID.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@
* @param (*pidOutput) The function pointer for delivering system output.
*/
template <class T>
PIDController<T>::PIDController(double p, double i, double d, T (*pidSource)(), void (*pidOutput)(T output))
PIDController<T>::PIDController(double p, double i, double d, std::function<T(void)> pidSource, std::function<void(T output)> pidOutput)
{
_p = p;
_i = i;
Expand Down Expand Up @@ -644,7 +644,7 @@ double PIDController<T>::getD()
* @param (*getFeedback) A function pointer that retrieves system feedback.
*/
template <class T>
void PIDController<T>::setPIDSource(T (*pidSource)())
void PIDController<T>::setPIDSource(std::function<T(void)> pidSource)
{
_pidSource = pidSource;
}
Expand All @@ -666,7 +666,7 @@ void PIDController<T>::setPIDSource(T (*pidSource)())
* @param (*onUpdate) A function pointer that delivers system output.
*/
template <class T>
void PIDController<T>::setPIDOutput(void (*pidOutput)(T output))
void PIDController<T>::setPIDOutput(std::function<void(T output)> pidOutput)
{
_pidOutput = pidOutput;
}
Expand All @@ -685,7 +685,7 @@ void PIDController<T>::setPIDOutput(void (*pidOutput)(T output))
* @param (*getSystemTime) Pointer to a function that returns system time.
*/
template <class T>
void PIDController<T>::registerTimeFunction(unsigned long (*getSystemTime)())
void PIDController<T>::registerTimeFunction(std::function<unsigned long(void)> getSystemTime)
{
_getSystemTime = getSystemTime;
timeFunctionRegistered = true;
Expand Down
16 changes: 9 additions & 7 deletions cpp/PID.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@
#define PID_H
#endif

#include <functional>

template <class T>
class PIDController
{
public:
PIDController(double p, double i, double d, T (*pidSource)(), void (*pidOutput)(T output));
PIDController(double p, double i, double d, std::function<T(void)> pidSource, std::function<void(T output)> pidOutput);
void tick();
void setTarget(T t);
T getTarget();
Expand Down Expand Up @@ -45,9 +47,9 @@ class PIDController
double getP();
double getI();
double getD();
void setPIDSource(T (*pidSource)());
void setPIDOutput(void (*pidOutput)(T output));
void registerTimeFunction(unsigned long (*getSystemTime)());
void setPIDSource(std::function<T(void)> pidSource);
void setPIDOutput(std::function<void(T output)> pidOutput);
void registerTimeFunction(std::function<unsigned long(void)> getSystemTime);
private:
double _p;
double _i;
Expand Down Expand Up @@ -76,7 +78,7 @@ class PIDController
T feedbackWrapUpperBound;

bool timeFunctionRegistered;
T (*_pidSource)();
void (*_pidOutput)(T output);
unsigned long (*_getSystemTime)();
std::function<T(void)> _pidSource;
std::function<void(T output)> _pidOutput;
std::function<unsigned long(void)> _getSystemTime;
};

0 comments on commit d754a1c

Please sign in to comment.