Official White House Photo by David Lienemann (public domain) https://obamawhitehouse.archives.gov/sites/default/files/image/image_file/V020811DL-0076edit.jpg PLC Inside
Photo by Daniel Åhs Karlsson at at Wikimedia Commons (CC-BY-SA 3.0) https://commons.wikimedia.org/wiki/File:OTIS_GEN_2_Comfort_Roof_Taket_(Daniel_Åhs).jpg PLC Inside
Table of Contents What is a PLC? (and how does one program a PLC?) How do I talk Python to it? But what about RaspberryPi? https: // jonasneubert.com / pycon2019 @jonemo Q: What was THE acronym “PLC” for again? A: Programmable Logic Controller slides: twitter:
“Compact” Fixed form factor Photo of Siemens Logo by Stehfun at Wikimedia Commons (CC-BY-SA 3.0 Unported) https://commons.wikimedia.org/wiki/File:Siemens_Logo.jpg Photo of Beckhoff CX by Beckhoff Automation GmbH at Wikimedia Commons (CC-BY-SA-3.0-DE) https://commons.wikimedia.org/wiki/File:Embedded-PC_Beckhoff_CX1020.jpg This is what a PLC looks like* “Modular” Backplane with fixed number of slots “Expansible” Infinitely* stackable modules * when it’s new
The Input-Process-Output Cycle Step 1: Input Scan Step 2: Program Scan Step 3: Output Scan Read process variables from Field Instruments into PLC memory Create “Process Image In” (PII) Execute application program (this is the Programmable Logic!) Creates “Process Image Out (PIQ)” STEP 2 is the one we have control over! Read “Process Image Out” (PIQ) Set output signals
Ladder Logic Still the most popular language for programming PLCs. Common denominator across all PLC brands. Part of curriculum for most Automation Engineering degrees. A language optimized for debugging, not writing.
Alternative Languages - LD: Ladder diagram - FBD: Function block diagram, graphical - ST: Structured text, textual - IL (deprecated):Instruction list, textual - SFC: Sequential function chart , has elements to organize programs for sequential and parallel control processing, graphical. - C/C++ - Matlab/Simulink - Custom graphical wiring diagram tools IEC-61131-3
Process Data Process Parameters Sensor Signals Actuator Signals The Two Control Loops of “IIoT” Field Devices PLC Logic Python Code “Inner Loop” “Outer Loop” milliseconds per iteration interfaces to the physical world timing critical & safety critical logic release process might be regulated minutes to months per iteration interfaces to databases and APIs optimization & customization regular software release cycle
Vendor/Brand Specific Protocols Protocol Ethernet/IP S7 ADS DirectNet DF1 PCOM [unnamed?] Vendor/Brand Allen Bradley Siemens S7 Series Beckhoff Koyo (Autom. Direct) Allen Bradley Unitronics Fatek Python libraries pycomm python-snap7 chwiede.pyads bfabio.pyads, adshli, pyads clickplc df1 pcom fatek-fbs-lib ✓ ✓ ✓ ✓ ✓ ✓ ✓ ✓ ✓ ✓ ✓ ✓ RS-232/485 Ethernet TCP/IP CAN bus see also: my spreadsheet of PLC brands and products and “List of PLC Manufacturers“ on Wikipedia Best Case Scenario: You’re using one of these PLC BrAnds Otherwise: go to next slide
No Python support: CC-Link IE, Ethernet Powerlink, PROFINET, Ethernet/IP SERCOS III. Utility libraries like rawsocketpy and dnet are handy for non-TCP/IP level networking. “Open Standard” Protocols Modbus TCP Modbus ASCII/RTU pymodbus, pyModbusTCP, cpppo pymodbus, MinimalModbus ✓ ✓ ✓ RS-232/485 Ethernet TCP/IP CAN bus Protocol Python Packages Ethernet/IP (TCP) Profinet (TCP) OPC-UA pycomm, cpppo GH: devkid/profinet opcua, asyncua, opcua-client (GUI), opcua-webclient ✓ ✓ ✓ ✓ ✓ ✓ EtherCAT pysoem ✓ ✓ Profibus-DP CANopen SAE J1939 OPC Windows OLE ✓ ✓ pyprofibus canopen j1939 OpenOPC Many more with no Python support. Standard library sockets and ctypes are your friends! Modbus is universal 40 years old this year! see also: “List of Automation Protocols“ on Wikipedia Ethernet based “Fieldbus” standards Many more with no Python support. Many require custom hardware or cables. Other TCP/IP based standards “Office-to-Machine networking”
Example: opcua-client Screenshot from project Readme: https://github.com/FreeOpcUa/opcua-client-gui/blob/c103b3f/README.md the “UA” is important! Previous versions of OPC are Microsoft-only legacy technology
Example: opcua-client Screenshot from project Readme: https://github.com/FreeOpcUa/opcua-client-gui/blob/c103b3f/README.md Data and Methods are discoverable in a tree of nested objects
Observations from reviewing 40ish Python packages Lots of recent activity: - 11 packages first published within the last 12 months - 24 packages with new releases within the past 12 months Many Python 3-only packages, few Python 2-only packages Most activity in Europe* * based on supported brands and package maintainer website information Overlap with the much more active home automation community e.g. homeassistant has support for two PLC brands built-in
Things you get from a PLC that you don’t get from a . Hardware Works in crazy environments EM interference protection ESD protected, isolated I/O 24VDC power, DIN rail mount Expected life measured in decades Supply chain guarantees Safety certifications Software (c.f. IEC-61131-3) Realtime guarantees PID control and Motion Control Zero-downtime code changes Industry-standard programming languages Support for common fieldbus protocols
But maybe the RaspberryPi is a PLC? - CODESYS for RaspberryPi (codesys.com, €50) Cross-platform IEC 61131-3 editor and runtime. The editor is free, the RaspberryPi runtime is €50. - OpenPLC (openplcproject.com, free) A IEC-61131-3 editor (written in Python) and runtime for various targets, including RaspberryPi - PiLC (bues.ch/cms/automation/pilc.html) Runs the awlsim PLC simulation software (an open source Python package!) on RaspberryPi. awlsim implements a subset of the languages and features of Siemens’ STEP 7.
Jonas’ PLC Shopping Guide How much it’s going to cost, approximately If you want to do this: Try out ladder logic Try software the pros use Program your like a PLC Buy a cheap “real” PLC Hack some real gear Learn about the market leaders Contribute to a Python project Own a traffic signal Then take a look at that: OpenPLC, plcfiddle.com List of free software in appendix Codesys for , OpenPLC Automation Direct, Siemens Logo! Ebay1, industrial surplus sales Allen Bradley (US), Siemens (Europe) FreeOpcIUa, package list in appendix County/state surplus auctions, Ebay 1 On Ebay, this is where you want to look: Business & Industrial → Automation, Motors & Drives → Control Systems & PLCs → PLC Processors free free $70 $200 $500 — — $250
What’s the URL for these slides again? https://jonasneubert.com/pycon2019 Want to work with me? https://www.zymergen.com/careers/ Watching this on Youtube? The best ways to contact me are Twitter (@jonemo) and email ([email protected]) Open Space for questions, errata, and playing with PLCs and traffic signals Friday (today!) 3pm, Room 21 #PyConOpenSpace http://pycon.us/os My last slide
Free PLC programming software Software packages that support multiple brands/platforms Codesys — editor is free download, addons and device-specific runtimes are not free Inforteam OpenPCS — same concept and pricing model as Codesys, but smaller market share logi.CAD3 — base edition is free download OpenPLC — open source Vendor/brand specific software platforms Beckhoff Twincat — trial license may be renewed indefinitely for personal use ABB — free basic version Vendors whose programming software is generally free: Automation Direct, Bedrock Automation, Phoenix Contact, Unistream. Generally, smaller brands offer cheap or free software while market leaders use software as a revenue stream. Avoid over-simplified “basic editions” that don’t resemble the full version or don’t use IEC-61131-3 languages, for example Allen Bradley’s “Connected Components Workbench”. Software with free trial versions that have reasonable limitations Siemens TIA Portal — 21 day free trial, requires registration Schneider Electric EcoStruxure Machine Expert — 42 day free trial Panasonic — limits program complexity, no time limit Keyence — only opens 50 times
Forums and Online Communities https://reddit.com/r/PLC/ — surprisingly friendly community, by Reddit standards https://control.com https://www.mrplc.com Books Sorry, I couldn’t find any books on the topic that seemed worth their price tag.
import socket HOST = "192.168.1.9" # IP address of the PLC PORT = 50505 # Arbitrary port I used in Ladder Logic with socket.socket() as s: s.connect((HOST, PORT)) s.sendall(b'REQ') print(s.recv(1024)) # → ACK s.sendall(b'DISCO') print(s.recv(1024)) # → ACK