How to measure angular position of a rotary encoder?

1 vue (au cours des 30 derniers jours)
Sam Ade
Sam Ade le 25 Juil 2020
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)

Réponses (0)

Catégories

En savoir plus sur Arduino Hardware dans Help Center et File Exchange

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by