Lejos bumpercar bluetooth takeover

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

Moderators: 99jonathan, roger, imaqine

Lejos bumpercar bluetooth takeover

Postby Shadowstep » Tue Mar 20, 2012 10:42 am

Ok so what I want to do is make a robot that makes use of Behavior programming to constantly listen on usb for a connection, and when it finds a connection I want it to stop doing all other things and from then on listen for commands received by bluetooth. I supporse I have to create another class that will be another behavior. but I don't really know where to start from there on.

greets Pieter
Shadowstep
New User
 
Posts: 22
Joined: Sun Jan 15, 2012 9:24 pm

Re: Lejos bumpercar bluetooth takeover

Postby Vertelman » Sun Apr 15, 2012 10:19 am

I have a similar problem. I have now got the following behaviors:
- driveforward (importance 0)
- hitobject (importance 1)
- waitforconnection (importance 2)

The first two work completely and now I'm to the point that I want to let the robot constantly listen to a connection being made. I have the following code:
Code: Select all
public class WaitForConnection implements Behavior {

    private boolean suppressed = false;
    private BTConnection btc = null;

    public WaitForConnection(){
        btc = null;
    }
         
    public void suppress() {
        suppressed = true;
    }

    public boolean takeControl() {
        btc = Bluetooth.waitForConnection();
        return (btc != null);
    }

    public void action() {
        suppressed = false;
        LCD.clear();
        LCD.drawString("connected", 0, 0);
        LCD.refresh();

        DataInputStream dis = btc.openDataInputStream();
        DataOutputStream dos = btc.openDataOutputStream();
        LCD.drawString("opened", 0, 1);
        try {
            char n = dis.readChar();
            LCD.drawChar(n, 0, 2);
            if (n == 'H') {
                LCD.drawString("accepted", 0, 3);
                dos.writeChar('A');
                dos.flush();
                ShakeHands();
            }
            while (!suppressed && !takeControl()) {
                Thread.yield();
            }
            dos.writeChar('E');
            LCD.drawString("ended", 0, 5);
            dos.flush();
            dis.close();
            dos.close();
            LCD.clear();
            LCD.drawString("closing", 0, 0);
            LCD.refresh();
            btc.close();
            LCD.clear();
        } catch (IOException ioe) {
            LCD.drawString(ioe.getMessage(), 0, 0);
        }

    }

    private static void ShakeHands() {
        LCD.clear();
        LCD.drawString("Shake hands", 0, 4);
        Sound.beepSequenceUp();
        //STUB
    }
}


If I run this, the robot doesn't do anything anymore and is only listening for a connection to be made. I suppose that the waitForConnection() method is always returning something that is not null, but what method or what instance variable can I use to check whether a connection is being made? I have checked the API's for Bluetooth, BTConnection and NXTConnection, but there are so many variables with minimum explanation that I don't see which one to use.
Vertelman
New User
 
Posts: 12
Joined: Tue Sep 01, 2009 8:14 am

Re: Lejos bumpercar bluetooth takeover

Postby gloomyandy » Sun Apr 15, 2012 11:15 am

waitForConnection does exactly hat it says it does, it WAITS for a connection so will not return until a connection is made to the NXT. If you want your robot to continue to do things while waiting fro the connection to be made you need to use another thread to run the connection code while allowing the main thread to run your other code....
User avatar
gloomyandy
leJOS Team Member
 
Posts: 4239
Joined: Fri Sep 28, 2007 2:06 pm
Location: UK

Re: Lejos bumpercar bluetooth takeover

Postby Vertelman » Sun Apr 15, 2012 11:20 am

sounds very easy, but I have no clue how to program that...
Any suggestions?
Or do you have an idea where I can find an example code on how to do that?
Vertelman
New User
 
Posts: 12
Joined: Tue Sep 01, 2009 8:14 am

Re: Lejos bumpercar bluetooth takeover

Postby skoehler » Sun Apr 15, 2012 1:45 pm

Google for a tutorial for using Threads in Java.
leJOS supports the standard Java Threads (java.lang.Thread, java.lang.Runnable) and the standard Java mechanisms for synchronization (synchronized, wait/notify/notifyAll()).
skoehler
leJOS Team Member
 
Posts: 1458
Joined: Thu Oct 30, 2008 4:54 pm


Return to NXJ Software

Who is online

Users browsing this forum: No registered users and 2 guests

more stuff