Arduino读取两个不提供所需输出的旋转编码器

时间:2016-12-24 17:09:16

标签: arduino embedded

我正在尝试在2个独立的电机上读取两个霍尔效应旋转编码器,这些电机将用于我的机器人车轮。我决定在两个编码器的A通道上使用中断,并通过读取ISR中的B通道输入来获取方向。

由于某种原因,当我旋转右电机输出轴磁铁时的串行输出给了我:

R 1 R 0 R 1 R 0 R 1 R 0 R 1 R 0 R 1 R 0 R 1

当我旋转左边的那个时,它给了我:

L -1 L -2 L -1 L -2 L -3 L -4 L -3 L -4 L -3 L -4 L -3 L -4 L -3 L -4 L -3 L - 4 L -3 L -4 L -3 L -4 L -3 L -4 L -3 L -4 L -3 L -4 L -3 L -4 L -3 L -4 L -3 L -4 < / p>

两者都以相同的顺时针方向旋转。谁能告诉我为什么我的脉冲计数器没有增加它应该是什么?

仅供参考,我正在使用Arduino pro mini。我实际上尝试在两个电机的A和B通道上使用中断,使用引脚2,3和0,1用于左右编码器。但由于某种原因,正确的编码器输出不正确。只有左边的那个。我认为使用引脚0和1可能会影响到USB的串行输出,因此我为每个编码器使用了1个中断,但它没有按预期工作。

非常感谢任何帮助。我在下面发布了我的代码。

// variables for encoders
// left
int AChnl_L = 3, BChnl_L = 7;
int lastPulse_L = 0, newPulse_L = 0;
bool BSIG_L = LOW;
int pulseCount_L = 0, prevPulseCount_L = 0;
int revs_L = 0;
// right
int AChnl_R = 2, BChnl_R = 8;
int lastPulse_R = 0, newPulse_R = 0;
bool BSIG_R = LOW;
int pulseCount_R = 0, prevPulseCount_R = 0;
int revs_R = 0;

void setup()
{
    // setup interrupt on left encoder channel A
    attachInterrupt(digitalPinToInterrupt(AChnl_L), ARise_L, RISING);
    // setup interrupt on right encoder channel A
    attachInterrupt(digitalPinToInterrupt(AChnl_R), ARise_R, RISING);

    Serial.begin(9600);
}

void loop()
{
    readEncoder_L();
    readEncoder_R();

    if (pulseCount_L != prevPulseCount_L)
    {
        Serial.println("L");
        Serial.println(pulseCount_L);
        prevPulseCount_L = pulseCount_L;
    }
    if (pulseCount_R != prevPulseCount_R)
    {
        Serial.println("R");
        Serial.println(pulseCount_R);
        prevPulseCount_R = pulseCount_R;
    }
}

/*
clears incoming serial buffer
*/
void serialFlush()
{
    while(Serial.available() > 0)
    {
        Serial.read();
    }
}

/* 
function for incrementing/decrementing signal pulses for left encoder
*/
void readEncoder_L()
{
    // rotated CW
    if(newPulse_L > lastPulse_L)
    {
        pulseCount_L++;
        lastPulse_L = newPulse_L;
    }
    // rotated CCW
    else if(newPulse_L < lastPulse_L)
    {
        pulseCount_L--;
        lastPulse_L = newPulse_L;
    }
}

/* 
function for incrementing/decrementing signal pulses for right encoder
*/
void readEncoder_R()
{
    // rotated CW
    if(newPulse_R > lastPulse_R)
    {
        pulseCount_R++;
        lastPulse_R = newPulse_R;
    }
    // rotated CCW
    else if(newPulse_R < lastPulse_R)
    {
        pulseCount_R--;
        lastPulse_R = newPulse_R;
    }
}

/*
ISR for rising pulse on channel A left
if B == 1 then CW, if B == 0 then CCW
swap to falling edge ISR
*/
void ARise_L()
{
    detachInterrupt(digitalPinToInterrupt(AChnl_L));
    BSIG_L = digitalRead(BChnl_L);
    // if ASIG_L == 1 & BSIG_L == 1 then CW
    if (BSIG_L == HIGH)
    {
        newPulse_L++;
    }
    // if ASIG_L == 1 & BSIG_L == 0 then CCW
    if (BSIG_L == LOW)
    {
        newPulse_L--;
    }
    attachInterrupt(digitalPinToInterrupt(AChnl_L), AFall_L, FALLING);
}

/*
ISR for falling pulse on channel A left
if B == 0 then CW, if B == 1 then CCW
swap to rising edge ISR
*/
void AFall_L()
{
    detachInterrupt(digitalPinToInterrupt(AChnl_L));
    BSIG_L = digitalRead(BChnl_L);
    // if ASIG_L == 0 & BSIG_L == 0 then CW
    if (BSIG_L == LOW)
    {
        newPulse_L++;
    }
    // if ASIG_L == 0 & BSIG_L == 1 then CCW
    if (BSIG_L == HIGH)
    {
        newPulse_L--;
    }
    attachInterrupt(digitalPinToInterrupt(AChnl_L), ARise_L, FALLING);
}

/*
ISR for rising pulse on channel A right
if B == 1 then CW, if B == 0 then CCW
swap to falling edge ISR
*/
void ARise_R()
{
    detachInterrupt(digitalPinToInterrupt(AChnl_R));
    BSIG_R = analogRead(BChnl_R);
    // if ASIG_R == 1 & BSIG_R == 1 then CW
    if (BSIG_R == HIGH)
    {
        newPulse_R++;
    }
    // if ASIG_R == 1 & BSIG_R == 0 then CCW
    if (BSIG_R == LOW)
    {
        newPulse_R--;
    }
    attachInterrupt(digitalPinToInterrupt(AChnl_R), AFall_R, FALLING);
}

/*
ISR for falling pulse on channel A right
if B == 0 then CW, if B == 1 then CCW
swap to rising edge ISR
*/
void AFall_R()
{
    detachInterrupt(digitalPinToInterrupt(AChnl_R));
    BSIG_R = analogRead(BChnl_R);
    // if ASIG_R == 0 & BSIG_R == 0 then CW
    if (BSIG_R == LOW)
    {
        newPulse_R++;
    }
    // if ASIG_R == 0 & BSIG_R == 1 then CCW
    if (BSIG_R == HIGH)
    {
        newPulse_R--;
    }
    attachInterrupt(digitalPinToInterrupt(AChnl_R), ARise_R, FALLING);
}

0 个答案:

没有答案