For a project I'm doing, I have 2 of these motors. They come with encoders. I have been told the calculation to find how many encoder ticks per full spin of the shaft is the gearbox ratio * the CPR of the encoder which is 70:1 and 64 in my case. Resulting in a 4480 CPR. On my motors this doesn't seem to be the case. It might be a problem with my code but I can't seem to place where.
Pololu states the CPR, as you have already pointed out. They don't make mistakes in cases of such basic device parameters, for the products they sell.
If you don't get that value with your code and wiring, there is a problem with the code or wiring.
Interrupts are a particularly bad choice for encoder readout if the encoder transitions are not very clean, in which case a transition can result in more than one count.
You can use interrupts if the encoder transitions don't result in more than one count per transition. If that is not the case, the code and/or electronics require significant changes.
Other approaches include input polling, with appropriately timed delays to skip the multiple transitions. You might try the Arduino encoder library, which is well designed and reliable. It offers both interrupt and input polling options.
A good place to start is to determine where the actual problem lies. It is a good idea to use an oscilloscope to examine the transitions on the encoder output pins.
Heres is the catch then:
Right now it seems that the motors are spinning around 4.3 revolutions for 4880 ticks. If the interrupts were counting more than once, wouldn't that mean it would spin less than one full revolution?
I merely pointed out one of the most common and severe problems with using interrupts to monitor switches or encoders.
It seems possible that you were shipped a motor with a different gear ratio. Post on the Pololu forum, where you can expect prompt, professional support from Pololu engineers.
Perhaps you are missing some encoder pulses due to interface or power problems. Which Arduino are you using, and how are the encoder power and ground connected?
EDIT: I finally took the time to look at the encoder counting procedure and there are three serious problems.
The code below counts only half the rising edges, since the B channel seems to be ignored. For the full encoder resolution, count all the rising and falling edges.
Furthermore, any variable modified by an interrupt service routine must be declared volatile, or changes may and often will be missed in the main loop.
void M2Encoderadd(){
encoder.countM2A++; //must be declared volatile
}
Finally, a multi-byte variable shared between an ISR and the main program can be corrupted by the ISR while it is being accessed by the main program. To prevent that, make a copy with the interrupts off, and use the copy.
Example:
noInterrupts();
int countM2A_copy = encoder.countM2A;
interrupts();
Serial.println(countM2A_copy);
I have found the solution:
I didn't account for the fact that I was only using the rising edge of one of the encoders. 64 CPR is both encoders' rising and falling. My equation therefore has been correctly adjusted to 16*70