2015-11-14


How to build a robot

Robotics is an exciting way to introduce people to programming but it can also be a little difficult sometimes for newcomers to get to grips with as well as being expensive.

Enabling anyone to create an easy to build and cost-effective robot is a significant step in their learning. So in this tutorial we shall build our own robot and create a Python 3 library that enables anyone to control it.

For this project you will need: any model of Raspberry Pi; Raspbian, a Wi-Fi dongle and have the Raspberry Pi connected to your home router; a USB battery pack, a robot chassis kit, an L298N motor controller; four AA batteries and some Blu-tack.

Building a robot chassis is a great activity and the kit (mentioned above) comes with everything that you need to get started. You will need to solder the red and black wires to the motor terminals, if you can't solder then now is a great time to learn from a friend.

Soldering

In this project we bought a robot chassis kit from eBay that included two DC motors. These motors come assembled but require soldering two wires to the terminals for power. Soldering is an essential maker skill and it is really easy to learn, though adult supervision is essential for our younger would-be solders out there.

There are many YouTube tutorial videos, but the best is from Carrie Anne Philbin (below).

YouTube : https://www.youtube.com/watch?v=P5L4Gl6Q4Xo

Soldering irons sets can be bought for around £10, but a good example is the Antex XS25 for around £25, which is a great starter to intermediate soldering iron.

Soldering should be undertaken in a spacious, well-ventilated room with a clear workspace. Soldering is great fun and your local hackspace/LUG can help you to learn in a safe manner.



With the chassis built, we now focus on the motor controller which is an L298N H bridge controller. An H bridge enables a motor to go forwards and backwards. Our L298N has two outputs for our motors, the left side is served by OUT1 and 2, the right by OUT3 and 4.

Connect the wires from your motors to these terminals and ensure they are secure. Our AA battery pack connects to +12V and GND terminal.

We also need to connect one of the GND from our Raspberry Pi to the L298N GND terminal. On the L298N we can see four pins marked IN1 to IN4. These are inputs that we use to connect the L298N to our Raspberry Pi GPIO (General Purpose Input Output) pins.

By turning a GPIO pin on or off we can trigger the input pins accordingly and control the motor direction. We connected our inputs to the following GPIO pins: IN1 to 17, IN2 to 22, IN3 to 18 and IN4 to 23. We used the Broadcom pin mapping, a standard set by the Raspberry Pi Foundation.

Check out this great reference for the GPIO which explains Broadcom pin mapping.

Software setup

Boot your Raspberry Pi to the desktop and open a terminal, you can find the icon in the menu bar at the top left corner of the screen.



In the LXTerminal type the following and press Enter to run:

$ sudo raspi-config

Using the arrow keys navigate to Advanced Options and press Enter. In the Advanced menu navigate to the SSH Server option, press Enter and in the new screen choose to Enable the SSH server.

Exit from the menus and reboot your Raspberry Pi. Reboot back to the desktop and open another LXTerminal and type the following for your IP address and write the address down:

$ hostname -I

In the same terminal type the following to launch the Python 3 editor with superuser powers:

$ sudo idle3 &.

We'll start our code by importing two libraries, the first enables our code to talk to the GPIO pins on our Raspberry Pi while the second provides the time library:

import RPi.GPIO as GPIO

import time

When using the GPIO pins we will refer to them using their Broadcom pin numbering and we must, in turn, configure our code to use those numbers with GPIO.setmode(GPIO.BCM).

Rather than refer to each pin throughout our code we shall create four variables to store the GPIO pin connected to each of the inputs on the L298N:

fwdleft = 17

fwdright = 18

revleft = 22

revright = 23

In order to use each GPIO pin we need to instruct the code what each pin will be: an input or output. As we will be sending current from the GPIO pins they will be an output.

So using a list, known in other languages as an array, and a for loop, we shall iterate over each item in the list, which are our variables, and configure each GPIO pin as follows:

motors = [fwdleft,fwdright,revleft,revright]

for item in motors:

[Tab] GPIO.setup(item, GPIO.OUT)

Not that where we type '[Tab]' hit the Tab key on your keyboard to make the code indented for that line - this is important when using Python.

Driving our robot

We now create four functions that will handle driving our motors in a particular direction. Each of the functions will take an argument, a duration of time that's expressed as an integer or a float:

def forward(i):

[Tab] GPIO.output(fwdright, True)

[Tab] GPIO.output(fwdleft, True)

[Tab] time.sleep(i)

[Tab] GPIO.output(fwdright, False)

[Tab] GPIO.output(fwdleft, False)

Remember that where we've put '[Tab]' just press the Tab key on your keyboard to indent the line. Our first function, forward(i) , will turn on fwdright and fwdleft pins and then wait for the value of i , our argument before turning the motors off.

On to our second function:

def right(i):

[Tab] GPIO.output(revright, True)

[Tab] GPIO.output(fwdleft, True)

[Tab] time.sleep(i)

[Tab] GPIO.output(revright, False)

[Tab] GPIO.output(fwdleft, False)

Our second function, right(i) , spins our robot on the spot in a clockwise direction for the duration provided as the argument (i) . To turn right we set the right motor to reverse and the left motor to forwards, wait for the user defined number of seconds and then turn off the motors.

For our left and reverse functions you can refer to the full code.

The last section of code is a try and except test:

try:

[Tab] print("R E A D Y")

except KeyboardInterrupt:

[Tab] print("E X I T")

[Tab] GPIO.cleanup()

This will print R E A D Y when the code is executed, but if we press CTRL+c it will print E X I T and then clean up the GPIO pins ready for use by another project:

Save your code as robot.py but we won't be running the code, rather we will now create a new file and save it as test. py in the same directory as robot.py.

Next, we'll import our robot.py code and use the functions inside of it to control our robot.

import robot

robot.forward(1)

robot.right(2)

robot.left(2)

robot.reverse(1)

Save the code and click Run > Run Module to test. Remember to pick up the robot before pressing Enter or you will have to chase after it!

Enjoyed this article? Expand your knowledge of Linux, get more from your code, and discover the latest open source developments inside Linux Format. Read our sampler today and take advantage of the offer inside.

The post How to build a robot with Raspberry Pi appeared first on Nexttac Technology.

Show more