How to measure angular position of a rotary encoder?

3 次查看(过去 30 天)
Hi everyone,
I am using the Arduino support package and an Arduino Mega2560 to measure the angular position of a HKT22 rotary encoder (encoder resolution is 300CPR). My goal is to rotate the shaft of the encoder by hand and have the angular position display in the workspace. However, with the code I wrote below, the angular position display increases continuously even when I stop rotating the shaft. Can anyone offer a solution to this problem?
clear all
a = arduino('COM8','Mega2560','Libraries','RotaryEncoder');
channelA = 'D2';
channelB = 'D3';
encoder = rotaryEncoder(a,channelA,channelB,300);
resetCount(encoder); time = 0; i = 1;
En_Measured_Angle = 0;
while i<150
[count,time] = readCount(encoder);
Encoder_Speed = readSpeed(encoder);
Counts_Array(:,i) = count;
Total_Count = sum(Counts_Array(:,1:i));
En_Measured_Angle = (Total_Count*360)/300;
fprintf('Current Angle: %d\n',En_Measured_Angle);
i = i+1;
end
resetCount(encoder)

回答(0 个)

类别

Help CenterFile Exchange 中查找有关 MATLAB Support Package for Arduino Hardware 的更多信息

产品

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by