Reader small image

You're reading from  Raspberry Pi Robotic Projects - Third Edition

Product typeBook
Published inOct 2016
Reading LevelBeginner
Publisher
ISBN-139781786467966
Edition3rd Edition
Languages
Concepts
Right arrow
Authors (2):
Richard Grimmett
Richard Grimmett
author image
Richard Grimmett

Dr. Richard Grimmett has been fascinated by computers and electronics from his very first programming project, which used Fortran on punch cards. He has bachelor's and master's degrees in electrical engineering and a PhD in leadership studies. He also has 26 years of experience in the radar and telecommunications industries, and even has one of the original brick phones. He now teaches computer science and electrical engineering at Brigham Young University, Idaho, where his office is filled with his many robotics projects.
Read more about Richard Grimmett

Jon Witts
Jon Witts
author image
Jon Witts

Jon Witts has been working within the IT industry since 2002 and specifically within Educational IT since 2004. He was introduced to Linux back in 2001 through his collaboration with two German artists who were visiting the arts organisation he was then working with. Having studied Fine Arts and Educational Technology and sought to innovate with open and accessible digital technologies within his creative practice, Jon is happiest when deconstructing technology and finding its limits. Jon has embedded within his school the use of Raspberry Pi computers, as an integral part of the delivery of the school's Computer Science curriculum as well as to run various school clubs and projects. Jon is a Raspberry Pi Certified Educator and also helps to organise and run the Hull Raspberry Jam events. I would like to thank my wife, Sally and our three daughters for putting up with all the cables and compoents around the house, and not least for being so tolerant of the need to dodge the robots racing round the kitchen floor!
Read more about Jon Witts

View More author details
Right arrow

Chapter 5. Creating a Robotic Hand with the Raspberry Pi

Now that you are becoming something of an expert at building amazing projects with the Raspberry Pi, this project will provide you with the knowledge to begin to build your own humanoid robot. You'll start with perhaps the most interesting body part; the hand.

To do this, you will learn the following things:

  • How to use Raspberry Pi to control servos that will control a robotic hand

  • How to add a USB webcam to the project to sense your hand

  • How to use OpenCV(Open Source Computer Vision), an open source image processing library, to determine the position of your hand, and then move the robotic hand accordingly

Creating the hardware platform


In this chapter, you'll build a human hand that has four fingers, a thumb, and a rotating wrist. There are actually several possible robotic hand configurations that you can purchase or build yourself. If you'd like to purchase an already 3D printed hand, my personal favorite is the hand that was designed by Christopher Chappelle and Easton LaChappelle available already 3D printed at http://www.shapeways.com/product/Z5CZ2RKLY/3d-printed-hand-right?li=search-results-1&optionId=42512474.

Here is an image of the hand:

If you have access to a 3D printer, you can also download and print the hand yourself. Here is the link: https://www.thingiverse.com/thing:288856. Assembling the hand is quite simple; follow the instructions at http://www.shapeways.com/product/Z5CZ2RKLY/3d-printed-hand-right in the instruction image. Once you have assembled the hand, you'll need to add the servos to control the hand and the wrist. To control the hand, you'll need to pull on five...

Moving the hand


In order to move the servos, you can use a servo controller similar to the one introduced in Chapter 3, Building a Wall-E Robot, to control six servos that will control these fingers, thumb, and wrist. As in Chapter 3, Building a Wall-E Robot, the servo controller you are going to use for this project is a simple servo motor controller utilizing USB from Pololu. Since you only need to control five servos, you can order the six servo controller version available at http://www.pololu.com.Here is an image of the unit:

Make sure you order the assembled version. This piece of hardware will turn USB commands from the Raspberry Pi into signals that control your servo motors. There are two connections that you'll need to make to the servo controller to get started; the first to the servo motors, the second to a power source.

First, connect the servos to the controller. In order to be consistent, let's connect your six servos to the connections marked 0 through 5 on the controller...

Connecting the servo controller to the Raspberry Pi


Now that you've checked the servo motor controller and the servos, you'll need to connect the servo controller up to the Raspberry Pi and make sure you can control the servos from it. Remove the USB cable from the PC and connect it to the Raspberry Pi.

Let's now focus on the motor controller by downloading the Linux code from Pololu at www.pololu.com/docs/0J40/3.b. Here are the steps to do so:

  1. First, log on to your Raspberry Pi by putty, then type wget.http://www.pololu.com/file/download/maestro-linux-100507.tar.gz?file_id=0J315.

  2. Move the file using mv maestro-linux-100507.tar.gz\?file_id\=0J315 maestro-linux-100507.tar.gz.

  3. Unpack the file by typing tar -xzfv maestro-linux-100507.tar.gz. This will create a directory called maestro_linux.

  4. Go to that directory by typing cd maestro_linux and then ls -l; you should see something like this:

The README.txt file will give you explicit instructions on how to install the software. This is basically...

Controlling your hand


You now know that you can talk to your servo motor controller and move your servos. In this section, you'll create a Python program that will let you talk to your servos to move them to specific angles.

Let's start with a simple program that sets the servos to one end of the range (which should open the hand) and then go the other end of the range (which should close your hand). This program starts with the code you wrote in Chapter 3, Building a Wall-E Robot. Here is the basic code to control the servos:

#!/usr/bin/python 
import serial 
import time 
def setAngle(ser, channel, angle): 
    minAngle = 0.0 
    maxAngle = 180.0 
    minTarget = 256.0 
    maxTarget = 13120.0 
    scaledValue = int((angle / ((maxAngle - minAngle) / (maxTarget - minTarget))) + minTarget) 
    commandByte = chr(0x84) 
    channelByte = chr(channel) 
    lowTargetByte = chr(scaledValue & 0x7F) 
    highTargetByte = chr((scaledValue...

Following your hand


It is essential to be able to see if you want to follow your hand. Fortunately, adding hardware and software for vision is both easy and inexpensive. As you learned in Chapter 2, Building Your Own Futuristic Robot, connecting a USB camera is very easy.

Most importantly, OpenCV and your webcam can track your hand position. OpenCV makes this amazingly simple by providing some high-level libraries that can help us with this task. First, follow the instructions in Chapter 2, Building Your Own Futuristic Robot, to install your USB webcam and OpenCV.

Then you'll create a set of code that looks like this:

#!/usr/bin/python 
import cv2 
import numpy as np 
import math 
 
cap = cv2.VideoCapture(0) 
cap.set(3, 360) 
cap.set(4, 240) 
 
while(cap.isOpened()): 
    ret, img = cap.read() 
    grey_image = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) 
    blur = cv2.GaussianBlur(grey_image, (5,5), 0) 
    ret, thresh1 = cv2.threshold...

Summary


You now have a hand that can move as your hand moves! By now you should have quite a few different capabilities that you can add to almost any project. In the next chapter, you'll add Raspberry Pi to a self-balancing, two-wheeled robot.

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Raspberry Pi Robotic Projects - Third Edition
Published in: Oct 2016Publisher: ISBN-13: 9781786467966
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at €14.99/month. Cancel anytime

Authors (2)

author image
Richard Grimmett

Dr. Richard Grimmett has been fascinated by computers and electronics from his very first programming project, which used Fortran on punch cards. He has bachelor's and master's degrees in electrical engineering and a PhD in leadership studies. He also has 26 years of experience in the radar and telecommunications industries, and even has one of the original brick phones. He now teaches computer science and electrical engineering at Brigham Young University, Idaho, where his office is filled with his many robotics projects.
Read more about Richard Grimmett

author image
Jon Witts

Jon Witts has been working within the IT industry since 2002 and specifically within Educational IT since 2004. He was introduced to Linux back in 2001 through his collaboration with two German artists who were visiting the arts organisation he was then working with. Having studied Fine Arts and Educational Technology and sought to innovate with open and accessible digital technologies within his creative practice, Jon is happiest when deconstructing technology and finding its limits. Jon has embedded within his school the use of Raspberry Pi computers, as an integral part of the delivery of the school's Computer Science curriculum as well as to run various school clubs and projects. Jon is a Raspberry Pi Certified Educator and also helps to organise and run the Hull Raspberry Jam events. I would like to thank my wife, Sally and our three daughters for putting up with all the cables and compoents around the house, and not least for being so tolerant of the need to dodge the robots racing round the kitchen floor!
Read more about Jon Witts