4 NXT robots communication protocol

Post your NXJ projects, project ideas, etc here!

Moderators: roger, 99jonathan, imaqine

4 NXT robots communication protocol

Postby Vertelman » Sun Apr 08, 2012 4:27 pm

Hi all,

Long time since I was here, but so far I didn't really have any problems, or could find my answers online.

The question I have now is quite a general one I'm afraid.

I want to let 4 robots (a blue, red, green and yellow one) wander around in a general space. Once a robot (say red) detects another robot (say blue) in front of it via the color sensor it should call blue and tell it to turn so that it faces red. In the mean time yellow and green keep wandering about and can also meet.

Now I need to figure out how to let the communication between the bots take place. All four of them should do their own thing (wander around and turn if they bump into a wall) until they meet, then the pair should connect and do something together.
The examples in the Lejos folder all have one master and one slave. The slave is only waiting for the master to call him. Is it actually possible to let the slave do things when the master is not calling him?

I suppose that we should start the entire thing by connecting all together (meaning 6 connections in total, maybe too much at the same time?). Then I could let one bot be the master, but if two slaves meet, how to make sure that they communicate: do it via the master? However the whole master slave communication is a hassle, it should be more intuitive if a robot just could say "hey, blue I see you turn around please" and that blue responds no matter who is calling him.

I find it quite hard to describe my question. I suppose the general question is: when using bluetooth do you really need one master and one slave, or could you do it in another way?
Vertelman
New User
 
Posts: 12
Joined: Tue Sep 01, 2009 8:14 am

Re: 4 NXT robots communication protocol

Postby skoehler » Sun Apr 08, 2012 4:40 pm

IMHO, it is best to let all four of them communicate with the PC, which server as a kind of "message hub". It would allow you to send messages from one robot to another.
skoehler
leJOS Team Member
 
Posts: 1128
Joined: Thu Oct 30, 2008 4:54 pm

Re: 4 NXT robots communication protocol

Postby Vertelman » Sun Apr 08, 2012 5:43 pm

I'm using now Lejos 0.9.0. I can find the following examples:
BTSend in the pcsamples folder. It states that the NXT should then run something like BTReceive or Signaltest from the samples folder. But isn't this still the same problem: all bots have now become a slave and are waiting for the pc to connect to them and are not actually doing anything. Or am I missing something here?
When a bot uses a program like BTreceive, can it do anything while it is waiting for something to call it?
Vertelman
New User
 
Posts: 12
Joined: Tue Sep 01, 2009 8:14 am

Re: 4 NXT robots communication protocol

Postby skoehler » Sun Apr 08, 2012 6:27 pm

Well, the solution I described has to be written by yourself. The PC can connect to all four NXTs and then allows all four NXTs to exchange messages. LeJOS does not contain a ready-made implementation of the proposed technique. In fact, BTSend and BTReceive are a good starting point.

Another solution is to let the NXTs connect to each other directly using Bluetooth, which is in fact possible. However, note that a single NXT should only have one Bluetooth connection at a time for performance reasons. Up to three connections are allowed, however, switching between them is slow.
skoehler
leJOS Team Member
 
Posts: 1128
Joined: Thu Oct 30, 2008 4:54 pm

Re: 4 NXT robots communication protocol

Postby dip » Fri Apr 13, 2012 7:33 am

I'm asking this in behalf of Vertelman, she doesn't have time to ask the question today herself;

Is it possible to have a NXT do a task while listening for an other command via bluetooth and be able to send a command.

Red wanders around, turning when it hits a wall. No problem programming that.
Red wanders around and sees blue. Stops wandering and Sends a message to blue (or a computer). No problems
Red wanders around and is seen by blue. Red gets a signal to turn around to face blue. Not quite clear how to do this

And combining the option that red either sends a signal or recieves a signal while performing another task, is that even possible?
dip
New User
 
Posts: 3
Joined: Fri Apr 13, 2012 7:22 am

Re: 4 NXT robots communication protocol

Postby skoehler » Fri Apr 13, 2012 7:40 am

Use Threads. leJOS supports the ordinary Java Threads (see java.lang.Thread) and the typical mechanism for synchronization (synchronized, wait, notify/notifyAll).
skoehler
leJOS Team Member
 
Posts: 1128
Joined: Thu Oct 30, 2008 4:54 pm

Re: 4 NXT robots communication protocol

Postby Vertelman » Sat Apr 14, 2012 9:30 am

Next problem;

We have bluetooth connection with the NXT via NXJcontrol (I can rotate a motor and see sensor data), we using lejos 0.9.0

We run BT recieve on the NXT

We compile BT send (gives warning, but compiles)
Code: Select all
compile-selected-files-in-BTSend:
D:\LegoProjects\pcsamples\nbproject\ide-file-targets.xml:105: warning: 'includeantruntime' was not set, defaulting to build.sysclasspath=last; set to false for repeatable builds
BUILD SUCCESSFUL (total time: 0 seconds)


Then we try to run the file...
Code: Select all
run-selected-file-in-BTSend:
java.lang.NoClassDefFoundError: BTSend
Caused by: java.lang.ClassNotFoundException: BTSend
   at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
   at java.security.AccessController.doPrivileged(Native Method)
   at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
   at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
   at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
   at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
Could not find the main class: BTSend.  Program will exit.
Exception in thread "main" D:\LegoProjects\pcsamples\nbproject\ide-file-targets.xml:25: Java returned: 1
BUILD FAILED (total time: 0 seconds)


Do we have a problem with pccomm and bluecove.jar? Or something else?

EDIT: also USB send fails
Vertelman
New User
 
Posts: 12
Joined: Tue Sep 01, 2009 8:14 am

Re: 4 NXT robots communication protocol

Postby skoehler » Sat Apr 14, 2012 12:36 pm

You have a problem with your Netbeans project setup. I can't really help you with that. All I know is that the output folder of the javac-task is not part of the classpath of the java-task. But as far as I know, ide-file-targets.xml is generated by netbeans, or is it?

Actually, switch to Eclipse if you can. Eclipse+leJOS plug-in is much better than Netbeans.
skoehler
leJOS Team Member
 
Posts: 1128
Joined: Thu Oct 30, 2008 4:54 pm

Re: 4 NXT robots communication protocol

Postby dip » Sun Apr 15, 2012 11:29 am

Fixed this problem! ( I now see that I posted the last question using my wife's account). It has taken me a day to (re-)install java, lejos and eclips. But now I have eclips installed and BT and USB test programs run fine. Now getting my wife to switch to eclips... Honey are you reading this?
dip
New User
 
Posts: 3
Joined: Fri Apr 13, 2012 7:22 am

Re: 4 NXT robots communication protocol

Postby dip » Fri Apr 27, 2012 6:02 am

Tought you guys might like to see the actual robots. They will be in action at afolcon.

Image
Red and yellow together by d124124 aka dip, on Flickr

Image
Searching for his friend by d124124 aka dip, on Flickr
dip
New User
 
Posts: 3
Joined: Fri Apr 13, 2012 7:22 am

Re: 4 NXT robots communication protocol

Postby Vertelman » Mon Apr 30, 2012 7:49 am

Hi all,

I have managed to write the programs for two bots and the PC at the moment.

Things that work:
- blue bot can spot the red bot, send a message to the PC, PC sends a message to red and then they start doing their routine together at the same time and after continue wandering around

Now I wanted to do the reverse as well (red sees the blue bot, etc.) with the same code on both sides, but that doesn't work. It seems to come down to the code of the PC: the PC first checks if blue sends a message and after checks if red sends a message:
Code: Select all
public class PCMain {
   private static Connection red;
   private static Connection blue;

   public static void main(String[] args) {
      blue = new Connection("BLUE", 'B');
      red = new Connection("RED", 'R');
      
      while(true)
      {
         try{
         
         if(blue.readCorrectChar())
         {
            char x = blue.readChar();
            goThroughConnections(blue, x);
         }

         if(red.readCorrectChar())
         {
            char x = red.readChar();
            goThroughConnections(red, x);
         }
      }
      catch(Exception e){}
      }
   }

   private static void goThroughConnections(Connection firstRobot, char x) throws Exception {
      if(x == 'R'){
         firstRobot.connectTo(red);
      }
   /**   else if(x == 'Y'){
         firstRobot.connectTo(yellow);
         Thread.sleep(50);
      }
      else if (x == 'G'){
         firstRobot.connectTo(green);
         Thread.sleep(50);
      }*/
      else if (x == 'B'){
         firstRobot.connectTo(blue);
      }
      
   }
}


In this way, the blue bot can spot the red bot and they perform their routine, but the red bot cannot spot the blue bot and then perform their routine. If I change the code of the PC like this:

Code: Select all
   ...   while(true)
      {
         try{
         
         if(red.readCorrectChar())
         {
            char x = red.readChar();
            goThroughConnections(red, x);
         }

         if(blue.readCorrectChar())
         {
            char x = blue.readChar();
            goThroughConnections(blue, x);
         }

      }
      catch(Exception e){}
      }...


The red bot can spot blue and perform their routine, but the blue bot cannot spot the red bot.
Who has any clue how to solve this?
I have already played around with the delay times in the sending of the messages of the two bots (making it shorter or longer).
If people need the complete source code from both bots and the PC, you can find it in my dropbox account: https://www.dropbox.com/sh/p2gkcjm0lh2mrcx/C0jNqf-ZA9

I would be very much obliged if someone can help me quickly, we want to demonstrate the entire thing (with four bots) on afolcon (http://www.afolcon.com) coming Thursday... (and we need to catch a boat to the UK from the Netherlands on Wednesday already)
Vertelman
New User
 
Posts: 12
Joined: Tue Sep 01, 2009 8:14 am


Return to NXJ Projects

Who is online

Users browsing this forum: No registered users and 1 guest

more stuff