There are few different methods for determining velocity from quadrature encoder signals. Here is a YouTube video with a good overview of a couple of those methods using different hardware (they use one of our 37D gearmotors with a pre-assembled 64 CPR encoder):
The creator also links to example code in the video description.
If you try to apply that to the Micro Metal Gearmotors and our encoder kits for them, keep in mind the 12 CPR value you mentioned for the disc means that for each rotation of the disc you could read up to 12 counts if you read both the rising and falling edges of both signal channels. However, for speed measurement and control, you might be more interested in counting pulses or measuring pulse lengths. The 12 CPR magnetic discs included with our recommended Micro Metal Gearmotor encoder kits consist of six approximately evenly spaced magnetic poles (three north-south pairs), so that means each of the two signal channel will read 3 pulses per revolution.
Also keep in mind that the encoder disc goes on the motor’s extended shaft, which comes before the gearbox, so you will need to account for that if you want to determine the gearmotor’s output shaft speed. For example, if the exact gear ratio for a motor were 100:1, and it generated a pulse frequency of 30 Hz on one of the encoder channels, then you could calculate the output shaft speed like so:
To determine direction the best way i can think of is to record the time of rising edge of encoder A and encoder B. If A -B = negative number then direction is forward , else positive direction backwads. But i guess that would depend on the motor orientation. Correct?
Im a bit confused about the magnetic disc. In 1 revolution of the disc:
channel a - 3 rising edge
channel a - 3 falling edge
channel b - 3 rising edge
channel b - 3 falling edge
thus making it 12 CPR?
If all i want is to get RPM value and not use it for PID purposes: A good way is to listen to only 1 channel, and :
[number of rising edge in a revolution] x [ Time Difference between 2 rising edge in seconds] x [60 seconds ÷ 1 minute] ÷ [ gear ratio ] = RPM
so if the time interval between rising edge is 1 second and has a gear ratio of 100:
0.3333 x 1 seconds x (60seconds/1minute) ÷ 100 = 0.20 RPM
This is assuming that my understanding of how the sensor reads the disc is correct. Am i correct?
A simpler method is whenever you detect the rising edge on encoder channel A, check whether channel B is low or high, and that will tell you the direction the motor shaft is spinning, which may more may not match the direction the gearbox output shaft is turning depending on the number of gear reduction stages.