How to measure angular position of a rotary encoder?

조회 수: 2 (최근 30일)
Sam Ade
Sam Ade 2020년 7월 25일
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