Linux Format

Build a robot

Les Pounder runs through how to build a simple but elegant, budgetbust­ing custom robot for scaring the cat.

-

The Python and hardware you need to take your first step onto the robot building ladder.

Robotics is an exciting way to introduce people to programmin­g 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 significan­t 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 (www.raspberryp­i.org/downloads), a Wi-Fi dongle and Pi connected to your home router; a USB battery pack, a robot chassis kit (http://bit.ly/LXF203_Robot_kit), an L298N motor controller (http://bit.ly/LXF203_L298N); 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 or a local hackspace.

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 accordingl­y 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. A great reference for the GPIO is http://pi.gadgetoid.com/ pinout 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:

GPIO.setup(item, GPIO.OUT)

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): GPIO.output(fwdright, True)

GPIO.output(fwdleft, True)

time.sleep(i) GPIO.output(fwdright, False) GPIO.output(fwdleft, False)

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): GPIO.output(revright, True)

GPIO.output(fwdleft, True) time.sleep(i)

GPIO.output(revright, False) 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 at http://bit.ly/LXF203_Robot.

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

try:

print("R EA D Y") except KeyboardIn­terrupt: print("E XI T")

GPIO.cleanup()

This will print RE A D Y when the code is executed, but if we press CTRL+c it will print EX 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!

 ??  ?? The L298N board is packed full of components. The screw terminals enable connection­s between the batteries, Raspberry Pi and motors. There are more images in our repository (http://bit.ly/LXF203_Robot).
The L298N board is packed full of components. The screw terminals enable connection­s between the batteries, Raspberry Pi and motors. There are more images in our repository (http://bit.ly/LXF203_Robot).
 ??  ?? Our finished robot is simple yet elegant. Its utilitaria­n design enables easy access to all of the components for any last minute tweaks or fixes.
Our finished robot is simple yet elegant. Its utilitaria­n design enables easy access to all of the components for any last minute tweaks or fixes.

Newspapers in English

Newspapers from Australia