Filter löschen
Filter löschen

How to measure angular position of a rotary encoder?

3 Ansichten (letzte 30 Tage)
Sam Ade
Sam Ade am 25 Jul. 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)

Antworten (0)

Kategorien

Mehr zu MATLAB Support Package for Arduino Hardware finden Sie in Help Center und File Exchange

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by