Robot Movement Detection

Post your NXJ projects, project ideas, etc here!

Moderators: 99jonathan, roger, imaqine

Robot Movement Detection

Postby win1for » Tue Mar 02, 2010 11:26 pm

Hi Guys,

I am developing a simulation for a robot in a warehouse. At the moment the robot and the warehouse are all in Lego.

My task is the following:

I have to develop a GUI for the robot and the crossings(roads/rails) on which the robot is moving on. This means that anytime the robot is moving, i have to visualize its movement on the GUI. Whenever the crossings changes direction, they have to be visualized as well on the GUI. The whole project is i have to visualize all the activities (the robot movements, the crossings changing directions) on the GUI.

The robot uses a Touch Sensor: that is, whenever the Touch Sensor is pressed, then the robot is not moving, and when the Touch Sensor is released, then it means the robot is moving until the sensor is pressed again which indicates that it has stopped.

My question are:

1)
How can i send information from the robot(whiles it is moving) to the GUI constantly to tell the GUI that the robot is moving(in order to update the GUI) till it stop.

2)
I need suggestions how to implement the GUI so that it can update itself in real-time with the robot movements and changing of crossings

3)
I would like to know how to detect if a robot get stuck whiles the Touch Sensor is still in released mode. I am using a robot that uses the Touch Sensor to detect its movement. When the Sensor is pressed, then the robot is still/stop. When it is released, then it is moving.

It is possible that the robot is hindered by an object whiles the Touch sensor is still released. In this case how can i detect that the robot is not moving while the sensor is still released.

4)
All other suggestions are welcomed



Thanks for your help.
win1for
Novice
 
Posts: 48
Joined: Tue Mar 02, 2010 11:22 pm

Postby exuvo » Sun Mar 14, 2010 1:09 pm

1. Have a look at the examples included with lejos. Specifically SocketTest.java/SocketServer.java .

2. I have a loop on my nxt that sends status info every 2sec and the pc interprets them and updates a swing gui. I send data as strings.

Take a look at the Navigator class it allows the robot to use X and Y to move around. Simplifies drawing on a GUI.

My NXT communication class (some code needs to be commented out due to that i don't post all classes used)
Code: Select all
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.EOFException;
import java.io.IOException;
import java.net.Socket;

import lejos.nxt.Sound;
import lejos.nxt.comm.BTConnection;
import lejos.nxt.comm.Bluetooth;

//NXT.waita(200)  = try{Thread.sleep(200) catch(Exception e){}
public class Communication extends Thread{
   private DataInputStream ins;
   private DataOutputStream outs;
   private BTConnection btc = null;
   private Socket sock = null;
   private Object writeLock = new Object();
   private Object readLock = new Object();
   
   Communication(){
      setDaemon(true);
   }
   
   public void run(){
      while(true){
         while(true){  //The break below stops this loop so that the other code gets skipped.
            try {
               btc = Bluetooth.waitForConnection();
               sock = new Socket("localhost", 8081, btc);
            } catch (IOException e) {
               System.out.println("Failed to connect to server");
//Debug sounds               
Sound.playNote(Sound.PIANO, 500, 500);
               NXT.waita(200); 
               Sound.playNote(Sound.PIANO, 500, 500);
               break;
            }
            
            try {
               ins = sock.getDataInputStream();
               outs = sock.getDataOutputStream();
            } catch (IOException e1) {
               System.out.println("IO Exception while getting IO streams");
               Sound.playNote(Sound.PIANO, 1000, 500);
               NXT.waita(200);
               Sound.playNote(Sound.PIANO, 1000, 500);
            }
            
            while(true){
               try {
                  String s = readLine();
                  
                  if (s.equals("bye")){
                     NXT.close();
                  }else if (s.equals("status")){
                     send("_MA" + Motor.A.getTachoCount());
                     send("_MB" + Motor.B.getTachoCount());
                     send("_MC" + Motor.C.getTachoCount());
                     send("_UL" + UltraSound.getDistance());
                  }else if (s.equals("reset")){
                     close();
                     System.exit(0);
                  }
      
               } catch(EOFException e){
                  send("EOFException while reading");
                  break;
               } catch(IOException e){
                  send("IO Exception while reading");
                  break;
               }
            }
            
            close();
         }
      }
   }
   
   public void close(){
      try {
         ins.close();
         outs.close();
      } catch (IOException e) {
         System.out.println("IO Exception while closing");
         Sound.playNote(Sound.PIANO, 1500, 500);
         NXT.waita(200);
         Sound.playNote(Sound.PIANO, 1500, 500);
      }
      outs=null;
      sock.close();
   }
   
   private String readLine() throws IOException{
      StringBuffer sb = new StringBuffer();
      
      synchronized(readLock){
         while(true) {
            char c = ins.readChar();
            if (c == '\n') break;
            sb.append(c);
         }
         return sb.toString();
      }
   }
   

         //Other threads can call this method and the synchronized ensures that the message remains intact.
    public void send(String s){
      if(outs!=null){ //avoids null pointer exception
         synchronized(writeLock){
            try {
               outs.writeChars(s + '\n');
               outs.flush();
            } catch (IOException e) {
               System.out.println("IO Exception while sending");

               Sound.playNote(Sound.PIANO, 2000, 500);
               NXT.waita(200);
               Sound.playNote(Sound.PIANO, 2000, 500);
            }
         }
      }
   }
   
}


PC code
Code: Select all
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
import javax.swing.*;

/**
 *I exuvo used the example code and then modified it to my needs.
 *Comments below may not be correct anymore.
 *----------------------------------------------
 * This PC sample works with the leJOS NXJ sample, SocketTest.
 *
 * It shows you how to connect a program running on the NXT to a TCP/IP server
 * on your PC, intranet on the Internet. The host program uses the standard Java
 * SocketServer class, and the NXT program uses the NXTSocket class.
 *
 * To run the sample, first start SocketTest on the NXT and it will wait for
 * a connection to the Socket Proxy.
 *
 * Then run this program and it will display a small GUI window. Change the port
 * it listens on if 8081 is used on your PC. If you do this you will need to change
 * SocketTest as well.
 *
 * Then run the Socket Proxy (nxjsocketproxy command) optionally specifying the name and
 * or address of the NXT you want to connect to. This will cause Socket Proxy to
 * connect to SocketTest on the NXT and SocketTest to connect to this socket server.
 *
 * You can then type words in the GUI and see the responses come back from the NXT (
 * on the standard output stream).
 *
 * Type "bye" to cause SocketTest on the NXT to stop.
 *
 *
 * @author Ranulf Green and Lawrie Griffiths and exuvo
 *
 */
public class NXTConsole {

   DataOutputStream outToSocket = null;
   DataInputStream inFromSocket = null;
   JLabel MA = new JLabel("Motor A Rotation:    ");
   JLabel MB = new JLabel("Motor B Rotation:    ");
   JLabel MC = new JLabel("Motor C Rotation:    ");
   JLabel Ultra = new JLabel("Ultra Distance:   ");
   JLabel LL = new JLabel("Light Left:   ");
   JLabel LR = new JLabel("Light Right:   ");

   public NXTConsole(){
      JFrame frame = new JFrame();
      JPanel panel = new JPanel(new FlowLayout());
      final JTextField input = new JTextField(10);
      JButton send = new JButton("Send");

      panel.add(input);
      panel.add(send);
      panel.add(MA);
      panel.add(MB);
      panel.add(MC);
      panel.add(Ultra);
      panel.add(LL);
      panel.add(LR);

      send.addActionListener(new ActionListener(){

         public void actionPerformed(ActionEvent arg0) {
            String s = input.getText();
            System.out.println("Sending: \"" + s + "\"");
            s+='\n';
            try {
               outToSocket.writeChars(s);
               outToSocket.flush();
            } catch (IOException e) {
               e.printStackTrace();
            }
         }
      });
      
       WindowListener listener = new WindowAdapter() {
           public void windowClosing(WindowEvent w) {
             System.exit(0);
           }
         };
        
       frame.addWindowListener(listener);
        
      frame.add(panel);
      frame.pack();
      frame.setVisible(true);
      
      new Listen();
      new Reader();
   }
   
   private class Listen extends Thread{
      
      Listen(){
         start();
      }
      
      public void run(){
         try {
            ServerSocket s = new ServerSocket(8081);
            while(true){
               Socket sock = s.accept();
               System.out.println("Socket Server Connected");
               outToSocket = new DataOutputStream(sock.getOutputStream());
               inFromSocket = new DataInputStream(sock.getInputStream());
            }
         } catch (IOException e) {
            e.printStackTrace();
         }
      }
   }
   
   private class Reader extends Thread{
      
      Reader(){
         start();
      }
      
      public void run(){
         // Read line
         while(true){
            while(inFromSocket!=null){
               try{
                  StringBuffer sb = new StringBuffer();
                  char c;
                  while(true){
                     c = inFromSocket.readChar();
                     if (c == '\n') break;
                     sb.append(c);
                  }
                  String s = sb.toString();
                  
                  
                  if(s.startsWith("_MA")){
                     MA.setText("Motor A Rotation: " + s.substring(3));
                  }else if(s.startsWith("_MB")){
                     MB.setText("Motor B Rotation: " + s.substring(3));
                  }else if(s.startsWith("_MC")){
                     MC.setText("Motor C Rotation: " + s.substring(3));
                  }else if(s.startsWith("_UL")){
                     Ultra.setText("Ultra Distance: " + s.substring(3));
                  }else if(s.startsWith("_LL")){
                     LL.setText("Light Left: " + s.substring(3));
                  }else if(s.startsWith("_LR")){
                     LR.setText("Light Right: " + s.substring(3));
                  }
                  
                  //System.out.println("Read: " + s);
               } catch (IOException e) {
                  System.err.println("IOException while reading");
                  try {
                     sleep(100);
                  } catch (InterruptedException e1) {
                  }
               } catch (NullPointerException e){
                  try {
                     sleep(100);
                  } catch (InterruptedException e1) {
                  }
                  //System.err.println("NullPointerException while reading");
               }
            }
         }
      }
      
   }


   public static void main(String[] args) {
      new NXTConsole();
   }
}


3. Check if the motors are still rotating using Motor.A.getTachoCount()
Easiest way is to have a second Thread that is counting the motors rotations (while(Motor.A.isMoving()) and if it gets too low its stalled.
Last edited by exuvo on Mon Mar 15, 2010 6:03 am, edited 2 times in total.
exuvo
New User
 
Posts: 4
Joined: Wed Feb 10, 2010 9:39 pm

Postby win1for » Sun Mar 14, 2010 8:55 pm

I am going to spend time to study the code and check the other examples you said.

But b4 i do that i want to say a million thanks for your reply. I appreciate it. Thanks.
win1for
Novice
 
Posts: 48
Joined: Tue Mar 02, 2010 11:22 pm

Postby exuvo » Mon Mar 15, 2010 9:13 pm

Also read http://lejos.sourceforge.net/nxt/nxj/tutorial/index.htm if you haven't already. The stuff after "Controlling the motors" is where it gets interesting.
exuvo
New User
 
Posts: 4
Joined: Wed Feb 10, 2010 9:39 pm

Postby SpikeT » Tue Mar 23, 2010 5:50 pm

Another thing or two just to keep in mind,
The Tachometer will not reset if the motor didn't stop first, and the NXT processor and I/O can't handle the same thing as PC does, so be prepared for that.
SpikeT
New User
 
Posts: 19
Joined: Tue Sep 22, 2009 3:44 am

NEED HELPING

Postby Zaleekha » Fri Apr 02, 2010 3:53 pm

Hi all,
i'm working in Lego mindstorms as a senior project i'm looking for a java code that can control the Robot by the mobile.

I download it icommand packege for windows but the same error is
" icommand package does not exit"

please help me and reply for me if anyone Know about this????[b]
[/b]
Zaleekha
New User
 
Posts: 5
Joined: Fri Apr 02, 2010 3:45 pm

Postby SpikeT » Sat Apr 03, 2010 5:01 pm

The example program that you have might use an older Lejos library. Have you checked the API list?
SpikeT
New User
 
Posts: 19
Joined: Tue Sep 22, 2009 3:44 am

Re: Robot Movement Detection

Postby ivymike » Sat Apr 03, 2010 5:11 pm

win1for wrote:I am developing a simulation for a robot in a warehouse.

I have to develop a GUI for the robot and the crossings(roads/rails) on which the robot is moving on.


Have you checked this project? http://ddi.uni-paderborn.de/software/le ... lator.html
ivymike
New User
 
Posts: 1
Joined: Sat Apr 03, 2010 5:10 pm

NXT

Postby Zaleekha » Sun Apr 04, 2010 6:29 pm

Hello SpikeT,
thanks for replay me......
but where can I found API???
Zaleekha
New User
 
Posts: 5
Joined: Fri Apr 02, 2010 3:45 pm

Re: NEED HELPING

Postby Shawn » Sun Apr 11, 2010 2:45 am

Zaleekha wrote:[b]Hi all,
i'm working in Lego mindstorms as a senior project i'm looking for a java code that can control the Robot by the mobile.


I assume mobile = phone. If it's j2me, then I'd go to http://sourceforge.net/projects/lejos/develop and checkout the project. I see j2mecomms, j2mesamples, and javame.

I'd try the j2mesamples. j2mecomms is likely what you'd need to connect via bluetooth but I'm not sure how to build it or if there is a binary someplace.

I'm sure of the status of those

If you mean android, then I'd look at http://github.com/Shawn-in-Tokyo/leJOS-Droid
User avatar
Shawn
Advanced Member
 
Posts: 723
Joined: Wed Sep 12, 2007 4:59 am
Location: Tokyo

NXT

Postby Zaleekha » Wed Apr 14, 2010 1:41 pm

I need help???
which one is better using NXT2.0 firmware or Legoes when I want to communicate the NXT by mobile=phone?????

How to control NXT with mobile using my own java program????
Zaleekha
New User
 
Posts: 5
Joined: Fri Apr 02, 2010 3:45 pm

Postby SpikeT » Sat Apr 17, 2010 11:44 pm

Zeleekha; you can find the API list in this website, well maybe you know this by now.

For a cell phone command,
a guy named iWitzand posted his work
(thesis) at http://code.google.com/p/gelway/

His robot can be controlled with a cell phone.
SpikeT
New User
 
Posts: 19
Joined: Tue Sep 22, 2009 3:44 am

help me

Postby Zaleekha » Mon Apr 26, 2010 9:10 am

SpikeT,
thanks alot to reply for me.....
can you give me brief idea of how to control the NXT by GPS from mobile?????????
i mean how the NXT going to reseive the GPS data from a mobile.
which one is better using RobotC or the standard fraimware NXT with NMEA format .........
can i control RobotC from mobile....??????
Zaleekha
New User
 
Posts: 5
Joined: Fri Apr 02, 2010 3:45 pm


Return to NXJ Projects

Who is online

Users browsing this forum: Yahoo [Bot] and 2 guests

cron
more stuff