We got back from vacation and what was waiting for us?Our four new LV-MaxSonar-EZ2 Range Finders to give us a total of five sensors for our robot.This will give us one looking forward, two at
45 degrees and two at 90 degrees.The
pictures below show how we mounted them.You may also notice the nice table that our robot is sitting on.Kailey, my eight-year-old daughter, made it
for me this afternoon.
Below is the diagram of how I wired all of the sensors.For the actual wiring I have one of the
sensors wired on a second breadboard because I did not have enough room to put
all five on one small breadboard and I also had an extra breadboard so why try
to cram it all on one.
Now you may be asking, what can we do with all those
sensors?One project is going to be to
expand on our autonomous robot that we showed in this post and give the robot a much broader view of the world.The other project we want to do is to have
the robot send the sensor readings back to a controller so we can control the
robot when we are not in the same room as the robot.This second project would be useful if the
robot was exploring someplace where we could not go like a different planet or
small caverns.I also want to eventually
connect a camera to the robot and stream video back.
My concern with the second project is how will we
communicate back with the controller.In
some earlier projects I used Bluetooth but I am thinking in the long term that
Bluetooth is not going to be the answer for my needs.I was
thinking about adding a WIFI USB adapter however I think the power drain will
be too much when I connect the robot to the EasyAcc power bank.So after thinking about the problem quite a
bit and doing a lot of reading, I decided to order a Spark Core. I ordered it from MakerShed and should be here this week.The Spark Core has WIFI built in and I can
have it act as my communication module that will relay commands from the
controller to the BeagleBone Black.This
should also allow us to use our iPhone/iPad to control our robot.
I am still debating on the language to use for our robot. If I can offload the communication piece from the BeagleBone to the Spark Core, I will probably use Javascript/Bonescript but I am still considering Python.
In Part 1 and Part 2 of this series I showed how to connect the BeagleBone Black to the Rover 5
Tracked Chassis, using the Rover 5 motor controller and also wrote a Python
module to help control the robot.In
this third and final post I will show how I connected the Rocketfish MicroBluetooth Adapter to the BeagleBone Black.I will then show
my Bluetooth client and server scripts, written in Python, that lets me control
my robot remotely to make it truly mobile. Also added one more video of my robot in action.
Adding the Rocketfish
Bluetooth adapter
Remember to always turn the power off on the BeagleBone
prior to plugging (or unplugging) anything into it.The Rocketfish Bluetooth adapter simply plugs
into a USB port.If you are using a USB
hub you can plug it into the hub or directly into the BeagleBone if you do not
have a hub.Once the Bluetooth adapter
is plugged in, power on the BeagleBone.
When the BeagleBone comes up, log into the BeagleBone and
run the following command:
lsusb
You should see a line similar to this if the BeagleBone
recognizes the RocketFish Bluetooth adapter:
Bus 001 Device 009: ID
0461:4d75 Primax Electronics, Ltd Rocketfish RF-FLBTAD Bluetooth Adapter
When I saw this line, my first thought was that Angstrom
Linux recognized my adapter and I was good to go, but that is not the
case.By default, Bluetooth is not
enabled with the default BeagleBone Black Angstrom Linux distribution.
Enable Bluetooth
support in Angstrom Linux
To enable Bluetooth support we will need to edit a
configuration files, enable the Bluetooth service and then start the Bluetooth
service.In your favorite editor (I
prefer vi) open the /var/lib/connman/settings
file.You should see something similar
to this:
[global]
OfflineMode=false
[Wired]
Enable=true
[WiFi]
Enable=true
[Bluetooth]
Enable=false
Notice that Bluetooth is disabled.To enable Bluetooth, change the Enable=false line
to Enabled=true under the [Bluetooth] heading and then save the
file.Now we will want to enable and
start the Bluetooth service.To do this,
run the following commands:
systemctl
enable bluetooth.service
systemctl
start bluetooth.service
Next restart the BeagleBone Black
and the Bluetooth service should be running.We now need to pair our BeagleBone with the device that we will use to
control it.
I have the ability to connect
my BeagleBone Black to a USB keyboard/touchpad and monitor so I paired my
BeagleBone Black to my laptop from the GUI interface.How you pair your devices will vary depending
on what device you pair your BeagleBone with.You can initiate the pairing from either the BeagleBone or the other
device.
If you do not have your
BeagleBone black connected to a Monitor, there are command line options but I
was unable to get any of them to pair properly.This post (http://pfalcon-oe.blogspot.com/2011/12/pairing-bluetooth-devices-from-command.html)
seems to be the closes one but it still requires a GUI to set the PIN.If anyone is able to get the BeagleBone to
pair purely from command line, please post the code (or a link) in the comment
section below for others.Another option
would be to install and configure VNC on the BeagleBone Black so you can access
the Desktop remotely.
Using Python to connect over Bluetooth
To begin, we need to install
a couple of packages, so connect your BeagleBone to the Internet and run the
following commands:
opkg
install bluez4 bluez4-dev
pip
install pybluez
This will install bluez and the bluez
development libraries.We then installed
the pybluez python module.The pybluez module is a python wrapper around
the system Bluetooth resources that allows us to easy creates python scripts
that communicate over Bluetooth.
I used the rfcomm-server.py and rfcomm-client.py examples
from the pybluez documentation as the base for my rover 5 client/server scripts.You can take a look at the examples and the
documentation on how the Bluetooth communication works.
Rover 5 rfcomm server
Lets begin by writing the server script that will run on the
BeagleBone Black and listen for incoming Bluetooth connections. This script must be located in a directory
that also contains the rover_beaglebone.py
module that we wrote in part 2 of this series. Here is the code for the rover5-rfcomm-server.py
script:
from bluetooth import *
import rover_beaglebone as
rover
import time
#open Bluetooth socket and
listen for connections
server_sock=BluetoothSocket(
RFCOMM )
server_sock.bind(("",PORT_ANY))
server_sock.listen(2)
port = server_sock.getsockname()[1]
uuid =
"34B1CF4D-1069-4AD6-89B6-E161D79BE4D8"
#advertise our Bluetooth
service so other devices can find it
print("Waiting for
connection on RFCOMM channel %d" % port)
#wait for an incoming
connection
client_sock, client_info =
server_sock.accept()
print("Accepted
connection from ", client_info)
#initialize rover
rover.init_rover()
try:
#wait for incoming commands and execute
appropriate function
while True:
data = client_sock.recv(1024)
if len(data) == 0: break
print("received [%s]" % data)
if data == 'u':
rover.all_full_speed()
elif data == 'j':
rover.all_stop()
elif data == 'i':
rover.increase_speed()
elif data == 'k':
rover.decrease_speed()
elif data == 'o':
rover.forward_dir()
elif data == 'l':
rover.reverse_dir()
elif data == 'z':
rover.spin_left(40)
elif data == 'x':
rover.spin_right(40)
elif data == 'c':
rover.spin_right(40)
else:
break
except IOError:
pass
#disconnection socket and
stop robot
print("disconnected")
rover.stop_rover()
client_sock.close()
server_sock.close()
print("all done")
This script begins by setting up a Bluetooth socket to
listen on and also advertises the service using SDP.Once a connection is made it then listens for
incoming text and executes the appropriate functions depending on the incoming
text.If there is no function tied to
the incoming text, the script disconnects the socket and stops the robot.
Now we need to configure this script to start up
automatically when the BeagleBone boots up since we will not have the robot
connected to anything (network or another computer) when we control it
remotely.This means we will want to
configure the script to run as a service when the BeagleBone first boots up.To do this we will create a file called rover5.service in the /lib/systemd/system directory and put the following code in it
(Note:You will need to change the path
of the WorkingDirectory and ExecStart variables to match the path to your rover5-rfcomm-server.py file.
Now run the following commands to enable and start the Rover5
service:
systemctl enable
rover5.service
systemctl start
rover5.service
The service is now started and will start each time the
BeagleBone starts up.
Rover 5 rfcomm client
Now that we have our Rover 5 Bluetooth server created and
started, let write our client application that will run on the device that we
will use to control the robot.Create a
file called rover5-rfcomm-client.py and add the following code:
from bluetooth import *
import sys, tty, termios
if sys.version < '3':
input = raw_input
addr = None
if len(sys.argv) < 2:
print("no device specified.Searching all nearby bluetooth devices
for")
print("the BeagleBoneService")
else:
addr = sys.argv[1]
print("Searching for BeagleBoneServic
e on %s" % addr)
print("connecting to
\"%s\" on %s" % (name, host))
# Create the client socket
sock=BluetoothSocket( RFCOMM
)
sock.connect((host, port))
fd = sys.stdin.fileno()
old_settings =
termios.tcgetattr(fd)
tty.setraw(sys.stdin.fileno())
#we are connected, now we can
type commands
print("connected.type stuff")
r=True
while r:
data = sys.stdin.read(1)
if len(data) == 0: break
sock.send(data)
if data == 'x':
r=False
sock.close()
If your run this scripts, it will search for the Rover 5 service
using the UUID as the key.If the
service is found it will attempt to connect.If the connection is successful it will display the message “connected.type stuff”.At this point we can begin typing commands to control the robot.Before we do too much with this, we will want
to disconnect the rover from external power supplies, monitors, USB hubs…. and
make it truly mobile.
Making the robot
mobile
The final piece of the puzzle is to free our robot of
external dependencies like USB hubs, power source, network cables… that limit
the range that we can go.For this we
will need to get an external power source.I am using the portable external cell phone charger from EasyAcc.
To make the robot mobile, begin by powering off the
BeagleBone and unplug all cables that connect the BeagleBone to a resource
external to the robot like power, network, USB hub… (Note:leave the BeagleBone connected to the motor
controller).The Beaglebone black’s
connections will look like this:
Now plug the Rocketfish Bluetooth adapter into the USB port
on your BeagleBone Black and run the USB power connector on the BeagleBone
Black to the external cell phone charger as pictured below:
If everything is connected correctly the robot should power
up. Once the robot is powered up; run
the client script by running this command “pythonrover5-rfcomm-client.py”. I normally give the BeagleBone about 30 seconds
to power up before I run the client script.
If all is well, you should see the prompt “connected.type
stuff”.At this point you can
enter the any of the one character commands defined in the rover5-rfcomm-server.py script to make your
robot go.
This ends the three part series on my first working robot.Now it is time to expand the robot and/or
experiment some more with the BeagleBone Black.Either way I will document what I am learning here.I hope this series of posts helped/inspired
others.