Yet Another DIY Bluetooth RC Car

Got a cool robotic project you've been working on that you want the rest of the world to know about? Post pictures, descriptions and links here, and let us see. :)

Re: Yet Another DIY Bluetooth RC Car

Postby el_es » Sun May 24, 2015 11:40 pm

So I programmed some more buttons (basically extended the Bluetooth Chat example by another set of buttons, each one sending a command,
and it goes well, as far as sending text commands at button press can go :)

Also the discussion about b/o protection just about to take an unexpected turn: i changed batteries for a new set of 5 x aaa...

and now, pf and tf command does not brown out any more :) and the speed, it's MASSIVE :)
can't wait to test it outdoors.

In the mean time, also looking which way better to share what I wrote...

I will paste patches into this post.
(I changed the BluetoothChatFragment.java and fragment_bluetooth_chat.xml and the BluetoothChatService.java)

Code: Select all
Index: Application/src/main/java/com/example/android/bluetoothchat/BluetoothChatService.java
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>windows-1252
===================================================================
--- Application/src/main/java/com/example/android/bluetoothchat/BluetoothChatService.java   (revision )
+++ Application/src/main/java/com/example/android/bluetoothchat/BluetoothChatService.java   (revision )
@@ -46,11 +46,11 @@
     private static final String NAME_SECURE = "BluetoothChatSecure";
     private static final String NAME_INSECURE = "BluetoothChatInsecure";
 
-    // Unique UUID for this application
+    // Unique UUID for this application  // el_es : serial port profile UUID
     private static final UUID MY_UUID_SECURE =
-            UUID.fromString("fa87c0d0-afac-11de-8a39-0800200c9a66");
+            UUID.fromString("00001101-0000-1000-8000-00805f9b34fb");  //fa87c0d0-afac-11de-8a39-0800200c9a66");
     private static final UUID MY_UUID_INSECURE =
-            UUID.fromString("8ce255c0-200a-11e0-ac64-0800200c9a66");
+            UUID.fromString("00001101-0000-1000-8000-00805f9b34fb");  //8ce255c0-200a-11e0-ac64-0800200c9a66");
 
     // Member fields
     private final BluetoothAdapter mAdapter;


Code: Select all
Index: Application/src/main/res/layout/fragment_bluetooth_chat.xml
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
--- Application/src/main/res/layout/fragment_bluetooth_chat.xml   (revision )
+++ Application/src/main/res/layout/fragment_bluetooth_chat.xml   (revision )
@@ -45,5 +45,68 @@
             android:layout_height="wrap_content"
             android:text="@string/send" />
     </LinearLayout>
+    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
+        android:layout_width="match_parent"
+        android:layout_height="63dp"
+        android:orientation="horizontal" >
+        <Button
+            android:id="@+id/button_lf"
+            android:layout_width="wrap_content"
+            android:layout_height="wrap_content"
+            android:text="Left \nFull" />
+        <Button
+            android:id="@+id/button_dc"
+            android:layout_width="wrap_content"
+            android:layout_height="wrap_content"
+            android:text="Dir \nCenter" />
+        <Button
+            android:id="@+id/button_rf"
+            android:layout_width="wrap_content"
+            android:layout_height="wrap_content"
+            android:text="Right \nFull" />
+        <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
+            android:layout_width="match_parent"
+            android:layout_height="match_parent"
+            android:orientation="horizontal"
+            android:gravity="right">
+            <Button
+            android:id="@+id/button_pf"
+            android:layout_width="wrap_content"
+            android:layout_height="wrap_content"
+            android:text="Forward \nFull" />
+            <Button
+            android:id="@+id/button_p8"
+            android:layout_width="wrap_content"
+            android:layout_height="wrap_content"
+            android:text="Forward \nHalf" />
+            <Button
+                android:id="@+id/button_p5"
+                android:layout_width="wrap_content"
+                android:layout_height="wrap_content"
+                android:text="Forward \nQuarter" />
+
+        </LinearLayout>
+    </LinearLayout>
+    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
+        android:layout_width="match_parent"
+        android:layout_height="63dp"
+        android:orientation="horizontal"
+        android:gravity="right">
+        <Button
+            android:id="@+id/button_tf"
+            android:layout_width="wrap_content"
+            android:layout_height="wrap_content"
+            android:text="Reverse \nFull" />
+        <Button
+            android:id="@+id/button_t5"
+            android:layout_width="wrap_content"
+            android:layout_height="wrap_content"
+            android:text="Reverse \nQuarter" />
+        <Button
+            android:id="@+id/button_s"
+            android:layout_width="wrap_content"
+            android:layout_height="wrap_content"
+            android:text="Full \nStop" />
+        </LinearLayout>
 
 </LinearLayout>


Code: Select all
Index: Application/src/main/java/com/example/android/bluetoothchat/BluetoothChatFragment.java
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>windows-1252
===================================================================
--- Application/src/main/java/com/example/android/bluetoothchat/BluetoothChatFragment.java   (revision )
+++ Application/src/main/java/com/example/android/bluetoothchat/BluetoothChatFragment.java   (revision )
@@ -60,6 +60,15 @@
     private ListView mConversationView;
     private EditText mOutEditText;
     private Button mSendButton;
+    private Button mRFButton;
+    private Button mLFButton;
+    private Button mDCButton;
+    private Button mPFButton;
+    private Button mP8Button;
+    private Button mP5Button;
+    private Button mTFButton;
+    private Button mT5Button;
+    private Button mStopButton;
 
     /**
      * Name of the connected device
@@ -151,6 +160,15 @@
         mConversationView = (ListView) view.findViewById(R.id.in);
         mOutEditText = (EditText) view.findViewById(R.id.edit_text_out);
         mSendButton = (Button) view.findViewById(R.id.button_send);
+        mRFButton = (Button) view.findViewById(R.id.button_rf);
+        mLFButton = (Button) view.findViewById(R.id.button_lf);
+        mDCButton = (Button) view.findViewById(R.id.button_dc);
+        mPFButton = (Button) view.findViewById(R.id.button_pf);
+        mP8Button = (Button) view.findViewById(R.id.button_p8);
+        mP5Button = (Button) view.findViewById(R.id.button_p5);
+        mTFButton = (Button) view.findViewById(R.id.button_tf);
+        mT5Button = (Button) view.findViewById(R.id.button_t5);
+        mStopButton = (Button) view.findViewById(R.id.button_s);
     }
 
     /**
@@ -176,6 +194,84 @@
                     TextView textView = (TextView) view.findViewById(R.id.edit_text_out);
                     String message = textView.getText().toString();
                     sendMessage(message);
+                }
+            }
+        });
+
+        mRFButton.setOnClickListener(new View.OnClickListener() {
+            public void onClick(View v) {
+                View view = getView();
+                if (null != view) {
+                    sendMessage("RF");
+                }
+            }
+        });
+
+        mDCButton.setOnClickListener(new View.OnClickListener() {
+            public void onClick(View v) {
+                View view = getView();
+                if (null != view) {
+                    sendMessage("L0");
+                }
+            }
+        });
+
+        mLFButton.setOnClickListener(new View.OnClickListener() {
+            public void onClick(View v) {
+                View view = getView();
+                if (null != view) {
+                    sendMessage("LF");
+                }
+            }
+        });
+
+        mPFButton.setOnClickListener(new View.OnClickListener() {
+            public void onClick(View v) {
+                View view = getView();
+                if (null != view) {
+                    sendMessage("PF");
+                }
+            }
+        });
+        mP8Button.setOnClickListener(new View.OnClickListener() {
+            public void onClick(View v) {
+                View view = getView();
+                if (null != view) {
+                    sendMessage("P8");
+                }
+            }
+        });
+        mP5Button.setOnClickListener(new View.OnClickListener() {
+            public void onClick(View v) {
+                View view = getView();
+                if (null != view) {
+                    sendMessage("P5");
+                }
+            }
+        });
+        mTFButton.setOnClickListener(new View.OnClickListener() {
+            public void onClick(View v) {
+                View view = getView();
+                if (null != view) {
+                    sendMessage("TF");
+                }
+            }
+        });
+
+        mT5Button.setOnClickListener(new View.OnClickListener() {
+            public void onClick(View v) {
+                View view = getView();
+                if (null != view) {
+                    sendMessage("T5");
+                }
+            }
+        });
+
+        mStopButton.setOnClickListener(new View.OnClickListener() {
+            public void onClick(View v) {
+                View view = getView();
+                if (null != view) {
+                    sendMessage("S");
                 }
             }
         });
el_es
 
Posts: 22
Joined: Sat Feb 07, 2015 3:12 pm

Re: Yet Another DIY Bluetooth RC Car

Postby el_es » Wed May 27, 2015 12:24 am

And now I'm trying to extend the BluetoothChat example further, by putting in a SeekBar, to do this:

Code: Select all
//        mSteeringBar.setOnSeekBarChangeListener(
//                new SeekBar.OnSeekBarChangeListener() {
^^^^^ when uncommented, this line goes red with error below ^^^^^^^
//                    @Override
//                    public void OnProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
//                        int aposition = progress - 16;
//                        String message = "";
//                        if (aposition < 0) {
//                            aposition = -aposition;
//                            message = "L".concat( Integer.toHexString(aposition));
//                        }
//                        else
//                        {
//                            message = "R".concat(Integer.toHexString(aposition));
//                        }
//                        sendMessage(message);
//                    }
//                });

It's in comments, because it won't let me :(
it says
Error:(287, 55) error: <anonymous com.example.android.bluetoothchat.BluetoothChatFragment$11> is not abstract and does not override abstract method onStopTrackingTouch(SeekBar) in OnSeekBarChangeListener


Pls help, I so don't understand this error :(
el_es
 
Posts: 22
Joined: Sat Feb 07, 2015 3:12 pm

Re: Yet Another DIY Bluetooth RC Car

Postby Alan » Wed May 27, 2015 11:08 pm

Hi there,

I think that what your compiler is saying is that you also need to provide an override for the method onStopTrackingTouch. This question on stackoverflow has a snippet which looks similar. So bascially you may have some joy by adding these 2 overrides

Code: Select all
@Override
    public void onStartTrackingTouch(SeekBar seekBar) {
        // TODO Auto-generated method stub
    }

    @Override
    public void onStopTrackingTouch(SeekBar seekBar) {
        // TODO Auto-generated method stub
    }


Regards

Alan
Alan
Site Admin
 
Posts: 311
Joined: Fri Jun 14, 2013 10:09 am

Re: Yet Another DIY Bluetooth RC Car

Postby el_es » Thu May 28, 2015 11:20 pm

Thanks will try that...
having been sitting on this problem till 1am in the morning, probably did not help my understanding either ;)

however, to me java is one of the less understandable languages :) Pascal Forever ;)
el_es
 
Posts: 22
Joined: Sat Feb 07, 2015 3:12 pm

Re: Yet Another DIY Bluetooth RC Car

Postby el_es » Thu May 28, 2015 11:35 pm

Yay thanks this worked,
also i finally got what the AS wanted of me... the prototype of the OnSeekBarChangeListener onProgressChanged to implement, looks different...

Code: Select all
mSteeringBar.setOnSeekBarChangeListener(
                new SeekBar.OnSeekBarChangeListener() {

                    @Override
                    public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
// i had something else ;) even if still technically same types of variables...
                        int aposition = i - 16;
                        String message = "";
                        if (aposition < 0) {
                            aposition = -aposition;
                            message = "L".concat( Integer.toHexString(aposition));
                        }
                        else
                        {
                            message = "R".concat(Integer.toHexString(aposition));
                        }
                        sendMessage(message);
                    }

                    @Override
                    public void onStartTrackingTouch(SeekBar seekBar) {
                        // TODO Auto-generated method stub
                    }

                    @Override
                    public void onStopTrackingTouch(SeekBar seekBar) {
                        // TODO Auto-generated method stub
                    }
                });

So now onwards to do a fully on-sliders app ;)
el_es
 
Posts: 22
Joined: Sat Feb 07, 2015 3:12 pm

Re: Yet Another DIY Bluetooth RC Car

Postby el_es » Fri May 29, 2015 12:57 am

So here is my code of the working app that has steering on sliders (SeekBar's):

Code: Select all
Index: Application/src/main/res/layout/fragment_bluetooth_chat.xml
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
--- Application/src/main/res/layout/fragment_bluetooth_chat.xml   (revision )
+++ Application/src/main/res/layout/fragment_bluetooth_chat.xml   (revision )
@@ -17,15 +17,17 @@
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
               android:layout_width="match_parent"
               android:layout_height="match_parent"
-              android:orientation="vertical" >
+              android:orientation="vertical"
+    android:weightSum="1">
 
     <ListView
         android:id="@+id/in"
         android:layout_width="match_parent"
-        android:layout_height="match_parent"
-        android:layout_weight="1"
+        android:layout_height="63dp"
         android:stackFromBottom="true"
-        android:transcriptMode="alwaysScroll" />
+        android:transcriptMode="alwaysScroll"
+        android:layout_weight="0.70"
+        android:clickable="false" />
 
     <LinearLayout
         android:layout_width="match_parent"
@@ -44,6 +46,64 @@
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"
             android:text="@string/send" />
+    </LinearLayout>
+    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
+        android:layout_width="match_parent"
+        android:layout_height="63dp"
+        android:orientation="horizontal" >
+
+        <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
+            android:layout_width="match_parent"
+            android:layout_height="match_parent"
+            android:orientation="horizontal"
+            android:gravity="center_horizontal">
+
+            <Button
+            android:id="@+id/button_pf"
+            android:layout_width="wrap_content"
+            android:layout_height="wrap_content"
+            android:text="Forward \nFull" />
+
+            <Button
+                android:id="@+id/button_tf"
+                android:layout_width="wrap_content"
+                android:layout_height="wrap_content"
+                android:text="Reverse \nFull" />
+
+            <Button
+                android:id="@+id/button_s"
+                android:layout_width="wrap_content"
+                android:layout_height="wrap_content"
+                android:text="Full \nStop" />
+        </LinearLayout>
+    </LinearLayout>
+    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
+        android:layout_width="match_parent"
+        android:layout_height="63dp"
+        android:orientation="horizontal"
+        android:gravity="right">
+
+        <SeekBar
+            android:layout_width="wrap_content"
+            android:layout_height="wrap_content"
+            android:id="@+id/SteeringBar"
+            android:max="30"
+            android:clickable="true"
+            android:longClickable="true"
+            android:progress="15"
+            android:mirrorForRtl="false"
+            android:layout_weight="0.10" />
+
+        <SeekBar
+            android:layout_width="wrap_content"
+            android:layout_height="wrap_content"
+            android:id="@+id/ThrottleBar"
+            android:layout_weight="0.10"
+            android:progress="15"
+            android:max="15"
+            android:clickable="true"
+            android:longClickable="true" />
+
     </LinearLayout>
 
-</LinearLayout>
+</LinearLayout>
\ No newline at end of file


Code: Select all
Index: Application/src/main/java/com/example/android/bluetoothchat/BluetoothChatFragment.java
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>windows-1252
===================================================================
--- Application/src/main/java/com/example/android/bluetoothchat/BluetoothChatFragment.java   (revision )
+++ Application/src/main/java/com/example/android/bluetoothchat/BluetoothChatFragment.java   (revision )
@@ -39,6 +39,8 @@
 import android.widget.Button;
 import android.widget.EditText;
 import android.widget.ListView;
+import android.widget.ProgressBar;
+import android.widget.SeekBar;
 import android.widget.TextView;
 import android.widget.Toast;
 
@@ -60,7 +62,19 @@
     private ListView mConversationView;
     private EditText mOutEditText;
     private Button mSendButton;
+/*    private Button mRFButton;
+    private Button mLFButton;*/
+    private Button mDCButton;
+    private Button mPFButton;
+   //private Button mP8Button;
+   // private Button mP5Button;
+    private Button mTFButton;
+   // private Button mT5Button;
+    private Button mStopButton;
+    SeekBar mSteeringBar;
+    SeekBar mThrottleBar;
 
+
     /**
      * Name of the connected device
      */
@@ -151,8 +165,15 @@
         mConversationView = (ListView) view.findViewById(R.id.in);
         mOutEditText = (EditText) view.findViewById(R.id.edit_text_out);
         mSendButton = (Button) view.findViewById(R.id.button_send);
+        mPFButton = (Button) view.findViewById(R.id.button_pf);
+        mTFButton = (Button) view.findViewById(R.id.button_tf);
+        mStopButton = (Button) view.findViewById(R.id.button_s);
+        mSteeringBar = (SeekBar) view.findViewById(R.id.SteeringBar);
+        mThrottleBar = (SeekBar) view.findViewById(R.id.ThrottleBar);
+
     }
 
+
     /**
      * Set up the UI and background operations for chat.
      */
@@ -180,12 +201,106 @@
             }
         });
 
+        mPFButton.setOnClickListener(new View.OnClickListener() {
+            public void onClick(View v) {
+                View view = getView();
+                if (null != view) {
+                  //  mPFButton.setBackgroundColor(0x00FF00);
+                  //  mTFButton.setBackgroundColor(0xFF0000);
+                   mPFButton.setTextColor(0xff0000);
+                   mTFButton.setTextColor(0x00ff00);
+                }
+
+            }
+        });
+
+        mTFButton.setOnClickListener(new View.OnClickListener() {
+            public void onClick(View v) {
+                View view = getView();
+                if (null != view) {
+                    //endMessage("TF");
+                    //mPFButton.setBackgroundColor(0xFF00);
+                   // mTFButton.setBackgroundColor(0x00FF00);
+                   mPFButton.setTextColor(0x00FF00);
+                   mTFButton.setTextColor(0xFF0000);
+                }
+
+            }
+        });
+
+        mStopButton.setOnClickListener(new View.OnClickListener() {
+            public void onClick(View v) {
+                View view = getView();
+                if (null != view) {
+                    sendMessage("S");
+                }
+            }
+        });
+
+        mSteeringBar.setOnSeekBarChangeListener(
+                new SeekBar.OnSeekBarChangeListener() {
+
+                    @Override
+                    public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
+                        int aposition = i - 15;
+                        String message = "";
+                        if (aposition < 0) {
+                            aposition = -aposition;
+                            message = "L".concat(Integer.toHexString(aposition));
+                        } else {
+                            message = "R".concat(Integer.toHexString(aposition));
+                        }
+                        sendMessage(message);
+                    }
+
+                    @Override
+                    public void onStartTrackingTouch(SeekBar seekBar) {
+                        // TODO Auto-generated method stub
+                    }
+
+                    @Override
+                    public void onStopTrackingTouch(SeekBar seekBar) {
+                        // TODO Auto-generated method stub
+                        seekBar.setProgress(15);
+                    }
+                });
+
+        mThrottleBar.setOnSeekBarChangeListener(
+                new SeekBar.OnSeekBarChangeListener() {
+
+                    @Override
+                    public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
+                        int aposition = 15-i;
+                        String message = "";
+                        if (mPFButton.getCurrentTextColor() == 0xff0000){
+                            message = "P".concat(Integer.toHexString(aposition));
+                        }
+                        if (mTFButton.getCurrentTextColor() == 0xff0000){
+                            message = "T".concat(Integer.toHexString(aposition));
+                        }
+                        sendMessage(message);
+                    }
+
+                    @Override
+                    public void onStartTrackingTouch(SeekBar seekBar) {
+
+                    }
+
+                    @Override
+                    public void onStopTrackingTouch(SeekBar seekBar) {
+                        seekBar.setProgress(15);
+                    }
+                }
+        );
+
         // Initialize the BluetoothChatService to perform bluetooth connections
         mChatService = new BluetoothChatService(getActivity(), mHandler);
 
         // Initialize the buffer for outgoing messages
         mOutStringBuffer = new StringBuffer("");
     }
+
+
 
     /**
      * Makes this device discoverable.
el_es
 
Posts: 22
Joined: Sat Feb 07, 2015 3:12 pm

Re: Yet Another DIY Bluetooth RC Car

Postby el_es » Sat May 30, 2015 9:12 pm

Hi,
so, googling for answers how to integrate the motion (axis angle/gyroscope sensor) into my project, i found an interesting page http://developer.android.com/reference/ ... nager.html and a few others but this seems like something ... only have no clue how to integrate this .

I added the snippet as is to a file (bar it did not like that the class was public) and took AS's suggestions so it wouldn't error out. But now since my sendmessage() is in a different class (the Fragment...) i need to do something else, but what?
This http://developer.telerik.com/products/b ... eter-data/ looks simpler... but still no clue how to add that so it would work in an existing fragment/java class...
Yeah I know it's noobish. Please help/point... I am after the left/right turn and front/back turn angles so to use the device as a throttle or steering wheel.
el_es
 
Posts: 22
Joined: Sat Feb 07, 2015 3:12 pm

Re: Yet Another DIY Bluetooth RC Car

Postby Alan » Mon Jun 01, 2015 12:41 pm

I'm flailing here as I don't have the IDE installed yet but have you created a variable for the SensorActivity and initialised it? Something like

Code: Select all
SensorActivity mSensorActivity;            // This goes with the other member variables

mSensorActivity = new SensorActivity();        // This goes wherever your setup code is


Looking at the sample you should just then need to put some printing code in the onSensorChanged code to see readings from the sensor. To get those sensor readings back into your control code you've got a number of options but essentially you need to use a callback that passes back the data.

Apologies if this is way off or completely unhelpful. I'm interested to see how this Android Bluetooth stuff works so will do my best to install the Android tools later this week. Then hopefully I can make some more sensible comments. :)

Regards

Alan
Alan
Site Admin
 
Posts: 311
Joined: Fri Jun 14, 2013 10:09 am

Re: Yet Another DIY Bluetooth RC Car

Postby el_es » Mon Jun 01, 2015 7:03 pm

Hi,

Then I'll have to replicate the code for sending strings to the BluetoothChat service, one that is in the BluetoothChat Fragment (https://android.googlesource.com/platfo ... rvice.java and around this is what I presume I've imported...)

(this https://android.googlesource.com/platfo ... hChat.java especially lines 206-221)

which I certainly hope will go well...

I am sorry, really... my Java skill level is approaching 0 exponentially ;J the more I read the less I understand of it ;)
el_es
 
Posts: 22
Joined: Sat Feb 07, 2015 3:12 pm

Re: Yet Another DIY Bluetooth RC Car

Postby el_es » Mon Jun 01, 2015 7:13 pm

Actually, now looking at the links pasted above, I see the AS BluetoothChat sample is horribly and deliberately made more complicated than probably ever needed... for the need to cater for different screen sizes mostly (like, different form definition for every other resolution, and all that mapped dynamically, omg.)

As of it working, I can definitively confirm, it does, on my MotoG 3G 2013 and on my Google/Asus Nexus 7 2012.
With sliders (SeekBars) for throttle and steering, and sending it text commands.

One thing I'd probably LIKE to do is to somehow add a feature to rename the BT receiver from HC-06 to something more, say, freeform. But this means the MD will need to send some AT commands at some specified speed to it probably. Which I hope won't be on their way to fry it.... Ever tried that?
el_es
 
Posts: 22
Joined: Sat Feb 07, 2015 3:12 pm

Previous

Return to Project Showcase

Who is online

Users browsing this forum: No registered users and 0 guests