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
advertise_service(
server_sock, "BeagleBoneService",
service_id = uuid,
service_classes = [ uuid,
SERIAL_PORT_CLASS ],
profiles = [
SERIAL_PORT_PROFILE ]
)
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.
[Unit]
Description=Rover 5 Bluetooth
Service
[Service]
WorkingDirectory=/home/root/bluetooth
ExecStart=/usr/bin/python
/home/root/bluetooth/rover5-rfcomm-server.py
SyslogIdentifier=rover5
Restart=on-failure
RestartSec=2
[Install]
WantedBy=multi-user.target
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)
# search for the Rover 5
service
uuid =
"34B1CF4D-1069-4AD6-89B6-E161D79BE4D8"
service_matches =
find_service( uuid = uuid, address = addr )
if len(service_matches) == 0:
print("couldn't find the Rover 5
service =(")
sys.exit(0)
#If we found a match
first_match =
service_matches[0]
port =
first_match["port"]
name =
first_match["name"]
host =
first_match["host"]
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 “python
rover5-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.