P201 Encoder Interface

The P201 series are single channel encoder interfaces suitable for use with a wide variety of 5V rotary and linear encoders. The USB versions allow encoders to be easily interfaced to and powered from a PC using only a USB cable. The new RS485 encoder interface (P201-9B-RS485) enables long distance, multidrop capability for SSI or BiSS C encoders.

Supported by PC based GUI Demonstration software and supplied with USB drivers that emulate a COM Port. Virtual COM Port access enables simple interfacing from LabVIEW, C#.Net, VB.Net, MATLAB.

More details can be referred to the data sheet of P201 interface.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
% Function: encoderP201Read
% Author: Yu Xia
% Date: 2024.7.1 modified on 2024.10.8
% Institution: University of Melbourne
% Email: xiayudlut@icloud.com
% Description: This function communicates with an encoder via a serial port
% to read the encoder count, detect reference index, or clear/set the reference.
% It incorporates error handling to manage serial communication issues.
% The function returns three numbers: the encoder count, the count value at the
% z-channel rising edge (reference index), and the status indicating if the reference
% is detected.

function numbers = encoderP201Read(port, command)
% Input:
% port - Serial port identifier (e.g., 'COM5')
% command - Command to send to the encoder ('?' for read, 'c' for clear reference, 'z' for set to zero)
% Output:
% numbers - A vector containing:
% [encoder count, count value at z-channel, reference status]
% or an empty array if the command is 'c' or 'z'

% Set up parameters
baudRate = 9600; % Baud rate for serial communication
maxRetries = 3; % Maximum number of retries
retryDelay = 0.5; % Delay between retries in seconds

% Attempt to communicate with the encoder
attempt = 0;
success = false;

while ~success && attempt < maxRetries
attempt = attempt + 1;
try
% Initialize serial port
sEncoder = serialport(port, baudRate);
sEncoder.Timeout = 2; % Set timeout for serial communication

% Flush the serial buffer
writeline(sEncoder, 'v');
pause(0.1);
read(sEncoder, sEncoder.NumBytesAvailable, 'char');

% Execute command based on the input
switch command
case '?' % Read encoder data
writeline(sEncoder, '?');
pause(0.1);
encoderStr = read(sEncoder, sEncoder.NumBytesAvailable, 'char');
pattern = '\d+'; % Regular expression to extract numbers
numbers = regexp(encoderStr, pattern, 'match');
numbers = str2double(numbers); % Convert string array to double array
case 'c' % Clear reference status
writeline(sEncoder, 'c');
pause(0.1);
fprintf('\n Clear reference status \n');
numbers = [];
case 'z' % Set current count value to zero
writeline(sEncoder, 'z');
pause(0.1);
fprintf('\n Set current count value to zero \n');
numbers = [];
otherwise % Invalid command
error('Invalid command. Use ''?'', ''c'', or ''z''.');
end
% If everything went well, mark as success
success = true;
catch ME
% Handle communication error and retry
fprintf('Error: %s. Retrying...\n', ME.message);
pause(retryDelay);
end

% Close serial port connection
if exist('sEncoder', 'var')
delete(sEncoder);
end
end

% Check if communication was successful
if ~success
error('Failed to communicate with the encoder after %d attempts.', maxRetries);
end
end

% Example Usage:
% 1. To calibrate the encoder and set the zero position:
% encoderP201Read('COM5', 'z');
%
% 2. To read the encoder count, reference index, and status:
% data = encoderP201Read('COM5', '?');
% encoderCount = data(1); % Number of pulses read by the encoder
% referenceIndex = data(2); % Count value at the reference index
% referenceStatus = data(3); % Indicates if the reference is detected
%
% The difference between two encoderCount readings determines the
% rotation angle, based on the encoder's resolution. For example,
% with a resolution of 4000 PPR (pulses per revolution), 360 degrees
% equals 4000 pulses. Therefore, 1.8 degrees corresponds to 20 pulses.
%
% After calibration, subtract the initial zero position count from
% the current encoderCount to find the position relative to zero.

打赏
  • © 2020-2025 Yu Xia
  • Powered by Hexo Theme Ayer
    • PV:
    • UV:

Buy me a cup of coffee~

支付宝
微信