This example shows EtherCAT communication using the Beckhoff® analog I/O terminals EL3062 and EL4002.
For this example, the EtherCAT network consists of the target computer as EtherCAT Master device and the two analog input/output terminals EL3062 and EL4002 as EtherCAT Slave devices. In order to run this example, a dedicated network card must be installed and available on the target computer to be used for the EtherCAT communication, in addition to the card used for the Ethernet link between the development and target computers. To test this model:
Connect the network port of the dedicated card in the target computer to the network IN port of the Beckoff® EK1100 coupler.
Assemble Terminals EL3062 and EL4002 with Coupler EK1100.
Loop back the I/O ports: Connect each output port of Terminal EL4002 to a corresponding input port of Terminal EL3062.
Make sure the terminals are supplied with the required 24-volt power supply.
Build and download the model onto the target.
For a worked-out example of configuring the EtherCAT network, configuring the EtherCAT master node model, and building and running the real-time application, see the Simulink Real-Time EtherCAT documentation.
Click here to open the model:
xpcEthercatBeckhoffAIO. This model creates two sine waves signals and sends them to the EL4002 terminal, then it receives input signal values from the EL3062 terminal (see Figure 1).
% The EtherCAT initialization block requires that the configuration ENI % file is present in the current folder. Copy the example configuration % file from the example folder to the current folder. copyfile(fullfile(matlabroot,'toolbox','rtw','targets','xpc','xpcdemos','BeckhoffAIOconfig.xml'), '.', 'f' ); % Open the model. mdl = 'xpcEthercatBeckhoffAIO'; mdlOpen = 0; systems = find_system('type', 'block_diagram'); if isempty( strcmp(systems, mdl ) ) mdlOpen = 1; open_system(mdl); end
Figure 1: EtherCAT model using Beckhoff® analog I/O slave devices EL3062 and EL40002.
Open the mask for the
EtherCAT Init block and provide the required values for the PCI bus and slot numbers for the network card being used for EtherCAT communication. You can obtain these values by using the command
tg.getPCIInfo('all') at the Matlab command line on the development computer.
Using a third-party EtherCAT configurator that you install on a development computer, generate an EtherCAT configuration file
BeckhoffAIOconfig.xml. This file describes the network to the master. Here is an overview of the process for creating the configuration file in the EtherCAT configurator. For more information, see the Simulink Real-Time EtherCAT documentation.
Connect the network (consisting of Terminals EK1100, EL3062 and EL4002 in this example) to the computer where the EtherCAT configurator is installed and scan the network to discover the connected devices.
Select the transmit and receive variables to be accessed as signals from the IO terminals.
Define at least one cyclic task, select a task execution rate and associate the desired input/output variables to the task. These input/output variables must belong to defined transmit/receive PDOs (i.e. PDOs defined in step 2 above) and be linked to the required terminals. You only need to select one variable from each PDO to make every variable in that PDO accessible.
Export the configuration file into an XML file. Make sure the name of the XML file is different from the name of your Simulink® model.
Each EtherCAT configuration file is specific to the exact network setup from which it has been created (i.e. the network discovered in step 1 above in the configuration file creation process). Therefore, the configuration file provided for this example is valid if and only if the EtherCAT network consists of Terminals EK1100, EL3062 and EL4002.
The configuration file defines a set of transmit and receive variables. For this example, a set of receive variables are defined for each input channel of Terminal EL3062. Make sure the variables for channel 1 and channel 2 of Terminal EL3062 are selected respectively in the two
EtherCAT PDO Receive blocks as illustrated on Figure 1. These two variables are 'Term 2 (EL3062).AI Standard Channel 1.Value' and 'Term 2 (EL3062).AI Standard Channel 2.Value'. In the same way, a set of transmit variables are defined for the two output channels of Terminal EL4002. Again, make sure the variables for channel 1 and channel 2 of Terminal EL4002 are selected in the two
EtherCAT PDO Transmit blocks as illustrated on Figure 1. These two variables are 'Term 3 (EL4002).AO Outputs Channel 1.Analog Output' and 'Term 3 (EL4002).AO Outputs Channel 2.Analog Output'.
% Build the model and download to the target computer. set_param(mdl,'RTWVerbose','off'); % Configure for a non-Verbose build. rtwbuild(mdl); % Build and download application. % Run the model tg.CommunicationTimeOut=20; +tg; pause(10); % Let the model run for 10 seconds
### Starting Simulink Real-Time build procedure for model: xpcEthercatBeckhoffAIO Warning: This model contains blocks that do not handle sample time changes at runtime. To avoid incorrect results, only change the sample time in the original model, then rebuild the model. ### Successful completion of build procedure for model: xpcEthercatBeckhoffAIO ### Looking for target: TargetPC2 ### Download model onto target: TargetPC2
Take a snapshot of the target computer video display. Plotted are the signals transmitted to Terminal EL4002 and received from Terminal EL3062. As expected, the transmitted and received signals displayed on the two scopes are identical, except the offset introduced by the transmission delay.
Scope 1 displays the outputs of the
Ethercat Init block. See the documentation of this block for the meaning of the displayed values.
Scope 2 displays the sine waves generated by the application and sent to channels 1 and 2 of Terminal EL4002 by the Master.
Scope 3 displays the sine waves received from channels 1 and 2 of Terminal EL3062 via the external wire connections.
tg.viewTargetScreen; %Take a snapshot of the target scopes
Figure 2: Target scopes view for the model of Figure 1.
%Stop the model -tg;
% Close the model if we opened it. if (mdlOpen) save_system(mdl); close_system(mdl); end % EOF xpcethercataiodemo.m