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
thiagoralves
Hi @a1b2c3d4 
Adding EtherCAT to OpenPLC would be fenomenal! However, before you start anything, make sure you're working with OpenPLC_v3. OpenPLC version 2 is old, unsupported and obsolete. I know that the previous attempt to get EtherCAT on OpenPLC was on v2, but it is better to use that just as an example and move to v3.

With that being said, let's move to your questions:

1. This is a great idea. In fact, that's exactly how Modbus Master works on OpenPLC (more on that below)

1a. Yes you can. If you're writing an OpenPLC driver, one of the things you have access to are the OpenPLC buffers. You could use one random location (for example %IB0) just to signal configuration things from the EtherCAT side. On the C code, %IB0 is tied to *byte_input[0].

1b. Currently untouched inputs are always zero

1c. This is done automatically during program compilation. All PLC variables in your program are mapped to OpenPLC buffer locations. Example:
%QX0.0 -> *bool_output[0][0]
%QX1.2 -> *bool_output[1][2]
%IX0.0 -> *bool_input[0][0]
%IX1.2 -> *bool_input[1][2]
%QB2 -> *byte_output[2]
%IB9 -> *byte_input[9]
%QW37 -> *int_output[37]
%IW253 -> *int_input[253]

Note: The mapping only occurs if these locations exist on the particular PLC program being uploaded. If the program does not reference any location, the mappings are not created, however the buffers are still there. You can freely manipulate all OpenPLC buffers even if the PLC program does not directly has a mapping to it.

2. OpenPLC can only load one driver at a time (if by driver you mean "hardware layer"). However, OpenPLC has many modules that are loaded concurrently. For example, Modbus Slave, Modbus Master, EtherNet/IP and DNP3 are modules that are loaded concurrently. Each one has its own .cpp file and can be loaded/unloaded under settings. Perhaps the module that is most similar to what you are trying to do is the Modbus Master module (modbus_master.cpp). This module has an entire page associated with it on the OpenPLC runtime webserver (Slave Devices page). For EtherCAT we probably will need something similar to that. A dedicated configuration page allowing settings to be configured and "slave" etherCAT devices to be added. For the modbus master module, any slave device that is added automatically attaches to address location 100 and over. For example, if you add a modbus slave device that has 3 coils (digital outputs), its coils will be automatically mapped on OpenPLC to %QX100.0, %QX100.1 and %QX100.2. You could do the same thing with EtherCAT, attaching slave devices on location 200 or something like that.

3 and 3a. Modbus is not a real time protocol, that' why the request/response is asynchronous with the PLC scan cycle. I could not let the PLC cycle stall while it waits for a Modbus response. For EtherCAT, you can have it running in sync with the PLC scan cycle. This is just a matter of how you implement it. Instead of having it running on a separate thread, you can just update inputs and outputs directly on each respective callback.

4. This is a bit more complicated. I'm not familiarized with EtherCAT, so my help here might be a bit superficial. But if I understood correctly, I believe the best way to implement this would be using compiler pragmas. On the editor, one can create a custom function block that makes use of compiler pragmas. What a compiler pragma does is simply let the code inside a given function block to be written directly in C. When the compiler gets to a pragma, it skips the compilation process for that block and inserts the block directly on the main code. Doing that, you can create custom function blocks with the proper C code calling the functions you need and reading the return value of the function. Here are some interesting posts about using compiler pragmas:
https://openplc.discussion.community/post/command-exec-in-openplc-rpi-9707083?pid=1303821827
https://openplc.discussion.community/post/oscat-library-arithmetics-section-10260702?pid=1309458163
Once the blocks are working, I can add them to the official block list on the editor and all the user has do is to include them on their PLC program just like any other block.

I hope this explanation clarifies some of your questions. If you still need more information, please do not hesitate to ask. Adding etherCAT is a very important feature and I would love to have that done. Unfortunately I don't have the experience required with etherCAT, nor the time to do it myself. But I'm willing to provide support along the way as much as I can.


Thanks!
Reply 0 0
a1b2c3d4
Thanks! That's answer is a big help. I'm going to do some work on integrating the ethercat user library into open PLC, and then I'm sure I'll have a few questions, but I this cleared up a lot for me.

Thanks!
Reply 0 0
a1b2c3d4
>> but it is better to use that just as an example and move to v3.

v3 was always my intention.

Thanks!
Reply 0 0
jurep
Integration of EtherCAT master in a future official release of OpenPLC runtime and editor will be a game changer. IO modules from Beckhoff as well as from other vendors come at very competitive prices and will enable us (OpenPLC users) to really make the industrial grade control systems from hardware perspective. The EtherCAT concept of adding/removing IO modules to the chain offers limitless possibilities for use and integration.

Integration for at least some digital and analog IO will be epic.

I am not an experienced developer, so I doubt I can help much with the development of this future. However I have access to various Beckhoff EtherCAT slaves. I could run some tests on various modules, if there would be a need for it. I run OpenPLC runtime on Raspberry.
Reply 0 0
thiagoralves
I agree with @jurep it would be great to have it on OpenPLC. @a1b2c3d4 how is the project going? Need some help?
Reply 0 0
a1b2c3d4
>> Integration of EtherCAT master in a future official release of OpenPLC runtime and editor will be a game changer

Agreed.

>> I could run some tests on various modules, if there would be a need for it.

@jurep: I have a lot of hardware here in my lab, so I should be able to run some tests. I also have a lot of experience getting slaves up and running with the ethercat master. I think it will be good have someone else try out the implementation, though, and will definitely take you up on that offer once I have a working system.

>> . @a1b2c3d4 how is the project going?

@thiagoralves: Grabbed a raspberry pi 4B. Got Linux rt-preempt running on it. Finally got etherlab ethercat master up and running on that hardware. Compiled and built openplc on the raspberry pi. I'm familiarizing myself with openplc a bit (great work, by the way).

I can definitely pull this off. The hardest part will be the configuration program. I don't think I'll have it done this week because I'm only able to put about an hour a day into it right now, but this is totally in my wheel-house. I expect to have a working demo within 2-4 weeks.

The end result run-time will be Linux-only, at least in the short term. I have an EtherCAT master I wrote that works in Windows, but it supports less features and isn't as performant as etherlab's.

I'll probably have some more intelligent questions next week, but I do have some stupid questions right now:
@thiagoralves, I noticed you have etherlab integrated into the editor. What's that for? Maybe that solves my configuration-editor issue.

Also (and this is just to make my life easier) is there a way to remote-edit/compile a PLC program in openplc. Right now, I'm running the editor on Windows and playing around with it. Works great, but the target system is Raspian running on ARM. Is the typical situation to run the editor on the target hardware, or is there a way to cross-compile the output from iec2c? This would just make my workflow easier.

Also, @thiagoralves: How do you want me to submit code when it's done? I can fork the repo, do the work there, then you can pull out whatever you like. Does that work for you?

Thanks!

Reply 0 0
thiagoralves
a1b2c3d4 wrote:

I can definitely pull this off. The hardest part will be the configuration program. I don't think I'll have it done this week because I'm only able to put about an hour a day into it right now, but this is totally in my wheel-house. I expect to have a working demo within 2-4 weeks.

Make it parse a .cfg file. Once that is working, I can create an HTML UI for the configuration on the OpenPLC webserver and make it generate the .cfg file for you. This is exactly how the Modbus master module works. The 'Slave Devices' page in the end is just the front-end for the .cfg file.

a1b2c3d4 wrote:

@thiagoralves, I noticed you have etherlab integrated into the editor. What's that for? Maybe that solves my configuration-editor issue.

OpenPLC Editor is a fork of the Beremiz IDE. They have something done with EtherCAT in there, but their documentation is in Russian, so it is hard to really understand what is functional and what is a "work in progress". OpenPLC Editor is mainly a wrap up of working features on Beremiz, or at least the things I noticed that are working well, with some adjustments specifically for the OpenPLC Runtime.

a1b2c3d4 wrote:

Also (and this is just to make my life easier) is there a way to remote-edit/compile a PLC program in openplc. Right now, I'm running the editor on Windows and playing around with it. Works great, but the target system is Raspian running on ARM. Is the typical situation to run the editor on the target hardware, or is there a way to cross-compile the output from iec2c? This would just make my workflow easier.

Ideally you should have the editor running on a PC and the runtime running on an embedded device, like your Raspberry Pi. You don't need to cross compile anything. OpenPLC runtime takes care of remotely compiling everything for you. Just create a silly program on the editor that blinks an output or whatever else you would like, and upload that to your raspberry pi through the webserver (more information here:  https://www.openplcproject.com/reference/basics/upload ). If you change something on the runtime code, you will need to force a recompilation process to check on your changes. You can do that by uploading another silly program (the hard way), or just going to the hardware tab and clicking on "Save Changes" without changing anything (the easy way). When you do that, it reloads the hardware driver and forces a recompilation. That's what I do when I need to develop stuff on the runtime.

a1b2c3d4 wrote:

Also, @thiagoralves: How do you want me to submit code when it's done? I can fork the repo, do the work there, then you can pull out whatever you like. Does that work for you?

Yup!
Reply 0 0
a1b2c3d4
[snip]
Make it parse a .cfg file. Once that is working, I can create an HTML UI for the configuration on the OpenPLC webserver and make it generate the .cfg file for you. This is exactly how the Modbus master module works. The 'Slave Devices' page in the end is just the front-end for the .cfg file.
[/snip]

I like that plan and think that's a great way to integrate, but we'll probably need to co-ordinate. Configuring an EtherCAT network can get complicated. My thinking is the configuration tool will need to:
- Ask the ethercat master which slaves are on the network.
- Parse through the ESI files to get the default PDO configuration for that device.
- Allow the programmer to select alternative pre-configured PDO layouts (these will be listed in the ESI file) or specify a custom PDO layout (often necessary, particularly with slaves not manufactured by Beckhoff and vendors that do a poor job created their ESI files).
- Allow the programmer to specify startup CoE commands.

That's all normal. From there, the next step is that the user will need to map a PDO (for instance, a digital input on an input card) to an address in the PLC.

There are also some files in the operating system which one would probably want editable through the webserver, but we can worry about that later.



[snip]
They have something done with EtherCAT in there, but their documentation is in Russian, so it is hard to really understand
[/snip]

Okay, I'll ignore it, then.

I thought of another stupid question: how do I throttle the PLC scan? I want to set it to "tick" at 1ms intervals. That way, the interface to the EtherCAT master can all be in the same thread and synchronous.

Thanks!

Reply 0 0
jurep

You can do that in the OpenPLC editor in Project double click on Res0. On the right you can set the interval for a task.
More details here:

https://www.openplcproject.com/reference/basics/first-project

Reply 0 0
a1b2c3d4
jurep wrote:

You can do that in the OpenPLC editor in Project double click on Res0. On the right you can set the interval for a task.
More details here:

https://www.openplcproject.com/reference/basics/first-project



Awesome. That solves that issue.
Reply 0 0
tycho
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 🙂
Reply 0 0
thiagoralves
In the meantime, if you guys know a good EtherCAT Python library, one could leverage PSM to write a quick glue logic to write an EtherCAT driver in PSM.
Reply 0 0
thiagoralves
PS: If you don't know what PSM is:  https://openplc.discussion.community/post/megaioind-industrial-building-automation-for-raspberrypi-9832564?pid=1325299322
Reply 0 0
jurep

We use this Python library:

https://pypi.org/project/pyads/

but this is used to access Beckhoff PLC variables and not directly the EtherCAT modules.

Another thing concerning Raspberry PI and EtherCAT, I was following, is this: http://www.simplerobot.net/. It was a bit to low level programming for me, but it might be of some use for you guys.

Reply 0 0
Reply