Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Factory Automation with Python (Pycon 2017)

Factory Automation with Python (Pycon 2017)

Jonas Neubert

May 19, 2017
Tweet

More Decks by Jonas Neubert

Other Decks in Programming

Transcript

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

    View full-size slide

  2. 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

    View full-size slide

  3. 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.

    View full-size slide

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

    View full-size slide

  5. Conveyor
    Belt

    View full-size slide

  6. Barcode
    Reader

    View full-size slide

  7. Pusher
    Paddles

    View full-size slide

  8. Pusher
    Paddles

    View full-size slide

  9. Pusher
    Paddles

    View full-size slide

  10. Pusher
    Paddles

    View full-size slide

  11. Pusher
    Paddles

    View full-size slide

  12. Sensing
    + Actuation
    + Brains
    ==========
    Automation

    View full-size slide

  13. Laser
    (photons)
    RS­232
    (electrons)

    View full-size slide

  14. 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()

    View full-size slide

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

    View full-size slide

  16. 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)

    View full-size slide

  17. # 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()

    View full-size slide

  18. 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

    View full-size slide

  19. Sensing
    + Actuation
    + Brains
    ==========
    Automation

    View full-size slide

  20. “API Docs”

    View full-size slide

  21. PROGRAMMABLE LOGIC CONTROLLER (PLC)

    View full-size slide

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

    View full-size slide

  23. 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!

    View full-size slide

  24. 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!

    View full-size slide

  25. 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

    View full-size slide

  26. 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)

    View full-size slide

  27. 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

    View full-size slide

  28. Sensing
    + Actuation
    + Brains
    ==========
    Automation

    View full-size slide

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

    View full-size slide

  30. 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)

    View full-size slide

  31. 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…

    View full-size slide

  32. 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

    View full-size slide