Thread: Linux Automation project.

  1. #1
    Registered User
    Join Date
    May 2011
    Location
    Sweden
    Posts
    24

    Linux Automation project.

    Hi!

    I am about to start working on a new project, and I am in way over my head. I was hoping that you guys could point me in the right direction as to what kind of IPC to use, and perhaps to a similar project.

    I want to write a kernel module that uses IgH's Ethercat master to read/write data to the bus, in an interval. And then make this data available to other processes in userspace. Preferably written in any programming language.

    Example:
    Light switches in my house are connected to some Beckhoff digital input modules.
    Relays controlling the lighting and power sockets, are connected to digital output modules.
    The bus couplers are connected via an Ethernet cable to a Linux computer, running the Ethercat master.
    A kernel module continuously reads and writes data to the bus, and handles time critical tasks.
    A c++ userspace application handles the “big picture”. Scheduled events, none time critical tasks, and such.
    A web application acts as an interface, made available to the entire local network. Accessible to PC's and hand held devices.

    As of today, I have only experimented with userspace applications and the Ethercat bus. IgH's API is quite straight forward. But i have very little experience in inter process communication, and KMOD development.
    What kind of IPC should I use?
    How do I handle variable declarations? XML?
    Does anyone know of a similar project with documentation?

    Any help will be highly appreciated.

    //John
    Last edited by John Erlandsson; 09-06-2011 at 03:05 PM.

  2. #2
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    since you'll primarily be communicating between the kernel and user space, your best bet will probably be to define a set of system calls and data structures to represent the operations that you need to perform and the data you need to convey. as far as i know, system calls are still done using a software interrupt and registers, so you'll need to figure out how to set up new kernel system calls, or how to set up your own software interrupt handler in the kernel module. you'll have to pass in a pointer to your data structure through a register, and either return a pointer to another structure with a register, or modify the structure passed in. I'm not a linux kernel developer, so I don't know all the details, but that's generally the way it's done.

  3. #3
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    I don't know anything about EtherCAT, but my first question would be, what makes you think you need any kernel space component at all? You said there are userspace applications available. In what way are they limited, that a kernel space driver would be able to overcome?

    If kernel mode really is the way to go, what you want is to create a device driver, not add a bunch of new system calls. You probably don't need anything more complex than some kind of command packet interface, and potentially some rudimentary ioctl() methods.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  4. #4
    Registered User
    Join Date
    May 2011
    Location
    Sweden
    Posts
    24
    Quote Originally Posted by brewbuck View Post
    I don't know anything about EtherCAT, but my first question would be, what makes you think you need any kernel space component at all? You said there are userspace applications available. In what way are they limited, that a kernel space driver would be able to overcome?

    If kernel mode really is the way to go, what you want is to create a device driver, not add a bunch of new system calls. You probably don't need anything more complex than some kind of command packet interface, and potentially some rudimentary ioctl() methods.
    Hi!

    I don't know anything about working with device drivers. Can anyone recommend a good read on the subject?
    The Etherlab api is well documented, and quite easy to use. So reading data from the bus is not a problem. My reason for wanting to use higher level languages is that I want to try the object oriented approach to machine automation.

    Is it possible to write a device driver in which I declare the variables, and then read them in a higher level application without redeclaring them?

    I want this to be a flexible solution.

    Thanks for helping

    //John
    Last edited by John Erlandsson; 09-07-2011 at 02:55 AM.

  5. #5
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by John Erlandsson View Post
    I don't know anything about working with device drivers. Can anyone recommend a good read on the subject?
    I've done this a wee bit. What you end up with is a /dev file (remember in *nix "everything is a file") and that is your kernel <-> userspace conduit. There are two basic flavours, char and block. Char devices are interfaced via streams and block devices via random access (blocks). You almost certainly want the former, presuming the API you refer to describes a stream interface to a device. So the kernel driver would just be something to move that into userland.

    Of course, most consumer devices do not have such a public API, and if yours does, dollars to donuts there already is a linux driver for it, which will save you some time -- then all you need to do is your application.

    The linux kernel API is in C, and it *does not* include much of the standard library. Meaning compilation is a particular task and I am 99% certain you cannot use anything but C. As in, not even C++, because libstdc++ will not be available. You can still use an "object oriented" style if you want -- the kernel does -- but you will not have any high-level sugar to do it with.

    This is a good, hands-on intro to device drivers that you can read in half an hour:

    Writing a Simple USB Driver | Linux Journal

    The author, Greg Kroah-Hartman, one of the principle kernel programmers for years, seems to have put out a bunch of free resources. He and a few other people wrote "Linux Device Drivers", the 3rd edition is available in pdfs. There are also pdf's of Linux Kernel in a Nutshell on his homepage, and something called the "Linux Device Driver Kit", which I've never looked at, but google implies there should be .iso's of that around as it is freely distributable (nb, kernel.org appears to be down right now for me).

    You'll also want to get on the kernel development mailing list, just beware it produces 1000+ messages a day.

    If all that doesn't scare you off, you are comfortable working in C, and you do not have to reverse engineer the device, good luck & have fun. But do check and see if someone already has a device driver for this, because with that, you'll have a language neutral stream based interface you can work with however you like.

    ...

    In fact, lol, I wrote all that before I went to look at "Etherlab", and I'm not going to erase it now BUT on the first page of this: http://www.ethercat.org/pdf/etg_memb.../flyerganz.pdf, I notice:

    Basically EtherLab works as a Real Time kernel module attached to the open source operating system Linux ® communicating with peripheral devices by a special ethernet technology, known as EtherCAT ®
    So hey, there's one big hill you won't have to climb after all. "Etherlab" is a kernel module. You do not have to do any more kernel programming! Have a closer look at your docs, hopefully it will become clear...
    Last edited by MK27; 09-07-2011 at 06:32 AM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  6. #6
    Registered User
    Join Date
    May 2011
    Location
    Sweden
    Posts
    24
    So hey, there's one big hill you won't have to climb after all. "Etherlab" is a kernel module. You do not have to do any more kernel programming! Have a closer look at your docs, hopefully it will become clear...
    Hi! Thanks for the links!

    Yes, The Ethercat master is a kernel module. That module creates a /dev/EtherCat0 device.
    You then access that device from your custom application, using the Etherlab API. There are functions for initiating the bus, defining the different Beckhoff modules, and accessing the data from the Beckhoff modules.
    This application can be either in user or kernelspace, but it is preferred to be i kernelspace in order to make use of the realtime functionality. Etherlab works with RTAI kernel.

    What I want is

    A kernel module that:
    • Reads a config file that describes the bus, and names the IO
    • Initiates the bus with that configuration
    • Continuously reads and writes to the bus IO
    • Handles time critical tasks(servo stuff, etc)
    • Forwards the data to any application in userspace


    I want to be able to make abstract classes of different parts of production lines and machines, in order to be more efficient.

    I want to replace the expensive industrial HMI systems with thin clients and handheld devices. Using web pages.

    I apologize for the poor description. English is not my primary language...

  7. #7
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by John Erlandsson View Post
    Hi! Thanks for the links!
    Yes, The Ethercat master is a kernel module. That module creates a /dev/EtherCat0 device.
    You then access that device from your custom application, using the Etherlab API.
    Yes, that will be a user space application.

    This application can be either in user or kernelspace, but it is preferred to be i kernelspace in order to make use of the realtime functionality.
    What gives you that impression? Also: isn't that what the existing driver provides to you? I mean, presuming that the etherlab driver is open source, you could write your own customized version of it...but it is hard to understand why that would be necessary.

    Software is engineered in layers. You do not have to write all of them yourself. Realtime applications (games, video, etc) are user land applications built on top of OS facilities provided by the kernel (including hardware interfaces), but they do not involve kernel programming.

    Split this in half:

    A kernel module that:
    • Reads a config file that describes the bus, and names the IO
    • Initiates the bus with that configuration
    • Continuously reads and writes to the bus IO
    Right, and etherlab should give you that, altho I am not too sure about the reading of a config file. That is more likely information you would feed to the driver from user space. Notice not even the kernel uses a .conf file. Why not ? Yet it does take command line switches at invocation. Next:

    • Handles time critical tasks(servo stuff, etc)
    • Forwards the data to any application in userspace
    Change the last line to "interacts with the device representation provided by the kernel module to send and receive data" and you have a description of your application. You do not need to write any kernel space code in order to "handle time critical tasks". Somebody already did that and provided a user space API for high resolution timers, qv. nanosleep(). The latency of the linux kernel is 10ms, so you can do things accurately at a granularity of hundredths of a second.

    SourceForge.net: POSIX timers - cpwiki

    I want to be able to make abstract classes of different parts of production lines and machines, in order to be more efficient.

    I want to replace the expensive industrial HMI systems with thin clients and handheld devices. Using web pages.
    Right, and it is probably good you found that etherlab thing because it will save you having to write device drivers!

    EtherLab® is very flexible, both softwaresides and hardwaresides. You only have to buy the channels you really need and you develop only the software you are in need of. EtherLab® concludes all the rest: Sources are translated by the control computer and loaded as Kernel module and the control unit is already done!
    EtherLab: Generation of realtime applications via a C-Interface or via MATLAB/Simulink with Real-Time Workshop.
    EtherLab

    It's a user space tool John.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  8. #8
    Registered User
    Join Date
    May 2011
    Location
    Sweden
    Posts
    24
    The latency of the linux kernel is 10ms, so you can do things accurately at a granularity of hundredths of a second.
    The most commonly used digital output module EL1004 has a filter time of only 3ms, but Beckhoff has modules with filter times down to 3us.

    Say that my machine has a bunch of inductive sensors and solenoid valves, all of which can be read and written to from userspace.
    But it also needs to control a steppermotor with pulse with modulation in a very high frequency. Then that method would have to be in kernelspace and most likely make use of RTAI.

    My background is in industrial automation, and I have very little experience in this kind of software development. But I do know that the main advantage of the EtherCat bus is the realtime functionality.

    From ethercat.org:
    The update time for 1000 distributed I/Os is only 30 µs. Up to 1486 bytes of process data can be exchanged with a single Ethernet frame - this is equivalent to almost 12000 digital inputs and outputs. The transfer of this data quantity only takes 300 µs.
    I will try list my reasons for beleving that a device driver between the EtherCat master and the userspace applications is the way to go.

    • The possibility to add methods for the high speed stuff in colaboration with RTAI
    • Only having to name the different IO once. (Somehow) Not redeclaring them in every application. (That would suck if I had to add/remove bus-modules)
    • Having a way of fetching the data to a c++ application(The Etherlab functions are all in c)


    Does that make sense?

  9. #9
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Does that make sense?
    This doesn't
    Having a way of fetching the data to a c++ application(The Etherlab functions are all in c)
    C++ is essentially a superset of C, so you can safely say that the functions can be used from C++.

    The rest, I'm ignorant about, you're the expert w.r.t that.

  10. #10
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by John Erlandsson View Post
    The most commonly used digital output module EL1004 has a filter time of only 3ms, but Beckhoff has modules with filter times down to 3us.
    That's good. The 10ms latency is because of the scheduler, but WRT to device drivers operating in the kernel, those can work at the real time frequency of the CPU. This still doesn't mean you have to program there if you already have a driver with an API that provides appropriate functionality.

    Eg, if you write a sound processing application, audio frequencies require a much finer granularity than 1/100th second. But you do not have to program the kernel, instead you access the sound system which is a facility provided to user space by the kernel hardware drivers.

    That is important. It means you do not have to worry about programming hardware when you write a sound processing application. It is completely abstracted. Notice that every sound processing application does not come with its own suite of hardware drivers, and that you only need one sound card driver to run all the various applications that use sound.

    The ethernet is exactly the same, and as you observe, operates at a very high frequency.

    But it also needs to control a steppermotor with pulse with modulation in a very high frequency. Then that method would have to be in kernelspace and most likely make use of RTAI.
    Why, if you already a driver/API (etherlab) that provides you that functionality?

    I will try list my reasons for beleving that a device driver between the EtherCat master and the userspace applications is the way to go.
    That would mean you are replacing etherlab with your own thing, wouldn't it?

    It is certainly possible. Just as well I didn't erase that first post, lol. There is another book that might interest you, "Essential Linux Device Drivers" by Sreekrishnan Venkateswaran, which is very in depth and includes lots of examples. It kind of skips the essential basic stuff tho, get that from the Greg K-H device driver book.

    But honestly, make very sure that you cannot do this with etherlab first, because it is going to be a LOT of work otherwise. I can provide you with a basic "hello world" module if you want.
    Last edited by MK27; 09-07-2011 at 09:23 AM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  11. #11
    Registered User
    Join Date
    May 2011
    Location
    Sweden
    Posts
    24
    MK27: I think your point is slowly working its way into my head.

    Writing the entire application iń c++(Except the web gui) would be pretty cool.

    I have been reading a lot of mailing lists and manuals regarding Etherlab and RTAI. And it seemed that kernel space + RTAI is the only way to guarantee the timing requirements of a modern industrial machine. I guess that is why I have been so stuck on that track.

    A couple of questions:
    • Is it certain that my userspace application takes the same time to execute every time (If the conditions are the same)?
    • Any ideas for an easy way of accessing the bus data for the web app?
    • Why are most of Etherlab's example code aimed at kernel modules?
      Mini example
      RTAI example
      Distributed clock cycles with RTAI example
      Userspace example
    • Say i want to separate my code into different processes. For instance one app for the main sequence, one calendar gui app for time scheduled writes to digital outputs, and one OpenCV app for vision stuff. How do I get around having to define and address the variables in every application? That would be a hassle if I make changes to the bus.
    Last edited by John Erlandsson; 09-08-2011 at 02:36 PM.

  12. #12
    Registered User
    Join Date
    May 2011
    Location
    Sweden
    Posts
    24
    manasij7479:
    C++ is essentially a superset of C, so you can safely say that the functions can be used from C++.
    Thanks for pointing that out. I didn't know that you could include just any c lib in a c++ application. I need to do some serious reading on the subject before undertaking this project.

  13. #13
    Registered User
    Join Date
    May 2011
    Location
    Sweden
    Posts
    24
    I will experiment a bit with the Etherlab Master and C++. Got a bit of reading to do as well.

    Thanks for all the input. I will pick this thread up again, when I have more questions.

    //John

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Windows/Linux C++ project
    By TriKri in forum Linux Programming
    Replies: 7
    Last Post: 01-01-2007, 06:13 PM
  2. OLE automation and C++
    By tkennedy in forum C++ Programming
    Replies: 3
    Last Post: 11-17-2006, 08:48 AM
  3. VS 7.1 Linux Source code solution / project file?
    By cboard_member in forum Tech Board
    Replies: 6
    Last Post: 07-30-2006, 02:07 PM
  4. Automation Anywhere
    By danno.c in forum C# Programming
    Replies: 1
    Last Post: 08-07-2005, 10:10 AM

Tags for this Thread