Delay

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

Moderators: roger, 99jonathan, imaqine

Delay

Postby nxtnewbie » Fri Mar 08, 2013 3:16 pm

Hello!
I have a code problem.
I have made two programs (using BTSend and BTReceive samples). The BTSend program, is running on the PC and the BTReceive on the NXT.
In BTReceive, the PC takes an integer number from the keyboard and sends it to the NXT.
In BTSend, the NXT receives the integer number and according to its value, it performs the corresponding action:
1: Move Forward
2: Turn Right
3: Turn Left
4: Stop

The problem is the following:
When I enter 1, the robot starts moving forward instantaneously, but if after a while press 4, the robot continues moving forward and stops after a delay that varies from 7 to 13 seconds. If after that I press 3 for example, it starts turning right instantaneously, but if after a while press 1 for example, it continues turning left and turns right after the delay I mentioned above.
I attach the code for both programs and I hope someone could help me out.

BTSend Code:
Code: Select all
package org.lejos.pcsample.btsend;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;

import lejos.pc.comm.NXTCommLogListener;
import lejos.pc.comm.NXTConnector;

/**
 * It connects to the NXT, and sends an integer
 *
 * Compile this program with javac (not nxjc), and run it
 * with java.
 *
 * You need pccomm.jar and bluecove.jar on the CLASSPATH.
 * On Linux, you will also need bluecove-gpl.jar on the CLASSPATH.
 *
 * Run the program by:
 *
 *   java BTSend
 *
 * Your NXT should be running a sample such as BTReceive or
 * SignalTest. Run the NXT program first until it is
 * waiting for a connection, and then run the PC program.
 */
public class BTSend {   
   public static void main(String[] args) {
      NXTConnector conn = new NXTConnector();
      while(true){
         conn.addLogListener(new NXTCommLogListener(){

            public void logEvent(String message) {
               System.out.println("BTSend Log.listener: "+message);
            }

            public void logEvent(Throwable throwable) {
               System.out.println("BTSend Log.listener - stack trace: ");
               throwable.printStackTrace();
            }
         }
               );
         // Connect to any NXT over Bluetooth
         boolean connected = conn.connectTo("btspp://");


         if (!connected) {
            System.err.println("Failed to connect to any NXT");
            System.exit(1);
         }

         DataOutputStream dos = new DataOutputStream(conn.getOutputStream());
         DataInputStream dis = new DataInputStream(conn.getInputStream());

         System.out.println("Hello! Please enter an integer number: ");
         BufferedReader userInput = new BufferedReader(new InputStreamReader(System.in ));
         int a=-1;
         try {
            a = Integer.parseInt(userInput.readLine());
         } catch (NumberFormatException | IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
         }      
         if (a==0)
            break;

         System.out.println("The number you entered is: " + a);

         try {
            System.out.println("Sending " + a);
            dos.writeInt(a);
            dos.flush();   
            try {
               Thread.sleep(200);
            } catch (InterruptedException e) {
               e.printStackTrace();
            } // wait for data to drain

         } catch (IOException ioe) {
            System.out.println("IO Exception writing bytes:");
            System.out.println(ioe.getMessage());
         }

         try {
            System.out.println("Received " + dis.readInt());
         } catch (IOException ioe) {
            System.out.println("IO Exception reading bytes:");
            System.out.println(ioe.getMessage());
         }

         try {
            dis.close();
            dos.close();
            conn.close();
         } catch (IOException ioe) {
            System.out.println("IOException closing connection:");
            System.out.println(ioe.getMessage());
         }
      }
   }
}



BTReceive Code:
Code: Select all
package org.lejos.sample.btreceive;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import lejos.robotics.navigation.DifferentialPilot;
import lejos.nxt.LCD;
import lejos.nxt.Motor;
import lejos.nxt.comm.BTConnection;
import lejos.nxt.comm.Bluetooth;
import lejos.nxt.Button;

/**
 * Receive data from another NXT, a PC, a phone,
 * or another bluetooth device.
 * Waits for a connection, receives an int and performs the corresponding action according to the
 * numbers below:
 * 1: Move Forward
 * 2: Move Right
 * 3: Move Left
 * 4: Stop
 */
public class BTReceive {

   public static void main(String [] args)  throws Exception
   {
      String connected = "Connected";
      String waiting = "Waiting...";
      String closing = "Closing...";
      DifferentialPilot pilot = new DifferentialPilot(3.5f,13f,Motor.C, Motor.B,false);
      pilot.setTravelSpeed(720);// 2 RPM
      
      while (!Button.ESCAPE.isDown())
      {
         LCD.drawString(waiting,0,0);
         LCD.refresh();

         BTConnection btc = Bluetooth.waitForConnection();

         LCD.clear();
         LCD.drawString(connected,0,0);
         LCD.refresh();   

         DataInputStream dis = btc.openDataInputStream();
         DataOutputStream dos = btc.openDataOutputStream();
         
         int n = dis.readInt();
         LCD.drawInt(n,7,0,1);
         LCD.refresh();
         dos.writeInt(n);

         if(n==1||n==2||n==3||n==4){
            if(n==1){
               pilot.forward();               
            }
            else if(n==2){
               pilot.rotateRight();
            }
            else if(n==3){
               pilot.rotateLeft();
            }
            else{
               pilot.stop();
            }
         }

         dos.flush();
         dis.close();
         dos.close();
         Thread.sleep(100); // wait for data to drain
         LCD.clear();
         LCD.drawString(closing,0,0);
         LCD.refresh();
         btc.close();
         LCD.clear();
      }
   }
}



Thank you in advance for your assistance.

P.S.: I am sure that the delay is not due to the Bluetooth connection.
nxtnewbie
New User
 
Posts: 11
Joined: Sat Apr 14, 2012 7:31 pm

Re: Delay

Postby gloomyandy » Fri Mar 08, 2013 3:44 pm

You seem to be opening and closing the connection between the PC and the NXT for every command. Don't do this. Structure your code so that you open the connection and then have an inner loop that read/writes the commands until you choose to quit.
User avatar
gloomyandy
leJOS Team Member
 
Posts: 3004
Joined: Fri Sep 28, 2007 2:06 pm
Location: UK

Re: Delay

Postby nxtnewbie » Thu Mar 14, 2013 10:21 pm

gloomyandy wrote:You seem to be opening and closing the connection between the PC and the NXT for every command. Don't do this. Structure your code so that you open the connection and then have an inner loop that read/writes the commands until you choose to quit.


I did that but the delay stays the same. The stop method takes too long. I tried the quickStop method, but it doesn't work at all. I attach the latest code. I would be grateful if someone could suggest a solution.

BTSend:

Code: Select all
package org.lejos.pcsample.btsend;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import lejos.pc.comm.NXTCommLogListener;
import lejos.pc.comm.NXTConnector;

/**
 * It connects to the NXT, and sends an integer
 */

public class BTSend {   
   public static void main(String[] args) {
      NXTConnector conn = new NXTConnector();
      conn.addLogListener(new NXTCommLogListener(){

         public void logEvent(String message) {
            System.out.println("BTSend Log.listener: "+message);
         }

         public void logEvent(Throwable throwable) {
            System.out.println("BTSend Log.listener - stack trace: ");
            throwable.printStackTrace();
         }
      }
            );   

      // Connect to any NXT over Bluetooth
      boolean connected = conn.connectTo("btspp://");

      if (!connected) {
         System.err.println("Failed to connect to any NXT");
         System.exit(1);
      }

      DataOutputStream dos = new DataOutputStream(conn.getOutputStream());

      while(true){
         System.out.println("Hello! Please enter an integer number: ");
         BufferedReader userInput = new BufferedReader(new InputStreamReader(System.in ));
         int a=-1;
         try {
            a = Integer.parseInt(userInput.readLine());
         } catch (NumberFormatException | IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
         }      

         System.out.println("The number you entered is: " + a);
         try {
            System.out.println("Sending " + a);
            dos.writeInt(a);
            dos.flush();
            if (a==0){               
               break;
            }
         } catch (IOException ioe) {
            System.out.println("IO Exception writing bytes:");
            System.out.println(ioe.getMessage());
         }

      }
      try {
         dos.close();
         conn.close();
      } catch (IOException ioe) {
         System.out.println("IOException closing connection:");
         System.out.println(ioe.getMessage());
      }
   }
}


BTReceive:

Code: Select all
package org.lejos.sample.btreceive;
import java.io.DataInputStream;
import lejos.robotics.navigation.DifferentialPilot;
import lejos.nxt.LCD;
import lejos.nxt.Motor;
import lejos.nxt.comm.BTConnection;
import lejos.nxt.comm.Bluetooth;

/**
 * Receive data from another NXT, a PC, a phone,
 * or another bluetooth device.
 * Waits for a connection, receives an int and performs the corresponding action according to the
 * numbers below:
   1: Move Forward
   2: Turn Right
   3: Turn Left
   4: Stop
 */
public class BTReceive {

   public static void main(String [] args)  throws Exception
   {
      String connected = "Connected";
      String waiting = "Waiting...";
      String closing = "Closing...";
      DifferentialPilot pilot = new DifferentialPilot(3.5f,13f,Motor.C, Motor.B,false);
      pilot.setTravelSpeed(720);// 2 RPM   
      LCD.drawString(waiting,0,0);
      LCD.refresh();
      BTConnection btc = Bluetooth.waitForConnection();
      LCD.clear();
      LCD.drawString(connected,0,0);
      LCD.refresh();   
      DataInputStream dis = btc.openDataInputStream();

      while (true){
         int n = dis.readInt();

         if(n==1){
            LCD.drawInt(n,7,0,1);
            LCD.refresh();
            if (pilot.isMoving())
               pilot.stop();
            pilot.forward();   
         }
         if(n==2){
            LCD.drawInt(n,7,0,1);
            LCD.refresh();
            if (pilot.isMoving())
               pilot.stop();
            pilot.rotateRight();
         }
         if(n==3){
            LCD.drawInt(n,7,0,1);
            LCD.refresh();
            if (pilot.isMoving())
               pilot.stop();
            pilot.rotateLeft();
         }
         if(n==4){
            LCD.drawInt(n,7,0,1);
            LCD.refresh();
            if (pilot.isMoving())
               pilot.stop();
         }

         //LCD.drawInt(n,7,0,1);
         //LCD.refresh();

         Thread.sleep(100); // wait for data to drain

         if (n==0){
            break;
         }
      }
      dis.close();
      Thread.sleep(100); // wait for data to drain
      LCD.clear();
      LCD.drawString(closing,0,0);
      LCD.refresh();
      btc.close();
      LCD.clear();
   }
}
nxtnewbie
New User
 
Posts: 11
Joined: Sat Apr 14, 2012 7:31 pm

Re: Delay

Postby gloomyandy » Thu Mar 14, 2013 11:50 pm

Are you sure it is the stop that is taking a long time? Do you see the display change (to show the new command) and then the robot takes a long time to stop?

You seem to be setting the speed to a very high value. The units used are not degrees per min but are whatever units you specified the wheel size in (I assume cm) per second (see the doc). Try much smaller values (like 1 or 2) and work your way up. You may also want to consider trying different values for acceleration (but the the speed first).
User avatar
gloomyandy
leJOS Team Member
 
Posts: 3004
Joined: Fri Sep 28, 2007 2:06 pm
Location: UK

Re: Delay

Postby nxtnewbie » Fri Mar 15, 2013 2:06 am

gloomyandy wrote:Are you sure it is the stop that is taking a long time? Do you see the display change (to show the new command) and then the robot takes a long time to stop?

Yes, the display changes and the robot doesn't stop immediately.

gloomyandy wrote:You seem to be setting the speed to a very high value. The units used are not degrees per min but are whatever units you specified the wheel size in (I assume cm) per second (see the doc). Try much smaller values (like 1 or 2) and work your way up. You may also want to consider trying different values for acceleration (but the the speed first).

You are right. In slow speed it stops immediately, so I changed it to 30. Thank you very much for your help!
nxtnewbie
New User
 
Posts: 11
Joined: Sat Apr 14, 2012 7:31 pm


Return to NXJ Software

Who is online

Users browsing this forum: No registered users and 3 guests

more stuff