Sunday, May 18, 2014

My first working robot, It’s Alive

Over the weekend I successfully connected my BeagleBone Black to a Dagu Rover 5 Tracked Chassis using the Rover 5 motor driver board.  I was then able to write Python client/server scripts that allowed me to controller the robot over a Bluetooth RFComm connection.  You can see the video of my first successful test drive below.  The next few posts will document the steps it took to get this working.

Here is the video of my first successful test drive.  For those of you thinking that this is actually a remote controller vehicle and not a robot, you are right but this is the pretty huge step towards my goal of creating a robot because I want my robot to be mobile.


This post will describe how to connect the BeagleBone Black to the Motor driver board and the Rover 5 Tracked Chassis.   I am assuming that you have the basic understanding of the BeagleBone Black expansion headers as described in my “Playingwith LEDs” post and have set the Adafruit_BBIO Python library as described in my “I just got myBeagleBone Black, now what?” post.

Connecting the Motor Controller to your Tracked Chassis:
The chassis I choose is the Rover 5 Tracked Chassis that contains a battery holder, two DC motors and a quadrature encoder for each tread.  For the motor controller I chose the four channel Rover 5 Motor Driver Board. As you can probably tell by the names, these are kind of designed to work together which makes it a lot easier for a beginner like myself.  Below are pictures of the motor controller and the chassis.




In this section we will be connecting the encoder and the power for the motor from the chassis to the motor controller.  From each motor housing, there is a think black power cable for the motors and four small wires, each with female connectors, which go to the encoder.  These connectors are:
RED:  +5V for the encoder
BLACK:  Ground
WHITE:  Input signal A
YELLOW:  Input signal B
To connect the Chassis to the motor driver board, we connect the thick motor power cable from the motor housing to the motor output connector on the motor controller.  We will then connect the four encoder cables to the encoder connections on the motor driver board.  Make sure you do not cross up the red (5V) and the black (ground) wires as this could damage the encoders.  This image shows how I made the connections.



Connecting the BeagleBone Black to the Motor Controller:
Now that we have the chassis connected to the motor controller, lets connect the BeagleBone Black to the motor controller.  Remember to disconnect the power to the BeagleBone Black prior to connecting it to the motor controller.  We will want to connect two PWM pins and two digital pins from the BeagleBone Black to the motor controller.

We will be connecting PWM pins 13 and 19 from the P8 expansion header and the digital pins 14 and 16 also from the P8 expansion header to the motor controller.  The image below shows a close up of the BeagleBone Black with the yellow wires connected to the PWM pins and the green wires connected to the digital pins.

I am running PWM pin 13 and digital pin 14 to one encoder and PWM pin 19 and digital pin 16 to the other encoder.  You can see how the wires are connected here:


Running Power to the Motor Controller:
Now lets connect power to the motor controller.  We have two power inputs on our motor controller.  One input is for +5V for logic and the other is to power the motors.  The motor controller is rated for a maximum motor supply voltage of 12V.  I am using the 6AA battery holder that came with the chassis to supply the motor power.  Note:  Do not put the batteries in the battery holder until everything is connected.
On the motor controller board, one of the power inputs is labeled “Vbat” and the other is labeled “VCC”.  We will connect the 6AA battery holder to the power input labeled “Vbat”.   We will then run 5V (pin 7 of expansion header P9) and ground (pin 1 of expansion header P9) from the BeagleBone Black to the power input label “VCC” on the motor controller.
Now we can put the batteries in the battery holder and power up the BeagleBone Black.

Testing the Connection:
Now that we have everything connected and powered up, lets see if we have everything connected correctly.  Lets create a script called rovertest.py and add the following code to it:
#!/usr/bin/python

import Adafruit_BBIO.PWM as PWM
import time

pin = "P8_13"

PWM.start(pin,0)
for i in range(0, 100):
              PWM.set_duty_cycle(pin,75)
              time.sleep(.1)

PWM.stop(pin)
PWM.cleanup()

We need to make sure that the chassis is not on the ground when we run the script because we do not want the chassis to actually move since the BeagleBone Black is still connected to the computer.  What I did was to type in “python rovertest.py”, picked up the chassis and then pressed enter to run the script.  If everything is connected correctly, one of the tracks should run for 10 seconds and then stop.  To test the other track, change the ‘pin = “P8_13”’ line to ‘pin = “P8_19”’ and rerun the script.

The next post will cover how to connect and configure a Bluetooth adapter with the BeagleBone Black running Angstrom Linux.  This will give us the ability to control our robot remotely so we do not need to have our robot connected to a computer.

Part 2:  http://myroboticadventure.blogspot.com/2014/05/my-first-working-robot-its-alive-part-2.html
Part 3:  http://myroboticadventure.blogspot.com/2014/05/my-first-working-robot-its-alive-part-3.html

Monday, May 12, 2014

Playing with buttons

I should be getting my Rover 5 Motor Driver Board to go with my Rover 5 Chassis within the next day or so.  This means I can start (for the third time) building my first robot.  Hopefully this time I will have more success then my first two attempts.  Before the new board arrives I wanted to write a post that will show how to use Python with the Adafruit library to read the state of a button.

What we need:
For the experiments in this post we will need a breadboard, a push button, four solder-less jumpers and a 10K ohm resister.  All of these parts come with Make’s Getting started with the BeagleBone Black kit that I mention in my Introduction post.  The 10K ohm resistor is the resister with the brown-black-orange-(gold or silver) bands.  If you want to find out the value of your resistors but you are not familiar with the resistor color codes, I would recommend using this digikeypage.

Our BeagleBone:
Rather than rehashing the information about the BeagleBone’s Expansion Headers here, I recommend that you read the “Our BeagleBone” section of my last post “Playing with LEDs” if you are not familiar with the expansion headers.

The Breadboard:
Lets jump right in and connect our external components to our breadboard.  Keep in mind, that whenever you connect items to your BeagleBone Black, it is a good idea to disconnect the power to the BeagleBoard first.

Using a solder-less jumper, connect the ground rail of your breadboard to pin 1 of the P9 expansion header.  Then use another solder-less jumper to connect the power rail of your breadboard to pin 3 of the P9 expansion header.  This will connect your breadboard to both power and ground.

Now lets add the push button to the breadboard.  You will want the button to straddle the middle section as show in the image below.  Using a solder-less jumper, connect the power rail of your breadboard to one end of the button.  Next connect the same end of the button to the ground rail of your breadboard using the 10K pulldown resistor.  Finally connect the other end of the button to pin 11 of the P8 expansion header.  The connections should look like the following image but hopefully you did a neater job hooking up the wires then I did.



Power up and writing code:
New lets power up our BeagleBone and write some code to monitor our button.  This first example can be used to check the current state of the button.  Create a file called “button.py” and add the following code.

#!/usr/bin/python

import Adafruit_BBIO.GPIO as GPIO
import time

GPIO.setup("P8_11", GPIO.IN)
while True:
                            if GPIO.input("P8_11"):
                            print "The button was pressed"
          time.sleep(.01)

Now run the script with this command:

python button.py

If everything is connected correctly, when you press the button you should see “The button was pressed” message appearing on the screen.  If you hold the button down you will see lots of “The button was pressed” messages.

The script starts off by importing the Adafruit_BBIO.GPIO and time libraries that are needed.  The next line sets pin 11 on expansion header P8.  You saw in the last post “Playing with LEDs” that when we set the pin to GPIO.OUT we wrote data to the pin.  For this post we want to read data from the pin so we set the pin to GPIO.IN.

We then create an endless loop and within that loop we check pin 11 of expansion header P8 using the GPIO.input function.  This function returns true if the button is pressed and false if the button is not being pressed.  We then pause for one-tenth of a second and check again.

This script works great if you want to check the current state of a button but what if we wanted to wait for the button to be pressed.  We could create an endless loop and then break out of it when the GPIO.input() function returned true but that is kind of a messy if you ask me.  Luckily there is a better way.  Try this next script; it waits for the status of the button to reach a certain state and them moves on.

#!/usr/bin/python

import Adafruit_BBIO.GPIO as GPIO
import time

GPIO.setup("P8_11", GPIO.IN)
while True:
              GPIO.wait_for_edge("P8_11", GPIO.RISING)
              print "Button Pressed"
              GPIO.wait_for_edge("P8_11", GPIO.FALLING)
              print "Button Released"

The GPIO.wait_for_edge() function waits for the state of the button to change.  In this script we wait for the button to be pressed (GPIO.RISING) and then waits for the button to be released (GPIO.FALLING).  You could also use GPIO.BOTH to wait for any change in the button’s state.


Hopefully my motor driver board will arrive in the next day or so and I can begin trying to get a robot working.  Whether the project is a success or another failure, I will write a post explaining what I did and how it worked.

Sunday, May 11, 2014

Playing with LEDs

Now that I had my BeagleBone Black setup as documented in my first post, I wanted to start building ROBOTS.  This is where I wasted a lot of time and money because I really did not understand how the BeagleBone Black worked or what I really needed.  So rather than diving right into building robots, lets spend a little time trying to understand how we can program the BeagleBone Black to control an external component like an LED (Light-Emitting Diode).

For the experiments in this post you will need a breadboard, an LED, three solder-less jumper cables and one 100 ohm resister.  All of these parts come with Make’s Getting startedwith the BeagleBone Black kit that I mention in my Introduction post.  The 100 ohm resister is the resister with the Brown-Black-Brown-(gold or silver) bands.  If you want to find out the value of your resistors but you are not familiar with the resistor color codes, I would recommend using this digikeypage.

Our BeagleBone:
In this section we will take a look at our BeagleBone Black and explain how we would connect external components to it.  If you look at this image from the BeagleBone Black’s documentation, you will see that our BeagleBoard Black has two expansion headers, they are the black connection strips on either side of the board.  


If you look closely at the board itself, you will see that each expansion header is labeled with its name (P8 or P9).  You will also notice that one end of the expansion header is labeled with a 1 and a 2 while the other end is labeled with a 45 and 46.  These labels represent the pin numbers of the headers. 

Now lets look at the Cape expansion headers image that is also from the BeagleBone Black documentation.  This image shows you how each pin is labeled and also which pins connect to power and ground.



For our first LED projects, we will want to use one of the digital I/O pins because all we want to do is to turn the LED on or off.  This next image, once again from the BeagleBone Black documentation, shows the possible digital I/O pins.



For our first LED experiment we will be using pins 1 and 3 on expansion header P9 to supply the ground and power respectively to our breadboard.  We will also use pin 8 (labeled GPIO_67) on the P8 expansion header for our digital I/O that will control the LED.

The Breadboard:
Now lets connect our external components to our breadboard.  Whenever you connect items to your BeagleBone black, it is a very good idea to disconnect the power first. 

Using a solder-less jumper, connect the ground rail of your breadboard to pin 1 of the P9 expansion header.  Then use another solder-less jumper to connect the power rail of your breadboard to pin 3 of the P9 expansion header.  This will connect your breadboard to both power and ground.

Now lets add the LED to our breadboard.  Connect the cathode end of the LED (shorter wire) to the ground rail of our breadboard and then connect the anode end of the LED (longer wire) to one of the rows on our breadboard.

Now take the 100 OHM resistor and connect one end to the row on the breadboard that the LED is connected to and the other end of the resistor to another row on the breadboard.  Finally run a solder-less jumper from pin 8 of the P8 expansion header to the row that the 100 OHM resistor is connected too.

The connections should look similar to the following image but hopefully you were neater hooking up the wires then I was.


Power up and writing code:
Now lets power up our BeagleBone.   The recommended way to program the BeagleBone Black is to use Bonescript which is an extension of Javascript however I will be using Python with the Adafruit_BBIO library more throughout this blog.  Once I get better with bonescript, maybe I will start using it more.  If you following the Setup Python section of my last post you have the Adafruit_BBIO library setup, if not you will need to setup Adafruit_BBIO before going any further.

I would recommend creating a directory to keep your python scripts in, this way they are not scattered throughout your system.  In your scripts directory create a file called “ledon.py” and add the following code:

#!/usr/bin/python
import Adafruit_BBIO.GPIO as GPIO
import time

GPIO.setup("P8_8",GPIO.OUT)
GPIO.output("P8_8", GPIO.HIGH)
time.sleep(5)

Now run the script with this command:

python ledon.py

If everything is setup correctly, the led will light up for five seconds and then shutoff.  Lets take a look at how this short script works.  We start off by importing the Adafruit_BBIO.GPIO and time libraries that are needed.  The next line sets up pin 8 on expansion header P8.  GPIO.OUT defines that we want to write to this pin (output data).   If we wanted to read the pin (as if we had a button connected), we would use GPIO.IN like this:  GPIO.setup(“P8_8”,GPIO.IN).  We then set pin 8 to GPIO.HIGH, which turns the LED on (GPIO.LOW would turn the LED off).  Finally we wait five seconds and the script then ends which resets the pin turning the LED off.

If we set pin 8 to GPIO.HIGH to turn it on, then setting the pin to GPIO.LOW should turn the LED off.  Using this assumption lets create a script that will cause the LED to blink on and off.

#!/usr/bin/python
import Adafruit_BBIO.GPIO as GPIO
import time

GPIO.setup("P8_8",GPIO.OUT)
while True:
              GPIO.output("P8_8", GPIO.HIGH)
              time.sleep(.5)
              GPIO.output("P8_8", GPIO.LOW)
              time.sleep(.5)

If you save and run this script, your LED should blink on and off indefinitely until you stop the script.  From here you can experiment some more with your LED.  I am wondering if I can use something like this next winter to control my Christmas lights.  Maybe turn certain lights on or off at various time intervals.

Turning a LED on and off is a pretty cool first step but how about having the LED slowly get brighter.  Can we do this?  Yes we can.  We can use one of the PWM pins for analog writes.  The PWM pins are shown in this image that also comes with the BeagleBone Black documentation.



Power down your BeagleBone black and move the jumper from pin 8 of the P8 expansion header to pin 13.  Then run this script:

#!/usr/bin/python
import Adafruit_BBIO.PWM as PWM
import time

PWM.start("P8_13",0)
for i in range(0, 100):
              PWM.set_duty_cycle("P8_13",float(i))
              time.sleep(.1)

PWM.stop("P8_13")
PWM.cleanup()

Your LED will slowly get brighter until it reaches full brightness and then shuts off when the script ends. 


In my next post, I will show how we can read the state of a button with python and the Adafruit_BBIO library.  In the mean time have fun playing with the LED.

I just got my BeagleBone Black, now what?

When I received my BeagleBone, I wanted to start build my Robot right away but I quickly realized that I needed to start off by setting up my BeagleBone and configuring the Linux Distribution.  You can install several different distributions on your BeagleBone but I would recommend starting with standardAngstrom distribution.  Once you are more familiar with your BeagleBone then you can try other distributions.  When you first login into the standard Angstrom distribution, you will login as the root user with no password.

A great place to start is BeagleBone’s Getting started page.  This page walks you though the initial setup, installation of the drivers on your local computer and flashing the latest software onto your BeagleBone.  This page documents these steps very well so I will not rehash them here but I did have one problem when I went though this page.   The problem was I have an Ubuntu laptop and a MacBook Pro but no Windows machine to write the image to the SD card as documented on the getting started page.  After a brief Google search, I found Adafruit’spage that walked me though using PiFiller to write the image to a SD card.  The total time for this setup was approximately two and a half hours for me so patience is needed. 

I connected my BeagleBone black to an old monitor I had lying around the house by using a micro HDMI to VGA converter.  I also bought a mini wireless keyboard with a trackpad so I was able to program my BeagleBone like a desktop computer.  While this is not necessary, it makes learning the BeagleBone a lot easer.  I used Cable Matters Active Micro HDMI to VGA Male to Female Adapter with 3 Feet USB Power Cable in Black  and Logitech Wireless Touch Keyboard K400 with Built-In Multi-Touch Touchpad  they both worked out of the box with no configuration.  I am also using a powered USB hub to connect and power USB devices.  I can also use the USB hub to power my BeagleBone.  The USB hub that I am using is the Belkin Ultra-Slim Series 7-Port USB 2.0 Hub .

Here is a picture of my rather messy setup.  

The USB hub is plugged into a wall outlet and the white cable from the hub goes into the USB port of the BeagleBone.   Below the white cable is the mini HDMI cable that is connected to the mini HDMI to VGA converted.  The USB hub also powers this converter.  The yellow cable, on the opposite side of the BeagleBone, is an Ethernet cable that connects the BeagleBone to my network (and the Internet).  The black cable next to the Ethernet cable goes from the USB client port to the USB hub to power the BeagleBone.  The USB client cable can also connect directly to your computer instead of the USB hub.  The dongle you see connected to the USB hub is for the Logitech keyboard.

If you do not want to connect your BeagleBone to a monitor and keyboard, you can access your board by connecting it to your computer’s USB port.  You could then use a SSH client to connect to your BeagleBone.  If you have a Windows machine you will need to download a SSH client like Putty.  If you have a Linux or Mac machine, you already have a SSH client installed.  If your BeagleBone is connected to the USB port on your local machine and the drivers are properly installed you can SSH to root@192.168.7.2 to access your BeagleBone.

Now lets spend a little bit of time setting up our Linux environment.  Note, To download and install new software you will need to connect your BeagleBone Black to the Internet using the Ethernet port or by adding a WIFI adapter.

Setting a root password:
By default, the root user on your BeagleBone Black does not have a password.  Obviously this is not good so lets set a password by running the following command:

passwd

Setting up NTP:
Now we will want to setup NTP (Network Time Protocol) because the BeagleBone Black does not contain a Real Time Clock therefore it loses its time/date settings each time it powers off. 

Lets begin by setting the timezone, for me this is the Eastern timezone.  We will then set the correct time so we can use the opkg utility to install the ntp software:

ln –s /usr/share/zoneinfo/America/New_York /etc/localtime
ntpdate –u pool.ntp.org

opkg update
opkg install ntp

Now we need to configure NTP.  We will start off by editing the /etc/ntp.conf file.  Since I live in the US, I use the US servers.  You can go to www.pool.ntp.org to find your local servers.  My ntp.conf file looks like this:

driftfile /etc/ntp.drift
logfile /var/log/ntp.log

server 0.us.pool.ntp.org
server 1.us.pool.ntp.org
server 2.us.pool.ntp.org
server 3.us.pool.ntp.org

# Defining a default security setting
restrict default

Now lets enable the NTP services but running the following commands:

systemctl enable ntpdate.service
systemctl enable ntpd.service

Finally we need to modify the /lib/system/system/ntpdate.service file.  My ntpdate.service file looks like this:
[Unit]
Description=Network Time Service (one-shot ntpdate mode)
Before=ntpd.service

[Service]
Type=oneshot
ExecStart=/usr/bin/ntpd -q -g -x
ExecStart=/sbin/hwclock --systohc
RemainAfterExit=yes

[Install]
WantedBy=multi-user.target

Now your BeagleBone Black will have the correct time when you start it up, if it is connected to the Internet.

Setting the terminal prompt:
We will be doing a lot of work from the Linux terminal and the default terminal prompt does not tell you much.  Lets set the terminal prompt so it shows the logged in user, machine and the directory names.  To do this, edit the /etc/profile file and add the following line at the end of the file:

export PS1="\u@\h \w> "

Setup Python:
We will be making extensive use of Python and the Adafruit library.  Python is already installed and setup but we need to install the Adafruit library.  To do this, we need to first install some Python utilities:

opkg install python-pip python-setuptools python-smbus

Now we can use the Python package management tool to install the Adafruit library.

pip install Adafruit_BBIO

Browse Local Documentation:
If you installed the standard Angstrom image, your BeagleBone will have a local web server running that contains a lot of useful information.  I would recommend reviewing the information on this page and going though some of the basic demos.  I would also recommend printing out the Hardware Documentation section that contains the layout of the expansion headers.  The reason for this is you will want to power off the BeagleBone when you are connecting items to the expansion headers and will not have access to the images showing the layout.

To access the local documentation when the BeagleBone is connected to the USB port on your local machine, open a web browser and go to 192.168.7.2.

If your BeagleBone Black is connected to your local network, you can put the IP Address of your BeagleBone Black into your web browser to see the documentation.

Beginning some projects:
Your BeagleBone is now setup and ready to go.  As I mentioned earlier, you will probably want to try some of the demos in the local documentation.  My next few posts will show some basic projects that have helped me learn how to connect items to the expansion headers and how use the Adafruit library with Python.