View Single Post
  #4   Spotlight this post!  
Unread 06-02-2016, 20:30
Sparx030 Sparx030 is offline
Registered User
FRC #1251
 
Join Date: Jan 2016
Location: Florida
Posts: 14
Sparx030 is an unknown quantity at this point
Re: Finding RPMs with encoder

Quote:
Originally Posted by Ether View Post
Not to be too pedantic, but what do you mean by "working perfectly"?

Could I talk you into posting a trace of your wheel speed vs time?

Others can maybe learn from it.

Thanks.



I managed to get the encoder to show me values in RPM as well as allow inputs to be in RPMs, my big issue was not getting values I wanted as well as not knowing what stood for what in the code. so my result was this...

Code:
        RPM = 5000.0;

    	eShooter = new Encoder(1, 2, true, EncodingType.k1X);
    	eShooter.setDistancePerPulse(3);
    	eShooter.setPIDSourceType(PIDSourceType.kRate);
    	pidShooter = new PIDController(0, 0, 0, eShooter, mShooter);

        pidShooter.setSetpoint(((RPM/60)*360)/3);

        SmartDashboard.putNumber("Motor RPM ", ((eShooter.getRate()*3)/360)*60);
So to explain things best I can (because this is the actual code for this and most of what I wrote from this thread is on a piece of paper that would be less easy for me to explain) this allows you to put a desired RPM to set the wheel to that speed as well as tell you on the computer (SmartDashboard) what RPM it is moving at.

Code:
        RPM = 5000.0;

    	eShooter = new Encoder(1, 2, true, EncodingType.k1X);
    	eShooter.setDistancePerPulse(3);
    	eShooter.setPIDSourceType(PIDSourceType.kRate);
    	pidShooter = new PIDController(0, 0, 0, eShooter, mShooter);
This has 'RPM = 5000;', what RPM you want the wheel to move at. 'eShooter = new' is creating the Encoder in the code with 1x decoding (EncodingType.k1X). 'setDistancePerPulse(3);' is the distance in degrees the wheel will move per pulse which with 1x decoding and a 120cpr encoder will be 1 pulse (or count) per 3 degrees. setPIDSourceType is to make the encoder read the rate as opposed to distance. 'pidShooter = new' creates a speed controller with P, I, and D variables and the encoder and the motor it is powering (this also has no values for P, I, or D these would have something in place to properly tune it).

That might have been lengthy and possibly unimportant for those who might look to this for learning about the RPM calculations but the 'setDistancePerPulse' was a big trouble of mine when trying to find what to put in there and is important to the code. 3 would not be the distance per pulse if this was not 1x decoding.

Code:
        pidShooter.setSetpoint(((RPM/60)*360)/3);

        SmartDashboard.putNumber("Motor RPM ", ((eShooter.getRate()*3)/360)*60);
setSetpoint wants an input in counts per second. To turn counts into pulses (with 1x decoding) will need you to turn desired RPM into RPS (into second) with 'RPM/60'(RPM divided by 60) then turn it from revolutions to degrees '*360'(multiply by 360) and finally degrees per second to counts per second '/3'(divide by 3).

now for what the encoder tells you. getRate() in also in counts per second so it's the exact opposite. the rate in counts per second multiplied by 3 to turn it into degrees, then divide by 360 to turn it into RPS(revolutions per second), and finally multiply by 60 to get RPMs.

This might have been a bad explanation or included too much rambling and not enough detail, especially with be just putting in the exact code, if that is the case I'm sorry I'm not much of a teacher, so if you want me to include more specific details let me know and I will say what I did, how I did it, and what value everything (related to this thread) intakes and outtakes.
Reply With Quote