Log in

View Full Version : UDP Recieving Problems


tyandjel94
17-05-2012, 16:18
We are trying to send information from a custom labview dashboard to the robot (runs java). We have a regular java applet that I can upload if you would like to see it, that works perfectly. When we try to enable the robot we get robots don't quit. Here is our code. Do you see any problems. Thanks.


package edu.wpi.first.wpilibj.templates;

import edu.wpi.first.wpilibj.Jaguar;
import edu.wpi.first.wpilibj.*;
import java.io.IOException;
import javax.microedition.io.Connector;
import javax.microedition.io.Datagram;
import javax.microedition.io.DatagramConnection;
import javax.microedition.io.UDPDatagramConnection;

public class RobotTemplate extends SimpleRobot {

double targX = 0.0;
double targArea = 0.0;
boolean aHasValue = false;
boolean xHasValue = false;
double distanceFromTarg = 0.0;
static final double distanceConversionFactor = 0;
static final int IMAGE_WIDTH = 320;
int batman = 0;
Datagram data;
DatagramConnection receivePacket;
String rawUdpData;
Jaguar jagL1 = new Jaguar(3);
Jaguar jagL2 = new Jaguar(4);
Jaguar jagR1 = new Jaguar(1);
Jaguar jagR2 = new Jaguar(2);
Joystick stick3 = new Joystick(3);

public void autonomous() {
}

public void operatorControl() {
while (isOperatorControl()) {
udp();
if (stick3.getRawButton(2)) {
tracking();
} else {
jagL1.set(0.0);
jagR1.set(0.0);
jagL2.set(0.0);
jagR2.set(0.0);
}
}
}

public void udp() {
try {
receivePacket = (DatagramConnection)
Connector.open("data://:1130");
// = new DatagramConnection(5021); //put port in here
try{
receivePacket.receive(data);
}finally{
receivePacket.close();
}

//rawUdpData = data.readUTF();

rawUdpData = (String.valueOf(data.getData()));
} catch (IOException ex) {
ex.printStackTrace();
}
// byte[] receiveData = new byte[1024]; //1024 is time out
String udpArea = new String();
String udpX = new String();


//data recieving
//DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
// serverSocket.receive(receivePacket);
//String rawUdpData = new String(receivePacket.getData());


// System.out.println("RECEIVED: " + rawUdpData);

//string analizing
if (rawUdpData.startsWith("Area: ")) { // 1 spaces
//System.out.println("worked");

udpArea = rawUdpData.replace('A', ' ');
udpArea = udpArea.replace('r', ' ');
udpArea = udpArea.replace('e', ' ');
udpArea = udpArea.replace('a', ' ');
udpArea = udpArea.replace(':', ' ');
// udpArea = (rawUdpData.replace("Area: ", ""));
udpArea = udpArea.trim();
}

if (rawUdpData.startsWith("Bounding X: ")) { // 1 spaces
udpX = rawUdpData.replace('B', ' ');
udpX = udpX.replace('o', ' ');
udpX = udpX.replace('u', ' ');
udpX = udpX.replace('n', ' ');
udpX = udpX.replace('d', ' ');
udpX = udpX.replace('i', ' ');
udpX = udpX.replace('n', ' ');
udpX = udpX.replace('g', ' ');
udpX = udpX.replace('X', ' ');
udpX = udpX.replace(':', ' ');
// udpX = (rawUdpData.replace("Bounding X: ", "")); //removes lables
udpX = udpX.trim(); //removes whitespace in string
}

//safty for converting string to double
if (udpArea.length() > 0) {
aHasValue = true;
} else {
aHasValue = false;
targArea = 0.0;
}
if (udpX.length() > 0) {
xHasValue = true;
} else {
xHasValue = false;
targX = 0.0;
}

//converts string to double
if (aHasValue) {
targArea = Double.parseDouble(udpArea);
}
if (xHasValue) {
targX = Double.parseDouble(udpX);
}
System.out.println("Area string: " + udpArea);
System.out.println("X string: " + udpX);
System.out.println("a: " + targArea);
System.out.println("X: " + targX);

}

public void tracking() {

if (xHasValue) {
double distOffCenter = ((IMAGE_WIDTH / 2) - targX);
double ratioOffCenter = distOffCenter / (IMAGE_WIDTH / 2);
double speed = (0.3 * (ratioOffCenter * ratioOffCenter) + (distOffCenter > 0 ? 0.4 : -0.4)); //Left Right

if ((Math.abs(speed)) <= 0.41 && batman <= 15) {
batman++;
}


if (Math.abs(speed) <= 0.41) {
jagL1.set(0.0);
jagL2.set(0.0);
jagR1.set(0.0);
jagR2.set(0.0);
} else {
jagL1.set(speed);
jagL2.set(speed);
jagR1.set(speed);
jagR2.set(speed);
}
} else {
System.out.println("NOIMAGE");//Middle of target // myDrive.tankDrive(0, 0);
jagL1.set(0.0);
jagL2.set(0.0);
jagR1.set(0.0);
jagR2.set(0.0);
}

}
}
//end of class

Fletch1373
20-05-2012, 00:33
We are trying to send information from a custom labview dashboard to the robot (runs java). We have a regular java applet that I can upload if you would like to see it, that works perfectly. When we try to enable the robot we get robots don't quit. Here is our code. Do you see any problems. Thanks.



This thread (http://www.chiefdelphi.com/forums/showthread.php?t=101175) says that it's probably an uncaught exception somewhere. Are you getting any other information printing out? i.e. stack trace, etc.?

tyandjel94
20-05-2012, 01:18
No. When we press enable it goes straight to robots don't quit. Do you see anything that isn't correctly coded for udp?

tyandjel94
20-05-2012, 01:48
Would a possibility be a security exception?

Sunstroke
20-05-2012, 15:16
No. When we press enable it goes straight to robots don't quit. Do you see anything that isn't correctly coded for udp?

When it says "robots don't quit," it means that you are getting an exception. I believe it prints out the exception as well...

Fletch1373
21-05-2012, 01:09
When it says "robots don't quit," it means that you are getting an exception. I believe it prints out the exception as well...

Yes, that message is part of the library, not your code. It is supposed to print a stack trace with the actual error message immediately following the "robots don't quit" message.

tyandjel94
21-05-2012, 21:38
We have been getting the IO exception.

tyandjel94
22-05-2012, 23:00
We aren't able to open the socket to the labview part can connect to it.

NotInControl
26-05-2012, 00:38
Can you post the complete output of the console where you are getting the "Robots don't quit" message.

To help point you in the right direction in the mean time:

That message means there is an uncaught exception and it bubbled up to the system level. If it is an uncaught IO exception it should tell you a line number as well, but it probably has to do with your something within the udp() function.

Are you sure the parameter to connector.open is correct? I believe you should be using a "datagram" protocol instead of a "data" protocol.

For example
A datagram connection for accepting datagrams on port 1234
"datagram://:1234"

A datagram connection for sending to a server on port 1234:
"datagram://123.456.789.12:1234"

And it also looks like you never instantiate the Datagram variable "data", so its null when you call data.readUTF, and null when you use it as a param in receive(data). But that should throw a null point exception, and you should be seeing that in the console. If you don't that's because the program is failing before it gets to that line, which case your problem lies where you open your connection to the datagram (or maybe you do instantiate it somewhere and I just didn't notice it).

You mentioned that you have a java applet that works. Is that written using the same version of JavaME that is running on the cRIO? My guess is that you copied your code from the applet to the CRIO, however, if the code was written under different java environments it will not be able to run on the cRIO without modification.

Hope that helps.
Kevin

ItzWarty
28-05-2012, 20:00
We had issues with networking in general. We used TCP for our robot-OI communication. I'd assume it's a bug on the FRC side, as opposed to yours - our code would work 3/4 of the time, and require a robot restart if it failed.

Joe Ross
24-09-2012, 21:58
Are you sure the parameter to connector.open is correct? I believe you should be using a "datagram" protocol instead of a "data" protocol.

For example
A datagram connection for accepting datagrams on port 1234
"datagram://:1234"

A datagram connection for sending to a server on port 1234:
"datagram://123.456.789.12:1234"

Were you able to use the datagram protocol on the cRIO? I get the following exception: "javax.microedition.io.ConnectionNotFoundException: The 'datagram' protocol does not exist". The following thread says that datagram wasn't supported as of 2011: http://www.chiefdelphi.com/forums/showpost.php?p=1012461&postcount=5