This tutorial describes guidelines and procedures for calculating rotational speed from the pulse variation of a fast counter on the fast input CPUs of the Nexto and Xpress series.
Components
Software: MasterTool IEC XE 3.20
PLC: XP325
Tutorial Sections
1. ARCHITECTURE
2. CONFIGURATIONS IN MASTERTOOL
2.1. Fast Inputs
2.2. CODE DEVELOPMENT
2.2.1. Code 1
2.2.2. Code 2
2.2.3. Notes
2.2.4. Differences between Code 1 and Code 2
2.2.5. RPM Calculation
2.2.6. Suggestion
1. ARCHITECTURE
In the architecture of this example, we will consider an Xpress performing RPM detection of a motor shaft using an inductive sensor.
An important note is the signal characteristics. The specifications of the CPU should be checked.
For the Xpress, the maximum pulse frequency is 100kHz and there must be a minimum pulse width of 2µs.
For the NX 3003, the maximum pulse frequency is 200kHz and there must be a minimum pulse width of 1µs.
The connection of the inductive sensor to the Xpress follows the following diagram:
2. CONFIGURATIONS IN MASTERTOOL
In MasterTool IEC XE, the programming software for both mentioned series, there is no available function or ready-made memory that provides the pulse frequency or the period between pulses of the fast counters. To obtain these quantities, it is necessary to perform mathematical processing on the pulse count. Thus, there are numerous ways to obtain RPM from fast pulses; in this document, we describe only two possibilities.
2.1. Fast Inputs
For the Xpress, it is only necessary to define the fast input I00 as UP/DOWN in Configuration/XP325 (XP325)/Integrated I/O/Fast Inputs.
In this example, the input must be of the UP/DOWN type due to the type of signal, which will come from an inductive sensor. However, if sensors with other characteristics are used, such as a quadrature-type encoder, the fast input should be configured accordingly.
Note that in the case of Xpress, Counter 0 as UP/DOWN has one input for direction, another for reset, and another for pulse counting. Thus, three digital inputs are reserved for counter 0.
The relevant input in our case is I02, which receives the pulses from the sensor. The counter value is incremented by one each time a pulse is detected on I02.
Thus, in MasterTool IEC XE, you can access the value of Counter 0 through the FastInputs structure; below is an example for Structured Text (ST).
2.2. CODE DEVELOPMENT
Pulse Frequency or Period Calculation
We will illustrate two ways to calculate pulse frequency or period. Both are ST codes that can be inserted in the UserPrg of the Main Task or in another POU called by UserPrg. In this example, we inserted the code in a POU called by UserPrg.
It is only necessary to use one of the codes.
2.2.1. Code 1
In this code, the frequency or period is calculated every time the CPU executes the task.
PROGRAM RPM
VAR
// VARIABLES CODE 1
NdePulsos: ULINT;
NdePulsosAnterior: ULINT;
time_1: ULINT;
time_2: ULINT;
Freq_Hz: REAL;
Period_s: REAL;
END_VAR
NdePulsos := DINT_TO_ULINT(FastInputs.Counter0.Counter);
//CODE 1
IF NdePulsos = NdePulsosAnterior THEN
SysTimeGetUs(time_1);
ELSE
SysTimeGetUs(time_2);
Freq_Hz := 1000000*ULINT_TO_REAL(NdePulsos - NdePulsosAnterior)/ULINT_TO_REAL(time_2-time_1);
Period_s := ULINT_TO_REAL(time_2-time_1)/(ULINT_TO_REAL(NdePulsos - NdePulsosAnterior)*1000000);
NdePulsosAnterior := NdePulsos;
SysTimeGetUs(time_1);
END_IF
2.2.2. Code 2
For this code, the frequency is calculated approximately every 100ms.
The frequency is obtained in the variable Freq_Hz_2
PROGRAM RPM
VAR
// VARIABLES CODE 2
time_10: UDINT;
first_cicle: BOOL := TRUE;
pulso_inicial: UDINT;
NdePulsos_2: UDINT;
Freq_Hz_2: REAL;
time_11: REAL;
END_VAR
//CODE 2
NdePulsos_2 := DINT_TO_UDINT(FastInputs.Counter0.Counter);
IF first_cicle THEN
pulso_inicial := NdePulsos_2;
time_10 := SysTimeGetMs();
first_cicle := FALSE;
END_IF
time_11 := DINT_TO_REAL(SysTimeGetMs() - time_10);
IF time_11 >= 100 THEN
Freq_Hz_2 := DINT_TO_REAL(NdePulsos_2 - pulso_inicial)*1000/time_11;
first_cicle := TRUE;
END_IF
2.2.3. Notes
Both codes use functions from the SysTimeCore Library. If this library is not included in your project, you need to add it.
To do this, you must go to Library Manager and Add library (1).
Click on advanced (2).
Once this is done, search for "Systime" (3) and double-click on the SysTimeCore library (4).
2.2.4. Differences between Code 1 and Code 2
In code 1, the frequency is calculated every time the task is executed, so the value in Freq_Hz is practically instantaneous.
In code 2, the frequency is calculated approximately every 100ms. In terms of monitoring, this may not be significant, but depending on the need, the timing may be relevant.
Attention: In the case of code 1, since the calculation is executed every time the task is called, depending on the task time (20ms in most applications), it is possible that the pulse samples are too small depending on the frequency. Thus, when there is a possibility of a frequency below 100 Hz, there may be errors in the measurement.
In this case, it would be necessary to create logic to increase the pulse sampling or use Code 2, for example.
2.2.5. RPM Calculation
With the pulse frequency, the RPM of the motor shaft will depend on the number of pulses per revolution that are generated.
This number of pulses, in our case, is the number of teeth on the toothed wheel that is coupled to the motor shaft.
If you are using a quadrature-type encoder, for example, you should check the equipment specifications for the number of pulses per revolution.
Another thing, if the coupling is not direct to the motor shaft, if there are pulleys or gearboxes, the respective rotation ratios should be observed and integrated into the calculation to obtain the correct value.
In our example, the wheel has 16 teeth.
So, our code is as follows:
In the image below, which shows the code running, we see that at this moment the motor shaft was rotating at approximately 3500 RPM.
2.2.6. Suggestion
You can also use the Statistics function to obtain an average value over a time interval, so the value will not fluctuate.
In this example, note that we have a variable that counts the number of times the Freq code is executed (cont_calc).
A logic was created that "resets" the Statistics function after a certain number of code executions, in this example, 100 executions, which is approximately 2 seconds.
Comments
0 comments
Please sign in to leave a comment.