Book Image

Raspberry Pi Robotic Blueprints

Book Image

Raspberry Pi Robotic Blueprints

Overview of this book

The Raspberry Pi is a series of credit card-sized single-board computers developed in the UK by the Raspberry Pi Foundation with the intention of promoting the teaching of basic computer science in schools. The Raspberry Pi is known as a tiny computer built on a single circuit board. It runs a Linux operating system, and has connection ports for various peripherals so that it can be hooked up to sensors, motors, cameras, and more. Raspberry Pi has been hugely popular among hardware hobbyists for various projects, including robotics. This book gives you an insight into implementing several creative projects using the peripherals provided by Raspberry Pi. To start, we’ll walk through the basic robotics concepts that the world of Raspberry Pi offers us, implementing wireless communication to control your robot from a distance. Next, we demonstrate how to build a sensible and a visionary robot, maximizing the use of sensors and step controllers. After that, we focus on building a wheeled robot that can draw and play hockey. To finish with a bang, we’ll build an autonomous hexcopter, that is, a flying robot controlled by Raspberry Pi. By the end of this book, you will be a maestro in applying an array of different technologies to create almost any imaginable robot.
Table of Contents (14 chapters)
Raspberry Pi Robotic Blueprints
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Controlling the RC car using Raspberry Pi in Python


The hardware is ready, now you can access all this functionality from Raspberry Pi. First, install the library associated with the control board, found at http://www.monkmakes.com/?page_id=698. Perform the following steps:

  1. Run the command wget https://github.com/simonmonk/raspirobotboard2/raw/master/python/dist/rrb2-1.1.tar.gz—this will retrieve the library.

  2. Then run tar -xzf rrb2-1.1.tar.gz—this will unarchive the files.

  3. Type cd rrb2-1.1—this will change the directory to the location of the files.

  4. Type sudo python setup.py install—this will install the files.

Now you'll create some Python code that will allow you to access both the drive motor and the steering motor. The code will look similar to the following:

The specifics on the code are as follows:

  • import RPi.GPIO as GPIO: This will import the RPi.GPIO library, allowing you to send out a PWM signal to the front steering mechanism.

  • import time: This will import the time library, allowing you to use the time.sleep(number_of_milliseconds), which causes a fixed delay.

  • from rrb2 import *: This will import the rrb2 library, allowing you to control the two DC motors. The rrb2 is the library you just downloaded from GitHub.

  • pwmPin = 18: This will set the PWM pin to GPIO Pin 18, which is physically Pin 12 on the Raspberry Pi.

  • dc = 10: This will set the duty cycle to 10 percent on the PWM signal.

  • GPIO.setmode(GPIO.BCM): This will set the definition mode in the RPi.GPIO library to the BCM mode, allowing you to specify the physical pin of the PWM signal.

  • GPIO.setup(pwmPin, GPIO.OUT): This will set the PWM pin to an output so that you can drive the control circuitry on the steering.

  • pwm = GPIO.PWM(pwmPin, 320): This will initialize the PWM signal on the proper pin and set the PWM signal to 320 Hz.

  • rr = RRB2(): This will instantiate an instance of the motor controller.

  • pwm.start(dc): This will start the PWM signal.

  • rr.set_led1(1): This will light LED 1 on the motor controller board.

  • rr.set_motors(1, 1, 1, 1): This will set both the motors to move so that the vehicle goes in the forward direction. This command will allow you to set the motors to forward or reverse and set it at a specific speed. The first number is the speed of motor one and it goes from 0 to 1. The second numbers is the direction of motor one, 1 is forward and 0 is reverse. The third number is the speed of motor two, which also goes from 0 to 1, and the fourth number is the reverse and forward setting of the second motor, either 1 or 0.

  • print("Loop, press CTRL C to exit"): This will instruct the user how to stop the program.

  • while 1: This will keep looping until Ctrl + C is pressed.

  • time.sleep(0.075): Causes the program to wait 0.075 seconds.

  • pwm.stop(): This will stop the PWM signal.

  • GPIO.cleanup(): This will cleanup the GPIO driver and prepare for shutdown.

Now you can run the program by typing sudo python xmod.py. LED 1 on the control board should turn on, the rear wheels should move in the forward direction, and the steering should turn. This confirms that you have connected everything correctly. To make this a bit more interesting, you can add more dynamic control of the motors by adding some control code. The following is the first part of the python code:

Before you start, you may want to copy your python code in a new file, you can call it xmodControl.py. In this code you'll have some additional import statements, which will allow you to sense key presses from the keyboard without hitting the enter key. This will make the real-time interface seem more real time. The getch() function senses the actual key press.

The rest of this code will look similar to the previous program. Now the second part of this code is as follows:

The second part of the code is a while loop that takes the input and translates it into commands for your RC car, going forward and backward and turning right and left. This program is quite simple, you'll almost certainly want to add more commands that provide more ways to control the speed and direction.