Bluetooth Motors Control GUI

Post your NXJ projects, project ideas, etc here!

Moderators: 99jonathan, roger, imaqine

Bluetooth Motors Control GUI

Postby soldierforce » Sat Mar 28, 2009 2:19 am

Hi,

I only started working with Lejos and Java a while ago. I have created a movable robot using the Pilot class and wanted to use a bluetooth GUI program to control the speed, rotation angle, distance forward or backward etc of the motors to have a idea of how I can work with bluetooth and the nxt. Does anyone have a bluetooth program which can control the motors of the NXT? So far I have looked at the BTSend and BTReceive samples but I couldn't understand how to edit that! :? It seems its easier with iCommand then NXJ! Surely this can be done with NXJ?

Thanks
soldierforce
New User
 
Posts: 15
Joined: Thu Oct 23, 2008 4:52 pm

Postby ChrisB01 » Sat Mar 28, 2009 9:20 am

I have a program that controls the nxt remotely but it controls the motors directly using my own patched together nxtcommand class from lejos and icommand. It looks like the developers are busy making a proper remote interface on sourceforge. You could either wait for the next release or try downloading a snapshot version and running that.

Chris
ChrisB01
Advanced Member
 
Posts: 189
Joined: Sat Mar 15, 2008 12:19 pm
Location: UK

Postby lawrie » Sat Mar 28, 2009 10:42 am

Yes, the next release will have more support for the LCP protocol including all the functionality of iCommand (other than possibly the vision stuff). It will also have a NXJControl tool that replaces NXJBrowser and includes remote monitoring and remote control.

However, you will always get better control of exactly what you want the NXT to do by sending your own commands over Java streams. This will also allow you to combine remote control with local autonomous behavior. What is hard about BTSend and BTReceive?

Better examples that do the sort of thing you want to do are the RCNavigator sample and the RCNavigationControl pcsample.
lawrie
leJOS Team Member
 
Posts: 909
Joined: Mon Feb 05, 2007 1:27 pm

Postby soldierforce » Sat Mar 28, 2009 1:02 pm

thanks for the replies! I'll have a look at the RCNavigator sample.

I know how to create the GUI and also how to connect from PC to NXT using the BTSend and Receive sample. What I don't understand is when a button is clicked how do I send that command to the NXT? as in the NXT isn't waiting for the value from that command as it already has default values so it will move around avoiding obstacles etc but what I want it to do is when I enter the value as in move forward 10 or rotate 30 I want it to do what the command said and replace the default values.
Do you think telling the NXT program to wait 10 seconds or so and if no commands issued by the PC then use default is a better idea?
My robot isnt stationary or doesnt wait for commands. It just keeps moving and avoiding obstacles all I want to try now is create a GUI to control the Pilot motors. something like an emergency control application if you know what I mean!
So far I actually enjoyed working with leJOS given that I only started a while ago I'm comfortable with that. Just trying to get a better understanding of bluetooth GUI PC-NXT communication.
soldierforce
New User
 
Posts: 15
Joined: Thu Oct 23, 2008 4:52 pm

Postby soldierforce » Sat Mar 28, 2009 1:04 pm

However, you will always get better control of exactly what you want the NXT to do by sending your own commands over Java streams. This will also allow you to combine remote control with local autonomous behavior.


That is exactly what I meant to do!
soldierforce
New User
 
Posts: 15
Joined: Thu Oct 23, 2008 4:52 pm

Postby lawrie » Sun Mar 29, 2009 6:35 pm

It sounds as if your problem is how to combine local autonomous behavior with remote control commands. This is really no different from deciding on which behavior to execute from different sensor input. The commands from the PC are just like another stream of sensor data. You can read the commands in a separate thread or poll them periodically in your main thread. A separate thread is probably the best approach. You then need to decide what priority to give your commands over local sensor input or local autonomous behavior.

There are lots of ways of doing this. You could do your local actions in a loop and before executing any of them, check to see if there is a remote command that might override the local action. You could use Behavior programming using the lejos.subsumption package, but I am not sure how easy it is to write a remote control behavior. Using the immediate return versions of the Pilot commands makes it easier to stop them if an overriding command comes in.
lawrie
leJOS Team Member
 
Posts: 909
Joined: Mon Feb 05, 2007 1:27 pm

Postby BHCluster » Thu Apr 02, 2009 11:28 pm

Hi soldierforce
I have done this for my final year project in the college if you are interested just click on the link bellow in the post you will find a link to my personal web site where you can download the source code and find all of the information about the application.

http://lejos.sourceforge.net/forum/viewtopic.php?t=840

Hope it helps.
http://nxt.tiim.info/
Eclipse
iCommand
Bluecove
leJOS (on NXT)
Installation and Setup Guide
Application Manual
User avatar
BHCluster
New User
 
Posts: 23
Joined: Thu Mar 13, 2008 2:36 pm

Postby soldierforce » Tue Apr 07, 2009 2:52 pm

Hi BHCluster, I already had a look at your project but I am not interested in iCommand. Basically because by default I want the robot to run without PC support. The Bluetooth application I want to create is opitional and will be used only when the user wants to, otherwise the robot works on its own without any interaction.
soldierforce
New User
 
Posts: 15
Joined: Thu Oct 23, 2008 4:52 pm

Postby soldierforce » Mon Apr 13, 2009 4:05 pm

Hi all, I have managed to create an application which uses bluetooth to send commands from the GUI to the program on the NXT. On the nxt the commands are received only when calling method is run - readGUICommand().
Isn't there any way to make a real-time application? That means the GUI buttons can be clicked anytime and everytime they are clicked the NXT will respond at the same moment. Something like there are three buttons on the GUI one is stop, second is rotate left (-90) and third is rotate right (90). The NXT is moving in a straight line and it keeps moving until the user clicks the stop button on the GUI. The NXT should also rotate left or right when the buttons are clicked all this has to be done while the NXT is still moving.
And there is no need to call the methods to read or write but is done automatically?
soldierforce
New User
 
Posts: 15
Joined: Thu Oct 23, 2008 4:52 pm

Postby lawrie » Tue Apr 14, 2009 6:39 pm

You can create a separate thread on the NXT that runs continuously, reads commands from the PC, and responds to them immediately.
lawrie
leJOS Team Member
 
Posts: 909
Joined: Mon Feb 05, 2007 1:27 pm

Postby soldierforce » Tue Apr 14, 2009 10:33 pm

are there any samples for doing this? couldn't quite understand what you mean. Do you mean a new method? like read(). and then keep calling read() in main? haven't actually used threads yet.
soldierforce
New User
 
Posts: 15
Joined: Thu Oct 23, 2008 4:52 pm

Postby lawrie » Wed Apr 15, 2009 8:27 pm

Here is an example with some local control and a thread that implements remote control from the PC.

Code: Select all
import lejos.navigation.*;
import lejos.nxt.*;
import lejos.nxt.comm.*;
import java.io.*;

public class LocalRemote {
  private Pilot pilot = new Pilot(5.6f,16.0f,Motor.A, Motor.C,true);
  private UltrasonicSensor sonic = new UltrasonicSensor(SensorPort.S1);
 
  public void run() {
    // Create thread, make it a daemon and start it
    Thread remote = new Thread(new RemoteControl());
    remote.setDaemon(true);
    remote.start();
   
    // Drive forward
    pilot.forward();
   
    // Avoid obstacles
    while(!Button.ESCAPE.isPressed()) {
      if (sonic.getDistance() < 40) {
        pilot.travel(-20);
        pilot.rotate(30);
        pilot.forward();
      }
      Thread.yield();
    }
  }

  public static void main(String[] args) {
    new LocalRemote().run();
  }

  /**
   * Thread to wait for a Bluetooth connection and execute remote commands
   */
  private class RemoteControl implements Runnable {
    private final static byte STOP = 0;
    private final static byte FORWARD = 1;
    private final static byte BACKWARD = 2;
    private final static byte LEFT = 3;
    private final static byte RIGHT = 4;
   
    public void run() {
      LCD.drawString("Waiting",0,0);
      NXTConnection conn = Bluetooth.waitForConnection();
      LCD.drawString("Connected",0,0);
      DataInputStream dis = conn.openDataInputStream();
     
      while(true) {
        try {
          byte command = dis.readByte();
         
          switch (command) {
          case STOP:
            pilot.stop();
            break;
          case FORWARD:
            pilot.forward();
            break;
          case BACKWARD:
            pilot.backward();
            break;
          case LEFT:
            pilot.steer(-200);
            break;
          case RIGHT:
            pilot.steer(200);
            break;
          }
          Thread.yield();
        } catch (IOException e) {
          System.out.println("IO Exception");
          System.exit(1);
        }
      }
    }
  }
}
lawrie
leJOS Team Member
 
Posts: 909
Joined: Mon Feb 05, 2007 1:27 pm


Return to NXJ Projects

Who is online

Users browsing this forum: No registered users and 1 guest

more stuff