Sunteți pe pagina 1din 15

!

Bluetooth ! ! Robot! ! Communication! !


!

An Introduction to Bluetooth Communication to Control Robots!

! !

Kevin Prasanna R R!

Version 1!

For HC-05

! ! ! ISBN : 978-1-304-94529-7! ! ! ! ! ! Version 1 for HC-05 Bluetooth Module: March 2014 ! ! ! ! ROS is an open-source, meta-operating system for robots : click here" ! Bluetooth is a wireless communication system : click here! !

Other products and company names mentioned herein may be the trademarks of their respective owners. Rather than use a trademark symbol with every occurrence of a trademark name, we are using the names only in an editorial fashion and to the benet of the trademark owner, with no intention of infringement of the trademark.! Information contained in this work (Paperback or eBook) has been obtained from sources believed to be reliable. However, the author does not guarantee the accuracy or completeness of any information contained in it, and the author shall not be responsible for any errors, omissions, losses, or damages caused or alleged to be caused directly or indirectly by the information published herein. This work is published with the understanding that the author is supplying information but is not attempting to render professional services. This product almost certainly contains errors. It is your responsibility to examine, verify, test, and determine the appropriateness of use, or request the assistance of an appropriate professional to do so.!

! ! ! ! ! ! ! ! !

Preface! !
This ebook is meant to introduce you a couple of ways in which you can control your robot wirelessly using a Bluetooth module using simple Serial Communication. We will be using a HC-05 Bluetooth module to connect to the micro controller. !

! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! !

Our rst method will involve developing a simple Android app to send commands to two different micro controllers : Arduino Uno and MSP 430 Launchpad. The second method will be using ROS (Robot Operating System) to send serial data to the above mentioned micro controllers from Ubuntu using Bluetooth. ! This ebook is not for beginners of any sort. It requires the reader to have some prerequisite knowledge about either Android application development or familiarity with ROS environment of course with knowledge of using a micro controller to receive serial data.!

! ! !

! Chapter ! ! HC-05 ! Android Bluetooth Interface ! ROS & Bluetooth ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! !

Contents!
Page No! 6 7 10

! !

The HC-05 !
The HC-05 is a Civil level Bluetooth serial interface module. The master and slave mode can be switched easily using AT commands. For more information about HC-05 have a look at the ofcial documentation.!

The default Bluetooth name is HC-05 and the Password is 1234.! Default communication baud rate: 9600 but it can be changed. It draws very less current (8mA) which is why I prefer this module since it can work with almost all micro controllers.!

! I advice you to use the HC-05 along with a break board since its safer and always preferred.! ! Check out the pin-out of the module:! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! !
Any other Bluetooth module will also work for the same way and all further discussion using Android or ROS will also work on any bluetooth modules. #

! ! ! ! ! ! !

Android Bluetooth! Interface!

To begin with, use the sample Bluetooth Chat App provided along with the SDK or download here. The rst thing the app has to establish is the Broadcast sender and receiver services. The app is self-explanatory so take your time to understand how each service is created and broadcasted. ! Unix and Linux users can notice that it is just like creating a device le using RFComm bind command. The created device le will be stored and communicated with just like any other le is being opened and data is sent serially by writing the command into the le. ! In the BluetoothChat.java le where the setting up of chat function is encountered I use 5 buttons to send hardcoded strings that can be decoded by my micro controller to move the robot :!
private void setupChat() {! Log.d(TAG, "setupChat()");! // Initialise the array adapter for the conversation thread! mConversationArrayAdapter = new ArrayAdapter<String>(this, R.layout.message);! mConversationView = (ListView) ndViewById(R.id.in);! mConversationView.setAdapter(mConversationArrayAdapter);! // Initialise different buttons with a listener that for click events! left= (Button) ndViewById(R.id.button_left);! right=(Button) ndViewById(R.id.button_right);! frwd=(Button) ndViewById(R.id.button_frwd);! back=(Button) ndViewById(R.id.button_back);! stop=(Button) ndViewById(R.id.button_stop);!

! ! !

// Create the string that has to be sent to the HC-05 module! left.setOnClickListener(new OnClickListener() {! public void onClick(View v) {! // Send a message using content of the edit text widget! ! sendMessage("*L000R040#");! }! });!

! !

right.setOnClickListener(new OnClickListener() {! public void onClick(View v) {! // Send a message using content of the edit text widget! ! sendMessage("*L040R000#");!

}! });! frwd.setOnClickListener(new OnClickListener() {! public void onClick(View v) {! // Send a message using content of the edit text widget! ! sendMessage("*L040R040#");! }! }) ! back.setOnClickListener(new OnClickListener() {! public void onClick(View v) {! // Send a message using content of the edit text widget! ! sendMessage("*L160R160#");! }! });!

! !

! ! ! ! ! }! ! ! ! ! ! ! ! ! ! ! ! ! ! !

stop.setOnClickListener(new OnClickListener() {! public void onClick(View v) {! // Send a message using content of the edit text widget! ! sendMessage("*L000R000#");! }! });!

// Initialize the BluetoothService to perform bluetooth connections! ! mChatService = new BluetoothService(this, mHandler);! // Initialize the buffer for outgoing messages! mOutStringBuffer = new StringBuffer(");!

! ! ! The buttons are customised in the following way :! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! !


You can replace the default code of the bluetooth chats setupChat() function with the above piece of code and make sure to use appropriate id for the corresponding buttons. In my case the MSP 430 (or Arduino) is made to serially receive data within the frame *L - - - R - - -# where L indicates the left motor speed and R indicates right motor speed.!

! ! ! ! ! !
!

To keep it simple you can also simple replace *L045R045# used above to move forward by a simple F and modify the micro controller program corresponding to the character F to move forward. As such one can add more buttons for more functionality to the robot.! Compile and run the apk on your android device. Scan for HC-05. If not paired, enter the password (1234) and connect. The LED glows once connected and now you can control your robot using your android device via Bluetooth.!

! ! ! !

ROS & Bluetooth! !


ROS is a widely used meta operating system for robots. I am running Ubuntu 12.04 LTS on MacBook Pro via Parallels. Even though there is support for other operating systems I would advice you to use Ubuntu as I found it very convenient and easy to use. If you have not installed ROS on your system click here. ROS Hydro is the latest version but I am working on Groovy. But the method will remain the same for either of them.!

First thing to do on Ubuntu is to pair your HC-05 or any other Bluetooth module via the GUI. Next scan for the device in the terminal using $hcitool scan!

! Note down the MAC address of HC-05:! ! !

! ! ! ! ! ! ! ! !

! ! !

Now let us congure the rfcomm.conf le to add a new device le. Use the command ! $sudo gedit /etc/bluetooth/rfcomm.conf to open and edit the conguration le. Type as shown in the following gure and replace the default MAC address by the MAC address of HC-05 obtained earlier. !

! Now save the le and execute the following command:! ! $sudo rfcomm bind rfcomm2! ! This will create a device le named rfcomm2 in the /dev/ directory.! ! !

Now let us begin ROS. Make sure you nish implementing the Subscriber-Publisher tutorial here.!

! ! ! !

Now that you have learnt how to subscribe to any ROS topic we only have to modify the listener.cpp program to communicate with the HC-05 module. Open the listener.cpp le and edit the chatterCallback() function. !

void chatterCallback(const boost::shared_ptr<geometry_msgs::Twist const>& msg)! {! //ros::Duration(0.5).sleep();! ROS_INFO("Received %f %f ", msg->linear.x,msg->angular.z);! ! //Open the rfcomm device le ! int robot = open ("/dev/rfcomm2", O_RDWR);! ! char *value;! ! //The contents of value string depends on your usage!

int val_Distance=(int)msg->linear.x;! int val_Angle=(int)(msg->angular.z*180/3.14);! val_Distance=((val_Distance*40)/400)+20;! ! ! if(abs(val_Angle)>10)! ! ! sprintf(value,"*L030R000#");! else ! ! sprintf(value,"*L045R045#");! if(val_Distance<=5)! ! sprintf(value,"*L000R000#");! ! ! ! ! ROS_INFO("Receiving %d",val_Distance);! ROS_INFO("Receiving %d",val_Angle);! ROS_INFO("Sending %s",value);! ! ! //Simply write into the bluetooth device le! ! write (robot, value, 10);! ! ! ! ROS_INFO("Closing");! ! //Clean up ! close(robot);! }!

And thats about it. Any robot can be serially controlled wirelessly using any Bluetooth modules using either of the mentioned two ways.

S-ar putea să vă placă și