I have a dc Motor with a quadrature encoder attached. The reading are inconsistent and it often over shoots and sometimes it under shoots. At the lowest operating speed I can get it to be pretty consistent, 90+% it hits the mark. The higher the speed the worse the reading. When pwn is about 100 its all but useless.
I suspect it might be an issue with the interrupt since after completing the ISR it goes back to where it was in the loop. Meaning it might move a fraction of a second longer than it should before starting over and rechecking the condition. Is there a way to force the loop to restart? Or a better way to handle stopping the motors when certain conditions are meet?
#include "Motor.h"
#include "Delay.h"
//167 ticks per rotation.
//Circumfrence of wheel 7
//resolution .04 inches
//degree per tick 2.2
#define rightEncoderA 0
#define rightEncoderB 1
#define leftEncoderA 2
#define leftEncoderB 3
int enablePinLeft = 5; //h bridge enable pin 1 for right motor
int leftInput1 = 4; // h bridge input 1 for right motor
int leftInput2 = 7; // h bridge input 2 for right motor
int enablePinRight = 6; //h bridge enable pin 2 for left motor
int rightInput1 = 8; // h bridge input 3 for left motor
int rightInput2 = 9; // h bridge input 4 for left motor
volatile double ticks;
bool move;
Motor rightMotor(enablePinRight, rightInput1, rightInput2, rightEncoderA, rightEncoderB, 5);
Motor leftMotor(enablePinLeft, leftInput1, leftInput2, leftEncoderA, leftEncoderB, 1);
CustomDelay customDelay;
ISR(INT1_vect)
{
if (ticks < 80)
{
ticks++;
}
else
{
leftMotor.SetSpeed(0);
leftMotor.SetDirection(OFF);
move = false;
}
}
void setup()
{
Serial.begin(115200);
ticks = 0;
move = true;
leftMotor.SetDirection(FORWARD);
leftMotor.SetSpeed(75);
DDRD = DDRD | B00000000;
EICRA &= ~(bit(ISC10) | bit(ISC11)); // clear existing flags
EICRA |= (bit(ISC10) | bit(ISC11)); // set wanted flags (falling level interrupt)
EIMSK |= bit(INT1);
}
void loop()
{
if (!move)
{
customDelay.Wait(1500);
leftMotor.SetDirection(FORWARD);
leftMotor.SetSpeed(75);
move = true;
ticks = 0;
}
}