← All talks

Robots For Complete Beginners - Mark Goodwin

BSides Cymru Wales41:5913 viewsPublished 2023-04Watch on YouTube ↗
Speakers
Show transcript [en]

look hello how's everyone have you been to besides before is this is your first time for us good good well hopefully you'll enjoy it so this this Talk's a little bit different um usually when I come to these things I talk about security stuff security is what I do for for a job um I leave an upset team for a company called Cotillion and but I also like doing other things and one of the things I like about the the security system that have you seen is that actually there's a lot of people that are into all sorts of different stuff right and and I wanted to to share some enthusiasm and um we're only to start with this is that I remember my first computer you remember your first computer my first computer was brilliant right it was absolutely fantastic it had um 32k of RAM it could display eight colors right I could I could um reprogram this in return or get this discs yeah and um it could do sound and I could share programmers with my friends at school sometimes we would get them printed out and type them in and it is brilliant right and computers are even better now right and I've got a computer in my pocket that's um apparently more powerful than the computer they use to the moon landings um my car has got computers it's got It's got computers on the computers it's got computers on the brakes um the charger from my car has got computers in it and the display adapter for my other laptop has has got a computer they're everywhere right but they're also a little bit disappointing aren't they you know if you told a six-year-old me that I have a computer in my car and I couldn't play Chucky head on it I'd be really really sad and so what this talks about is it's mostly about how to recapture the fun of having computers that do exciting things for you so that's what I'm hoping to do and so the talk is about building robots but what is robot does anyone want to venture an idea as to what a robot is any ideas okay that's not a definition I've heard before I like it okay that's what we're going to talk like this someone said it's someone with no Personality yeah that sounds pretty good isn't it okay so so I'm going to talk a little bit about what I am going to talk about which is I'm going to talk about things like this okay so my journey with this subject starting when when one of my kids came to me and we used to like have a summer holiday project in one year we made a go-kart and the next year my younger son said to me Daddy yes can we build a robot to mow the lawn and anyway that's ridiculous we can't build a robot maybe they're actually like do you know what that's a really cool idea and the thing is that you can't you can't just go to like a shop and buy parts of the Lord made robots so first we had to build a 3D printer to make the parts and then we built this robot and and this is the first version and and and this is just a machine that's controlled by computers right um and it's a little bit like what people think about when they think about robots because it moves around and it had a camera and a little display and stuff um but it could also be something like this did anyone tell what that is from looking at it anything in the picture you recognize looks like a potato it's actually eggs it's an incubator Well Done Right so so this is a robot I built which is a Linux powered chicken mama it's a it's a box with a heater in it and a little Raspberry Pi and a humidity sensor and a temperature sensor and when it gets too dry it blows water through this bit here to make the air more humid and when it gets too cold it turns on the light into that foil and it warms it up a bit and then a few weeks later those eggs turned into chickens it was magic right but that's kind of like a robot isn't it it's a computer it does stuff or continuing on the theme Here you see a theme right so when the chicken disappeared we wanted ways of keeping track of them so we have um computers that kept their environment warm gave them food took pictures of them for us and I was very bored during the pandemic chickens with my friends um continuing theme here this was a a computer controls door to openly close to like can everyone guess what it's for it's for chickens right it's a door that opens and closes and lets the chickens in and out and it's certain important purpose right it stops the fox you get things are kind of Road can sense their environment and do things and and so that's what we're talking about we're talking about physical computing and actually you know if we're going to be uh really um strict about it well most of the computers we use can sense and affect their environment apparently because you know I'm looking at a screen now and when I touch it it does stuff but I'm talking about how to build our own you know how we can um make our own things that sense or affect our own so let's make a start I think who does a bit of programming and we're doing some programming before here if you wanted to tell me what this program does it's python come on someone knows the other secretary yeah it does it help us hello world right and this is like the it's the um definitive simple program isn't it you run it and it has one piece of data that deals with the stream hello world and it puts out on the screen so what we're going to do now um is we're going to do hello world but with physical Computing right now um a word of warning um everything you see here is alive which probably means something's going to go wrong it might mean everything that goes right is gonna it might go wrong it's going to go wrong and that's okay right you know this is this is like um Tech talks is it adrenaline support I'm okay with that um so what we're going to do is we're going to find a program that I made earlier blue Pizza style and we're gonna make it do something so I need um an output device what can I use as an output device uh I've got an LED in this bag you know how hard it is to type when people are watching you and this has a close cousin or like motor skills generally so I've got to put some leads on an LED and plug it into something um it'll be fine I'm sure okay so you saw me start up the camera program earlier on the reason for that was I wanted you to be able to see what I'm doing okay so you can see here what I've got is an Arduino and other single board computers are available at the microcontroller development boards and stuff and what I'm going to do is I'm going to put one of the legs of the LED into one of the i o ports on the Arduino and the other one on the ground like this and if you go to the the diagram on the slide uh oops there we go I've got a little circuit diagram that's what's going on here and then we're going to find a program and run it and then that led should flash it okay so let's find our program uh soil open Okay so really simple program we're telling it that we've got an LED on pin 11. uh actually I don't want that one I want blue let's get a different one um [Music] well it will blink but I want it to Blink differently laughs we want this one instead there we go so we want no input here so literally just an LED we tell it that the LED pin is an output pin and we have a loop which will turn the LED on and then turn the LED off it'll sleep for a second after it's turned it off on little sleep reset Netflix off again and it'll do that over and over again so let's plug this in and then we'll pray to the demo gods what whether you wanted to succeed or fail and determine which prayer you give to the demo gods and then hopefully when we run this we'll be able to see a flashing LED okay and file there we go upload so what the status it builds the Arduino project it sends the code to the microcontroller and with any luck so microcontroller is here is there LED is there yay success okay [Applause] who speaks Welsh here by the way anyone speak Welsh a little bit I've been learning because my number's Welsh but I'm not very good it's hard like properly hurt um okay so that was that was hello world uh as a as a Arduino project okay so let's go back to some slides okay another program can you guess what I'm gonna ask here what does this program do not sanitize you but it doesn't you're right it takes some input and then it uses that input to generate something to Output okay so so it's a little bit like the last one only it's taking some input so how are we going to do some input any guesses foreign yeah that's right so uh I've got a button in here okay so this is a micro switch okay it's called the micro switch because it's a switch and it's small uh closer look for those of you at the back looks like this it's got uh three little legs and a little button that you can push down and when you push the button down it closes the contact between this leg here and this leg here all right and what we're going to do is we're going to find a different program this time the one that I opened before when I meant to open the first one and it's expecting an LED on pin 11 and a button on pin 8 okay so let's go to our circuit diagram so I can explain what's going to happen when this works okay um so pin 11 we have the LED before and on pin eight we have a switch where the switch closes the input voltage here changes and now there's a little detail here which I'm going to tell you about and then if you completely forget about for the rest of the tool is you've got an input on an electric controller board or something like this and you can't be certain that has a particular voltage to start with right it just sort of floats around and so when you put a switch on it and close the switch it might do something or the computer might not realize anything's happened okay so what that means is that you need to make sure that the voltage is something to start with and then it changes when you press a button and now you can mostly forget about this because most of the boards that you'll use have little built-in things called pull up or pull down resistors and it means that to start with your PIN 8 will be in this case pulled up to five volts and when you close the switch it goes back down again so just imagine a little resistor between the five bolt line and heater because that's what's going on inside the Arduino you can normally forget about this as long as you realize that that's a thing and if something is going weird that might be something to check okay so the important part here is we've got our LED on pin 11. we're going to take our switch and we're going to put it between an eight and the ground okay um all we've already had one demo work um those bad statistics will tell you that makes this one less likely to okay so we're setting up an LED on pin 11 a button on pin 8 we are starting the delay to a 1024 milliseconds we're setting the LED pin to be an output we're telling the button pin to be an input that's pulled up to five volts and we're going to Loop that turns the LED on waits for a delay turns the LED off and then if the button is being pressed it halves the delay time okay so it'll start flashing on and off roughly every second when I press the button that time is going to reduce or at least that's Theory I have no idea if it's going to work let's find out so it's flashing that's a good start okay okay yeah I just wanted to make sure it worked before I put it on camera and shoot tips okay so let's reset the the board so we've got the light blinking on and on on and off you can see that okay I'm gonna press the button now and it's blinking Fastener and if I keep on pressing the button down it blinks so fast that you can't even see it blinking anymore right now what do you notice about that the brightness of that compared to the brightness of that anyone notice anything it's roughly half okay and that's going to become important in a moment all right when we talk about another concepts of their moment okay so there we go that is our equivalent of hello Mark right so far so good okay so I mentioned that the fact that the LED sort of dimmed when it went really really fast was important the reason for that is that when you've got a digital system like it might go controller and you're dealing with on and off right what happens if you want your sort of Shades of Gray well the way it works is um you're actually dealing with um different amounts of on and off when you're thinking about outputs right and and this is something we call Pulse grip modulation because when something is sort of on or sort of powerful as an analog output what's actually happening is it's turning it on and off very very quickly and how much it's on versus how much it's off determines how much on the thing is right so um LEDs you can change the brightness two ways you can reduce the voltage but that doesn't always have the desired effects firstly the output of the lights and competitive voltages and video secondly um the color changes right and similarly if you've got a motor if you turn down the voltage it's more likely to stall and you more likely to burn the motor out whereas if you turn them on and off again really really quickly they behave themselves and you get the effect that you want okay so that little demo where it was having the the um sleep time every time until it got down to a millisecond it is useful because it shows you like a fifty percent of script modulation so that would be a teeny bit on that that would be a teeny bit off and that would be sort of similar in between yeah have I explained that remotely coherently brilliant fantastic okay now we've done that we can move on to an analog stuff okay um so here we have another diagram does anybody know what the symbol here mean it's a variable resistor yeah so it's a potentially I would say one side of it is on ground the other upside down and put voltage and there's a bit that can move on the in-between and this is connected to all of the analog input pins on the microcontroller yeah and when you turn the dial on the variable resistor which is the one that I've got here has a little knob I'll show you the camera there we go so when I turn this the resistance between the red and the black leads here you see and the yellow one changes the yellow one is at one end effectively close to the black and at the other end effectively close to the red okay so what we're going to do now is we're going to attach this leg here to the ground on the microcontroller board this one here to five volts this one here to the analog input pin and then hopefully we can run another sketch that will allow us to have an analog input into our microcontroller have I explained that okay just before I move on to actually uh failing to put the ball together probably there we go so black in the ground red you know dropped the LED that's not what I wanted to happen there we go and that's in the five volts I'll put the LED back on and then we will update the sketch that it's running foreign I'll be honest with you I'm already surprised how much it works it's got no right to go this well I'll do the rest of it yeah right so we've got an analog program here and this is more or less the same as the one where we were reading the button value and it's different in a couple of minor ways so let's upload the program to the controller and okay I've actually made a mistake here but it doesn't matter because it'll still work right so I've got an output pin again the LEDs on pin 11. I set it up as an output and then I probably should have set up the analog pin but it probably doesn't matter if it's the Arduino monthly do it for you but in the loop all that's happening here is reading a sense about you now um the Arduino Uno which this board is here has a 10 bit analog input um and when you're doing analog output it has um bits output so what we're doing is we're taking the input we're dividing it by four because that makes the maths work and it means that the amount that the LED is on is proportional to the amount that the voltage is on the input in does that make sense sort of um so hopefully that's going to work organic little pressure to the demo of gods please oh and the LEDs falling off foreign just put the LED from the board if it falls out again I'll do that and how come this time you didn't use the pin mode output pull up and well so we need to be the pull-up for the input not the output um and um so because there's not an input um that isn't analog we didn't need to pull up it's kind of already um uh threshold set for the analog because the one end of the potential which is on the ground the other ends of the Firefall so when you turn it it's somewhere between the two so it's always going to be something you expected you see on turning that potentiometer and the LED is getting brighter as I turn that up and down okay now what we're going to do now is we're going to leave the hardware completely the same um aside from me dropping things and having to put it together again because what I want to demonstrate here is that once you've built some Hardware because this is a system that you've built you can change what the hardware does based on the software that's on it right so same Hardware we're going to change the program and this time we're going to [Music] uh bring in a different component to the system uh and the name of this project is analog i o plus serial if anybody guess what I'm going to do right back to the console yeah we're gonna we're gonna interact with the console okay and and actually this is a um really useful point because um one of the first questions I get when I show people this sort of thing one of the first questions I get is what should I use because there's a dazzling array of stuff that you can buy for doing these kinds of things these days so you've seen the Arduino I've got there and you might come across these things here so this is a um the Raspberry Pi 2040 microcontroller board here so it's it's on a little um project board you can see that tiny little thing there is it will break it there we go so that that's another type of board you can use this is another like microcontroller or you can even get like little single board full featured computers so this here um is a Raspberry Pi computer which rather than being a microcontroller is actually a fully featured computer running limit so you can actually do stuff um we have on there doing something with the AV and but you can you can build projects using either of these and so people very often ask the question what should I use and um the the short answer is that um for most applications well for some applications it doesn't really matter it's mostly about the tools that you use and if you like writing software for example in um JavaScript and node that's your promoting ecosystem you're obviously going to be happy on a computer that can run those things and something that they only do and see for example and the other way they use is CNC plus and but there are some important differences so for example microcontroller boards typically have um richer low-level IL options so we've used an analog input in there there's no such thing as Raspberry Pi yeah and and they're often simpler to get really because all they run is the single program that you put on it you know the program you purchase the ROM on the controller when you press play on your Arduino IDE or similar it's only got that to worry about and single broad computers can do more you know they're really limited to put whatever Services you want on here including your favorites of machine learning libraries or whatever and and they can run a full OS with all the benefits that that will give you like you know network connectivity and all that sort of stuff and but they come with drawbacks you know one being fewer options for Io another being that actually you're not you've not got quite as much control over the real-time aspects of the i o you know if you need something to happen within a few milliseconds well actually