Quote:
Originally Posted by ProfessorAlekM
Hello!
I've wrote UDP code to communicate with a beaglebone from the roboRIO, however when the I put on autonomous mode and the UDP is used none of the motors work, and when I change back to teleop I still can't control anything.
Here is the code:
Code:
public class Robot extends IterativeRobot {
//Beaglebone Communications
DatagramSocket serverSocket;
byte[] receiveData;
byte[] sendData;
Code for robot init
Code:
try {
serverSocket = new DatagramSocket(9876);
} catch (SocketException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
receiveData = new byte[1024];
sendData = new byte[1024];
Code:
public void autonomousPeriodic() {
//Receive Data
DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
try {
serverSocket.receive(receivePacket);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String sentence = new String( receivePacket.getData());
System.out.println("RECEIVED: " + sentence);
InetAddress IPAddress = receivePacket.getAddress();
int port = receivePacket.getPort();
//Return data
String capitalizedSentence = sentence.toUpperCase();
sendData = capitalizedSentence.getBytes();
DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, IPAddress, port);
try {
serverSocket.send(sendPacket);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
switch (sentence) {
case "a":
myRobot.tankDrive(0.5, 0.5);
break;
case "b":
myRobot.tankDrive(0,0);
break;
}
Timer.delay(0.005);
}
Thanks for any help!
|
You may be running into a Java subtlety here.
Code:
boolean eq = ("abc" == "abc");
System.out.println(eq); // false!
I'm not entirely sure, but it may be the same problem in your switch statement. Try using String.equals in an if-else instead.
Otherwise, use the debugger to verify that sentence contains the "a" or "b" you need.