Thursday, 24 August 2017

Building Our First Robot

Using the CamJam Robotics kit and a Raspberry Pi, the kids and I has started building our first programmable robot. As this was my first adventure with the Raspberry Pi, I kept the cost to a minimum and used a Pi Zero. If you are serious about getting into building and controlling hardware from a computer, I would really recommend starting with the full size Pi 3 model, as it will make setting up and prototyping experiments easier. You can read the post about my recommended Pi 3 set up for my recommendations on what to buy if you want the fully featured Pi. If you decide to start out with the cheaper Pi Zero, then see my post about the challenges of getting started with the Raspberry Pi Zero.

Here is a picture of the parts for our first robot:

This is what you can see in the picture:
  • USB Powerpack (portable battery to power the Pi)
  • Raspberry Pi zero (in a case)
  • USB Wifi dongle, and adapter to fit the mini USB port
  • CamJam Edukit #3: Robotics
  • 4 x AA NiMH rechargable batteries
  • Micro SD card (8GB or larger)
In order to initially configure the Pi zero, you'll also need a keyboard which can be plugged into the single micro USB port on the Pi, and a mini HDMI to HDMI adapter plus an HDMI lead to connect a TV or monitor. Once you have it configured you can remove the keyboard and display, and connect remotely over the WiFi network from another computer. This allows you to program your robot without having it connected to anything by cables, so it can roam freely around the room (or anywhere within range of your WiFi network). In addition to connect the robot interface board from the CamJam kit, you will need a male 40 way header which you'll have to solder onto the Pi zero.   See my post about on getting started with the Raspberry Pi Zero for full details of the initial set up. If you don't want to have to solder anything then you can build this same robot kit using a Pi 3 instead, which has both built in WiFi and the 40 way header already attached. (Since I wrote this, the Pi Zero W board has been released. This is like the Pi Zero but has Bluetooth and WiFi built in. You can also get a 'hammer in' header if you are not ready for soldering. See supplier links for details where to buy these).

After the initial set up of the Pi, you will have access to it from a computer over a wireless network and can start building the robotics kit. At this point I was able to get the kids involved, starting with opening the CamJam Edukit box to see what was inside. My children are aged 9 years (girl) and 6 years (boy). We built our robot using the box that the kit came in. The motors can be stuck onto the bottom of the box and the wires threaded through holes in the bottom of the box to attach to the interface board screw terminals. Then the Pi and the batteries can all go inside the box and it is ready to program and roam around the house. My children enjoyed sticking on the motors and threading the wires. I helped them connect it all up correctly. The worksheets for the CamJam kit (which you can download here) walk you through all of this.



The first step is to load a script to get the robot to simply move in a straight line. This is done by calibrating the scripts so that the motors run at the same speed. My children are not ready to be exposed to Python code yet, so I talked them through what we wanted to do, and asked them questions about how the robot was behaving as we calibrated the script. The code controls the motor speed by sending a pulse width modulated signal to the motors. This is a square wave and we are varying what percentage of the time the voltage is high compared to zero in the waveform. 100% high would be sending full power to the motors all the time. 50% high would send half the power, making the motors run at a slower speed. So we started with 50% and saw that the robot drove along a curved path. At this point I asked the kids why it was not going in a straight line, and nudged them towards the answer that one motor was running faster than the other. I got them to work out which one was going faster. We then reduced the power slightly to that motor and run the script again so see what happened. After a few attempts the kids understood the affect the code changes were having (without having to look at the code) and we had some values which made the robot run more or less in a straight line. I later found that this varied depending on the floor surface. So in the kitchen it behaved slightly differently than on the carpet in the living room.

Next we looked at making the robot turn left and right, again asking the kids what directions we needed to drive the motors to make it turn. This made for a great first lesson for the kids, and a good introduction to programming in Python on the Pi for me. Away from the kids I then set about writing some functions to hide the lower level code so that we could look at some simple Python scripts calling functions to move the robot forward, and turn left or right. With these simpler scripts I was able to show my daughter some code and she could understand how to start to chain together commands to make the robot drive in a square around the kitchen on another day.

No comments:

Post a Comment