NXT Comms

Post your NXJ projects, project ideas, etc here!

Moderators: 99jonathan, roger, imaqine

NXT Comms

Postby esmetaman » Sun Sep 16, 2007 9:12 am

Hi,

I am developing a set of Classes to maintain a communication between 2 NXT Bricks.

At the moment, I have a Main class that it uses Threads to manage a User Interface and a BTListener. Currently, I have deactivated BTListener because I am tryiing to know the example BTReceive.java didn't manage a second connection.

When I execute in NXT1 NXTComm.nxj and in the second NXT brick I execute BTReceive.nxj, I notice that NXTComm.nxj send packet by BT to NXT brick2, but BTReceive finish to receive data and you see the message "Awaiting" if you try to send another packet with NXTComm, then BTReceive cant manage this connection.

I thought that my NXTComm runs badly, but I tried to turn off NXT with BT receive and run BTReceive again, and I was able to send twice with NXTComm.

Anybody knows the cause of this behavior?
Last edited by esmetaman on Sun Sep 16, 2007 10:16 am, edited 1 time in total.
Juan Antonio Breña Moral
http://www.juanantonio.info/lejos-ebook/
https://github.com/jabrena/livingrobots
http://www.iloveneutrinos.com/
User avatar
esmetaman
Advanced Member
 
Posts: 311
Joined: Wed Sep 13, 2006 12:16 am
Location: Madrid, Spain

NXT Comms

Postby esmetaman » Sun Sep 16, 2007 10:15 am

Code: Select all
import lejos.nxt.*;

public class NXTComm {

   /**
    * @param args
    */
   public static void main(String[] args) {

      try{
         BotSoul bObj = new BotSoul();
         NXTCommUI UIObj = new NXTCommUI(bObj,"NXT","NXT");
         //NXTCommListener NCL = new NXTCommListener();
         UIObj.start();
         //NCL.start();
      }catch(Exception e){
         
      }
   }

}

Juan Antonio Breña Moral
http://www.juanantonio.info/lejos-ebook/
https://github.com/jabrena/livingrobots
http://www.iloveneutrinos.com/
User avatar
esmetaman
Advanced Member
 
Posts: 311
Joined: Wed Sep 13, 2006 12:16 am
Location: Madrid, Spain

BotSoul

Postby esmetaman » Sun Sep 16, 2007 10:17 am

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

public class BotSoul {

   private String _botName;
   private int battery;
   private int memory;
   
   public BotSoul(){}

   public void setName(String botName){
      _botName = botName;
      BTUtilities BTUObj = new BTUtilities();
      BTUObj.setNXTName(botName);
   }

   public String getName(){
      return _botName;
   }

   public int getBattery(){
      battery = Battery.getVoltageMilliVolt();
      return battery;      
   }
   
   public int getMemory(){
      memory = (int)(Runtime.getRuntime().freeMemory());
      return memory;
   }
   
   //EXAMPLE
   static void sendCommand(String NXTReceiver) throws Exception{
      LCD.drawInt(0,0,5);
      //LCD.drawString("Try to connect...", 0, 6);
      LCD.refresh();
      
      //Bluetooth.
      BTRemoteDevice btrd = Bluetooth.getKnownDevice(NXTReceiver);

      BTConnection btc = Bluetooth.connect(btrd);
      
      //LCD.clear();
      LCD.drawInt(1,0,5);
      //LCD.drawString("                 ",0,6);
      //LCD.drawString("Connected", 0, 6);
      LCD.refresh();
      
      DataInputStream dis = btc.openDataInputStream();
      DataOutputStream dos = btc.openDataOutputStream();
            
      for(int i=0;i<100;i++) {
         try {
            LCD.drawInt(i*30000, 0, 6);
            LCD.refresh();
            dos.writeInt(i*30000);
            dos.flush();         
         } catch (IOException ioe) {
            LCD.drawString("Exception", 0, 7);
            LCD.refresh();
         }
         
         try {
            LCD.drawInt(dis.readInt(),0,7);
            LCD.refresh();
         } catch (IOException ioe) {
         }
      }
      
      try {
         dis.close();
         dos.close();
         btc.close();
      } catch (IOException ioe) {
      }      
   }
}

Last edited by esmetaman on Sun Sep 16, 2007 12:31 pm, edited 1 time in total.
Juan Antonio Breña Moral
http://www.juanantonio.info/lejos-ebook/
https://github.com/jabrena/livingrobots
http://www.iloveneutrinos.com/
User avatar
esmetaman
Advanced Member
 
Posts: 311
Joined: Wed Sep 13, 2006 12:16 am
Location: Madrid, Spain

BTUtilities

Postby esmetaman » Sun Sep 16, 2007 10:18 am

Code: Select all
import lejos.nxt.comm.*;

public class BTUtilities {

   public BTUtilities(){
      
   }
   
   public void setNXTName(String NXTName){
      
      byte[] nameBytes = new byte[16];
      
      nameBytes = getBytes(NXTName);
      
      Bluetooth.setFriendlyName(nameBytes);       
   }
   
   public byte[] getBytes(String BTDeviceName){
      byte[] nameBytes = new byte[16];
      
      for(int i=0;i<BTDeviceName.length();i++){
         nameBytes[i] = (byte) BTDeviceName.charAt(i);
      }
      nameBytes[BTDeviceName.length()] = 0;

      return nameBytes;
   }
}


Last edited by esmetaman on Sun Sep 16, 2007 11:54 am, edited 1 time in total.
Juan Antonio Breña Moral
http://www.juanantonio.info/lejos-ebook/
https://github.com/jabrena/livingrobots
http://www.iloveneutrinos.com/
User avatar
esmetaman
Advanced Member
 
Posts: 311
Joined: Wed Sep 13, 2006 12:16 am
Location: Madrid, Spain

NXTCommsUI

Postby esmetaman » Sun Sep 16, 2007 10:19 am

Code: Select all
import javax.microedition.lcdui.*;
import lejos.nxt.*;

public class NXTCommUI extends Thread implements CommandListener{

   String NXTCommsVersion = "NXT Comms 0.1";
   
   private static final int CMDID_BACK_TO_MAIN    = 1;
   private static final int CMDID_EXIT_APP       = 2;

   private static final Command BACK_COMMAND = new Command(CMDID_BACK_TO_MAIN, Command.BACK, 0);
    private static final Command EXIT_COMMAND = new Command(CMDID_EXIT_APP, Command.STOP, 2);

   private List    menu       = new List("Test Components", Choice.IMPLICIT);
   
   // Main menu items
   private TextBox input       = new TextBox("Your NXT:", "", 16, TextField.ANY);
   private TextBox input2       = new TextBox("Oponent NXT :", "", 16, TextField.ANY);
      
   private Alert    soundAlert    = new Alert("Sound Alert");
   private Alert    exitAlert    = new Alert("Exit");
   
   private Gauge   alertGauge  = new Gauge(null, false, 20, 0);

   
    private Display display;
   
    String NXT_WIN_FORM ="EMPTY";
   
    String NXTName;
    String NXTOponentName;

   private BotSoul _bObj;
   
   public NXTCommUI(BotSoul bObj){
      NXTName = "NXT";
      NXTOponentName = "NXT";
      input.setText(NXTName);
      input2.setText(NXTOponentName);
      
      _bObj = bObj;
   }

   public NXTCommUI(BotSoul bObj, String _NXTName,String _NXTOponentName){
      NXTName = _NXTName;
      NXTOponentName = _NXTOponentName;
      input.setText(NXTName);
      input2.setText(NXTOponentName);
      
      _bObj = bObj;
   }   
   
   public void menu(){
      // Create main menu
      menu = new List(NXTCommsVersion, Choice.IMPLICIT);
      menu.append("Set Name", null);
       menu.append("Set Oponent", null);
       menu.append("Send Command", null);
       menu.append("NXT Status", null);
       menu.append("About", null);

       menu.setSelectedIndex(0, true);
       menu.addCommand(EXIT_COMMAND);
       menu.setCommandListener(this);

       input.addCommand(BACK_COMMAND);
       input.setCommandListener(this);

       input2.addCommand(BACK_COMMAND);
       input2.setCommandListener(this);
     
       // Start displaying main menu and handling buttons
       display = Display.getDisplay();
       display.setCurrent(menu);
       display.show(true);      
   }
   
   private void setNXTBrickName(String name){
      if(name.length() >0){
         _bObj.setName(name);
      }
   }
   
   private void showNXTBrickStatus() throws Exception{
      LCD.clear();
      LCD.drawString("Battery / Memory",0,0);
      LCD.drawInt(_bObj.getBattery(),0,1);
      LCD.drawInt(_bObj.getMemory(),0,2);
      LCD.refresh();
   }

   private void showCredits() throws Exception{
      LCD.clear();
      LCD.drawString("Juan Antonio",0,2);
      LCD.drawString("Brenha. Moral",0,3);
      LCD.drawString("juanantonio.info",0,4);
      LCD.refresh();
   }      
   
   
   //Event controller
   public void commandAction(Command c, Displayable d){
      NXTName = input.getText();
      NXTOponentName = input2.getText();

      if (c.getCommandId() == CMDID_BACK_TO_MAIN) {
         this.setNXTBrickName(input.getText());

         // Display main menu again
         display.setCurrent(menu);
      } else if (c.getCommandId() == CMDID_EXIT_APP) {
         // Request to exit application
         exitAlert.setType(Alert.ALERT_TYPE_CONFIRMATION);
         exitAlert.setString("Exit Lejos?");
         exitAlert.setCommandListener(this);
         display.setCurrent(exitAlert);
      } else {
         // Handle system commands
         if (d == exitAlert) {
            if (exitAlert.getConfirmation()) {
               display.quit();
            } else {
               display.setCurrent(menu);
            }
         } else if (d == menu) {
            List list = (List) display.getCurrent();
            if (list.getSelectedIndex() == 0) {
               input.setText(NXTName);
               display.setCurrent(input);
            }
            else if (list.getSelectedIndex() == 1) {
               input2.setText(NXTOponentName);
               display.setCurrent(input2);
            }
            else if (list.getSelectedIndex() == 2) {
               try{
                  this._bObj.sendCommand(input2.getText());
               }catch(Exception e){
                  LCD.drawString(e.toString(),0,7);
               }
            }
            else if (list.getSelectedIndex() == 3) {
               try{
                  showNXTBrickStatus();
                  Thread.sleep(1000);
               }catch(Exception e){
                  LCD.drawString(e.toString(),0,7);
               }               
            }
            else if (list.getSelectedIndex() == 4) {
               try{
                  showCredits();
                  Thread.sleep(1000);
               }catch(Exception e){
                  LCD.drawString(e.toString(),0,7);
               }               
            }
         }
      }
   }   
   
   //Thread method
    public void run()
    {
        while(true)
        {
            try{               
              this.menu();
           }catch(Exception  e){

           }
        }
    }

}


Juan Antonio Breña Moral
http://www.juanantonio.info/lejos-ebook/
https://github.com/jabrena/livingrobots
http://www.iloveneutrinos.com/
User avatar
esmetaman
Advanced Member
 
Posts: 311
Joined: Wed Sep 13, 2006 12:16 am
Location: Madrid, Spain

NXTCommListener

Postby esmetaman » Sun Sep 16, 2007 10:21 am

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

public class NXTCommListener extends Thread{

   private String MESSAGE_AWAITING = "Awaiting";
   private String MESSAGE_CONNECTED = "Connected";
   
   //private Boolean NXT_CONNECTED = false;
   
   private Thread _MENU;
   
   private BTConnection btc;
   
   private InputStream is;
   private OutputStream os;
   private DataInputStream dis;
   private DataOutputStream dos;
   
   public NXTCommListener()throws Exception{
      
   }   
   
    public void setOpponents(Thread th1)
    {
       _MENU = th1;
        //opponent2 = th2;
    }   
   
   private void showMessage(String message){
      //.clear();
      LCD.drawString(message,0,1);
      LCD.refresh();
   }
   
    public void run()
    {
       while(true)
        {
            try
            {
               //Connect with other NXT Brick
                btc = Bluetooth.waitForConnection();
 
                //Create Input/Output Streams
                is = btc.openInputStream();
             os = btc.openOutputStream();
             dis = new DataInputStream(is);
             dos = new DataOutputStream(os);
             
            try {
                for(int i=0;i<100;i++) {
                   int ii = dis.readInt();
                   LCD.drawInt(ii,3,0,2);
                   LCD.refresh();
                   dos.writeInt(-ii);
                   dos.flush();
                }
            } catch (IOException ioe) {
            
            }
            
              try {
                 dis.close();
                 dos.close();
                 btc.close();
              } catch (IOException ioe){
                 //
              }
            }
            catch(Exception ex)
            {
               //
            }
        }
    }
}

Juan Antonio Breña Moral
http://www.juanantonio.info/lejos-ebook/
https://github.com/jabrena/livingrobots
http://www.iloveneutrinos.com/
User avatar
esmetaman
Advanced Member
 
Posts: 311
Joined: Wed Sep 13, 2006 12:16 am
Location: Madrid, Spain

Problems with NXT Comms

Postby esmetaman » Sun Sep 23, 2007 6:43 pm

Hi, I was testing NXTComm when I tried to send a message to NXT 2 and lejos Crashed. LCD showed:

Data Abort
PC 0010388E
AASR 00208D0F
ASR 00020502

I thought that I had to defrag NXT Brick and i made, but i tried to run again and NXT brick crashed. Then I delete old programs and I tried again, but crashed.

What is the cause?

I tried with the program BTConnectTest but I saw a Exception Message: Class 8, Method 79.

What is the problem?

thanks
Juan Antonio Breña Moral
http://www.juanantonio.info/lejos-ebook/
https://github.com/jabrena/livingrobots
http://www.iloveneutrinos.com/
User avatar
esmetaman
Advanced Member
 
Posts: 311
Joined: Wed Sep 13, 2006 12:16 am
Location: Madrid, Spain

Postby PatrickLismore » Fri Nov 16, 2007 8:48 pm

I would like to chat with you about your work,

Part of my project will involve connecting two robots together by Bluetooth and i will send data from the 1st robot to the 2nd,

So i will be doing exactly what you are doing so , get back to me as soon as you can.
User avatar
PatrickLismore
Active User
 
Posts: 81
Joined: Tue Oct 16, 2007 7:28 pm
Location: Prague

Postby esmetaman » Fri Nov 16, 2007 11:47 pm

Hi,

I would like to work in your project to evolve NXTComms.

My idea is to create a basic mechanics to set a bidirectional communication between 2 NXT bricks in a first phase.
Juan Antonio Breña Moral
http://www.juanantonio.info/lejos-ebook/
https://github.com/jabrena/livingrobots
http://www.iloveneutrinos.com/
User avatar
esmetaman
Advanced Member
 
Posts: 311
Joined: Wed Sep 13, 2006 12:16 am
Location: Madrid, Spain

Postby PatrickLismore » Thu Nov 22, 2007 4:08 am

I'd would very much like for you to join me in my project but its a University Dissertation,

So i have to do it myself,

but it will be very helpful to chat about your work and my work and maybe we will both learn together.
User avatar
PatrickLismore
Active User
 
Posts: 81
Joined: Tue Oct 16, 2007 7:28 pm
Location: Prague

Postby esmetaman » Thu Nov 22, 2007 8:42 pm

No problem!

I am deligthed with the idea. If you like we can work together.
Juan Antonio Breña Moral
http://www.juanantonio.info/lejos-ebook/
https://github.com/jabrena/livingrobots
http://www.iloveneutrinos.com/
User avatar
esmetaman
Advanced Member
 
Posts: 311
Joined: Wed Sep 13, 2006 12:16 am
Location: Madrid, Spain


Return to NXJ Projects

Who is online

Users browsing this forum: Yahoo [Bot] and 1 guest

cron
more stuff