Skip to content

Commit

Permalink
Actuators and Sensors are no longer singletons.
Browse files Browse the repository at this point in the history
  • Loading branch information
iridin committed Jun 18, 2015
1 parent 078051c commit b0c0820
Show file tree
Hide file tree
Showing 6 changed files with 122 additions and 120 deletions.
20 changes: 2 additions & 18 deletions jdeeco-ros/src/cz/cuni/mff/d3s/jdeeco/ros/Actuators.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,31 +66,15 @@ public class Actuators extends TopicSubscriber {
*/
private Object soundLock;

/**
* The singleton instance of the {@link Actuators} class.
*/
private static Actuators INSTANCE;

/**
* Private constructor enables the {@link Actuators} to be a singleton.
* Internal constructor enables the {@link Actuators} to be a singleton.
*/
private Actuators() {
Actuators() {
ledColor = new HashMap<>();
soundLock = new Object();
}

/**
* Provides the singleton instance of the {@link Actuators}.
*
* @return the singleton instance of the {@link Actuators}.
*/
public static Actuators getInstance() {
if (INSTANCE == null) {
INSTANCE = new Actuators();
}
return INSTANCE;
}

/**
* Register and subscribe to required ROS topics for actuator commands.
*
Expand Down
32 changes: 22 additions & 10 deletions jdeeco-ros/src/cz/cuni/mff/d3s/jdeeco/ros/Communication.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,8 @@
import org.ros.node.ConnectedNode;

public class Communication extends TopicSubscriber {

private static Communication INSTANCE;

private Communication(){}

public static Communication getInstance(){
if(INSTANCE == null){
INSTANCE = new Communication();
}
return INSTANCE;
}
Communication(){}

@Override
void subscribe(ConnectedNode connectedNode) {
Expand All @@ -22,3 +13,24 @@ void subscribe(ConnectedNode connectedNode) {
}

}

/* TODO:
* /joint_states
* /mobile_base/commands/controller_info
+ /mobile_base/commands/motor_power
+ /mobile_base/commands/reset_odometry
* /mobile_base/controller_info
+ /mobile_base/events/button
+ /mobile_base/events/cliff
* /mobile_base/events/power_system
* /mobile_base/events/robot_state
* /mobile_base/sensors/core
* /mobile_base/sensors/imu_data
+ /mobile_base/version_info
* /mobile_base_nodelet_manager/bond
* /rosout
* /rosout_agg
* /tf
*/
25 changes: 18 additions & 7 deletions jdeeco-ros/src/cz/cuni/mff/d3s/jdeeco/ros/RosServices.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,16 @@ public class RosServices extends AbstractNodeMain implements DEECoPlugin {

/**
* The list of {@link TopicSubscriber}s in the DEECo-ROS interface.
* This variable is static because the creation of ROS node loads new instance
* of this class an otherwise it initialize different TopicSubscribers
* than the ones available in jDEECo simulation.
*
* @return the list of {@link TopicSubscriber}s in the DEECo-ROS interface.
*/
private TopicSubscriber[] topicSubscribers() {
return new TopicSubscriber[] {
Sensors.getInstance(),
Actuators.getInstance(),
Communication.getInstance()};
}
private static TopicSubscriber[] topicSubscribers = new TopicSubscriber[] {
new Sensors(),
new Actuators(),
new Communication()};

/**
* A list of DEECo plugins the {@link RosServices} depends on.
Expand All @@ -50,6 +51,16 @@ private TopicSubscriber[] topicSubscribers() {
public List<Class<? extends DEECoPlugin>> getDependencies() {
return new ArrayList<>();
}

@SuppressWarnings("unchecked")
public<T> T getService(Class<T> serviceType){
for(TopicSubscriber service : topicSubscribers){
if(service.getClass().equals(serviceType)){
return (T) service;
}
}
return null;
}

/**
* Initialize the {@link RosServices} DEECo plugin. Launch the ROS node that
Expand Down Expand Up @@ -149,7 +160,7 @@ public void onShutdownComplete(Node node) {
*/
@Override
public void onStart(ConnectedNode node) {
for (TopicSubscriber subscriber : topicSubscribers()) {
for (TopicSubscriber subscriber : topicSubscribers) {
subscriber.subscribe(node);
}
}
Expand Down
41 changes: 14 additions & 27 deletions jdeeco-ros/src/cz/cuni/mff/d3s/jdeeco/ros/Sensors.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,33 +56,16 @@ public class Sensors extends TopicSubscriber {
private Map<Wheel, WheelState> wheelState;

/**
* The singleton instance of the {@link Sensors} class.
* Internal constructor enables the {@link Sensors} to be a singleton.
*/
private static Sensors INSTANCE;

/**
* Private constructor enables the {@link Sensors} to be a singleton.
*/
private Sensors() {
Sensors() {
bumper = Bumper.RELEASED;
wheelState = new HashMap<>();
for (Wheel wheel : Wheel.values()) {
wheelState.put(wheel, WheelState.RAISED);
}
}

/**
* Provides the singleton instance of the {@link Sensors}.
*
* @return the singleton instance of the {@link Sensors}.
*/
public static Sensors getInstance() {
if (INSTANCE == null) {
INSTANCE = new Sensors();
}
return INSTANCE;
}

/**
* Register and subscribe to required ROS topics of sensor readings.
*
Expand Down Expand Up @@ -138,13 +121,17 @@ public void onNewMessage(BumperEvent message) {
// System.out.println("released");
} else {
bumper = Bumper.fromByte(message.getBumper());
/*
* if (message.getBumper() == BumperEvent.LEFT) {
* System.out.println("left"); } if (message.getBumper() ==
* BumperEvent.RIGHT) { System.out.println("right"); } if
* (message.getBumper() == BumperEvent.CENTER) {
* System.out.println("center"); }
*/

/*if (message.getBumper() == BumperEvent.LEFT) {
System.out.println("left");
}
if (message.getBumper() == BumperEvent.RIGHT) {
System.out.println("right");
}
if (message.getBumper() == BumperEvent.CENTER) {
System.out.println("center");
}*/

}
// TODO: log
}
Expand All @@ -168,7 +155,7 @@ public void onNewMessage(WheelDropEvent message) {
WheelState state = WheelState.fromByte(message.getState());
if (wheel != null && state != null) {
wheelState.put(wheel, state);
// System.out.println(wheel + " wheel state: " + state);
System.out.println(wheel + " wheel state: " + state);
}
// TODO: log
}
Expand Down
54 changes: 31 additions & 23 deletions jdeeco-ros/src/cz/cuni/mff/d3s/jdeeco/ros/test/TestApp.java
Original file line number Diff line number Diff line change
@@ -1,35 +1,43 @@
package cz.cuni.mff.d3s.jdeeco.ros.test;

import cz.cuni.mff.d3s.deeco.logging.Log;
import cz.cuni.mff.d3s.deeco.runners.DEECoSimulation;
import cz.cuni.mff.d3s.deeco.runtime.DEECoNode;
import cz.cuni.mff.d3s.deeco.timer.DiscreteEventTimer;
import cz.cuni.mff.d3s.deeco.timer.SimulationTimer;
import cz.cuni.mff.d3s.jdeeco.network.Network;
import cz.cuni.mff.d3s.jdeeco.publishing.DefaultKnowledgePublisher;
import cz.cuni.mff.d3s.deeco.timer.WallTimeTimer;
import cz.cuni.mff.d3s.jdeeco.ros.Actuators;
import cz.cuni.mff.d3s.jdeeco.ros.RosServices;
import cz.cuni.mff.d3s.jdeeco.ros.Sensors;

public class TestApp {

public static void main(String[] args) {
try
{
final SimulationTimer simulationTimer = new DiscreteEventTimer();
final DEECoSimulation simulation = new DEECoSimulation(simulationTimer);
simulation.addPlugin(Network.class);
simulation.addPlugin(DefaultKnowledgePublisher.class);
simulation.addPlugin(RosServices.class);

DEECoNode node = simulation.createNode(0);
node.deployComponent(new TestComponent("testComponent"));

Log.i("Simulation started.");
simulation.start(120000);
Log.i("Simulation finished.");
} catch (Exception e){
e.printStackTrace();
}

try {
/*
* final SimulationTimer simulationTimer = new DiscreteEventTimer();
* final DEECoSimulation simulation = new
* DEECoSimulation(simulationTimer);
* simulation.addPlugin(Network.class);
* simulation.addPlugin(DefaultKnowledgePublisher.class);
* simulation.addPlugin(RosServices.class);
*/

// DEECoNode node = simulation.createNode(0);

WallTimeTimer t = new WallTimeTimer();
RosServices services = new RosServices();
DEECoNode node = new DEECoNode(0, t, services);
Sensors sensors = services.getService(Sensors.class);
Actuators actuators = services.getService(Actuators.class);
node.deployComponent(new TestComponent("testComponent", sensors,
actuators));

Log.i("Simulation started.");

t.start();
// simulation.start(120000);
Log.i("Simulation finished.");
} catch (Exception e) {
e.printStackTrace();
}

}

Expand Down
70 changes: 35 additions & 35 deletions jdeeco-ros/src/cz/cuni/mff/d3s/jdeeco/ros/test/TestComponent.java
Original file line number Diff line number Diff line change
@@ -1,70 +1,70 @@
package cz.cuni.mff.d3s.jdeeco.ros.test;

import cz.cuni.mff.d3s.jdeeco.ros.datatypes.Sound;
import cz.cuni.mff.d3s.deeco.annotations.Component;
import cz.cuni.mff.d3s.deeco.annotations.InOut;
import cz.cuni.mff.d3s.deeco.annotations.In;
import cz.cuni.mff.d3s.deeco.annotations.Local;
import cz.cuni.mff.d3s.deeco.annotations.PeriodicScheduling;
import cz.cuni.mff.d3s.deeco.annotations.Process;
import cz.cuni.mff.d3s.deeco.task.ParamHolder;
import cz.cuni.mff.d3s.jdeeco.ros.Actuators;
import cz.cuni.mff.d3s.jdeeco.ros.Sensors;
import cz.cuni.mff.d3s.jdeeco.ros.datatypes.Bumper;
import cz.cuni.mff.d3s.jdeeco.ros.datatypes.LedColor;
import cz.cuni.mff.d3s.jdeeco.ros.datatypes.LedId;
import cz.cuni.mff.d3s.jdeeco.ros.datatypes.Sound;

@Component
public class TestComponent {

public String id;

public String msg;

@Local
public Sensors sensors;

public TestComponent(final String id) {
@Local
public Actuators actuators;

public TestComponent(final String id, Sensors sensors, Actuators actuators) {
this.id = id;
msg = "initial msg";
this.sensors = sensors;
this.actuators = actuators;
}

@Process
@PeriodicScheduling(period = 200)
public static void checkMessage(@InOut("msg") ParamHolder<String> message) {
/*System.out.println(message.value);
if (Sensors.getInstance().getPosition() != null) {
message.value = String.format("[%f, %f]", Sensors.getInstance()
.getPosition().getX(), Sensors.getInstance().getPosition()
.getY());
}*/
public static void checkMessage(@In("sensors") Sensors sensors,
@In("actuators") Actuators actuators) {
/*
* System.out.println(message.value); if
* (Sensors.getInstance().getPosition() != null) { message.value =
* String.format("[%f, %f]", Sensors.getInstance()
* .getPosition().getX(), Sensors.getInstance().getPosition() .getY());
* }
*/

//Actuators.getInstance().setVelocity(0.5,0);
Bumper bumper = Sensors.getInstance().getBumper();
//System.out.println(bumper);
switch(bumper){
actuators.setVelocity(0.5, 0);
Bumper bumper = sensors.getBumper();
// System.out.println(bumper);
switch (bumper) {
case CENTER:
Actuators.getInstance().setLed(LedId.LED1, LedColor.RED);
Actuators.getInstance().setLed(LedId.LED2, LedColor.RED);
Actuators.getInstance().playSound(Sound.RECHARGE);
actuators.setLed(LedId.LED1, LedColor.RED);
actuators.setLed(LedId.LED2, LedColor.RED);
actuators.playSound(Sound.RECHARGE);
break;
case LEFT:
Actuators.getInstance().setLed(LedId.LED1, LedColor.RED);
Actuators.getInstance().setLed(LedId.LED2, LedColor.BLACK);
Actuators.getInstance().playSound(Sound.CLEANING_END);
actuators.setLed(LedId.LED1, LedColor.RED);
actuators.setLed(LedId.LED2, LedColor.BLACK);
actuators.playSound(Sound.CLEANING_END);
break;
case RIGHT:
Actuators.getInstance().setLed(LedId.LED1, LedColor.BLACK);
Actuators.getInstance().setLed(LedId.LED2, LedColor.RED);
Actuators.getInstance().playSound(Sound.CLEANING_START);
actuators.setLed(LedId.LED1, LedColor.BLACK);
actuators.setLed(LedId.LED2, LedColor.RED);
actuators.playSound(Sound.CLEANING_START);
break;
default:
Actuators.getInstance().setLed(LedId.LED1, LedColor.BLACK);
Actuators.getInstance().setLed(LedId.LED2, LedColor.BLACK);
actuators.setLed(LedId.LED1, LedColor.BLACK);
actuators.setLed(LedId.LED2, LedColor.BLACK);
break;
}
try {
Thread.sleep(200);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

}

0 comments on commit b0c0820

Please sign in to comment.