Magnetic Encoder Pair Kit is not read correctly

Hi I am new with using encoder. I have difficult to get the Magnetic Encoder Pair Kit read correct count by using pololu encoder function such as encoders_get_counts_m1(). It is keep return 0 to me. Below is my code please point out where is my mistake? The reason I am using encoder is I want to make both motor turn in same speed then can make the robot drive straight. It is great to have someone give me some code example for pololu encoder. thanks

#include <PololuWheelEncoders.h>
#include <ATX2.h>

#define PololuWheelEncoders encoders
void setup() {
  // put your setup code here, to run once:
  XIO();
  OK();
  Serial.begin(9600);
  Serial.println("Basic Encoder Test:");
}

void loop() {
  // put your main code here, to run repeatedly:
  encoders_init(24, 26, 28, 30);
  Serial.print(encoders_get_counts_m1());
  Serial.print("   ");
  Serial.println(encoders_get_counts_m2());
  
  
}

but I also tried another encoder library, it returns correct increment count while I turn my motor forward manually. But when I turn motors automatic it doesn’t return correct value again.Below is my code

/* Encoder Library - Basic Example
 * http://www.pjrc.com/teensy/td_libs_Encoder.html
 *
 * This example code is in the public domain.
 */
#include <ATX2.h>
#include <Encoder.h>

// Change these two numbers to the pins connected to your encoder.
//   Best Performance: both pins have interrupt capability
//   Good Performance: only the first pin has interrupt capability
//   Low Performance:  neither pin has interrupt capability
Encoder myEncL(28, 30);
Encoder myEncR(24, 26);
//   avoid using pins with LEDs attached

void setup() {
  XIO();
  Serial.begin(9600);
  Serial.println("Basic Encoder Test:");
  myEncL.write(0);
  myEncR.write(0);
}

long oldPositionL  = -999;
long oldPositionR  = -999;

void loop() {
  motor(34, 2);
  motor(56, 2);
  
  long newPositionL = myEncL.read();
  long newPositionR = myEncR.read();
  if (newPositionL != oldPositionL || newPositionR != oldPositionR) {
    oldPositionL = newPositionL;
    oldPositionR = newPositionR;
    Serial.print(newPositionL);
    Serial.print("   ");
    Serial.println(newPositionR);
  }
}

Hi.

Your code that references our wheel encoder library calls the encoders_init function in your loop, and that is resetting the count values each time the loop runs. You should move the initialization to the setup section of your code instead and use a command like encoders_get_counts_m1 to read the encoder counts. We do not have any example code for using that library to drive two motors at the same speed.

In general, if the other library is working better for you, I do not see a reason to use our library over it. Quadrature encoders are fairly standard devices so the libraries for them are for the most part interchangeable.

-Claire

Hi Claire

Thank you for your reply. I am confusing about the revolution counting, according to the data sheet it shown 12 counts per revolution, therefore if I am using 1:100 ratio motor, Is It 12 * 100, 1200 counts per revolution is for my motor? And I also see some example is shown 4 pairs of N-S x 48:1 gearbox = 192 ticks per wheel rev. For now I am using pololu Magnetic Encoder Pair Kit, from the data sheet shown the encoder contains 6 pairs of N-S on each encoder, so is it 6 * 100 = 600ticks per wheel rev?

If you are using one of our micro metal gearmotors with a magnetic encoder, the counts per revolution of the back shaft that you get if you read both the low to high transitions and high to low transitions on both encoder channels is 12. To get the counts per revolution of the output shaft, you should multiply that by the gear ratio of your motor (i.e. 12 x 100 = 1200).

-Claire