Fixed FRCSim artf2594 - JavaGazebo no longer crashes if gzserver hasn't started, and cleaned up some code in the area.

Change-Id: I4daae199fb6dda6561c2cb85fc5254e36bcb3066
This commit is contained in:
Colby Skeggs
2014-06-20 10:18:42 -07:00
parent e9ade472e4
commit 698f38d404
6 changed files with 107 additions and 81 deletions

View File

@@ -12,6 +12,8 @@ import java.net.URL;
import java.util.Enumeration;
import java.util.jar.Manifest;
import edu.wpi.first.wpilibj.simulation.MainNode;
import edu.wpi.first.wpilibj.networktables.NetworkTable;
/**
@@ -175,9 +177,18 @@ public abstract class RobotBase {
* the robot.
* @throws javax.microedition.midlet.MIDletStateChangeException
*/
public static void main(String args[]) { // TODO: expose main to teams?{
public static void main(String args[]) { // TODO: expose main to teams?
boolean errorOnExit = false;
try {
MainNode.openGazeboConnection();
} catch (Throwable e) {
System.err.println("Could not connect to Gazebo.");
e.printStackTrace();
System.exit(1);
return;
}
ds = DriverStation.getInstance();
String robotName = "";

View File

@@ -1,5 +1,8 @@
package edu.wpi.first.wpilibj.simulation;
import java.io.IOException;
import java.util.logging.Logger;
import org.gazebosim.transport.Node;
import org.gazebosim.transport.Publisher;
import org.gazebosim.transport.Subscriber;
@@ -7,27 +10,34 @@ import org.gazebosim.transport.SubscriberCallback;
import com.google.protobuf.Message;
public class MainNode{
private Node main;
public class MainNode {
private MainNode() {
main = new Node("frc");
}
private static MainNode instance;
public static MainNode getInstance() {
if (instance == null) {
instance = new MainNode();
}
return instance;
}
public static <T extends Message> Publisher<T> advertise(String topic, T defaultMessage) {
return getInstance().main.advertise(topic, defaultMessage);
}
public static <T extends Message> Subscriber<T>
subscribe(String topic, T defaultMessage, SubscriberCallback<T> cb) {
return getInstance().main.subscribe(topic, defaultMessage, cb);
private static final Logger LOG = Logger.getLogger("Simulation MainNode");
private static Node mainNode;
public static synchronized void openGazeboConnection() throws IOException, InterruptedException {
if (mainNode != null) {
LOG.warning("MainNode.openGazeboConnection() was already called!");
return;
}
mainNode = new Node("frc");
mainNode.waitForConnection();
}
public static <T extends Message> Publisher<T> advertise(String topic, T defaultMessage) {
if (mainNode == null) {
throw new IllegalStateException("MainNode.openGazeboConnection() should have already been called by RobotBase.main()!");
}
return mainNode.advertise(topic, defaultMessage);
}
public static <T extends Message> Subscriber<T> subscribe(String topic, T defaultMessage, SubscriberCallback<T> cb) {
if (mainNode == null) {
throw new IllegalStateException("MainNode.openGazeboConnection() should have already been called by RobotBase.main()!");
}
return mainNode.subscribe(topic, defaultMessage, cb);
}
}