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.
