Skip to content

Commit

Permalink
Merge pull request #320 from krkeegan/velo_calc_improvements
Browse files Browse the repository at this point in the history
Fix Error in Quantization Buffer; Allow RPM to Approach 0
  • Loading branch information
BarbourSmith authored Oct 17, 2017
2 parents ecf7e23 + f328de5 commit 99dabad
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
11 changes: 11 additions & 0 deletions cnc_ctrl_v1/Encoder.h
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,17 @@ class Encoder
interrupts();
return ret;
}
inline int32_t lastStepTime() {
if (interrupts_in_use < 2) {
noInterrupts();
update(&encoder);
} else {
noInterrupts();
}
int32_t ret = encoder.lastTime;
interrupts();
return ret;
}
inline void write(int32_t p) {
noInterrupts();
encoder.position = p;
Expand Down
14 changes: 12 additions & 2 deletions cnc_ctrl_v1/MotorGearboxEncoder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,8 @@ float MotorGearboxEncoder::_computeSpeed(){
// This dampens some of the effects of quantization without having
// a big effect on other changes
float saveDistMoved = distMoved;
if (distMoved - _lastDistMoved <= -1){ distMoved + .5;}
else if (distMoved - _lastDistMoved >= 1){distMoved - .5;}
if (distMoved - _lastDistMoved <= -1){ distMoved += .5;}
else if (distMoved - _lastDistMoved >= 1){distMoved -= .5;}
_lastDistMoved = saveDistMoved;

unsigned long timeElapsed = currentMicros - _lastTimeStamp;
Expand All @@ -143,6 +143,16 @@ float MotorGearboxEncoder::_computeSpeed(){
}
else {
float elapsedTime = encoder.elapsedTime();
float lastTime = micros() - encoder.lastStepTime(); // no direction associated with this
if (lastTime > abs(elapsedTime)) {
// This allows the RPM to approach 0
if (elapsedTime < 0){
elapsedTime = -lastTime;
}
else {
elapsedTime = lastTime;
}
};

_RPM = 0 ;
if (elapsedTime != 0){
Expand Down

0 comments on commit 99dabad

Please sign in to comment.