Arduino RPM代码与正交编码器600ppr
Arduino RPM code with Quadrature Encoder 600ppr
我正试图设置我的arduino uno,以便它为我提供在编码器上测量的RPM。到目前为止,我已经能够找到如何准确测量第一个值的RPM,但我很难在循环结束时重置值,所以我没有给我实际的转数,而是随着之前的数据递增。以下是我的代码,以便更好地理解我的意思。。。
// Red - 5V
// Black - GND
const int encoder_a = 2; // Green - pin 2 - Digital
const int encoder_b = 3; // White - pin 3 - Digital
long encoder = 0;
long encoderO=0;
void setup() {
Serial.begin(9600);
pinMode(encoder_a, INPUT_PULLUP);
pinMode(encoder_b, INPUT_PULLUP);
attachInterrupt(0, encoderPinChangeA, CHANGE);
attachInterrupt(1, encoderPinChangeB, CHANGE);
}
void loop() {
Serial.println(encoder/2400.00*60);
delay(1000);
}
void encoderPinChangeA() {
encoder += digitalRead(encoder_a) == digitalRead(encoder_b) ? -1 : 1;
}
void encoderPinChangeB() {
encoder += digitalRead(encoder_a) != digitalRead(encoder_b) ? -1 : 1;
}
Wessels的回答很好。提供的代码中的一些小错误,完整的工作和测试代码:
const int encoder_a = 3; // Pin 3
const int encoder_b = 5; // Pin 5
long encoder_pulse_counter = 0;
long direction = 1;
void encoderPinChangeA()
{
encoder_pulse_counter += 1;
direction = digitalRead(encoder_a) == digitalRead(encoder_b) ? -1 : 1;
}
void encoderPinChangeB()
{
encoder_pulse_counter += 1;
direction = digitalRead(encoder_a) != digitalRead(encoder_b) ? -1 : 1;
}
void setup()
{
Serial.begin(9600);
pinMode(encoder_a, INPUT_PULLUP);
pinMode(encoder_b, INPUT_PULLUP);
attachInterrupt(0, encoderPinChangeA, CHANGE);
attachInterrupt(1, encoderPinChangeB, CHANGE);
}
void loop()
{
long speed = encoder_pulse_counter/1024.00*60; // For encoder plate with 1024 Pulses per Revolution
Serial.print("RPM: ");
Serial.println(direction*speed);
encoder_pulse_counter = 0; // Clear variable just before counting again
delay(1000);
}
我会尝试重新构造一下代码。这主要是为了区分速度和方向,因为它们不相互依赖。例如:
long encoder_pulse_counter = 0;
long direction = 1
void setup() {
Serial.begin(9600);
pinMode(encoder_a, INPUT_PULLUP);
pinMode(encoder_b, INPUT_PULLUP);
attachInterrupt(0, encoderPinChangeA, CHANGE);
attachInterrupt(1, encoderPinChangeB, CHANGE);
}
void loop(){
long speed = encoder_pulse_counter/2400.00*60; // Not sure, encoder not specified
Serial.println(direction*speed);
encoder_pulse_counter = 0; // Clear variable just before counting again
delay(1000);
}
void encoderPinChangeA(){
encoder_pulse_counter += 1;
direction = digitalRead(encoder_a) == digitalRead(encoder_b) ? -1 : 1;
}
void encoderPinChangeB(){
encoder_pulse_counter += 1;
direction = digitalRead(encoder_a) != digitalRead(encoder_b) ? -1 : 1;
}
我没有测试这个代码,所以可能有一些小错误。但我认为分开计算会容易得多。希望这能有所帮助!
volatile long encoder_pulse_counter = 0;
volatile long direction = 1
void setup() {
Serial.begin(9600);
pinMode(encoder_a, INPUT_PULLUP);
pinMode(encoder_b, INPUT_PULLUP);
attachInterrupt(0, encoderPinChangeA, CHANGE);
attachInterrupt(1, encoderPinChangeB, CHANGE);
}
void loop(){
long speed = encoder_pulse_counter/2400.00*60; // Not sure, encoder not specified
Serial.println(direction*speed);
encoder_pulse_counter = 0; // Clear variable just before counting again
delay(1000);
}
void encoderPinChangeA(){
encoder_pulse_counter += 1;
direction = digitalRead(encoder_a) == digitalRead(encoder_b) ? -1 : 1;
}
void encoderPinChangeB(){
encoder_pulse_counter += 1;
direction = digitalRead(encoder_a) != digitalRead(encoder_b) ? -1 : 1;
}
同意,只需将volatile添加到ISR中使用的任何变量中就可以了。。
相关文章:
- 使用 MATLAB 编码器生成C++代码:编译错误"undefined reference to `rgb2gray_tbb_real64'"
- 绝对编码器十六进制输入
- *新的编码器*格式和运算符
- Arduino RPM代码与正交编码器600ppr
- 如何获得GUID编码器,如果我在IMFTransform上有一个指针?
- 如何将 MATLAB 图像处理库内置函数转换为 MATLAB 编码器代码生成不支持的 C++?
- Openh264 编码器增加了 IDR 或 I 帧的数量
- 确定输入"conscalc: calc"的类型时出错。不能联合编码器。具有不同字段集的结构类型
- 将原始视频图像转换为VPX编码器的I420
- 使用 MATLAB 编码器将代码从注册估算器应用程序导出到C++
- 如何读取旋转编码器
- 防止编码器错误 - 忘记在"std::enable_if<>::type"中添加"::type" (SFINAE)
- 如何使用Imebra库将压缩像素(用我自己的编码器压缩)回到DICOM图像文件中
- 如何从Intel MFT编码器的D11纹理创建IMFSample
- Matlab 编码器转换可变大小单元格错误
- 使用 Arduino 将位置编码器值转换为 RPM 时出现问题
- 防止编码器错误 - 通过参考(悬挂指针)返回临时值
- 编码器的PDF规格:Adobe或ISO
- Matlab 编码器错误 "for loop index expressions.." 如何修改代码?
- C和MATLAB:为什么MATLAB中的一行成为MATLAB编码器生成的C 代码中的许多行