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 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 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 Slide

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

    View Slide

  5. View Slide

  6. Conveyor
    Belt

    View Slide

  7. Barcode
    Reader

    View Slide

  8. Pusher
    Paddles

    View Slide

  9. Pusher
    Paddles

    View Slide

  10. Pusher
    Paddles

    View Slide

  11. Pusher
    Paddles

    View Slide

  12. Pusher
    Paddles

    View Slide

  13. Sensing
    + Actuation
    + Brains
    ==========
    Automation

    View Slide

  14. View Slide

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

    View Slide

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

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

    View Slide

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

  19. # 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 Slide

  20. 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 Slide

  21. Sensing
    + Actuation
    + Brains
    ==========
    Automation

    View Slide

  22. “API Docs”

    View Slide

  23. PROGRAMMABLE LOGIC CONTROLLER (PLC)

    View Slide

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

    View Slide

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

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

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

  28. 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 Slide

  29. 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 Slide

  30. Sensing
    + Actuation
    + Brains
    ==========
    Automation

    View Slide

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

    View Slide

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

  33. View Slide

  34. View Slide

  35. View Slide

  36. View Slide

  37. View Slide

  38. View Slide

  39. View Slide

  40. View Slide

  41. View Slide

  42. View Slide

  43. View Slide

  44. 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 Slide

  45. 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 Slide