Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 7 additions & 8 deletions AutoPID.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,14 @@ void AutoPID::run() {
//if bang thresholds are defined and we're outside of them, use bang-bang control
if (_bangOn && ((*_setpoint - *_input) > _bangOn)) {
*_output = _outputMax;
_lastStep = millis();
_lastStep = micros();
} else if (_bangOff && ((*_input - *_setpoint) > _bangOff)) {
*_output = _outputMin;
_lastStep = millis();
_lastStep = micros();
} else { //otherwise use PID control
unsigned long _dT = millis() - _lastStep; //calculate time since last update
unsigned long _dT = micros() - _lastStep; //calculate time since last update
if (_dT >= _timeStep) { //if long enough, do PID calculations
_lastStep = millis();
_lastStep = micros();
double _error = *_setpoint - *_input;
_integral += (_error + _previousError) / 2 * _dT / 1000.0; //Riemann sum integral
//_integral = constrain(_integral, _outputMin/_Ki, _outputMax/_Ki);
Expand All @@ -73,7 +73,7 @@ void AutoPID::stop() {
reset();
}
void AutoPID::reset() {
_lastStep = millis();
_lastStep = micros();
_integral = 0;
_previousError = 0;
}
Expand All @@ -92,13 +92,12 @@ void AutoPID::setIntegral(double integral){

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


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