Hello Everyone,
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.
Code:
void setup()
{
foreman.init();
Serial.begin(115200);
attachInterrupt(digitalPinToInterrupt(2), M1Encoderadd, RISING);
attachInterrupt(digitalPinToInterrupt(3), M2Encoderadd, RISING);
foreman.forward();
}
in forman.init():
pinMode(M1DIR, OUTPUT);
pinMode(M2DIR, OUTPUT);
pinMode(M1PWM, OUTPUT);
pinMode(M2PWM, OUTPUT);
(don't worry the pins are all the right ones)
interrupt handlers:
void M1Encoderadd(){
encoder.countM1A++;
}
void M2Encoderadd(){
encoder.countM2A++;
}
and foreman.forward():
void Foreman::forward(){
int speed = 100;
int target = endr.countM1A + 4800;
while(endr.countM1A < target){
Serial.println(endr.countM1A);
digitalWrite(M1DIR, HIGH);
analogWrite(M1PWM, speed);
}
digitalWrite(M1DIR,HIGH);
analogWrite(M1PWM, 0);
digitalWrite(M2DIR,HIGH);
analogWrite(M2PWM, 0);
}
The motor driver I'm using is the one recommended by JRemington in this post.
Please feel free to ask for any other info you need.
Thanks,
Ian
2 posts - 2 participants