calculating rpms

I wanted to use the optical shaft encoder to calculate rpms so that my intake will only work when my flywheel is >= 1200 rpms. How do I even start doing this? We just started using the encoder to calculate distance in autonomous mode, so we are new to this and are using easy c.

All I know is that the formula rpms= ticks/seconds x 2/3. I am guessing that I would have to have some kind of timer to track the initial ticks and the final ticks and total number of seconds. any help would be appreciated. thanks.

First, where do you have the encoder mounted? If it’s on your flywheel it won’t work properly at such high speeds, so you will have to mount it on a slower axle and calculate for the gear ratio.

To program it, you will need to take two readings of the sensor some set amount of time apart. Here is some basic pseudo code of what you will need to do inside of your main loop.

EncoderCurr = current sensor reading of the encoder
RPM = (EncoderCurr - EncoderLast) / 360 * 1200 * gear ratio after encoder
EncoderLast = EncoderCurr 

The rest of your driver control code here

Wait 50 msec

The calculations are to convert from degrees per 1/20 seconds to rotations per minute, you will have to adjust this for added external gear ratios and if you use a different time wait.

Ok thanks. So in the program loop, can I just throw in a preset variable and always restar the encoder at zero or do I make two more variables labeled current and final encoder?

Resetting it will work too, however it is generally better not to reset it an use two variables instead so that you can still use the encoder for other purposes that might need it to count up normally. But if you’re only using it to get the RPM of the flywheel resetting it should work fine too.

Thanks again. I’ll try it out soon.

What woud you do if you do not know what the required rpm is ajd u just want to figure out what it is and put it in an lcd?
Im using easy c btw.

I’m a ROBOTC guy, so I’m not familiar with using easyC. However, I can give you what something might look like. You’d have a function that calculates the RPM and uses a return to pass in the calculated value to the LCD, using whatever would be the easyC equivalent of the displayLCDNumber command. Another function would also control the current motor power (or RPM if you’re using the TBH algorithm) and adjust it based on an input from the user, using what would be a button on a joystick or two of the three buttons on the LCD. Those two functions should be separate and not interfere with each other.