This example shows how to communicate with EtherCAT devices using the Beckhoff® analog I/O terminals EL3062 and EL4002.
To run this example, you need an EtherCAT network that consists of the target computer as EtherCAT Master device and two analog input/output terminals EL3062 and EL4002 as EtherCAT Slave devices. This example requires a dedicated network card that is installed and available on the target computer. Use the dedicated card for the EtherCAT communication. The dedicated card is 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 that the terminals are supplied with the required 24-volt power supply.
Build and download the model onto the target.
For a complete example that configures the EtherCAT network, configures the EtherCAT master node model, and builds then runs the real-time application, see the Simulink Real-Time EtherCAT documentation.
To open the model, in the Command Window, type:
This model creates two sine wave signals and sends the signals to the EL4002 terminal. Then, the model receives input signal values from the EL3062 terminal.
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. Then, open the model.
copyfile(fullfile(matlabroot,'toolbox','rtw','targets','xpc','xpcdemos','BeckhoffAIOconfig.xml'), '.', 'f' ); copyfile(fullfile(matlabroot,'toolbox','rtw','targets','xpc','xpcdemos','xpcEthercatBeckhoffAIO.slx'), '.', 'f' ); 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. To get these values, in the Command Window, type
tg.getPCIInfo('ethernet'). An example command to set configuration parameters for the
EtherCAT Init block is:
set_param('xpcEthercatBeckhoffAIO/EtherCAT Init ','pci_bus','5','pci_slot','0','pci_function','0')
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. An overview of the process for creating the configuration file in the EtherCAT configurator is:
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 previously defined transmit/receive PDOs (for example, PDOs defined in step 2) and be linked to the required terminals. You only must 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 for which it has been created (for example, the network discovered in step 1 of the configuration file creation process). 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. 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. Make sure the variables for channel 1 and channel 2 of terminal EL4002 are selected in the two
EtherCAT PDO Transmit blocks. 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. Let the model run for 10 seconds
set_param(mdl,'RTWVerbose','off'); rtwbuild(mdl); tg = slrt('TargetPC1'); load(tg,mdl); tg.CommunicationTimeOut=20; start(tg); pause(10);
### 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 ### Created MLDATX ..\xpcEthercatBeckhoffAIO.mldatx ### Looking for target: TargetPC1 ### Download model onto target: TargetPC1
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.
To take a snapshot of the target scopes, type:
When the example completes its run, stop and close the model.
stop(tg); if (mdlOpen) save_system(mdl); close_system(mdl); end