使用Arduino将位置编码器值转换为RPM的问题

问题描述 投票:-1回答:1

我正在使用带有Arduino的400 PPR编码器来测量编码器值。我能够让编码器读取正值旋转值0-400,负旋转读取0到-400,编码器完全旋转后重置为0。我的代码的这部分工作,我遇到的问题是将这些位置值转换为RPM。

我的方法是:我使用millis()函数来跟踪整个程序时间,并使用变量time_now来存储该值。我有一个if语句,每当millis()变得大于time_now由REFRESH_RATE运行时(在我的情况下,这被定义为1毫秒)所以每运行1毫秒。

我的下一个方法是每毫秒采样两个样本。 pointSample1和pointSample2之间的差异应该是给我编码器在1毫秒内移动的滴答数量。这有点复杂,因为它基本上是一个400度的圆,所以例如如果pointSample1是395度而pointSample2读取5度,我们需要取编码器分辨率和pointSample1的差值,然后添加到pointSample 2或(PPR - pointSample1)+ pointSample2。

同样,这应该给我每mSecond的刻度,然后很容易转换成RPM。

#define REFRESH_RATE 1 //sets the refresh rate for tracking RPM (in mSec)

volatile float temp, counter = 0;    //This variable will increase or decrease depending on the rotation of encoder
int revsTotal = 0;
int pulseInitial = 0;
int PPR = 400;                      //Equal to the number of ticks per revolution of your encoder

unsigned long time_now = 0;         //time since program start
int pulseDifferential = 24;         //number of ticks per mSec for desired RPM
float pointSample1 = 0;             //first tick sample
float pointSample2 = 0;             //second tick sample
float pulsemSec = 0;                //the amount of ticks the encoder is reading every millisecond
float RPM = 0;                      //RPM of the motor

void setup() {

  pointSample1 = counter;

  Serial.begin (4800);

  pinMode(2, INPUT_PULLUP); //sets pin mode for pin 2

  pinMode(3, INPUT_PULLUP); //sets pin mode for pin 3
//Setting up interrupt
  //A rising pulse from encoder activated ai0(). AttachInterrupt 0 is DigitalPin nr 2 on most Arduinos.
  attachInterrupt(0, ai0, RISING);

  //B rising pulse from encoder activated ai1(). AttachInterrupt 1 is DigitalPin nr 3 on most Arduinos.
  attachInterrupt(1, ai1, RISING);
  }

  void loop() {

  // Send the value of counter
  if( counter != temp ){                        //if change is detected in the encoder, print the positional data values
  Serial.println ("Positional Data: ");
  Serial.println (counter);
  temp = counter;

    if( counter >= PPR or counter <= -PPR){ //This if statement resets the counter every time the encoder does a full revolution, protecting from reset after memory becomes filled
      counter = 0;
    }
  }
  if(millis() > (time_now + REFRESH_RATE) or millis() == 0){         //should run once every time the refresh rate is met (refresh rate = 1mSec, if statement runs once every mSec). millis() = 0; is for varibale overflow protection
    pointSample2 = counter;                       //sets pointSample2 to the encoder position, pointSample1 starts at 0. This allows the difference between the two to be taken to determine the rotation per mSec
    if(pointSample1 - pointSample2 < 0){          //conditional if / elseif statement checks the sign (positive or negative) of the ticks between samples, and makes sure that the pulses per mSec is always positive
      pulsemSec = pointSample2 - pointSample1;
    }
    else if (pointSample1 - pointSample2 > 0){
      pulsemSec = (PPR - pointSample1) + pointSample2;
    }
    RPM = (((pulsemSec / PPR) * 1000) * 60);      //pulsemSec / PPR = revs per msec; revs per msec * 1000 = revs per sec; revs per sec * 60 = RPM
    pointSample1 = pointSample2;                  //changes pointSample1 for the next cycle
    time_now = millis();                          //sets time_now to the amount of time since program start
    Serial.println ("pulsemSec: ");
    Serial.println (pulsemSec);
    Serial.println ("RPM: ");
    Serial.println (RPM);
  }
  }

  void ai0() {
  // ai0 is activated if DigitalPin nr 2 is going from LOW to HIGH
  // Check pin 3 to determine the direction
  if(digitalRead(3)==LOW) {
  counter++;
  }else{
  counter--;
  }
  }

  void ai1() {
  // ai0 is activated if DigitalPin nr 3 is going from LOW to HIGH
  // Check with pin 2 to determine the direction
  if(digitalRead(2)==LOW) {
  counter--;
  }else{
  counter++;
  }
  }

在测试期间,我使用数字转速计测量了大约473 RPM的电机旋转。根据Arduino输出,这转换为由我的代码测量的RPM为13,350 RPM,一个更高的数字。我认为测量pulsemSec变量的东西是不正确的,而不是转换成RPM的方式,但我不确定究竟是什么问题。

c++ c arduino encoder
1个回答
0
投票

每次毫秒更改时,您都会写入大约30个字节。 4800波特的30个字节是30/480 * 1000 ms = 62ms。因此,一旦缓冲区已满,这将阻止。但是你假设计算中只有一毫秒。

你计算脉冲的方式也是偏斜的:

// ai0 is activated if DigitalPin nr 2 is going from LOW to HIGH
// Check pin 3 to determine the direction

// ai[1] is activated if DigitalPin nr 3 is going from LOW to HIGH
// Check with pin 2 to determine the direction

所以如果我们有偏移编码,我们每个脉冲周期得到两个计数器增量:

pin 2   ________********________********

pin 3   ****________********________****


                !ai0            !ai0
                    !ai1            !ai1
                counter++       counter++
                    counter++       counter++

所以你可能会在一次革命中增加800次计数器。

© www.soinside.com 2019 - 2024. All rights reserved.