Slide 1

Slide 1 text

FACTORY AUTOMATION WITH PYTHON STORIES ABOUT ROBOTS, SERIAL PORTS, AND BARCODE READERS Jonas Neubert

Slide 2

Slide 2 text

SOFTWARE IS EATING THE DICTIONARY Oxford Dictionary of English, 3rd revised ed., Oxford University Press, 2010, p 626 Gamma, Helm, Johnson & Vlissides: “Design Patterns: Elements of Reusable Object- Oriented Software”, Addison-Wesley, 1995, p 99 factory (n.): A building or group of buildings where goods are manufactured or assembled chiefly by machine. W hat this talk is about factory (n.): An interface for creating families of related or dependent objects without specifying their concrete classes. What this talk is *not* about

Slide 3

Slide 3 text

SOFTWARE IS EATING JOB TITLES, TOO International Society of Automation, isa.org Recruiters who fill my Linkedin Inbox without reading my profile first Automation Engineer (n.) Creates and applies technology to monitor and control the production and delivery of products and services. Automation Engineer (n.) Software engineer with special interest in testing and deploying code.

Slide 4

Slide 4 text

CAUSING FACTORY DOWNTIME SINCE 2002 Jonas Neubert Software Engineer at Tempo Automation slides: http://jonasneubert.com/pycon2017 twitter: @jonemo

Slide 5

Slide 5 text

No content

Slide 6

Slide 6 text

Conveyor Belt

Slide 7

Slide 7 text

Barcode Reader

Slide 8

Slide 8 text

Pusher Paddles

Slide 9

Slide 9 text

Pusher Paddles

Slide 10

Slide 10 text

Pusher Paddles

Slide 11

Slide 11 text

Pusher Paddles

Slide 12

Slide 12 text

Pusher Paddles

Slide 13

Slide 13 text

Sensing + Actuation + Brains ========== Automation

Slide 14

Slide 14 text

No content

Slide 15

Slide 15 text

Laser (photons) RS­232 (electrons)

Slide 16

Slide 16 text

class BarcodeReaderDriver: def __init__(self, port): self.port = serial.Serial( port, 9600, 7, 'E', 1, 5) def read_barcode(self): self.port.write(b'*') line = self.port.readline() return line.strip().decode('ascii') def close(self): self.port.close() def __enter__(self): return self def __exit__(self, *args): self.close()

Slide 17

Slide 17 text

Awesome! An instrument driver in under 15 lines of Python.

Slide 18

Slide 18 text

Caveats Serial cable only works up to 15 meters (50 feet) (That's 1.4% of the length of the Tesla Gigafactory 1) Your server doesn't have enough serial ports (Inspecting a single bottle for barcodes usually takes six)

Slide 19

Slide 19 text

# server.py from xmlrpc.server import SimpleXMLRPCServer DEV = '/dev/cu.usbserial-AL01Z5WB' srv = SimpleXMLRPCServer(("localhost", 2121)) with BarcodeReaderDriver(DEV) as driver: srv.register_instance(driver) srv.serve_forever() # client.py from xmlrpc.client import ServerProxy cl = ServerProxy('http://localhost:2121') cl.read_barcode()

Slide 20

Slide 20 text

SPOILER: IN THE NEXT DEMO WE'LL LAUNCH A DUCK! 1. Industrial automation equipment exists 2. (pure) Python works fine for interfacing with it 3. Python's “batteries” are useful in this domain, too 4. … and often result in more elegant and efficient solutions

Slide 21

Slide 21 text

Sensing + Actuation + Brains ========== Automation

Slide 22

Slide 22 text

“API Docs”

Slide 23

Slide 23 text

PROGRAMMABLE LOGIC CONTROLLER (PLC)

Slide 24

Slide 24 text

Industrial PC Usually a simple OS (this one is Windows CE) Real-time kernel Programmed with IEC-61131

Slide 25

Slide 25 text

I/O Slices Library of slices for signal types — mix and match Examples: 5V Digital, 0-10V Analog, Relays, Motor Control, … Each signal binds to a variable!

Slide 26

Slide 26 text

VAR_GLOBAL DO_CONVEYOR_ON AT %QX0.1: BOOL; DO_CONVEYOR_REVERSE AT %QX0.2: BOOL; END_VAR PROGRAM MAIN VAR CONVEYOR_ON: BOOL := FALSE; CONVEYOR_REVERSE: BOOL := TRUE; END_VAR IF CONVEYOR_ON <> DO_CONVEYOR_ON THEN DO_CONVEYOR_ON := CONVEYOR_ON; ELSIF CONVEYOR_REVERSE <> DO_CONVEYOR_REVERSE THEN DO_CONVEYOR_REVERSE := CONVEYOR_REVERSE; END_IF; Writable over Ethernet!

Slide 27

Slide 27 text

Python implementations of the ADS Protocol (Automation Device Specification) Wrapper around vendor supplied C library — Python implementations of protocol pip install pyads github.com/chwiede/pyads github.com/counsyl/counsyl-pyads

Slide 28

Slide 28 text

from counsyl_pyads import AdsConnection from counsyl_pyads import AdsClient from counsyl_pyads.adsdatatypes import * ads_conn = AdsConnection( target_ip='192.168.99.25', target_port=801, target_ams='5.10.208.214.1.1:801', source_ams='192.168.99.1.1.1:5555', ) plc = AdsClient(ads_conn) plc.write_by_name( 'MAIN.CONVEYOR_ON', BOOL, True)

Slide 29

Slide 29 text

Python IEC-61131 (PLC) business logic connects to services user interface signal I/O human safety machine safety timing critical little to no downtime part of test suite requires downtime factory tests re-certification? regular release schedule don't touch this

Slide 30

Slide 30 text

Sensing + Actuation + Brains ========== Automation

Slide 31

Slide 31 text

Start Noread? Read Barcode Yes Find Package Color Red or Blue? Left Paddle Right Paddle No Gum Color Database Blue Red

Slide 32

Slide 32 text

ms3 = ServerProxy('http://localhost:2121') plc = AdsClient(ads_conn) plc.write_by_name( 'MAIN.CONVEYOR_ON', BOOL, True) while True: bc = ms3.read_barcode() col = upc_to_color(bc) if not col: time.sleep(0.25) elif col.r > col.b: time.sleep(1) plc.write_by_name( 'MAIN.LEFT_PADDLE', BOOL, True) else: time.sleep(1) plc.write_by_name( 'MAIN.RIGHT_PADDLE', BOOL, True)

Slide 33

Slide 33 text

No content

Slide 34

Slide 34 text

No content

Slide 35

Slide 35 text

No content

Slide 36

Slide 36 text

No content

Slide 37

Slide 37 text

No content

Slide 38

Slide 38 text

No content

Slide 39

Slide 39 text

No content

Slide 40

Slide 40 text

No content

Slide 41

Slide 41 text

No content

Slide 42

Slide 42 text

No content

Slide 43

Slide 43 text

No content

Slide 44

Slide 44 text

WANT TO GET STARTED? Device Drivers “APIs for robots” File Formats & Protocols parsers, converters, viewers, … Data Science & Machine Learning vibration monitoring, predictive maintenance, scheduling optimization, … Security you'll be busy…

Slide 45

Slide 45 text

QUESTIONS http://jonasneubert.com/pycon2017 twitter: @jonemo packages mentioned pypi: pyserial pypi: microscan pypi: pyads github: chwiede/pyads github: counsyl/counsyl-pyads equipment used Barcode Reader: Microscan MS3 Coneyor: Dorner 1100 Series PLC: Beckhoff CX 1020