Bluetooth data delay when receiving?

This is where you talk about the NXJ software itself, installation issues, and programming talk.

Moderators: 99jonathan, roger, imaqine

Bluetooth data delay when receiving?

Postby Rickz2020 » Sun Jan 22, 2012 4:19 pm

Hi all,

Using latest version of leJOS, eclipse and all JAVA updates. Running Windows 7 x64.

Using Bluetooth, I am currently sending sensor data from my NXT to an Android phone. The Android phone sends data to the NXT, which the NXT then uses to decide which direction to move the robot in. All's fine when I do one or the other, ie. Either do receive data from Android for movements, or send sensor data to Android.

If I try do both of these, the response time for movement is almost unusable - while the sensor data is still fine with close to zero delay. Any Idea's on why I would see a delay on the DataInputStream side of things?

I've tried putting them on separate threads, but the delay remains.

On main thread...
Code: Select all
public void Start() throws IOException {

                //Sets up the MovementManager - hands over the mode (100 = manual mode) and the DataOutputStream.
      mm.Setup(100, _dis);
      //start a new thread to read movement commands.
      Thread movement_Thread = new Thread(new Runnable(){
         
         @Override
         public void run() {            
         try {         
            mm.ManualMovement();
         } catch (IOException e) {
         }               
         }
      });            
      movement_Thread.start();   
      
                 //Sets up the DataSender - hands over the sensors and the DataOutputStream.
      sendData.Setup(sensors, _dos);
      //start a new thread to send the sensor data.
      Thread sendSensorData_thread = new Thread(new Runnable(){         
         @Override
         public void run() {            
         sendData.Start();               
         }
      });            
      sendSensorData_thread.start();         
   }


On separate thread... for receiving movement commands
Code: Select all
public void ManualMovement() throws IOException {
      while (true) {
         int direction = dis.readInt();
         int speed = dis.readInt();
         Move(direction, speed);         
         LCDDrawData(direction, speed);
      }
   }


On another thread... for sending the sensor data
Code: Select all
public void Start() {
      while (true) {      
         try {
            value = (int) sensors.GetComapssAngle();
            dos.writeInt(value);
            dos.flush();
         } catch (IOException e) {
         }
      }
   }


It all works, just like I mentioned, the receiving commands part is slow - delay of like 2 or 3 seconds...

I even tried introducing a delay into how often it sends sensor data, thinking that maybe it was sending data too frequently and thus blocking the network, but it made no difference.

Any ideas?
Rickz2020
Active User
 
Posts: 121
Joined: Thu Feb 10, 2011 5:47 pm
Location: London, Earth.

Re: Bluetooth data delay when receiving?

Postby gloomyandy » Sun Jan 22, 2012 7:07 pm

Hi,
Firstly I think you should have a delay when sending the sensor data, how large a value did you try?
Secondly how do you know the problem is on the NXT end of things? It may be that that issue is with the Android code. Do you have a PC with Bluetooth, that you could try doing something similar?

If all else fails you could change things so that the Android app polls the NXT for sensor data when it has no movement commands to send to it. This should ensure that your movement commands are handled in a timely manner...

Andy
User avatar
gloomyandy
leJOS Team Member
 
Posts: 4119
Joined: Fri Sep 28, 2007 2:06 pm
Location: UK

Re: Bluetooth data delay when receiving?

Postby Rickz2020 » Sun Jan 22, 2012 7:25 pm

Hey Andy,

(1) 100ms was the highest value I tried. Delay.msDelay(100);
(2) The android only sends data When I tilt the phone past a certain angle. With that method I am able to control precisely when data is transmitted. Yeah, I've got a PC and the LEGO Bluetooth device. I suppose I could write a small console app but I really don't think the problem is in the Android code.

Here's how I send the data from the Android:
Code: Select all
// make a wide forward left turn. Pitch and roll are sensor values and this statement is called in the onSensorChanged event.
         if (((pitch < -10) && (roll > 10))) {
            bt.sendNXTcommand(FWD_WIDE_LEFT, 680);
            fwd_wideTurn = true;
            movement = "Forward, Wide Left";
         }


If all else fails you could change things so that the Android app polls the NXT for sensor data when it has no movement commands to send to it. This should ensure that your movement commands are handled in a timely manner...


That's a good suggestion. I'll implement a check to see data is being sent and if it is, then don't receive. However, I'd really like to have the Android UI updated with sensor data constantly as well as being able to send data to the robot at the same time.
Rickz2020
Active User
 
Posts: 121
Joined: Thu Feb 10, 2011 5:47 pm
Location: London, Earth.

Re: Bluetooth data delay when receiving?

Postby gloomyandy » Sun Jan 22, 2012 8:05 pm

Hi,
I don't think you understand my point. How do you know that the Android app is not too busy receiving the data and so is unable to send it? Also when I said poll I really do mean poll. If the Android app has no commands to send make it send a "send me the sensor data now" and wait for the reply. That way you will throttle the data rate to match the capability of both ends of the connection.

Andy
User avatar
gloomyandy
leJOS Team Member
 
Posts: 4119
Joined: Fri Sep 28, 2007 2:06 pm
Location: UK

Re: Bluetooth data delay when receiving?

Postby Rickz2020 » Mon Jan 23, 2012 12:37 pm

Hmm, I did try put a delay after reading the DataInputStream as I thought, like you said, that the Android may have been tied busy sending data, but it made no difference. I'll give the polling idea a try a bit later. I really should try write a Bluetooth service, too.

Thanks.
Rickz2020
Active User
 
Posts: 121
Joined: Thu Feb 10, 2011 5:47 pm
Location: London, Earth.

Re: Bluetooth data delay when receiving?

Postby Rickz2020 » Mon Jan 23, 2012 8:23 pm

OMG. I decided to try out the Bluetooth Chat service example provided by Android and realized I needed to update the firmware on my device for it to work... Downloaded Samsung Kies, installed it, booted it up and attempted to update and guess what... It failed the update - got stuck at 8% - and my phone is now what they call "bricked". Just brilliant. I'll have to see what I can do and then attempt the communications again. What a setback :cry: :cry: :cry:
Rickz2020
Active User
 
Posts: 121
Joined: Thu Feb 10, 2011 5:47 pm
Location: London, Earth.

Re: Bluetooth data delay when receiving?

Postby Shawn » Tue Jan 24, 2012 10:29 am

Rickz2020 wrote:Hmm, I did try put a delay after reading the DataInputStream as I thought, like you said, that the Android may have been tied busy sending data, but it made no difference. I'll give the polling idea a try a bit later.


It really may have something to do with your threading on Android. I'm not exactly sure what you are doing there, but if it's not busy sending data, are you sure it's not tied up listening. Also, Android will read sensor tilt too often --way too often , so you may need to pause that thread reading it.

You are doing three things right.

1) listening for sensor data
2) monitoring tilt
3) sending

I don't think you can rule out Android being the trouble.

Anway, have a good look at the Android chat. That is where I got started. There is also some Android stuff in SVN. You might want to get to use that as a base, patch it where necessary and commit it back.
User avatar
Shawn
Advanced Member
 
Posts: 723
Joined: Wed Sep 12, 2007 4:59 am
Location: Tokyo

Re: Bluetooth data delay when receiving?

Postby Rickz2020 » Tue Jan 24, 2012 4:54 pm

Hi Shawn.

Only got my Android back a couple of hours ago because of the damn Samsung Kies (or so I thought). Turned out my USB port was not working properly and in the end that was the reason for my phone being bricked... Oh well, sorted that out now and have started studying the bluetooth chat app.

For anyone wondering why the bluetooth chat app wont work on their mobile device: It requires 3.0 and some phones (like mine) can only go up to 2.3.6 (samsung galaxy ace), so you have to remove the Actionbar and all it's references as it's not supported below 3.0.

Anyway, I've managed to get it all working and am currently setting up a BT service for my android.

As for tilt sensor sending data too often, I did think of that, but I have made it so that it only sends data when the phone is tilted past certain angles. But come to think about it... I don't think I was pausing the thread which was reading the sensor data while sending commands to the NXT. So I'll most definitely give that ago as well.

Thanks for the advice.

Rich.

PS: Nice, I didn't even know about SVN! :oops:
Rickz2020
Active User
 
Posts: 121
Joined: Thu Feb 10, 2011 5:47 pm
Location: London, Earth.

Re: Bluetooth data delay when receiving?

Postby gloomyandy » Tue Jan 24, 2012 5:02 pm

Hi you should also probably consider adding a delay into the send loop on the NXT, you almost certainly don't want to send the compass heading or whatever more than a few times a second...
User avatar
gloomyandy
leJOS Team Member
 
Posts: 4119
Joined: Fri Sep 28, 2007 2:06 pm
Location: UK

Re: Bluetooth data delay when receiving?

Postby Shawn » Wed Jan 25, 2012 8:40 am

Rickz2020 wrote:For anyone wondering why the bluetooth chat app wont work on their mobile device: It requires 3.0 and some phones (like mine) can only go up to 2.3.6 (samsung galaxy ace), so you have to remove the Actionbar and all it's references as it's not supported below 3.0.


Hey what? There is or was another version! It worked with anything 2.1 or higher IIRC, and it was even possible to backport it back to 1.5.
User avatar
Shawn
Advanced Member
 
Posts: 723
Joined: Wed Sep 12, 2007 4:59 am
Location: Tokyo

Re: Bluetooth data delay when receiving?

Postby Rickz2020 » Thu Jan 26, 2012 1:36 pm

Shawn wrote:Hey what? There is or was another version! It worked with anything 2.1 or higher IIRC, and it was even possible to backport it back to 1.5.


I've never found it :D

Anyways, I was still able to learn a lot from it and (start drum roll here) finally sorted this out.

After "bricking" my Android mobile and then "unbricking" it, I managed to study the Service API and some examples. I've written a service which Activites can bind to and then write data back to the NXT by calling one of the Service's methods. It's great because I can use the BroadcastReceiver in Activities to receive any data needed such as sensor data.

There is a slight issue. When I eg. tilt phone forward - sends data to NXT telling it to move forward - there is no delay unless I hold it in that position for a while...

It's as if there is a queue of data being sent out and data is being added to the queue faster than it is being sent. I think I'll have a go at polling the data when by brain stops smoking from the intense workout.

I'm still, as always, open to further suggestions to improve performance.

Thank you Andy and Shawn for you help!
Rickz2020
Active User
 
Posts: 121
Joined: Thu Feb 10, 2011 5:47 pm
Location: London, Earth.


Return to NXJ Software

Who is online

Users browsing this forum: No registered users and 2 guests

cron
more stuff