Skip to content

Commit

Permalink
Added violation infrastructure, joint and link constrains, and bugfixes
Browse files Browse the repository at this point in the history
  • Loading branch information
tommasbakker committed Jun 3, 2015
1 parent 6283a8b commit 41c3772
Show file tree
Hide file tree
Showing 49 changed files with 1,246 additions and 108 deletions.
23 changes: 21 additions & 2 deletions src/REXOS/HAL/BlackboarRosInterface.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import HAL.libraries.blackboard_client.data_classes.MongoOperation;
import HAL.libraries.blackboard_client.data_classes.OplogEntry;
import HAL.listeners.EquipletCommandListener.EquipletCommandStatus;
import HAL.listeners.ViolationListener.ViolationType;
import HAL.steps.HardwareStep;
import HAL.steps.HardwareStep.HardwareStepStatus;

Expand All @@ -42,10 +43,12 @@ public class BlackboarRosInterface extends RosInterface implements BlackboardSub
private BlackboardClient stateBlackboardBBClient;
private BlackboardClient hardwareStepsBBClient;
private BlackboardClient equipletCommandsBBClient;
private BlackboardClient violationsBBClient;

private BasicOperationSubscription stateSubscription;
private FieldUpdateSubscription hardwareStepStatusSubscription;
private FieldUpdateSubscription equipletCommandStatusSubscription;
private BasicOperationSubscription violationsSubscription;

private Map<ObjectId, HardwareStep> hardwareSteps;

Expand Down Expand Up @@ -85,6 +88,13 @@ public BlackboarRosInterface(AbstractHardwareAbstractionLayer hal) {
equipletCommandsBBClient.setDatabase(databaseName);
equipletCommandsBBClient.setCollection((String) Configuration.getProperty("rosInterface/equipletCommands/blackboardName/", equipletName));
equipletCommandsBBClient.subscribe(equipletCommandStatusSubscription);

violationsSubscription = new BasicOperationSubscription(MongoOperation.INSERT, this);

violationsBBClient = new BlackboardClient((String) Configuration.getProperty("rosInterface/violations/ip/", equipletName));
violationsBBClient.setDatabase(databaseName);
violationsBBClient.setCollection((String) Configuration.getProperty("rosInterface/violations/blackboardName/", equipletName));
violationsBBClient.subscribe(violationsSubscription);
} catch (InvalidDBNamespaceException | UnknownHostException | GeneralMongoException ex) {
Logger.log(LogSection.HAL_ROS_INTERFACE, LogLevel.CRITICAL, "Unable to initialize HAL.BlackBoardHandler", ex);
}
Expand Down Expand Up @@ -133,11 +143,19 @@ public void onMessage(MongoOperation operation, OplogEntry entry) {
dbObject = equipletCommandsBBClient.findDocumentById(entry.getTargetObjectId());
if(dbObject != null){
String status = dbObject.get("status").toString();
//String id = ((org.bson.types.ObjectId) dbObject.get("_id")).toString();
Logger.log(LogSection.HAL_ROS_INTERFACE, LogLevel.DEBUG, "Reloading of the Equiplet has: " + status);
Logger.log(LogSection.HAL_ROS_INTERFACE, LogLevel.DEBUG, "Equiplet command status: " + status);

this.onEquipletCommandStatusChanged(EquipletCommandStatus.valueOf(status));
}
} else if(collectionName.equals((String) Configuration.getProperty("rosInterface/violations/blackboardName/", hal.getEquipletName()))) {
dbObject = violationsBBClient.findDocumentById(entry.getTargetObjectId());
if(dbObject != null){
ViolationType type = ViolationType.valueOf(dbObject.get("type").toString());
String message = dbObject.get("message").toString();
Logger.log(LogSection.HAL_ROS_INTERFACE, LogLevel.DEBUG, "Violation occured: " + type + " " + message);

this.onViolationOccured(type, message);
}
}
} catch(InvalidDBNamespaceException | GeneralMongoException ex) {
Logger.log(LogSection.HAL_ROS_INTERFACE, LogLevel.ERROR, "Unknown exception occured:", ex);
Expand All @@ -149,6 +167,7 @@ public void postHardwareStep(JSONObject hardwareStep) throws InvalidJSONExceptio
}

public void shutdown() {
violationsBBClient.close();
equipletCommandsBBClient.close();
hardwareStepsBBClient.close();
stateBlackboardBBClient.close();
Expand Down
16 changes: 16 additions & 0 deletions src/REXOS/HAL/BridgeRosInterface.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import util.log.Logger;
import HAL.dataTypes.ModuleIdentifier;
import HAL.listeners.EquipletCommandListener.EquipletCommandStatus;
import HAL.listeners.ViolationListener.ViolationType;
import HAL.steps.HardwareStep;
import HAL.steps.HardwareStep.HardwareStepStatus;
import edu.wpi.rail.jrosbridge.Ros;
Expand Down Expand Up @@ -41,6 +42,7 @@ public class BridgeRosInterface extends RosInterface {
Topic modeChangedTopic;
Topic hardwareStepsTopic;
Topic equipletCommandsTopic;
Topic violationsTopic;

AbstractHardwareAbstractionLayer hal;

Expand Down Expand Up @@ -138,6 +140,20 @@ public void handleMessage(Message message) {
}
}
});
violationsTopic = new Topic(rosNode, path + "violationOccured", "std_msgs/String");
violationsTopic.subscribe(new TopicCallback() {
@Override
public void handleMessage(Message message) {
try {
JSONObject messageJson = new JSONObject(message.toJsonObject().getString("data"));
ViolationType violationType = ViolationType.valueOf(messageJson.getString("type"));
String violationMessage = messageJson.getString("message");
BridgeRosInterface.this.onViolationOccured(violationType, violationMessage);
} catch (JSONException ex) {
Logger.log(LogSection.HAL_ROS_INTERFACE, LogLevel.ERROR, "Reading mode changed failed", ex);
}
}
});

hardwareStepsTopic = new Topic(rosNode, path + "hardwareSteps", "std_msgs/String");
hardwareStepsTopic.advertise();
Expand Down
2 changes: 1 addition & 1 deletion src/REXOS/HAL/HardwareAbstractionLayer.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public void onTranslationFinished(ProductStep step, ArrayList<HardwareStep> hard
if(shadowHal != null) {
try {
shadowHalSemaphore.acquire();
shadowHal.testHardwareSteps(hardwareSteps, step.getCriteria());
shadowHal.testHardwareSteps(hardwareSteps, step);
} catch (InterruptedException ex) {
Logger.log(LogSection.HAL, LogLevel.ERROR, "Acquiring semaphore lock failed");
}
Expand Down
16 changes: 16 additions & 0 deletions src/REXOS/HAL/NodeRosInterface.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import util.log.Logger;
import HAL.dataTypes.ModuleIdentifier;
import HAL.listeners.EquipletCommandListener.EquipletCommandStatus;
import HAL.listeners.ViolationListener.ViolationType;
import HAL.steps.HardwareStep;
import HAL.steps.HardwareStep.HardwareStepStatus;

Expand All @@ -31,6 +32,7 @@ public class NodeRosInterface extends RosInterface implements NodeMain {
private Subscriber<std_msgs.String> equipletCommandStatusChangedSubscriber;
private Subscriber<std_msgs.String> stateChangedSubscriber;
private Subscriber<std_msgs.String> modeChangedSubscriber;
private Subscriber<std_msgs.String> violationSubscriber;

private Publisher<std_msgs.String> hardwareStepPublisher;
private Publisher<std_msgs.String> equipletCommandPublisher;
Expand Down Expand Up @@ -162,6 +164,20 @@ public void onNewMessage(std_msgs.String message) {
}
}
}, 10);
violationSubscriber = node.newSubscriber(path + "violationOccured", std_msgs.String._TYPE);
violationSubscriber.addMessageListener(new MessageListener<std_msgs.String>() {
@Override
public void onNewMessage(std_msgs.String message) {
try {
JSONObject messageJson = new JSONObject(message.getData());
ViolationType violationType = ViolationType.valueOf(messageJson.getString("type"));
String violationMessage = messageJson.getString("message");
NodeRosInterface.this.onViolationOccured(violationType, violationMessage);
} catch (JSONException ex) {
Logger.log(LogSection.HAL_ROS_INTERFACE, LogLevel.ERROR, "Reading mode changed failed", ex);
}
}
}, 10);


hardwareStepPublisher = node.newPublisher(path + "hardwareSteps", std_msgs.String._TYPE);
Expand Down
21 changes: 21 additions & 0 deletions src/REXOS/HAL/RosInterface.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,15 @@
import HAL.listeners.EquipletListener;
import HAL.listeners.ModuleListener;
import HAL.listeners.ProcessListener;
import HAL.listeners.ViolationListener;
import HAL.listeners.ViolationListener.ViolationType;
import HAL.steps.HardwareStep;

public abstract class RosInterface {
private ArrayList<ModuleListener> moduleSubscribers;
private ArrayList<EquipletListener> equipletSubscribers;
private ArrayList<ProcessListener> processSubscribers;
private ArrayList<ViolationListener> violationSubscribers;

protected RosInterface() {
moduleSubscribers = new ArrayList<ModuleListener>();
Expand Down Expand Up @@ -99,6 +102,24 @@ protected void onModuleModeChanged(Module module, Mode mode) {
}
}

public void addViolationListener(ViolationListener listener) {
synchronized (violationSubscribers) {
violationSubscribers.add(listener);
}
}
public void removeViolationListener(ViolationListener listener) {
synchronized (violationSubscribers) {
violationSubscribers.remove(listener);
}
}
public void onViolationOccured(ViolationType violationType, String message) {
synchronized (violationSubscribers) {
for (ViolationListener violationListener : violationSubscribers) {
violationListener.onViolationOccured(violationType, message);
}
}
}

abstract public void postHardwareStep(HardwareStep hardwareStep);
abstract public void postEquipletCommand(JSONObject equipletCommand);

Expand Down
5 changes: 3 additions & 2 deletions src/REXOS/HAL/ShadowHardwareAbstractionLayer.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import HAL.listeners.MastListener;
import HAL.listeners.TestProcessListener;
import HAL.steps.HardwareStep;
import HAL.steps.ProductStep;
import HAL.tasks.ExecutionProcess;
import HAL.tasks.TestProcess;

Expand All @@ -27,8 +28,8 @@ public ShadowHardwareAbstractionLayer(String equipletName, MastListener mastList
this.testProcessListener = testProcessListener;
}

public void testHardwareSteps(ArrayList<HardwareStep> hardwareSteps, JSONObject criteria) {
TestProcess testProcess = new TestProcess(this, hardwareSteps, criteria);
public void testHardwareSteps(ArrayList<HardwareStep> hardwareSteps, ProductStep productStep) {
TestProcess testProcess = new TestProcess(this, hardwareSteps, productStep, testProcessListener);
testProcess.start();
}

Expand Down
162 changes: 162 additions & 0 deletions src/REXOS/HAL/dataTypes/GazeboJoint.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
package HAL.dataTypes;

import java.io.Serializable;
import java.util.ArrayList;

import org.json.JSONException;
import org.json.JSONObject;

import HAL.libraries.knowledgedb_client.KnowledgeDBClient;
import HAL.libraries.knowledgedb_client.Row;

public class GazeboJoint implements Serializable {
private static final long serialVersionUID = 4744706739412950824L;

public static final String JOINT_NAME = "jointName";
public static final String MAX_ERROR_POSE = "maxErrorPose";

private static final String getGazeboJointsForModuleType =
"SELECT gazeboModel, jointName, maxErrorPose \n" +
"FROM GazeboJoint \n" +
"WHERE gazeboModel = ( \n" +
" SELECT gazeboModel \n" +
" FROM ModuleType \n" +
" WHERE manufacturer = ? AND \n" +
" typeNumber = ? \n" +
");";
private static final String getGazeboJointsForGazeboModel =
"SELECT gazeboModel, jointName, maxErrorPose \n" +
"FROM GazeboJoint \n" +
"WHERE gazeboModel = ?;";
private static final String getGazeboJointsForPartType =
"SELECT gazeboModel, jointName, maxErrorPose \n" +
"FROM GazeboJoint \n" +
"WHERE gazeboModel = ( \n" +
" SELECT gazeboModel \n" +
" FROM PartType \n" +
" WHERE typeNumber = ? \n" +
");";
private static final String addGazeboJoint =
"INSERT INTO GazeboJoint \n" +
"(gazeboModel, jointName, maxErrorPose) \n" +
"VALUES(?, ?, ?);";
private static final String updateGazeboJoint =
"UPDATE GazeboJoint \n" +
"SET maxErrorPose = ? \n" +
"WHERE gazeboModel = ? AND \n" +
" jointName = ?";
private static final String removeGazeboJoint =
"DELETE FROM GazeboJoint \n" +
"WHERE gazeboModel = ? AND \n" +
" jointName = ?";

public GazeboModel gazeboModel;
public String jointName;
public double maxErrorPose;

public GazeboJoint() {
}
public GazeboJoint(GazeboModel gazeboModel, String jointName, double maxErrorPose) {
this.gazeboModel = gazeboModel;
this.jointName = jointName;
this.maxErrorPose = maxErrorPose;
}

/**
* This method will get the JavaSoftware associated with the module.
* @param moduleIdentifier
* @return
*/
public static ArrayList<GazeboJoint> getGazeboJointsForModuleIdentifier(ModuleTypeIdentifier moduleIdentifier, KnowledgeDBClient knowledgeDBClient) {
Row[] rows = knowledgeDBClient.executeSelectQuery(getGazeboJointsForModuleType, moduleIdentifier.manufacturer, moduleIdentifier.typeNumber);

ArrayList<GazeboJoint> joints = new ArrayList<GazeboJoint>();
for (Row row : rows) {
String jointName = (String) row.get("jointName");
double maxErrorPose = (double) row.get("maxErrorPose");

GazeboModel gazeboModel = GazeboModel.getGazeboModelForModuleIdentifier(moduleIdentifier, knowledgeDBClient);
joints.add(new GazeboJoint(gazeboModel, jointName, maxErrorPose));
}
return joints;
}

/**
* This method will get the JavaSoftware associated with the module.
* @param moduleIdentifier
* @return
*/
public static ArrayList<GazeboJoint> getGazeboJointsForGazeboModel(GazeboModel model, KnowledgeDBClient knowledgeDBClient) {
Row[] rows = knowledgeDBClient.executeSelectQuery(getGazeboJointsForGazeboModel, model.id);

ArrayList<GazeboJoint> joints = new ArrayList<GazeboJoint>();
for (Row row : rows) {
String jointName = (String) row.get("jointName");
double maxErrorPose = (double) row.get("maxErrorPose");

joints.add(new GazeboJoint(model, jointName, maxErrorPose));
}
return joints;
}

/**
* This method will get the JavaSoftware associated with the part.
* @param moduleIdentifier
* @return
*/
public static ArrayList<GazeboJoint> getGazeboJointsForPartTypeNumber(String partTypeNumber, KnowledgeDBClient knowledgeDBClient) {
Row[] rows = knowledgeDBClient.executeSelectQuery(getGazeboJointsForPartType, partTypeNumber);

ArrayList<GazeboJoint> joints = new ArrayList<GazeboJoint>();
for (Row row : rows) {
String jointName = (String) row.get("jointName");
double maxErrorPose = (double) row.get("maxErrorPose");

GazeboModel gazeboModel = GazeboModel.getGazeboModelForPartTypeNumber(partTypeNumber, knowledgeDBClient);
joints.add(new GazeboJoint(gazeboModel, jointName, maxErrorPose));
}
return joints;
}
public static GazeboJoint deSerialize(JSONObject input, GazeboModel gazeboModel) throws JSONException {
GazeboJoint output = new GazeboJoint();

output.gazeboModel = gazeboModel;
output.jointName = input.getString(JOINT_NAME);
output.maxErrorPose = input.getDouble(MAX_ERROR_POSE);

return output;
}
public JSONObject serialize() throws JSONException {
JSONObject output = new JSONObject();

output.put(JOINT_NAME, jointName);
output.put(MAX_ERROR_POSE, maxErrorPose);

return output;
}

/**
* Deserializes java software from a JSONObject and stores in in the knowledge database using the provided KnowledgeDBClient.
* @param javaSoftware
* @param knowledgeDBClient
* @return the now stored java software
* @throws JSONException
*/
public void insertIntoDatabase(KnowledgeDBClient knowledgeDBClient) {
knowledgeDBClient.executeUpdateQuery(addGazeboJoint,
gazeboModel.id, jointName, maxErrorPose);
}
/**
* This method will update the java software in the knowledge database.
* @param javaSoftware
* @throws JSONException
*/
public void updateGazeboModel(GazeboJoint gazeboJointToBeUpdated, KnowledgeDBClient knowledgeDBClient) {
knowledgeDBClient.executeUpdateQuery(updateGazeboJoint,
maxErrorPose,
gazeboJointToBeUpdated.gazeboModel.id, gazeboJointToBeUpdated.jointName);
}
public void removeFromDatabase(KnowledgeDBClient knowledgeDBClient) {
knowledgeDBClient.executeUpdateQuery(removeGazeboJoint, gazeboModel.id, jointName);
}
}
Loading

0 comments on commit 41c3772

Please sign in to comment.