a1b2c3d4
Hi, everyone! Thanks in advance for any advice.

I'd like to integrate Etherlab EtherCAT master into openplc. I see that someone has already had success with SOEM, but I have more experience in working industrial projects with the Etherlab master, and have found it to be more performant and reliable. It also runs as its own service, which I think will be a benefit, particularly when doing motion.

I wanted to run my plan by the forums and check my understanding before trying to implement this.

I know from the blank.cpp driver example (https://github.com/thiagoralves/OpenPLC_v2/blob/master/core/hardware_layers/blank.cpp) that I would have to initialize the hardware (initializeHardware callback), update input buffers (updateBuffersIn callback) and update the ouput buffers (updateBuffersOut callback). That seems pretty straight-forward, however there are a few details about which I'm unsure and I couldn't find much else in the way of API documentation.

1. My plan is to have a separate program generate a network definition file. The initializeHardware callback can read that file, configure the ethercat master, and put it into run. I actually wrote an EtherCAT master for Windows years ago that was able to load ESI files, and I think that can be adapted to that purpose.

1a. However, attempting to put an EtherCAT network into run and actually doing it are two different things. Is there a way to feedback a status object that the PLC program could use to check the status of the network? This may seem trivial, but there won't be much other way for a programmer or user to know the status of the network unless it can be reflected back into the PLC.

1b. If the network isn't in OP and the updateBuffersIn callback is ticked, is there a standard value for when a value hasn't actually been received for an i/o point? I'm assuming the value is just left at 0.

1c. Does the driver get any information about what variable mappings have been assigned to it? Just because an i/o point exists on an EtherCAT network doesn't mean it gets mapped into the PLC program. Basically, I'm asking what information the driver gets about where network values should be
routed to which variables.

2. Can a PLC program have more than one driver? What if I want to have both an EtherCAT network and poll a Modbus/TCP server? How are i/o variables routed to a particular driver? Or is it one driver per PLC instance?

3. The EC master needs to be "ticked" at a steady frequency. My current plan is 1KHz. It looks like the modbus_master.cpp driver is running in its own thread, then simply exchanges data when the updateBuffersIn and updateBuffersOut callbacks occur.

3a. But how does the communication driver synchronize with the scan thread? For instance, if I'm using a PLC and I set an output, I generally assume that the output will get set in the real-world at the end of the scan (I know this isn't always true, but it was generally true back in my GE90/30 days). If the PLC scan is running faster than the communications thread, this won't be true. How is this usually addressed?

4. Everything I've discussed so far is PDO data, but SDO and CoE commands are an important part of an EtherCAT implementation. On Codesys, there are function blocks one can use to read or write SDOs, or execute CoE commands. Is there a way to add such function block support to openplc? My first thought on approaching this would be to include functions in the driver file to execute the SDO or CoE commands; that's rather straightforward. But how do I enable the PLC program to call those functions?

Thanks!
Reply 1 0
a1b2c3d4
tycho wrote:
Hi a1b2c3d4, could you please share any your recent update on EtherCAT with OpenPLC?
I did some work on connecting OpenPLC and EtherCAT master by socket, but I think your way might be more interesting 🙂


Hello!

The updates will be here:

Still working on this. My plan is:
1. Get a small program working with EtherCAT hardware where the network configuration is hard-coded (which is how I've worked with EtherLAB in the past.)
2. Add loading configuration of EtherCAT network from a file. Validate still working.
3. Integrate into OpenPLC. There are a couple of details upon which I'm still fuzzy, but I'm not there yet.

I have hardware setup and I'm working through this. I'm getting slammed by tech support calls right now, so I'm not getting much time during the week. Generating and parsing the configuration files is going to be the hard part. For instance, this is the structure I'll need to generate to get the digital input card on the network:
ec_pdo_entry_info_t slave_1_pdo_entries[] = {
    {0x6000, 0x01, 1}, /* Input */
    {0x6010, 0x01, 1}, /* Input */
    {0x6020, 0x01, 1}, /* Input */
    {0x6030, 0x01, 1}, /* Input */
};

ec_pdo_info_t slave_1_pdos[] = {
    {0x1a00, 1, slave_1_pdo_entries + 0}, /* Channel 1 */
    {0x1a01, 1, slave_1_pdo_entries + 1}, /* Channel 2 */
    {0x1a02, 1, slave_1_pdo_entries + 2}, /* Channel 3 */
    {0x1a03, 1, slave_1_pdo_entries + 3}, /* Channel 4 */
};

ec_sync_info_t slave_1_syncs[] = {
    {0, EC_DIR_INPUT, 4, slave_1_pdos + 0, EC_WD_DISABLE},
    {0xff}
};


The PDO mapping also needs to be configured, but that's a little easier. I still haven't seen any reason why I can't make this happen.

In my experience, EtherLAB does a bad job of figuring out a bus configuration automatically. I've always had to generate those structures manually. The good news is, once you get that configuration right, EtherLAB works every time. Also, graphical tools that help people configure a network really isn't my strong-point. If anyone wants to work with me on a network editor, please let me know.

There isn't currently much to see, but if you want to follow along with step 1/2, I have to fix a couple things in the test program, but I'm pushing up my work here:
Reply 0 0
a1b2c3d4
Quick update: Finally had a chance to sit down and get the test program working with my test rack. I had forgotten a few things about working with EtherLAB. The ectest program in the repository I mentioned now works. My next step is to make it work by loading a configuration file (I'm thinking JSON), then I'll integrate that into the openplc engine.
Reply 1 0
mrea
Just wondering if you had any luck getting all this running? I'm currently using CodeSys with EtherCat remote IO, but there prices keep going up and now looking for alternatives. Thanks.
Reply 0 0
a1b2c3d4
It's been awhile since I've worked on this issue. I've got everything figured out, I just need to write the code. The first thing I have to do is port over my ESI parser, which generates the network layout. After that, integrating the EtherLAB master into OpenPLC should be straight-forward. I'm hoping to have it done before the end of the year.
Reply 0 0
r2k-in-the-vortex
That half-baked SOEM implementation that never went anywhere was by me.
Got interested in running EtherCAT on open source again, also saw that master from Etherlabs comes recommended. Well, this time I'm approaching the conf task from the xml that etherlabs master spits out itself, it has all the actually connected device data in it already, and I guess if you need to make it offline or customized its always possible to compile the file manually or with some external py utility or something.

My c experience is limited, but I'm gobbling something together  https://github.com/r2k-in-the-vortex/ecat_test lets see how far I get with it this time. 

End goal I'm thinking is Raspberry pi 4 + xenomai 4 + etherlabs EtherCAT master + OpenPLC + some reasonably usable configuration glue in between which is yet to be written. If all that would work together, wouldn't that be fancy. That's my work cut out for me in any case. 

The complicated part will be getting it all running in realtime, which I think is possible on pi 4 as it has gige interface unlike earlier pies, but I'm not yet entirely sure how. Xenomai 4 runs, so far-so good, etherlabs master also runs so that's two points. But integrating them? Google-fu suggests a native real time capable broadcom driver exists, somwhere, but I haven't found where or how. Etherlabs has xenomai integration possible, but for earlier versions, version 4 is supposed to work somehow differently without RTDM, so.. I dunno, I will have to revisit that topic, maybe xenomai 3 is the way to go instead. Etherlabs generic driver I suspect might have problems with realtime because it goes through non-realtime part of the os to send a package? maybe? Unanswered questions there.

Well, non-realtime it can communicate with devices just fine, so first I'll focus on the simpler part of configuring and linking ethercat data, once I get that to part I'll try and do a better job of OpenPLC integration than last time. It'll should work well enough for simple IO at least.
Reply 0 0
r2k-in-the-vortex
Drumroll please... r2k presents - a working EtherCAT on rpi with OpenPLC_v3, code available at  https://github.com/r2k-in-the-vortex/OpenPLC_v3
Pay special attention to utils/ethercat_src/conf/
Need to install IgH EtherCAT master, submodule under ethercat_src/external/
XML file is obtained by scanning ethercat, command:
sudo ethercat xml > masterconf.xml (spits out ascii, but needs to be utf-8, massage manually or figure out a way to do it automatically)

all EtherCAT operations require elevation, so if EtherCAT enabled then sudo start openplc, otherwise it doesn't work

ethercat config has configonly flag available that can be used to simulate the entire config and glue module of ethercat_src, when enabled it does basically everything short of actually interacting with ethercat master, one bool var is also looped back in simulation of a output connected to input  https://github.com/r2k-in-the-vortex/ethercat_src/blob/main/src/ecat_handler/ecat_handler.c#L480 

openplc platform is linux even though it's on pi
sudo ./install.sh linux

ethercat_src also has it's own ./install.sh so it's not needed to reinstall the entire openplc just to update ethercat_src

the commands to massage IgH master are
./bootstrap
./configure --sysconfdir=/etc --enable-8139too=no
make
make modules

sudo make install
sudo make modules_install install
sudo depmod

sudo apt-get install udev

sudo nano /etc/ethercat.conf
in here, set
DEVICE_MODULES="generic"
and master mac to whatever it is
MASTER0_DEVICE="dc:a6:32:7c:13:7a"

sudo reboot

ethercat should now be running, but if not the commands to start/stop it are
sudo systemctl start ethercat
sudo systemctl restart ethercat
sudo systemctl stop ethercat

for debugging ethercat master start with
ethercat --help 

  https://www.youtube.com/shorts/vSwZJPbkI8s
Reply 3 0
r2k-in-the-vortex
Couple notes, runtime logs are important, this is from the web dashboard of OpenPLC runtime

Below I have a setup of 4 slaves
EK1814, EL1018, EL1018 and EL2008

It prints out the locations and variable sizes for all pdo channels(inputs/outputs)
You have to use those ones in your program. Variable names you are welcome to change at will, but locations and types must be correct for everything to glue together correctly.

Little bit below that is EtherCAT initialization and diagnost
WC working counter depends on how many slaves you have configured, each slave increments it in package so in my case it has to be 7
master->EK1814->EL1018->EL1018->EL2008->EL1018->EL1018->EK1814->master that's seven incrementations, it also increments on way back
if you get less, then some slave in the way is not working right, might be disconnected or not initialized right.

AL states: OP means all slaves are in operational state, that's a good line have in log.

Link is up. that just means the ethernet port on master is up, if you don't have that obviously things don't work. 

Domain1: State 2. number 2 is a good number, means EC_WC_COMPLETE    /**< All registered process data were exchanged. */
if it's zero or 1(incomplete) you have problems


OpenPLC Runtime starting...
Interactive Server: Listening on port 43628
Configuring EtherCAT
Slave0_Channel_1 AT %IX0.0 : BOOL; (* EK1814 EtherCAT-EA-Koppler (1A E-Bus, 4 K. Dig. Ein, 3ms, 4 K.  *)
Slave0_Channel_2 AT %IX1.0 : BOOL; (* EK1814 EtherCAT-EA-Koppler (1A E-Bus, 4 K. Dig. Ein, 3ms, 4 K.  *)
Slave0_Channel_3 AT %IX2.0 : BOOL; (* EK1814 EtherCAT-EA-Koppler (1A E-Bus, 4 K. Dig. Ein, 3ms, 4 K.  *)
Slave0_Channel_4 AT %IX3.0 : BOOL; (* EK1814 EtherCAT-EA-Koppler (1A E-Bus, 4 K. Dig. Ein, 3ms, 4 K.  *)
Slave1_Channel_1 AT %IX4.0 : BOOL; (* EL1018 8K. Dig. Eingang 24V, 10�s *)
Slave1_Channel_2 AT %IX5.0 : BOOL; (* EL1018 8K. Dig. Eingang 24V, 10�s *)
Slave1_Channel_3 AT %IX6.0 : BOOL; (* EL1018 8K. Dig. Eingang 24V, 10�s *)
Slave1_Channel_4 AT %IX7.0 : BOOL; (* EL1018 8K. Dig. Eingang 24V, 10�s *)
Slave1_Channel_5 AT %IX8.0 : BOOL; (* EL1018 8K. Dig. Eingang 24V, 10�s *)
Slave1_Channel_6 AT %IX9.0 : BOOL; (* EL1018 8K. Dig. Eingang 24V, 10�s *)
Slave1_Channel_7 AT %IX10.0 : BOOL; (* EL1018 8K. Dig. Eingang 24V, 10�s *)
Slave1_Channel_8 AT %IX11.0 : BOOL; (* EL1018 8K. Dig. Eingang 24V, 10�s *)
Slave2_Channel_1 AT %IX12.0 : BOOL; (* EL1018 8K. Dig. Eingang 24V, 10�s *)
Slave2_Channel_2 AT %IX13.0 : BOOL; (* EL1018 8K. Dig. Eingang 24V, 10�s *)
Slave2_Channel_3 AT %IX14.0 : BOOL; (* EL1018 8K. Dig. Eingang 24V, 10�s *)
Slave2_Channel_4 AT %IX15.0 : BOOL; (* EL1018 8K. Dig. Eingang 24V, 10�s *)
Slave2_Channel_5 AT %IX16.0 : BOOL; (* EL1018 8K. Dig. Eingang 24V, 10�s *)
Slave2_Channel_6 AT %IX17.0 : BOOL; (* EL1018 8K. Dig. Eingang 24V, 10�s *)
Slave2_Channel_7 AT %IX18.0 : BOOL; (* EL1018 8K. Dig. Eingang 24V, 10�s *)
Slave2_Channel_8 AT %IX19.0 : BOOL; (* EL1018 8K. Dig. Eingang 24V, 10�s *)
Slave0_Channel_5 AT %IX0.0 : BOOL; (* EK1814 EtherCAT-EA-Koppler (1A E-Bus, 4 K. Dig. Ein, 3ms, 4 K.  *)
Slave0_Channel_6 AT %IX1.0 : BOOL; (* EK1814 EtherCAT-EA-Koppler (1A E-Bus, 4 K. Dig. Ein, 3ms, 4 K.  *)
Slave0_Channel_7 AT %IX2.0 : BOOL; (* EK1814 EtherCAT-EA-Koppler (1A E-Bus, 4 K. Dig. Ein, 3ms, 4 K.  *)
Slave0_Channel_8 AT %IX3.0 : BOOL; (* EK1814 EtherCAT-EA-Koppler (1A E-Bus, 4 K. Dig. Ein, 3ms, 4 K.  *)
Slave3_Channel_1 AT %IX4.0 : BOOL; (* EL2008 8K. Dig. Ausgang 24V, 0.5A *)
Slave3_Channel_2 AT %IX5.0 : BOOL; (* EL2008 8K. Dig. Ausgang 24V, 0.5A *)
Slave3_Channel_3 AT %IX6.0 : BOOL; (* EL2008 8K. Dig. Ausgang 24V, 0.5A *)
Slave3_Channel_4 AT %IX7.0 : BOOL; (* EL2008 8K. Dig. Ausgang 24V, 0.5A *)
Slave3_Channel_5 AT %IX8.0 : BOOL; (* EL2008 8K. Dig. Ausgang 24V, 0.5A *)
Slave3_Channel_6 AT %IX9.0 : BOOL; (* EL2008 8K. Dig. Ausgang 24V, 0.5A *)
Slave3_Channel_7 AT %IX10.0 : BOOL; (* EL2008 8K. Dig. Ausgang 24V, 0.5A *)
Slave3_Channel_8 AT %IX11.0 : BOOL; (* EL2008 8K. Dig. Ausgang 24V, 0.5A *)
Warning: Persistent Storage file not found
enabled4 slave(s).
AL states: PREOP.
Link is up.
Domain1: WC 3.
Domain1: State 1.
Domain1: WC 4.
Issued stop_modbus() command
Issued stop_dnp3() command
Domain1: WC 5.
Issued stop_enip() command
Issued stop_pstorage() command
Domain1: WC 7.
Domain1: State 2.
AL states: OP.

Reply 1 0
r2k-in-the-vortex
As not everyone has EtherCAT slaves lying around and with current component drought the decent ones are seriously overpriced  I ordered a cheap chinesium one for testing  https://www.ebay.com/itm/274506812229

I expect it to work as any other EtherCAT slave, but when I get it I'll test it and find out for sure, so there would be some accessible reference hardware options for anyone that cares to test/use this implementation.

When the industrial supply chain normalizes then of course such parts can be had for much less, but that's sometime in future.
Reply 1 0
markk
This project is so great, I have two question for you.
How can I be sure that the Ethercat has been installed successfully? Because I don't have slave device at present.
How to configure parameters on OpenPLC Runtime, if  the slave device has be connected.
Thank you very much.

Drumroll please... r2k presents - a working EtherCAT on rpi with OpenPLC_v3, code available at  https://github.com/r2k-in-the-vortex/OpenPLC_v3
Pay special attention to utils/ethercat_src/conf/
Need to install IgH EtherCAT master, submodule under ethercat_src/external/
XML file is obtained by scanning ethercat, command:
sudo ethercat xml > masterconf.xml (spits out ascii, but needs to be utf-8, massage manually or figure out a way to do it automatically)

all EtherCAT operations require elevation, so if EtherCAT enabled then sudo start openplc, otherwise it doesn't work

ethercat config has configonly flag available that can be used to simulate the entire config and glue module of ethercat_src, when enabled it does basically everything short of actually interacting with ethercat master, one bool var is also looped back in simulation of a output connected to input  https://github.com/r2k-in-the-vortex/ethercat_src/blob/main/src/ecat_handler/ecat_handler.c#L480 

openplc platform is linux even though it's on pi
sudo ./ install.sh linux

ethercat_src also has it's own ./ install.sh so it's not needed to reinstall the entire openplc just to update ethercat_src

the commands to massage IgH master are
./bootstrap
./configure --sysconfdir=/etc --enable-8139too=no
make
make modules

sudo make install
sudo make modules_install install
sudo depmod

sudo apt-get install udev

sudo nano /etc/ethercat.conf
in here, set
DEVICE_MODULES="generic"
and master mac to whatever it is
MASTER0_DEVICE="dc:a6:32:7c:13:7a"

sudo reboot

ethercat should now be running, but if not the commands to start/stop it are
sudo systemctl start ethercat
sudo systemctl restart ethercat
sudo systemctl stop ethercat

for debugging ethercat master start with
ethercat --help 

 
Reply 0 0
r2k-in-the-vortex
ls dev/Ethercat0
It's there? Great
ethercat --help
Helper program working?

You have it installed. Actually ethercat_src doesn't even build if its not installed(I should do something about that)

When you have slaves connected you check it's working with
sudo ethercat scan

Runtime conf is through conf files, there is no web ui for this feature yet.
Reply 0 0
markk
Thank  your reply,
I input 'ethercat --help' to terminal, then it shows  following
and how to configure parameters on runtime web (http://localhost:8080/modbus)

sage: ethercat <COMMAND> [OPTIONS] [ARGUMENTS]

Commands (can be abbreviated):
alias Write alias addresses.
config Show slave configurations.
crc CRC error register diagnosis.
cstruct Generate slave PDO information in C language.
data Output binary domain process data.
debug Set the master's debug level.
domains Show configured domains.
download Write an SDO entry to a slave.
eoe Display Ethernet over EtherCAT statictics.
foe_read Read a file from a slave via FoE.
foe_write Store a file on a slave via FoE.
graph Output the bus topology as a graph.
ip Set EoE IP parameters.
master Show master and Ethernet device information.
pdos List Sync managers, PDO assignment and mapping.
reg_read Output a slave's register contents.
reg_write Write data to a slave's registers.
rescan Rescan the bus.
sdos List SDO dictionaries.
sii_read Output a slave's SII contents.
sii_write Write SII contents to a slave.
slaves Display slaves on the bus.
soe_read Read an SoE IDN from a slave.
soe_write Write an SoE IDN to a slave.
states Request application-layer states.
upload Read an SDO entry from a slave.
version Show version information.
xml Generate slave information XML.

Global options:
--master -m <master> Comma separated list of masters
to select, ranges are allowed.
Examples: '1,3', '5-7,9', '-3'.
Default: '-' (all).
--force -f Force a command.
--quiet -q Output less information.
--verbose -v Output more information.
--help -h Show this help.

Numerical values can be specified either with decimal (no
prefix), octal (prefix '0') or hexadecimal (prefix '0x') base.

Call 'ethercat <COMMAND> --help' for command-specific help.

Send bug reports to fp@igh.de.
Reply 0 0
r2k-in-the-vortex
Well you have ethercat IgH master installed, so congrats. As for configuring OpenPLC side, you can't do that from web interface, /modbus doesn't do anything for EtherCAT and the interface for EtherCAT doesn't exist yet.

openplc calls ethercat_src module from here, giving it path to configuration file "../utils/ethercat_src/build/ethercat.cfg"

you need to have that file at that relative location, among other things it says where the slaves configuration is, xmlfilename=""

It's an XML file like so, but has to be specific to which slaves you actually have, you would get this file by command
sudo ethercat xml > savefile.xml


But as you don't actually have any slaves, you might as well use the example xml file, it has EK1100, 2xEL018 and EL2008 for slaves in it. The EtherCAT master will run regardless of the slaves being connected or not.
Reply 0 0
r2k-in-the-vortex
A hopefully more useful readme written, some code cleanups tested and added https://github.com/r2k-in-the-vortex/ethercat_src
Reply 0 0
markk
Thank your help, I'm trying to do this
Reply 0 0
r2k-in-the-vortex
Than you for being the first guine... *khm, beta tester to try it out. Let me know if you run into any issues I'll help you through and hopefully in the process find out what needs to be improved to make it more user friendly.
Reply 0 0
Reply