Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 package com.github.c77.base_controller;
00018
00019 import com.github.c77.base_driver.BaseDevice;
00020
00021 import org.apache.commons.logging.Log;
00022 import org.apache.commons.logging.LogFactory;
00023 import org.ros.message.MessageListener;
00024 import org.ros.namespace.GraphName;
00025 import org.ros.node.AbstractNodeMain;
00026 import org.ros.node.ConnectedNode;
00027 import org.ros.node.Node;
00028 import org.ros.node.topic.Subscriber;
00029
00030 import geometry_msgs.Twist;
00031
00036 public class BaseControllerNode extends AbstractNodeMain implements MessageListener<Twist> {
00037 private final BaseDevice baseDevice;
00038 private double linearVelX = 0.0;
00039 private double angVelZ = 0.0;
00040 private String CMD_VEL_TOPIC;
00041
00042 private static final Log log = LogFactory.getLog(BaseControllerNode.class);
00043 Thread baseControllerThread;
00044
00045 @Override
00046 public GraphName getDefaultNodeName() {
00047 return GraphName.of("android/move_base");
00048 }
00049
00056 public BaseControllerNode(BaseDevice baseDevice, String vel_topic) {
00057
00058 CMD_VEL_TOPIC = vel_topic;
00059 this.baseDevice = baseDevice;
00060 }
00061
00068 @Override
00069 public void onStart(ConnectedNode connectedNode) {
00070 log.info("Base controller starting.");
00071
00078 baseControllerThread = new Thread() {
00079 @Override
00080 public void run() {
00081
00082 try {
00083 while(true) {
00084 baseDevice.move(linearVelX, angVelZ);
00085 Thread.sleep(250);
00086 }
00087 } catch (Throwable t) {
00088 log.error("Exception occurred during move loop", t);
00089
00090
00091 try {
00092 setTwistValues(0.0, 0.0);
00093 baseDevice.move(0.0, 0.0);
00094 } catch(Throwable t0) {
00095 }
00096 }
00097 }
00098 };
00099
00100
00101 baseDevice.initialize();
00102 baseControllerThread.start();
00103
00104
00105 Subscriber<Twist> vel_listener = connectedNode.newSubscriber(CMD_VEL_TOPIC, Twist._TYPE);
00106 vel_listener.addMessageListener(this);
00107
00108 log.info("Base controller initialized.");
00109 }
00110
00111 @Override
00112 public void onShutdown(Node node) {
00113
00114 super.onShutdown(node);
00115 }
00116
00117 @Override
00118 public void onShutdownComplete(Node node) {
00119
00120 super.onShutdownComplete(node);
00121 }
00122
00123 private synchronized void setTwistValues(double linearVelX, double angVelZ) {
00124 this.linearVelX = linearVelX;
00125 this.angVelZ = angVelZ;
00126 log.info("synchronized setting: (" + this.linearVelX + "," + this.angVelZ + ")");
00127 }
00128
00135 @Override
00136 public void onNewMessage(Twist twist) {
00137 log.info("Current Twist msg: " + twist);
00138 setTwistValues(twist.getLinear().getX(), twist.getAngular().getZ());
00139 }
00140 }