Skip to content

Commit

Permalink
Merge pull request #306 from MaslowCNC/make-encoder-resolution-setable-
Browse files Browse the repository at this point in the history
Make the encoder resolution value in the RPM calculation updated from Ground Control
  • Loading branch information
BarbourSmith authored Sep 19, 2017
2 parents bd2da12 + 4a9085b commit 4b4145c
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 1 deletion.
3 changes: 3 additions & 0 deletions cnc_ctrl_v1/Axis.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,9 @@ void Axis::changeEncoderResolution(const int& newResolution){
*/
_encoderSteps = newResolution;

//push to the gearbox for calculating RPM
motorGearboxEncoder.setEncoderResolution(newResolution);

}

int Axis::detach(){
Expand Down
13 changes: 12 additions & 1 deletion cnc_ctrl_v1/MotorGearboxEncoder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,17 @@ void MotorGearboxEncoder::setPIDAggressiveness(float aggressiveness){

}

void MotorGearboxEncoder::setEncoderResolution(float resolution){
/*
Change the encoder resolution
*/

_encoderStepsToRPMScaleFactor = 60000000.0/resolution; //6*10^7 us per minute divided by 8148 steps per revolution

}

float MotorGearboxEncoder::computeSpeed(){
/*
Expand All @@ -162,7 +173,7 @@ float MotorGearboxEncoder::computeSpeed(){
float distMoved = _runningAverage(encoder.read() - _lastPosition); //because of quantization noise it helps to average these

//Compute the speed in RPM
float RPM = (7364.0*distMoved)/float(timeElapsed); //6*10^7 us per minute, 8148 steps per revolution
float RPM = (_encoderStepsToRPMScaleFactor*distMoved)/float(timeElapsed);

//Store values for next time
_lastTimeStamp = micros();
Expand Down
2 changes: 2 additions & 0 deletions cnc_ctrl_v1/MotorGearboxEncoder.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
void initializePID();
void setPIDAggressiveness(float aggressiveness);
void setPIDValues(float KpV, float KiV, float KdV);
void setEncoderResolution(float resolution);
private:
double _targetSpeed;
double _currentSpeed;
Expand All @@ -46,6 +47,7 @@
PID _posPIDController;
PID _negPIDController;
double _Kp=0, _Ki=0, _Kd=0;
float _encoderStepsToRPMScaleFactor = 7364.0; //6*10^7 us per minute divided by 8148 steps per revolution
int _oldValue1;
int _oldValue2;
int _oldValue3;
Expand Down

0 comments on commit 4b4145c

Please sign in to comment.