SOEM
v1.4.0
|
The SOEM is a library that provides the user application with the means to send and receive EtherCAT frames. It is up to the application to provide means for:
The following sections show some basic examples on how to get the SOEM up and running, as well as making use of the process data and checking for errors. Since all code is local to the application or global variables, it is possible to tweak and optimize when possible.
The following example shows how to add a main function that will be called by startup code. In this example main's only purpose is to spawn a new task that executes SOEM.
Followed by start of the application we need to set up the NIC to be used as EtherCAT Ethernet interface. In a simple setup we call ec_init(ifname) and if SOEM comes with support for cable redundancy we call ec_init_redundant that will open a second port as backup. You can send NULL as ifname if you have a dedicated NIC selected in the nicdrv.c. It returns >0 if succeeded.
SOEM is a light weight ethercat master library used in embedded systems, It supports only runtime configuration. It requests a BRD (Broad Cast Read) of address 0, all fully functional slaves in the network will respond to this request, and therefore we will get a working counter equal to the number of slaves in the network. ec_config_init also sets up the mailboxes for slaves that support it. When ec_config_init finishes it will have requested all slaves to state PRE_OP. All data read and configured are stored in a global array which acts as a placeholder for key values, consult ec_slave for detailed information.
SOEM has now discovered and configured the network it is connected to. Now we can verify that all slaves are present as expected. These definitions could be generated by an external tool in an offline .h file. The definitions could be replaced by a struct keeping slave number.
We now have the network up and configured. Mailboxes are up for slaves that support it. Next we will create an IOmap and configure the SyncManager's and FMMU's to link the EtherCAT master and the slaves. The IO mapping is done automatically, SOEM strives to keep the logical process image as compact as possible. It is done by trying to fit Bit oriented slaves together in single bytes. Below is an example of 8 slaves and how they are ordered. During mapping SOEM also calculates an expected WKC for the IO mapped together. That is the primary key to detect errors.
When the mapping is done SOEM requests slaves to enter SAFE_OP.
To enter state OP we need to send valid data to outputs. The EtherCAT frame handling is split into ec_send_processdata and ec_receive_processdata.
Do custom configuration with PDO Assign or PDO Config. SOEM support custom configuration during start via a PreOP to SafeOP configuration hook. It can be done per slave and should be set before calling the configuration and mapping of process data, e.g. the call to ec_config_map. Setting the configuration hook ensure that the custom configuration will be applied when calling recover and re-configuration of a slave, as described below.
IOmap options legacy versus overlapping. Overlapping IOmap was introduced to handle the TI ESC that doesn't support RW access to non-interleaved input and output process data of multiple slaves. The difference is that legacy IOmapping will send IOmap as is on the EtherCAT network while the overlapping will re-use logic addressing per slave to replace RxPDO process data coming from the Master with TxPDO process data generated by the slave sent back to the master.
Overview of legacy pdo map
Overview of overlapping pdo map
Slave groups can be used to group slaves into separate logic groups within an EtherCAT network. Each group will have its own logic address space mapped to an IOmap address and make it possible to send and receive process data at different update rate.
Below is an example on how to assign a slave to a group. OBS! A slave can only be member in one group.
Alternative 1, configure all slave groups at once, call ec_config_map or ec_config_map_group with arg 0. This option will share IOmap and store the group IOmap data at offset EC_LOGGROUPOFFSET.
Alternative 2, configure the slave groups one by one, call ec_config_map or ec_config_map_group with arg X, Y. This option will use different, supplied by the user, IOmaps.
To exchange process data for given group(s) the user must call send/recv process data per group. The send and receive stack of process data don't consider groups, so the application has to send and receive the process data for one group before sending/receiving process data for another group.
IOmap is the fastest mechanism for accessing slaves' IO data. Using this mechanism, the ec_slave struct keeps pointers to the start byte in the IO map on slave level together with start bit within the start byte. This way we can bit mask IO on bit level even though SOEM has combined slave data to minimize the frame size to be sent. We'll use slave 8 in the picture above as an example. From a printout from ec_slave we have the following:
The Outputs address: 18cf6 is the pointer to slave 8's start byte. The FMMU's Lsb:4 (LogicalStartBit) = ec_slave.Ostartbit telling us how to mask for the individual bits in the combined byte. The same goes for byte addressed slaves, but byte slaves only need the byte start address since they are byte aligned, the start bit will be 0.
Some example on how to access different types of data
Set an output int 16 value when memory alignment needs to be considered, arguments is:
Target can handle non aligned pointers to the IOmap
Identify and manage errors. The key is the Working Counter, CRC errors and errors local to the slave causing a state change can be detected by loss of Working Counter since the syncmanagers won't get updated. When returning Working Counter don't match Expected Working Counter something is wrong, then it is up to an error handler to act, locate the erroneous slave and decide what action to perform. The error may not be fatal. Some basic code from simple_test.
There are multiple ways a slave can communicate with the master. CANopen over EtherCAT (CoE) is a (slow but flexible) asynchronous mechanism for transferring data via mailboxes.
SOEM provides the ecx_SDOread() and ecx_SDOwrite() functions for reading and writing a CoE SDO (Service Data Object) given the corresponding index and subindex.
SOEM does not provide specific functions for accessing CoE PDOs (Process Data Objects). On most slaves, however, it is possible to use the same functions available for SDOs. In the seldom case in which the PDO object has been marked in the CoE dictionary as "PDO only", only IOmap access is allowed. Note that a list of the PDO mappings can be retrieved through the "slaveinfo <interface> -map" command.
This tutorial is just one way of doing it. Enjoy and happy coding!
Andreas Karlsson, rt-labs AB, www.rt-labs.com