我正在将400 PPR编码器与Arduino一起使用以测量编码器值。我能够让编码器读取值0-400(正向旋转)和0到-400(负向旋转),并在编码器完成一整圈旋转后将其重置为0。我的代码的这一部分有效,我遇到的问题是将这些位置值转换为RPM。
我的方法是这样的:我正在使用millis()函数来跟踪整个程序时间,并使用一个变量time_now来存储该值。我有一个if语句,每次millis()变得比time_now大REFRESH_RATE(在我的情况下,它定义为1 mSec),因此每1毫秒运行一次。
我的下一个方法是每毫秒采样两个样本。 pointSample1和pointSample2之间的差异应为我提供编码器以1毫秒为单位移动的滴答声的数量。这有点复杂,因为它实际上是一个400度的圆,因此,例如,如果pointSample1为395度,pointSample2的读数为5度,我们需要采用编码器分辨率和pointSample1之差,然后将其加到pointSample 2或(PPR- pointSample1)+ pointSample2。
同样,这应该给我每毫秒的滴答声,然后可以轻松转换为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的方法存在问题,但是我不确定问题出在什么地方。
答案 0 :(得分:0)
每毫秒更改一次,您写入大约30个字节。 4800波特时30字节为30/480 * 1000毫秒= 62毫秒。因此,一旦缓冲区已满,它将阻塞。但是您假设计算中只经过了1毫秒。
计算脉冲的方式也偏斜:
// 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倍。