Factory Automation with Python Stories about Robots, Serial Ports, and Barcode Readers

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
good afternoon everybody welcome to our next session at PyCon 2017 two brief announcements before we start firstly if you have any electronic devices that are liable to make noise please make sure that they don't because if they do everyone's going to stare at you and you're going to feel bad secondly there is a ten minute gap between presentations this is to allow people to get from one room to another so please don't feel that you need to leave the session before the end in order to get to your next one with those out of the way I would like to welcome you honest know that who will be talking about factory automation automation with Python they can feel welcomed please [Applause] let's talk about factory automation this is my title slide obviously and I did some keyword stuffing there and I put this nice stock photo of a car assembly line into the background just to make it extra clear that this talk is about factory s and building a group of buildings or goods our manufacturer to assemble chiefly by machines that's from the Oxford English Dictionary this talk is not about an interface for creating families of related or dependent objects without specifying the concrete classes which is the design pattern that we all know maybe a couple of people leaving in the back there it's okay this is also an example of how the field of software development sometimes borrows or maybe steals terminology from other fields of engineering funny story I got my job title stolen this way I write software for factories and a couple years ago I could say hey I'm an automation engineer people would understand what I'm doing then a DevOps crowd came along and it claimed that word for themselves in fact I think that this lack of clear terminology and job titles is one of the reasons that industrial automation is not as prominent on people's minds when they think about where they can apply their software development skills as like the hot topics like web development and data science so this is what this talk is about it's going to be a very base take introduction to the fact that you can use your Python skills in factories so a couple words about me I'm a software engineer temper automation we have a circuit board Factory in in the middle of San Francisco and we use automation there to automate the setup process of the factories so that we can send you your prototype or small batch circuit boards really fast but I've been causing downtime in other factories for many years I'm on the internet if you're sitting all the way in the back we want to follow along with the slides this is the link if you have trouble spelling my name don't worry I sometimes do - you can go to my Twitter and I just send out a link to this slide so about this talk it's my first time at PyCon and the organizers and and the organizers had a couple pieces of advice for me as a first timer one of them was don't do an on-stage live demo and I was like okay so this thing on the screen is also right here and this can be the pike on 2017 chewing gum sorting Factory and we'll be using this and running this life in fact there's a webcam here that's going to like livestream onto the screens what's going on for the demo later so everyone cross your fingers because this joke might be on me so this photo this top view but I brought us a conveyer belt so it goes back and forth there's a barcode reader here the little black square with the red and white writing on it scans barcodes there's some little pusher paddles so I got a flipper machine they go like this and the other one obviously so and we'll be using those that's really all you need for for an automation demo every automation demo needs sensing so I brought you what I think is the most common sensor and any factory automated setting these days the barcode scanner next to its siblings like the temperature sensor pressure sensor different of things barcode scanner is what we are using today actuation is the second thing you need for an automation demo I wish I could have brought a robot arm but they don't travel so well in suitcases so the conveyor is what you get and brains we're just going to skim over that at the very end because we're all software developers we all know how to implement business logic and this is really an automation demo if anyone ever wants to sell you an automation demo that's missing one of those pieces return it it's a scam everyone's waiting for the first line of Python on the screen I think so let's let's dive right in this is a barcode this is the barcode scanner that's the one I brought today the left side that's very photons come shooting out that's the laser that's ruining a barcode on the right side that's where the electrons are coming out that's the data that we're interested in we want to know a barcode scanner of what did you read and it's using this protocol called rs-232 some people in this room might be too young to remember computers having serial ports rs-232 is what specifies the type of cable how long how many strands all that kind of stuff also the type of data going through it the good news is Python is old enough to remember the serial port so there there's a library to talk to the serial port in Python so this is when I flip over that totally works and so what we have here is the terminal at the top I think you've seen that sort of thing before and then this camera here so we're looking right over the shoulder of the barcode scanner this is it let's try to ask it for Parkwood so I'm just going to pip install PI serial which is the library obviously I'm prepared I had it installed right so we import serial and then we open a port if you have never opened a serial port this is what it looks like so on Windows this this would be like come one come two come three I use a USB to serial adapter that's why it's that long name it wants to know a couple of things you know the baud rate is how fast it is parody is like how the bits are positioned relative to each other kind of bite size is something I think it wants to know from us how do I know what it wants well I read the as or what that I got a type on my side I think that would have been a disaster and I'm going to ask for a timeout that's pretty long because this is a demo let's see okay so looks like we open that port and now we have to tell the barcode together or something so it's going to be in byte strings are for humans bytes are for machines easy so we're just going to send it that character did you see the laser come up on a thing it did some reading now we have to ask it hey barcodes you know what did you read read line boom that's a barcode let's try another one same thing again we read this time let's strip off that white space maybe decode that to turn it into a string for humans that's right if you don't believe me they actually print the number right next to the barcode so it is right okay so we're software developers who like to abstract things make them more modular reusable so I wrote a class that does the same thing just open the port I'm doing it a little abbreviated on that slide because I don't have enough space on the slide here reading barcodes just what we just did we sent the trigger we're eagle-eyed we clean it up we return it with these things when you write instrument drivers here's a trick make them context managers because that way if your code that Adam that does something with the barcode of distress has an exception cleans up after itself closes that port especially on Windows it can be a bit of a pain having a program hugging that that port that's that's crashed so it's just try if this works and I'm just going to copy that right over here paste it in I'm gonna call this the emissary that's the name of the instrument that I'm using barcode reader driver gonna give it that annoying name again and then we just to read a barcode oh there's no barcode there I wonder what it's going to do now oh no worry okay there we go so that's cool instrument driver 15 lines of Python we are automation engineers now couple of caveats zero cables are only up to 15 meters long if you have a big factory you don't wanna have your server that's running the business logic right in the middle there you want it off to the side maybe somewhere else in the world also you'll be using lots of these things because this is the toy factory like Tyson toy example a real toy factory will have hundreds of barcode readers you won't find a server to plug them on so it would sure be nice if we could read that barcode over the network and and you know how Python gets advertised less like comes with batteries included well this is this is one of the ones the best batteries in Python but in Python I think it comes with this cool library xml-rpc that's in the standard library XML you guys know what that is RPC remote procedure call and what it does is it wraps our class that which is road while could read a driver and a little bit of setup code and runs it as a server on the network I'm going to show you how that works paste that in again it's not returning because we told it to surf forever so it's stuck in a loop let's open up a new shell i python from xml-rpc this time we've got the client import server proxy so it's going to be on localhost obviously anyone remember the port we use 21.1 that's right one person still paying attention read barcode read barcode was the was was a function on the class few Road and over the network so now we could have done this we could have done this from anywhere in the world read this barcode that's pretty cool so let's just recap what we achieved so far so first of all I hope I managed to communicate that there is such a thing as industrial automation equipment we saw that Python works fine for interfacing with it in fact pure Python we use pi0 it's a Python only library knows he extensions or anything in there and our code was like fifteen lines in fact but pythons batteries are really useful for industrial automation even though they were probably not written with industrial automation in mind and I would actually argue that the results that we can achieve with these tools are often more elegant and efficient than what's documented or recommended by the vendors because turns out for this barcode scanner that's more than twenty lines of code of each is rode cool so we've got sensing covered we talked to a barcode reader in fact I'm skipping some slides here so we're going to go back and just get this ready what I was going to say in those slides is that it does make sense when you work with the device to maybe publish the package on PI pi which is what I did it's called micro scan I like the company that makes these things and I'm going to use a sorry maybe exit out of here first I'm just going to use what I wrote in that library it there's no rocket science in there it just deals with like this reads and things like that better so I want to use that for the demo we've got coming up later but back to the presentation sorry for the information and let's move on to actuation so what what is this conveyor belt and this is kind of the documentation you get with it it's like this box with all the cables this is kind of the API Docs of that thing just in case it's not clear that's a black cable white cable these are physical cables and they've on voltages 5 0 to 5 volts to control the speed of the conveyor nobody wrote a python library for that sort of stuff because your computer can output those so I'm actually going to use this slide to introduce you to the most important cast member of every factory out there the programmable logic controller I know called PLC maybe you've heard about it not this is what I look like if you work in a real factory your controls engineer will tell you what's running in the factory what it's doing what variables are in and all that if you just build a little demo setup for PyCon you are your own controls engineer you go to ebay because ebay is where factories are going to factory dies the PLC's go to ebay and that's very good what is in there to the left side the same with the heatsink that's an industrial PC industrial means it's very rugged and low powered usually and it's running a simple OS depending on the vendor it might be a proprietary one this one's running Windows CE the magic thing and there is a real-time kernel that's running a little piece of software at guaranteed time intervals over and over and over again and you program it in a family of languages called IEC 611 31 which is pretty ancient and kind of funny on the Left fire on the right side we've got these IO slices you buy those this is the 5 volt one for the speed output this is the one for the digital one which is I kind of slide them into each other slide them into your PLC and you've got a configure PLC you can mix and match them this is what the code looks like this is a language called structured text don't worry understanding all the details the interesting bit is these variables up here those are bound to an actual physical output voltage that's getting switched and even better these ones are writable over the ethernet so that's what we are after every PLC vendor has a different protocol all the ones I looked up like you know cement and alike there are packages available for Python same for this one the vendor uses a protocol called ADF to make these variables accessible over the network multiple options out there is one on pi pi that's called pi ADF that's wrapping the C library I'm kind of a fan of pure Python because it's more portable so Chris vitamin is published one called also pi ADF kind of name flash there and then my former colleagues at Council I put some pressure on them because pythons coming up they just open sourced the the fork of that which is kept with more variable types and things so we'll be using this council PI ADF library in this demo so let's see what we can do with that there's a lot of boilerplate to set up the connection similar to serial to the serial port here there is a security layer because if anyone can write those variables over a network you know like maybe your uranium centrifuges suddenly start running too fast or things like that we don't want that so what we're going to do is we copy that so this is basically going to set up the connection set this conveyor on variable to true which should switch the conveyor on paste that in just one little thing I'm doing here I'm going to switch the camera over so that you all can see the entire thing let's see here we go ducky sitting on the conveyor belt and switches on oh it's moving now it's up to me to save ducky [Applause] I'm telling you typing on this podium is hard let's put duckie back on there a couple of other variables who can write let's say we want to reverse it I want to reverse it I want to go the other way true this one called speed that's a integer and it's between zero and a hundred and I like a hundred okay everyone say goodbye to ducky oh no it's not oh thank you I actually brought another ducky okay so you might be asking like what is Python there's code running on the scale see what's going on rule of thumb and your controls engineer might argue with you on this but really anything that touches voltage outputs and signals directly runs on the PLC anything that's related to human safety machine safety or otherwise timing vertical running on a PLC like in my case here you might have seen that note and the conveyor belt documentation don't switch the reverse and the on input at the very same time that's implemented in the PLC that's machine safety anything that connects to outside services your ERP or database or anything like that or that provides a user interface it's probably a better fit for Python and one of the reasons is deploying code to this PLC almost certainly requires factory down time might need recertification extensive testing veracity of your Python code it's probably on a much faster deploy schedule so you want to align the purpose of what you're writing with the kind of deploy schedule and procedures you have to build around it generally you should for you know your usual daily or continuous deploy of your Python code and just don't touch this approach to PLC's cool so we covered sensing actuation brains I'm just going to tell you right now I brought this little Python package I call it gum it's got a database with eight lines of barcodes for all these different chewing gum colors positive barcode it tells you what the color is and this is the business logic of the Python 2017 chewing gum sorting Factory so basically we start the conveyor we read the barcode if you don't get one you try again once you have a barcode find out the color from our proprietary chewing gum database if it's more red than blue we flip the right paddle it's more blue than red with the left pedal this is what that looks in Python in the interest of time I'm not going to copy it this time instead of going to go right over here you know what's ironic we're going to talk about automation the auto focus is switched off there you go better picture okay so let's see exit out of here we already got that XML RPC server running that's telling us the barcodes and we're talking to the PLC over the network so there's no direct to machine communication going on from our central server it's all happening over to network so we've got the barcode it scanned it and it's sorting it to the right side guess who forgot to put a little trace here that catch let's catch the chewing gum that's perfect setup and let's just run a couple more down this is pink coming pink is more red than blue this is kind of orange yeah more red than blue another different kind of orange just FYI Trident is the only brand that prints the barcode on the short side of the package Oh I don't even chew gum I yeah so this is this is the factory total you know total lines of code of Python code running here really less than 100 and we did something kind of meaningful well kind of meaningful right we we've got three instruments using two Ethernet connections obviously a real factory oops the real factory is going to be having more instruments more diverse and just more in sure quantity so it's going to look more like this and the first thing that your factory manager will ask you is for HMIS human-machine interfaces they want to like find out what these variables are there aren't appeal so you maybe click a button trigger a barcode scan you're going to put a little web app in front you know you dig through a Python tool chest mega flask Django something like this and for they'll be more and they'll also be talking to all the other data sources in your business maybe comparing you know barcodes to chewing gum color library then you'll run into this interesting dichotomy like people are interested in State they want a quick response from the web server and want to know the current status it running is not running machines you know things take time if you're if your shaker process it has to shake a minute but the response time is a minute and you're not going to make it faster also they are inherently singleton like a web api you send a ton of requests to it they just spin up more AWS instances that's not how it works for six axis robot arms a really good tool to actually bridge that gap is using task runners like celery in the background that keep you know your your instruments running while the front-end can carry them for what this status so they're like the virtual representation of your device you know then you automated everything there's buttons to trigger all the actions your operators are you know going to run clicking the same button every day someone's going to be like can't we automate that too and you'll be putting it like a career level scheduler that's clicking the buttons for people so we put in an API to read Django rest framework maybe something something like that like that exposes all those button clicks that operators were doing as an API and you have a process scheduling server that's going around making sure you know to chewing them packages arrive at the barcode scanner in the right time see and then you know your your operators come in in the morning and they're like what's going on all the tooling gums all over the floor we need like metrics and logs of what happened so you put in like logs and stuff and and soon enough this is looking like your standard service-oriented architecture you know micro-services and you're probably using just the standard tools of modern software engineering you'll be putting in a message queue maybe so you can go out there and and you know all the like all the big ones that have Python libraries available like 0 mq Kafka are probably a good fit for what you're trying to achieve here so this is really this is what could be coming after you've done your little modest barcode scanner with one conveyor belt I hope this was a good intro to just give you a taste of you know how this works how do you talk to machines how do you make factories run I think industrial automation is a really cool domain for software engineers if you if you show up as a newbie in data science it might look like everything's been done now industrial automation because it's a bit of a niche not like that at all it's a wide-open field here here a couple of things you could just start doing and do something useful you could write device drivers they're basically the api's for robot the way I see it is every time you publish a device driver as open-source you make that device available to academics hobbies' tinkerers who don't have the luxury of a field Application Engineer from the manufacturer who can tell them how to set it up for them if you're not into like tinkering with tangible things that's ok file formats and protocols there are hundreds of every machine has its own proprietary protocol and people on the factory floor and many factories spend a lot of time just converting between one format to another using tools like Excel by writing viewers converters parsers there's just a huge scope of things that can be done out there if you're into data science sorry data scientist I cannot like you see as the example for you overcrowded two popular field if you're into that actually this is also exploding in factories right now this concept of predictive maintenance we monitor the vibration of a motor over time and then predict when it will fail so that you can monitor it is huge if you want to Google at industrial Internet of Things is the buzzword and same for scheduling factories are super complicated networks of you know good and data making them all show up in the right place at the right time is a huge scheduling from same same thing lots of work to be done there one security I'm just going to say you'll be busy and with that I am on my last slide and I think this went surprisingly fast so we probably have time for a question I think we have time for one maybe two questions if anyone has a question please go up to one of the microphones in the aisle a great presentation for the device drivers do you have any tips on writing those like entirely in Python or do use some C libraries or are there any standards for like that's no methods for creating those drivers so many questions from there the answer is no so basically for every device and for every application you really have to like judge what's the right thing to do in some cases it is cut the cable sent the voltage just ahead at once and you're good sometimes there is a pretty good like let's say Windows software and they even like the vendor gives you a calm object or dll or access X object something like that and maybe then you just want to wrap that it really depends it also obviously depends on like encryption and things like that some vendors encrypt the communication so long story short it really depends on a specific device I always and you kind of hurt that during this presentation try to shoot for pure Python and sending bits and bytes over Ethernet or serial port hello a great talk thank you very much I have a quick question about the serial ports in fact if you would like having a serial server that converts all the serial input into network right away that's how it's done well I actually skimmed over the part where most factories usually have these serial to Ethernet converters scattered around and that's true but you would obviously be using that even though sometimes those can be hard to communicate with iron Devils in the detail there one place I worked at actually the folks at Council who contributed that PI 80s library our approach there was to use XML RPC servers just as this boundary like one team works up to the XML RPC server and then other teams have a unified interface to every device and that's how we did it there but is again you know they're like hundreds of manufacturers with like thousands of devices so it really depends on the case thank you thank you very much that's a way of time for um thank you please thank you on the front again [Applause]
Info
Channel: PyCon 2017
Views: 22,638
Rating: 4.9848485 out of 5
Keywords:
Id: cEyVfiix1Lw
Channel Id: undefined
Length: 30min 4sec (1804 seconds)
Published: Sat May 20 2017
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.