Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Raspberry Pi Robotic Blueprints

You're reading from   Raspberry Pi Robotic Blueprints Utilize the powerful ingredients of Raspberry Pi to bring to life your amazing robots that can act, draw, and have fun with laser tags

Arrow left icon
Product type Paperback
Published in Oct 2015
Publisher
ISBN-13 9781784396282
Length 198 pages
Edition 1st Edition
Concepts
Arrow right icon
Toc

Table of Contents (9) Chapters Close

Preface 1. Adding Raspberry Pi to an RC Vehicle FREE CHAPTER 2. Adding Raspberry Pi to a Humanoid Robot 3. Building a Tracked Vehicle That Can Plan Its Own Path 4. Building a Robot That Can Play Laser Tag 5. A Robot That Can Draw 6. A Robot That Can Play Air Hockey 7. A Robot That Can Fly 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:

Controlling the RC car using Raspberry Pi in Python

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:

Controlling the RC car using Raspberry Pi in Python

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:

Controlling the RC car using Raspberry Pi in Python

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.

You have been reading a chapter from
Raspberry Pi Robotic Blueprints
Published in: Oct 2015
Publisher:
ISBN-13: 9781784396282
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.
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 $19.99/month. Cancel anytime
Banner background image