Hi,
I bought 2 x Lynxmotion 12v 90 rpm motors with encoders and Cytron MD10C r3 motor controllers recently. To test them I have written a test harness using Sign-Magnitude PWM but am getting unexpected results for the Encoder B (ENCB) counts:
Motor 1. Clockwise(ENCB) Anti-Clockwise(ENCB)
Run for 18 secs. 874 -4706
“ 963 -4737
“ 894 -4750
Motor 2. 890 0
Run for 18 Secs 905 1
“ 896 0
As the Encoder B counts are not as expected and vary between motors I suspect the motor encoders are faulty. Motor 1 anti-clockwise ENCB counts are not as expected, I would have expected them to be a negative value similar to the clockwise values as the motor run duration was the same for both clockwise and anti-clockwise rotations.
Motor 2 ENCB counts for anti-clockwise rotation with values 0 and 1 are not as expected.
I suspect the encoders are faulty. What’s your view?
Regards,
Graeme
PS
My test harness code is as follows:
//************************************************************************
// Description:
// Test harness
//
// Cyton MD10C r3 Single DC Motor Controller
// Lynxmotion 12V 90 rpm high torque motor with rotary encoder
//
//************************************************************************
#define DEBUG true // comment out this line for production
// include libraries
#include <SimplyAtomic.h> // For the ATOMIC macro
const int CW = 1; // clockwise
const int CCW = -1; // anti-clockwise
int speed = 1024; // motor speed in range 1 .. 1024
int motor_run_ms = 18000; // time for motor to run in on ms
int delay_ms = 1000;
#define ENCA D2 // pin 4 of the Arduino
#define ENCB D1 // pin 5 of the Arduino
#define PWM D4 // pin 2 of the Arduino
#define DIR D3 // pin 0 of the Arduino
volatile int posi = 0;
void anticlockwise(void) {
#if DEBUG
Serial.println("anticlockwise");
#endif
int pos;
setMotor(CCW, speed);
delay(motor_run_ms); // allow motor to run for n us
ATOMIC() {
pos = posi;
}
posi = 0;
analogWrite(PWM,0); // force motor to stop
digitalWrite(DIR,0);
#if DEBUG
Serial.println(pos);
#endif
}
void clockwise(void) {
#if DEBUG
Serial.println("clockwise");
#endif
int pos;
setMotor(CW, speed);
delay(motor_run_ms); // allow motor to run for n us
ATOMIC() {
pos = posi;
}
analogWrite(PWM,0); // force motor to stop
digitalWrite(DIR,LOW);
posi = 0;
#if DEBUG
Serial.println(pos);
#endif
}
void setup() {
Serial.begin(115200);
pinMode(ENCA,INPUT);
pinMode(ENCB,INPUT);
attachInterrupt(digitalPinToInterrupt(ENCA),readEncoderA,RISING);
pinMode(DIR, OUTPUT);
pinMode(PWM, OUTPUT);
Serial.println("Cytron MD10C / Lynxmotion 12v 90rpm motor w/ encoder test harness");
}
// main loop
void loop() {
clockwise();
// wait
delay(delay_ms);
// hide target
anticlockwise();
// wait
delay(delay_ms);
}
void setMotor(int dir, int pwmVal){
if(dir == CW){
// clockwise
#if DEBUG
Serial.println("***clockwise***");
#endif
analogWrite(PWM,pwmVal);
digitalWrite(DIR,HIGH);
}
else if(dir == CCW){
// anti-clockwise
#if DEBUG
Serial.println("***anti-clockwise***");
#endif
analogWrite(PWM,pwmVal);
digitalWrite(DIR,LOW);
}
else {
#if DEBUG
Serial.println("***stop***");
#endif
analogWrite(PWM,0);
digitalWrite(DIR,LOW);
}
}
void readEncoderA() {
int b = digitalRead(ENCB);
if(b > 0){
posi++;
}
else{
posi--;
}
}