Friday, September 26, 2008

Python in Industial Automation

Before celebrating 10th Google birthday I would like to make a gesture of goodwill to fellow comrades from industrial automation field, who over years battled with a very special kind of the most conservative software in world.

Industrial Automation Software

It is the Software that directly puts people lives at stake. A little bug may kill people, inflict tremendous physical damage to buldings, vehicles and equipment. No wonder that Industrial Automation Software is among the most slowly developed technologies in the world. People fear to change things that work if the cost of error is so high. But using outdated software that nobody is capable to maintain carries even greater risks. Just as connected to Internet Windows without updates is brought down by viruses in minutes, the Enterprise is brought down by unmaintained legacy systems that just await the time to explode. Industrial software is ought to be stable, error free and resistant to all kind of threats, but it is not true in most cases. Most industrial software products lack public exposure, and so far I haven't seen any serious industrial product that didn't crash. Industrial software is the classic example of vendor lock-in. It happens just everythere. If you have some hardware sensors in the field and would like to collect signals from them - most likely you would have to buy industrial controller or PLC, which will be on 24/7/365/10 to gather your data. PLC's were mostly used solely for control automation without the need to share data with external systems, but in IT age you would need that data sooner or later. To get it from he PLC you would need to buy expensive OPC server that is capable to poll PLC using their own protocol and provide you with data in OPC format. OPC stood for OLE for Process Control and as you can guess from its name was vendor-locked to Microsoft from the start. It was based on OLE, COM and DCOM - its proprietary technology. To read one byte from your PLC you would have to buy Windows, buy OPC server and pay specialist to write an OPC client to access one byte of your sensor. Even after that some companies dared to include "Lower TCO" slogan in their markting campaigns. There was no alternative those days though. MS SQL is the only promoted "realtime database" even if there was a word "near" before the quotes, but it has support. Now there are plenty of open protocols and standards for data communication, many databases and time-tested solutions from other IT areas, but things are moving slowly in Industrial Automation world where "open protocol" still means that you are free to implement a specification, but need to pay for texts and it is ok. DCOM is slowly replaced by .NET, new consortiums appear just to put their label "approved" on community accepted standards. Industrial Automation is a business with enterprises that are primary sources of capital directly extracted from raw materials, so there is nothing exceptional in constant buzz that is generated by efforts of players to bite off a piece from this cake. But even more don't want to lose their share. The players are huge and so are the problems with Industrial Applications. It takes a lot of efforts to prove something different when it comes against mature and experienced players with a big sum of arguments in their bank accounts.

Industrial Automation applications are meant to be error free and stable, but there are still many errors, flakiness and unstability there. High cost of specialized instruments to debug and troubleshoot problems, low community response, closed protocols, formats and sale-oriented support that is not allowed to give any developers feedback - all these things are on the back side of the modern software development trends. Sooner or later the situation will change and industrial software will undergo the same change that affected internet and made it enjoy the fruits of open source developers collaboration. In the meanwhile let me show an example, how to "Lower your TCO" for the task of accessing data from Siemens S7 controllers.

Introducing Python for Automation World

You can control everything in Python. Starting from virtual game characters and application objects to real robots and trains. Just like you can control them in C. That means that Python is capable to directly use C libraries. Even if they are not really suitable to do such things it gives you the freedom of choice. Pulling slow snake's tail may be more efficient than guarding against fast mosCuitoes aiming at sucking you memory away. Python gives you a finer degree of control focusing your efforts on program logic while in C about 30% of code is dealing with memory allocation and type conversion. Python has more clear syntax than C and that is more important - the same syntax is used to control speedy C libraries.

Controller Data - Corner Stone of Automation

Data is everything. In Automation the primary source of data are sensors - any kind of device that is capable to measure something in common terms. Sensors are usually made to be simple to keep them free from errors, so they are often provide their data using electrical signals. These signals are usually transmitted in rough industrial environment where electrical disturbances are so common that direct connection of such sensors with computers (through COM port, for example) is a reason for frequent PC component replacements. That why specialized devices exist to poll sensor data. These are usually PLCs (programmable logic controllets) and distributed periphery devices that have a higher degree of protection against physical threats than common PC stations. Data inside PLC is the starting point to source of information for are used by industrial panels to visualize and control technical process parameters - link of PLC + visualization panel is commonly referenced as SCADA solution. Access to data hidden inside PLC from a panel is made using proprietary protocols even if the medium is Ethernet. Communication with upper levels require aforementioned Industrial Software provided for a price. But some protocols have alternative open source C libraries or drivers for PLC direct data access and I am going to show how to use such library with Python to run and stop PLC.

Parts of Assembly: libnodave, SWIG and Python

libnodave - communication library for exchanging data with Siemens PLCs released under LGPL license (you are welcomed to support this project if you find it useful). Python is a general-purpose scripting language. SWIG - tool to bind C and Python. Another required thing is C compiler. Because automation designers are mostly windows folks, we will use GCC from MinGW project Minimalistic user interface is done with wxWidgets library, which is also accessible from Python as wxPython library (created with SWIG, by the way) The total cost of all tools is zero. The performance of libnodave solution is still have to be measured. Siemens site contains performance data that can be useful for comparison.

libnodave allows to communicate with PLC with an API described on it's site. To start/stop controller you need to compile and execute the following C program:


#include <stdlib.h>
#include <stdio.h>
#include <string.h>

#include "nodavesimple.h"
#include "openSocket.h"

#ifdef LINUX
#include <unistd.h>
#include <sys/time.h>
#include <fcntl.h>
#define UNIX_STYLE
#endif

#ifdef BCCWIN
#include <time.h>
void usage(void);
void wait(void);
#define WIN_STYLE
#endif

#include <errno.h>

int main(int argc, char **argv) {
int useProtocol, useSlot;

daveInterface * di;
daveConnection * dc;
_daveOSserialType fds;

useProtocol=daveProtoISOTCP;
useSlot=2;

fds.rfd=openSocket(102, "192.168.1.244");
fds.wfd=fds.rfd;

if (fds.rfd>0) {
di =daveNewInterface(fds,"IF1",0, useProtocol, daveSpeed187k);
daveSetTimeout(di,5000000);
dc =daveNewConnection(di,2,0,useSlot);

if (0==daveConnectPLC(dc)) {
daveStart(dc);
return 0;
} else {
return -2;
}
} else {
return -1;
}
}


What I am trying to achieve is the following equivalent in Python:

import nodave
plc = nodave.NoDaveTCPConnect('192.168.1.244')
plc.start()


To make this happen the libnodave library should be made available to Python as a module. Module is imported to make its functions available for Python scripters. To make module for libnodave we need to compile library, generate source for module and compile module itself.

[To be continued..]

6 comments:

  1. Excellent insight. I fully agree with your commentary on both past and future. As far as I'm concerned, present day Open Source Industrial Control software, including: drivers, OPC software, HMI/SCADA applications, etc, are dangerous for critical real world applications. However, I'm a huge open source proponent, hoping that it gets to a point where Open Source/Free/Very Cheap and standardized industrial software beats the big vendors hands down.

    ----
    Nathan Boeger
    http://notanotherindustrialblog.blogspot.com

    ReplyDelete
  2. Thanks for response. Sorry that the post is not finished. I pushed a wrong button in a hurry instead of saving it in drafts. I'll update it in a few days with additional chapter on using Python ability to call C libraries for direct access to PLC. There also will be an example of truly minimalistic cross-platform wxWidgets interface.

    Yep, the software is dangerous as long as there is insufficient number of reviews from various parties. Owning a software product and maintaining its source top secret doesn't add anything to reliability, but makes it easier to hide flaws and keep your business running. Tremendous amount of resources wasted on marketing campaigns in automation solutions market to promote the software that doesn't really deserve the buzz. I mentioned Google for a reason and I believe that their attitude is a good alternative to be introduced in Industrial Automation world. But as long as investors are only automation software vendors this is unlikely to happen.

    To make this big marketing machine move in alternative direction there still must be financial power to start and support the efforts. Existing practice of committees, boards and associations will not change the situation. Only independent organization could provide the force to open Industrial Software world for true collaboration over the tools that are up to date, pleasant to look and to work with to make the difference in reliability, maintenance and constant improvement of technology. These tools could be made better by joint efforts of manufacturing enteprises themselves to be easily adopted to their needs by their own teams. Tools that will introduce the best practices and not only ready to use products. The organization behind should do a lot of research and development work that will allow people make their choice reasonably instead of enforcing own standards. This will contribute back to open source world as well.

    ReplyDelete
  3. Hi,

    Many thanks for your work.
    I need some help to create .py modules from c-files using swig.
    I have been reading swig manual, but I don't know how I can use 'libnodave' in my own python application to read and write data from Siemens S7-200 PLC via CP243-1 ethernet module on Windows XP OS.

    I hope you can help me.

    Best Regards,
    László Révész

    ReplyDelete
  4. I've ended up in the process automation world coming from the 'open' webworld. I'm totally agree with you about the lack of good open tools. So i'm currently hacking away creating solutions using whatever open tools I can find.

    What are the current developments? Did anything change the past few years? what do you think about opc-ua and how to connect that with python?

    ReplyDelete
  5. Hi,

    I'm going to develop my own Python SCADA system. I need to communicate a Linux OS with a S7-300 by Ethernet. So, I want to use libnodave. Did you get the python libnodave equivalent?

    Thank you,

    José M.

    ReplyDelete
  6. I abandoned my efforts to make industrial automation more open. It is an interesting task that is hard to pursue without additional support to access the hardware and find time for research. I believe Android has a lot of potential with its ADK, but in Python world there still should be some syndicate to push the progress. I think PSF could gather corporate sponsors to organize around their specific Python problems to make it more fun for community.

    As for Siemens S7, it is certainly possible to use Python ctypes module to gain easier access through libnodave, but I doubt that it is possible to create an equivalent in Python due to the closed nature of Siemens protocols. This, however, didn't stop Stuxnet.

    ReplyDelete