Skip to content

Commit

Permalink
Reset the last pulse time when needed.
Browse files Browse the repository at this point in the history
Inspired by r-downing#18
  • Loading branch information
ademuri committed Oct 22, 2022
1 parent 8e40446 commit 51e85e6
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
9 changes: 9 additions & 0 deletions AutoPID.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,18 @@ void AutoPID::setIntegral(double integral) { _integral = integral; }

void AutoPIDRelay::run() {
AutoPID::run();
if (!_hasRun) {
_lastPulseTime = millis();
_hasRun = true;
}
while ((millis() - _lastPulseTime) > _pulseWidth)
_lastPulseTime += _pulseWidth;
*_relayState = ((millis() - _lastPulseTime) < (_pulseValue * _pulseWidth));
}

double AutoPIDRelay::getPulseValue() { return (isStopped() ? 0 : _pulseValue); }

void AutoPIDRelay::reset() {
AutoPID::reset();
_lastPulseTime = millis();
}
7 changes: 5 additions & 2 deletions AutoPID.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class AutoPID {
void run();
// Stops PID functionality, output sets to
void stop();
void reset();
virtual void reset();
bool isStopped();

double getIntegral();
Expand Down Expand Up @@ -56,9 +56,12 @@ class AutoPIDRelay : public AutoPID {

double getPulseValue();

void reset() override;

private:
bool *_relayState;
unsigned long _pulseWidth, _lastPulseTime;
unsigned long _pulseWidth;
unsigned long _lastPulseTime;
double _pulseValue;
}; // class AutoPIDRelay

Expand Down

0 comments on commit 51e85e6

Please sign in to comment.