 Okay, so we good to start excellent. Hello everyone. My name is Anna if you don't know me And today I'm just going to give you a bit of a rundown of building robots using Node.js So this is a really just gonna be really quick intro Usually at CamJS. We have like a whole half-day node bots workshop But I didn't want to risk taking a bunch of robots in my suitcase this time because of the whole Airport situation. So yeah, so if you want to actually get your hands on some hardware and build robots Just look for the node bots group in your local area. So there's one based here in well in Melbourne. Anyway, it's CCHS There's one in Brisbane and there's one in Sydney talk to Damon find out details And I'm sure that there are others in other locations as well that I'm not aware of So the kind of robots that we're talking about here Robots are autonomous systems that sense and respond to the world So that's a very loose definition and the kinds of robots that I'm talking about are really simple robots that do very little of actual use We're not talking about the Mars rover here So an example of one Just pull it out of my pocket Is like this for example, so it it actually doesn't even move. It's just one that I've built that Reminds me to do my exercise and I can kind of hit it on the head It triggers a button so that it records when I've done that exercise So it's kind of there to trigger me to change my behavior. So it could be something as simple as this It's sensing whether I've done something and it's responding by reminding me Or it could be something that drives around and and does something So kind of the basic parts that you have in most robotics projects all the ones that I'm talking about anyway you've got some kind of chassis or Like for example here a 3d printed shell that all of the parts attached to You've got some kind of power whether it's batteries or whether it's running off Very long USB cable, which is what we've done at some node bots Sessions and then the stuff in the middle, which is kind of the stuff we'll talk about today You've got the sensors. These are your inputs. They are the things that sense things that are happening in the world So for a robot that sort of drives around and detects obstacles It might be something like an ultrasonic sensor For this thing it might be something like a position or a PIR sensor to detect when I'm moving so it knows to remind me You've got some kind of brain or control so typically this will be a microcontroller or a single board computer like a Raspberry Pi and Then you've got your outputs or your actuators So these are things that that do something they cause some effect in the world So there might be the motors that make the robot drive around or they might be lights or screens or something like that Now we're interested in JavaScript, obviously It's kind of becoming more popular for building especially hobbyist Projects like this and there are a lot of different options out there. So this is just some of the ones that I know about I'm sure there are others Johnny five is the one that I'll be talking about today It's the one that I've been involved with We also we've done stuff with Esprino JS in the past. So Esprino is like a very cut-down JavaScript runtime that runs on microcontrollers Tesla which also is Yeah, so Tesla works with Johnny five and it also they had their own platform it's a Tied into a particular hardware platform Particle IO is another example of Hardware platforms have got things like the photon and the electron boards that you can program using a JavaScript SDK Cylon JS, which kind of is a project that takes the best bits of all the other projects and kind of pulls them all In together under a single umbrella sometimes without attributions So just be aware that they haven't done all of that work But they have done a good job at integrating it all and documenting it Jerascript, MuJS, duct tape, all of these are kind of very Small versions of JavaScript designed to be embeddable. So I'm just going to talk about Johnny five today But if you're interested in this field, definitely go check out those those projects So one of the reasons why I like Johnny five is because it works with lots and lots of different platforms So when you're selecting hardware for a project, it really depends on the requirements of your project So these days there are so many of these development boards and kits available For all different price ranges and lots of different features. So something like a Raspberry Pi So I've got a Raspberry Pi Zero W here, which is a kind of single board computer. So it's got quite a lot of memory It's got quite a lot of expandable flash for storing programs and applications So it's very powerful and you could build a quite a powerful robot with this But it's obviously a little bit more expensive Then say a $2 microcontroller. So, you know, if I'm running a class for 50 school children and they're likely to destroy these things by plugging power in backwards and that sort of thing Then I'm more likely to use microcontrollers Because they're almost disposable Johnny five runs on all of these different platforms. So Arduino's which are microcontrollers Raspberry Pi's tassels, BeagleBones, which are also like a single-board computer like a Raspberry Pi the particle boards and many others which you can see The icons for and if you go to the Johnny five website, you can get lots more detail about all these individual platforms And the cool thing is that the way that it works is that the API is Standardised across all of these so you can write some code that works with say a motor or a button or an LED And then if you decide that you need to upgrade to a different platform because perhaps you've started building on an Arduino And now you want some kind of network connectivity and you want some more powerful Number crunching of your sensor data. You want to go to a Raspberry Pi? You can move that code straight across and just plug in an IO Plug-in for Johnny five and it will just keep working regardless of the fact that you've changed platforms So yeah Johnny five it's open source robotics framework for Node.js There's been about I guess 75 or 80 people who've contributed to this project Including a number of people locally and every year we have this big event called international node bots day We've been doing that for about the last five years where we all Get together a bunch of people in the community and build node bots Unfortunately, we had to miss it in Brisbane this year as a couple weeks ago because we were busy getting ready for CamJS But we will have it a bit later in the year So Johnny five was originally designed to work with Arduino and it communicates with the Arduino's over a protocol called Formata, so it's over a serial connection so you plug in say over USB or over Bluetooth and Your microcontroller your robot with the right microcontroller is kind of like a really dumb device It just responds to commands and you're running a Node.js program on your laptop or maybe on a Raspberry Pi or some other device It's doing all the smarts and it's communicate communicating with that peripheral microcontroller and it's controlling the pins that control the sensors and the actuators or read from the sensors and control the actuators So yeah, that formata protocol is supported On a number of different devices and it's not just for Node.js Incidentally, so there are Python libraries that support formata. There are Ruby bindings I think as well. So if you're into other languages, you can use that same protocol With Johnny five originally it just used formata and it was primarily for microcontrollers But then through this idea of IO plug-ins, which pretty much Take the Johnny five API and implement it using whatever Local GPI capabilities are on other platforms like Raspberry Pi You can run it on board So originally it was designed to have a kind of a primary Computer running Node and then your peripheral microcontroller, but now what you can do is run Node on Your device like your Raspberry Pi big bone talking directly to its own Johnny five instance So I'm just going to step through how you would get set up using a Raspberry Pi zero W Because it's a recipe is pretty popular platform a lot of people have them so hanging around at home So if you're looking for something cool to do with the Raspberry Pi, this is something that you could do so Raspberry Pi zero W is interesting because it doesn't have an ethernet Port it's just got Wi-Fi built-in Which means that a lot of the guides that you find online for setting up a Raspberry Pi Don't work really well with the Raspberry Pi zero and it always just tells you to go and plug in to a keyboard and You know a monitor and that but you don't actually need that to get set up Because I don't know about you, but I only have a laptop I don't actually have an external keyboard and a monitor to plug into so for a long time I didn't use Raspberry Pi for that reason But what you do is you install Raspbian and pretty much the Raspberry Pi zero Uses a micro SD card, which you can see seeking out the top here So what you do is you plug that into your laptop and you pretty much copy that Raspbian image onto the SD card And then instead of having to plug into the Raspberry Pi booted up and configure it There's a little trick here Which is not particularly well documented But you create a file on the boot partition of your micro SD card call it WPA supplicant.conf and Put in your details. So if you were here, you had a Raspberry Pi here You could just put in the SSID camp.js and the password more coffee and it would be on the network here And then if you want to enable SSH access so that you can just SSH into your Raspberry Pi Simply add a file called SSH. So just use touch or something. You don't doesn't need any contents Put it on that boot partition of your micro SD card And then once you put that card into your Raspberry Pi and booted up It will be on the network and you can connect to it and you ask well How do I know how to connect to it? What's its IP address? The cool thing is that it's running MDM MDNs So you could connect using the host name local If everybody did this, I guess it would be difficult because they'd all have the same host name But I guess you can configure that as well in the configuration files on the SD card So yeah, you just connect like that and by default password is raspberry Obviously, please change this as you know as soon as you get your device set up so that everyone's not able to connect into your device So if you're using just the default Raspberry and distribution on Raspberry Pi It comes with Node.js installed as well as a couple of other cool things like Node-Raid that we'll talk about in a minute If you're running something else or if you want the absolute latest version and the one that's bundled isn't Isn't what you want then you can just use your regular Linux, you know apt-get So on to install node and MPM And then once you've got that you've got a little Linux computer So you can use MPM to install node packages just like you normally would so the ones that we're using Johnny five and Raspberry Pi IO Raspberry IO and Also, they will install the serial port MPM package, I've just got that there because it actually it takes a long time to build so if you're doing this sort of If you're going somewhere you probably want to install that in advance because it will take a while All right, so the Raspberry Pi device It's a single board computer. I know how well you can see this that that bit there is the Raspberry Pi Zero W and you can see it's got a whole row of pins along the side there So these are your GPIO pins a general purpose input output pins There are 40 of them on the Raspberry Pi zero W and they're compatible with the pins that you find on other Raspberry Pi modules Like the Raspberry Pi 3 Has the same sort of pin layout So to the side there you can see what all the different pins are So this is where you plug in all your peripheral sensors and actuators and so on One of the things that if you're coming from the world of Arduino is different here Is that it's 3.3 volt logic rather than 5 volt logic so when you're plugging in your save you have a sensor if it is Generating 5 volts you might want to use something like a voltage divider Just to step that amount of voltage down to 3.3 so that you get the full range of values on the Raspberry Pi That doesn't make sense to you. Don't worry. That's just a detail for people who already use Arduino and Pins in Johnny 5 are labeled in a bit of a weird way. So You can see on the side that they've all got numbers So from the top you've got pin one and then pin two all the way down to pin 40 down at the bottom Now in Johnny 5 the way that they label them is P1 Dash and then the pin number so P1 is corresponding to the header number because some other Raspberry Pi's Have more than one pin header. Raspberry Pi zero only has one so it's always P1 dash and then the pin number There's a couple of other ways to you can identify them using the GPIO number directly But most of the examples you find online will use P1 dash and then I'm all right. So getting started I'm going to assume that some people you haven't done much electronics So one of the ways that I like to get started is by using a breadboard a soldless breadboard to prototype my circuits So this yellow thing here is an example of a soldless breadboard. It's a little one Like the one that you see on the side there You can get bigger ones like this one here that have power rails along the side You can get you know even bigger ones than that and the way that these are designed is on the inside They've got these little metal kind of clips underneath That when you plug your wires into the holes They just grip onto those wires so that you can connect them all together instead of having to sold the wires together So it makes it very fast to plug your components in and out and change the circuit around So if you wanted to attach an LED a light emitting diode, so this is one of those very brightly little bright lights you would simply Plug it that into the breadboard and then what you need to do is connect your jumper wires So jump wires are these these wires here and they've typically got little pins on the ends From the pins on the Raspberry Pi Across to the LED and you'll notice that on the breadboard there you can see there's a resistor Which is that little striping component in the middle there so resistor Introduces some resistance into the circuit It's really there to limit the current that's going through the LED so that it doesn't Burn really brightly and eventually burn out so What we've got here is The pins on the Raspberry Pi so if we go back and look at that diagram there. We're connecting to GPIO 7 And we're connecting to ground. So that's the fourth one down on the left-hand side and the third one down I think on the No hang on GPIO 4 is what we're connected to sorry and ground so I've got one jumper wire going across to the LED and the other one going across to ground so If I want to write a program to control that LED using Johnny 5 I Simply create a JavaScript file So I might call it something like blink JS if I say I'm blinking some LEDs and Because I'm using a Raspberry Pi I would create that on the Raspberry Pi or I'd create it on my computer And then I'd copy it across to the Raspberry Pi, but it needs to be on the Raspberry Pi eventually and It needs to be in the same place where you've installed your npm modules So maybe you've created a little folder project somewhere on your Raspberry Pi It's just JavaScript so you can edit using whatever JavaScript editor you like to use so if you're doing it on the pie You might want to use nano or vi or emacs or something like that If you're doing it on your machine add a more sublime or whatever in your program Pretty much every Johnny 5 program that you write for Raspberry Pi will have this at the start So you need to require both the Johnny 5 library and the Raspberry Pi IO plug-in for Raspberry Pi and Then what we do is we set up Board object, which is an instance of the Johnny 5 board class and because we're doing it for Raspberry Pi We give it this IO Configuration option, which is a new raspy plug-in So you'll see that at the front of every Raspberry Pi Johnny 5 program Once you've done that then pretty much all of the action happens in the event handler for the board on ready event so this is the same object that we just set up as an instance of the board class and When it's ready We can run code to set up our LEDs or sensors or whatever other things that we're working with So for an example if we were working with this LED that we've just plugged in inside that on ready event We'd have something like this. We'd have a Variable LED which is a new LED object and we give it the pin number that that LED is on So if I'm using GPIO For that's pin number seven on pin header number one on the Raspberry Pi. I hope you're following So that would be LED P1-7 and Then once you've got that object you can then do stuff to it So you can depending on what type of object it is. This is an LED So there's Johnny 5 API for LED types of objects and one of the functions that it supports is strobing Which is flashing and you give it a number of milliseconds and it will flash every 1,000 milliseconds or every second So you could change that to whatever you want to different behavior. So let's just do a quick demo of that Okay, so this is the folder that I've created on my Raspberry Pi so I've already ssh'd into my Raspberry Pi and In this folder. I've created a couple of JavaScript files, which are my programs. I'm going to show you so blink is the first one. So Something's happened just to make an action. Let me just check. Oh, that's gonna be bad for demos. All right. Well We might come back to that in a minute and see if that works in a couple of minutes. Oh There we go. Oh, hang on that might not be my Raspberry Pi No, yes. All right. So we've got Well, we've got some network. We've got the program just there. So I'm just gonna run that. Oh, come on, please don't drop out good So the way that you run that is using just node like you normally would run a node program Let me zoom in a bit more again Everyone see that okay And then you just go node.blink.js, but because GPIO requires Root-level privileges you'd have to add your use to the group or I'll just do it using sudo. Don't do this at home That's bad. But yeah And then it runs and I don't know if you can see this but there is a little LED on here that is blinking right now Magic of JavaScript Cool, and you'll see there's a little prompt there This is what's called the REPL the read-a-val print loop and it's just like the node prompt you can type stuff in so I'll show you in a minute how I set this up But I've got this variable in my REPL that I can use to control this LED so I could tell it Do you let me pull that up a bit further so you can see it in the middle of the screen? I can tell it stopped blinking and It stopped probably can't see that but yeah, it's stopped and then I can do things like LED on and it turns on Or LED off and it turns off and you'll see it's printing out a whole bunch of state That's not an error. That's normally what it does is just so you can see what the result of that operation was so That's how you run these on the Raspberry Pi And back to here. All right, so that was the whole program It was the first bit that we saw setting up the libraries require Requiring them the on-ready event and then inside of that the stuff that we actually want to do with the LEDs Which is setting up and strobing Now when you're building a robot you probably want more than a single LED You're going to want some sensors and you're going to want some outputs Actuates and there are lots and lots of different components available that are out there. So Some examples you've got environmental sensors that can sense things like Temperature the humidity the amount of smoke in the air the amount of ozone you name it Magnetic sensors like Hall effect sensors that you find on things like 3d printers detect when you get to the end of an axis light sensors like photo resistors that can detect the amount of ambient light or the ones that you have that you face downwards and you You blink an LED at the same time as detecting the light bounces back You can detect whether you're on a black line or a white surface so you can use line following programs Sounds so microphones that can detect the presence of sound they can't actually recognize the words or anything Piezos that you can use for detecting vibration or or the presence of sound Things like accelerometers gyroscopes tilt switches buttons, etc So that's just a few examples of some of the components that you'll typically find if you get sort of a getting started Robotics kid or electronics kid And same thing with outputs on your robot You've typically got things like LEDs LCD screens to display states about what's going on with your robot sound like buzzers Movement obviously is probably one of the more important things with the robot Most people think of a robot as something that moves around so you've got your motors that drive the wheels or tracks You've got things like solenoids if you're talking about industrial robots in particular And relays for controlling things controlling power, so you're switching the lamps on and off in your home that sort of thing So that's just a few examples of some different Output components that you might find in your your average getting started to electronics kit And one thing that's important to note with these is that there's this idea of digital and analog sensors and and Components so digital when we say digital we're talking about digits zero or one So things that are off or on So in the case of a sensor, it might be something like a button. It's pressed or it's not pressed. It's never kind of half-pressed But you can also have outputs that are digital So an LED for example, you can blink it all the way on all the way off and just treat it like it's on or off Or you can treat it like an analog component. So this is where you've got values in between so for sensors, this is things where like An analog sensor like a temperature sensor where you're getting a reading and it's typically with the raw reading will be something like Between zero and 255 or between zero and a thousand and twenty three depending on the number of bits that your sensor supports And for outputs, it'll be things like fading in LED the brightness Yeah, so we looked at the repl and yeah, so button is a really good example of a digital input So with Johnny five so this when I show you this code It's all what goes inside the on the on ready event So with the button you've got the button class and you've got event handlers So on down and on up and you can do different things So with your LED you might want to turn it on when you press a button or turn it off when you press the button So this is an example of setting up those event handlers to control the behavior Something like servo motors common in pretty a lot of robotics projects for like moving things around Sometimes you can get continuous rotation ones that control wheels Again, you set it up using a Johnny five servo object So the API supports a lot of different common components as you can see on a pin so pin 35 You can put it in your repl if you want to be able to control it interactively and Some of the things that you might want to do you might want to sweep the servo back and forth You might want to stop it. You might want to center it You can also set the position of the servo and servos work using pulse width modulation So it's this idea of having values in between one and zero but on a digital pin rather than on an analog pin So it's kind of like Simulating voltages in between zero and five volts or zero and three point three volts piezo also works with these sort of values in between one and zero so with a piezo It's pretty much just controlling the tone that you hear from the buzzer So there's really those beeps that you get they can be high-pitched. They can be low-pitched and The same thing with motors so motors you actually usually use a separate motor controller So this is a circuit for Raspberry Pi with a motor controller and two DC motors attached So these are really common in those budget robotics kits you get from China So this would be the code that you'd use to control those two motors. So you've got your Using the motor class in Johnny five you set it up on the pins And in this case because your controller actually has two wires for each side one which is controlling the direction and one which is controlling the speed Then you set those two up and you can see the thing that I've got in there about inverting the PWM That's yeah, that's just an example Probably don't have time to go into that It's just an example of how you can control the motors and you can find out lots more from the Johnny five API documentation All right, so really quickly You can write all this code from scratch yourself and you can just run the node programs directly But one thing that's really cool if you want to be able to integrate with other systems other components is using node red I don't know if Chris talked about node red at all, but node red is this awesome UI that supports a whole bunch of MPM packages and It is installed on Raspberry Pi by default. So let me just pull it up really quickly Just so you can see what it looks like So you've got an interface where you've got a whole palette of different nodes that you can drop in This is an example of a program. I've got on my Raspberry Pi that reads from some sensors and then it's actually using the node red modules that talk to homekit, which is the Apple Automation sort of framework so I can Get it on my phone and I can hit a button and it will actually talk directly to those senses that are hooked up to my Raspberry Pi or get readings from them and display them on my phone all using the magic of node red Which I don't have time to go into right now, but very very cool stuff And I highly recommend that you check it out. I have a video all about that That's on the IBM developer works blog. So if you're really interested in that project, you can follow that video All right, so that's pretty much how to get started with robotics in JavaScript Using Johnny five If you want to find out more my blog are sometimes Sporadically post projects there so you can check out some of the things that I'm building on crafty.com Johnny five.io is the main library a website for Johnny five. So lots of examples there I also have a website called node ARDX where I've got some Johnny five examples that are tailored for Arduino particularly for the getting started kits that you get from places like J car but Yeah, it's through Arduino. So if you're interested in Raspberry Pi I just recommend that you use the Johnny five website. So That was a super quick Introduction to JavaScript robotics. So if anyone has any questions, I'm around happy to answer questions And if you've got some hardware you want to play with happy to help. Thank you