I recently spent the day at Houston University for Node Bots day. Throughout the course of the day I learned all about building and controlling a robot with the power of NodeJS, which is something I dind’t even know was possible until recently.
Experimenting with the Tessel
We were provided with a starter kit that had all of the components needed to start building. It included a Tessel 2, an ActoBotics Peewee, battery packs, and a set of miscellaneous components for experimenting with. The first half of the day was spent placing components on a proto-board and writing some code to make them work.
The Tessel IO and Johnny Five libraries are what we used to allow NodeJS to interface with the Tessel 2. With these available all you need to do is import the libraries and then call the board constructor.
For an LED all you need to do is define the pin placement when calling the constructor, and then tell it to blink when the board is considered ready using
You can even interface with physical buttons, for exmaple you can make a button turn on an LED when it’s pressed.
Building the Robot
Once we were done experimenting we moved on to building the robot. We watched a video on how to assemble the chasis and then got straight to work. The robot at this point has been dubbed “Montezooma”.
Once the physical assembly was completed I began writing some code with the help of some examples provided by the event organizers. The Tessel 2 transmits a wireless access point you can connect and configure, so the goal was to create a simple web app that would allow the robot to be controlled when connected to it. Using Express and Socket IO made this quite simple, all I needed to do was emit some events from my Front-End to fire a function in my NodeJS app.
Within my NodeJS code I imported Johnny Five, Tessel IO, and Socket IO. I then setup my board and some Express routes. Because this robot uses DC motors it was necessary to call a constructor that would create two motors so we could control each of them individually.
board.on('ready') I added some control sockets. These look for named events to be emitted, and then fire a function in their second argument. For example when the Front-End emits
moveForward function gets fired within NodeJS.
mouseup occurs another event gets emitted called
stopMotors, which stops the motors.
I wanted my robot to be able to switch speeds with the use of a slider. For this I created a
settings module, the goal of this was to hold on to the current speed and return it if
settings.getSpeed() is called, or update it if
settings.setSpeed(value) is called.
Within my Front-End code I setup an event listener that watched for changes to the slider, and then emitted a Socket event with the value of it.
I then made another listener for the event and passed the speed value into it. I call upon
settings.setSpeed() to update the value passed from the Front-End.
I can then update my robot movement functions to grab the speed value from the settings before telling the motors to do anything.
We had a great day and learned a quite a lot about NodeJS robotics, we’re already talking about our next build. Special thanks to the NodeJS Houston community for putting on such an awesome event.