This example shows how to control the velocity of a motor using EtherCAT communication.
|On this page…|
For this Example, the EtherCAT network consists of the target computer as EtherCAT Master device and an Accelnet™ AEP 180-18 drive from Copley Controls as EtherCAT Slave device. A supported brushless or brush motor must be connected to the drive. An example of motor that can be used is the SM231BE-NFLN from PARKER. 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 host-target communication. To test this model:
Connect the network port of the dedicated card in the target machine to the EtherCAT IN port of the Accelnet™ drive.
Connect a motor to the Accelnet™ drive.
Make sure the Accelnet™ drive is supplied with a 24-volt power supply.
Build and download the model onto the target.
% 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( [matlabroot,'\toolbox\rtw\targets\xpc\xpcdemos\CopleyMotorVelocityConfig.xml'], '.', 'f' ); % Open the model. mdl = 'xpcEthercatVelocityControl'; mdlOpen = 0; systems = find_system('type', 'block_diagram'); if isempty( strcmp(systems, mdl ) ) mdlOpen = 1; open_system(mdl); end
Figure 1: EtherCAT model for motor velocity control.
Open the mask for the EtherCAT Init block and provide appropriate 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 host computer.
The configuration file CopleyMotorVelocityConfig.xmlCopleyMotorVelocityConfig.xml is generated using the Beckoff® ET9000 configurator. This file describes the slaves' network to the master. Below is an overview of the process to create the configuration file in the EtherCAT Configurator (see the documentation for more details):
Connect the EtherCAT network (consisting of the Accelnet™ drive in this example) to the computer where the EtherCAT Configurator is installed and scan the network to discover the connected slave devices.
Select the transmit and receive Process Data Object (PDO) variables to be accessed from the Accelnet™ drive.
Define at least one cyclic task and associate the selected PDO variables to the task.
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 it was created for (i.e. the network discovered in step 1 above in the configuration file creation process). The configuration file provided for this example is valid if and only if the EtherCAT network consists of an Accelnet™ AEP drive from Copley Controls.
For this example, three receive PDO variables are defined and used in the three EtherCAT PDO Transmit blocks: Control Word, Modes of Operation and Target Velocity. The Control Word PDO variable serves to control the state of the drive. The constant value 15 is given as input to the block to set the first four bits to 1 in order to enable the drive (refer to the CANOpen manual from Copley Controls for details on the bits mapping of this variable). The Modes of Operation PDO variable serves to set the drive operating mode. The constant value 3 is given as input to the block to set the mode of the drive to 'Profile Velocity mode' (refer to the CANOpen manual from Copley Controls for details on supported modes of operation). The Target Velocity PDO variable serves to set the desired velocity. In this example, the velocity command at the input of the block can be tuned through the gain block.
Three transmit PDO variables are also defined in the configuration file and used in the three EtherCAT PDO Receive blocks: Status Word, Actual Motor Velocity and Actual Motor Position. The Status Word PDO variable indicates the current state of the drive. The Actual Motor Velocity and Actual Motor Position PDO variables indicates the current values of the motor velocity and position as read in the drive. Make sure the appropriate transmit and receive PDO variables are selected in the blocks as illustrated in Figure 1 before running the example (you may need to refresh them).
% 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;
### Starting Simulink Real-Time build procedure for model: xpcEthercatVelocityControl ### Generated code for 'xpcEthercatVelocityControl' is up to date because no structural, parameter or code replacement library changes were found. ### Successful completion of build procedure for model: xpcEthercatVelocityControl ### Looking for target: TargetPC1 ### Download model onto target: TargetPC1
The velocity command for the motor is a low frequency sine wave. The actual velocity read back from the controller is delayed by 1 sample time and the actual position is out of phase by 90 degrees from the velocity, as expected.
Take a snapshot of the target computer video display.
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 PDO variables received and transmitted to the drive, once the drive initializes and the state goes to Op state (=8)
tg.viewTargetScreen; % Take a snapshot of the target scopes
Warning: The EraseMode property will be removed in a future release.
%Stop the model -tg;
% Close the model if we opened it. if (mdlOpen) save_system(mdl); close_system(mdl); end % EOF xpcethercataiodemo.m