BaseOdomPublisher.java
Go to the documentation of this file.
00001 package com.github.c77.base_controller;
00002 
00003 import com.github.c77.base_driver.BaseDevice;
00004 import com.github.c77.base_driver.OdometryStatus;
00005 
00006 import org.apache.commons.logging.Log;
00007 import org.apache.commons.logging.LogFactory;
00008 import org.ros.message.MessageFactory;
00009 import org.ros.message.Time;
00010 import org.ros.namespace.GraphName;
00011 import org.ros.node.AbstractNodeMain;
00012 import org.ros.node.ConnectedNode;
00013 import org.ros.node.NodeConfiguration;
00014 import org.ros.node.topic.Publisher;
00015 
00016 import java.util.List;
00017 
00018 import geometry_msgs.Point;
00019 import geometry_msgs.Quaternion;
00020 import geometry_msgs.Transform;
00021 import geometry_msgs.TransformStamped;
00022 import geometry_msgs.Twist;
00023 import geometry_msgs.Vector3;
00024 import nav_msgs.Odometry;
00025 import std_msgs.Header;
00026 import tf2_msgs.TFMessage;
00027 
00031 public class BaseOdomPublisher extends AbstractNodeMain {
00032     Thread basePublisherThread;
00033     private final BaseDevice baseDevice;
00034     NodeConfiguration mNodeConfiguration = NodeConfiguration.newPrivate();
00035     MessageFactory mMessageFactory = mNodeConfiguration.getTopicMessageFactory();
00036     private Publisher<Odometry> odometryPublisher;
00037     private Publisher<TFMessage> tfPublisher;
00038     private TransformStamped odomToBaseLink;
00039     private TransformStamped baseToLaser;
00040 
00041     private static final Log log = LogFactory.getLog(BaseOdomPublisher.class);
00042 
00043     public BaseOdomPublisher(BaseDevice baseDevice) {
00044         this.baseDevice = baseDevice;
00045     }
00046 
00047     @Override
00048     public GraphName getDefaultNodeName() {
00049         return GraphName.of("mobile_base/odom_publisher");
00050     }
00051 
00052     @Override
00053     public void onStart(ConnectedNode connectedNode) {
00054         basePublisherThread = new Thread() {
00055             @Override
00056             public void run() {
00057                 OdometryStatus odometryStatus;
00058                 try {
00059                     while(true){
00060                         odometryStatus = baseDevice.getOdometryStatus();
00061                         BaseOdomPublisher.this.publish(odometryStatus);
00062                         Thread.sleep(100, 0);
00063                     }
00064                 } catch (Throwable t) {
00065                     log.error("Exception occurred during state publisher loop.", t);
00066                 }
00067             }
00068         };
00069 
00070         odometryPublisher = connectedNode.newPublisher("/odom", "nav_msgs/Odometry");
00071         tfPublisher = connectedNode.newPublisher("/tf", TFMessage._TYPE);
00072         odomToBaseLink = mMessageFactory.newFromType(TransformStamped._TYPE);
00073         baseToLaser = mMessageFactory.newFromType(TransformStamped._TYPE);
00074 
00075         // Set laser transform.  (Static, never changes)
00076         Header h = baseToLaser.getHeader();
00077         h.setFrameId("base_link");
00078         baseToLaser.setChildFrameId("laser_link");
00079         Transform t = baseToLaser.getTransform();
00080         Vector3 tr = t.getTranslation();
00081         Quaternion q = t.getRotation();
00082         //TODO: Move laser_link tf publisher out of the Odometry Publisher.
00083         tr.setX(0.235); // For the Husky
00084         tr.setY(0.135); // For the Husky
00085         //tr.setX(0);   // For the Kobuky
00086         //tr.setY(0);   // For the Kobuky
00087         tr.setZ(0.0);
00088         q.setW(1.0);
00089         q.setX(0.0);
00090         q.setY(0.0);
00091         q.setZ(0.0);
00092 
00093         // Set constant childFrame
00094         odomToBaseLink.setChildFrameId("base_link");
00095 
00096         basePublisherThread.start();
00097     }
00098 
00099     private void publish(OdometryStatus odometryStatus) {
00100         // Create odomentry message
00101         Odometry odometryMessage = odometryPublisher.newMessage();
00102 
00103         // Header
00104         Header header = odometryMessage.getHeader();
00105         header.setFrameId("odom");
00106         header.setStamp(Time.fromMillis(System.currentTimeMillis()));
00107 
00108         // Child frame id
00109         odometryMessage.setChildFrameId("base_link");
00110 
00111         // Pointers to important data fields
00112         Point position = odometryMessage.getPose().getPose().getPosition();
00113         Quaternion orientation = odometryMessage.getPose().getPose().getOrientation();
00114         Twist twist = odometryMessage.getTwist().getTwist();
00115 
00116         // Populate the fields. Synchronize to avoid having the data updated in between gets
00117         synchronized (odometryStatus) {
00118             position.setX(odometryStatus.getPoseX());
00119             position.setY(odometryStatus.getPoseY());
00120             orientation.setZ(Math.sin(odometryStatus.getPoseTheta()/2.0));
00121             orientation.setW(Math.cos(odometryStatus.getPoseTheta()/2.0));
00122             twist.getLinear().setX(odometryStatus.getSpeedLinearX());
00123             twist.getAngular().setZ(odometryStatus.getSpeedAngularZ());
00124         }
00125 
00126         // Publish!
00127         odometryPublisher.publish(odometryMessage);
00128 
00129         // Set odom transform
00130         odomToBaseLink.setHeader(header);
00131         Transform t = odomToBaseLink.getTransform();
00132         Vector3 tr = t.getTranslation();
00133         tr.setX(position.getX());
00134         tr.setY(position.getY());
00135         tr.setZ(position.getZ());
00136         t.setRotation(orientation);
00137 
00138         // Set laser transform time
00139         baseToLaser.getHeader().setStamp(header.getStamp());
00140 
00141         // Publish transforms
00142         TFMessage tfm = tfPublisher.newMessage();
00143         List<TransformStamped> tfl = tfm.getTransforms();
00144         tfl.add(odomToBaseLink);
00145         tfl.add(baseToLaser);
00146         tfPublisher.publish(tfm);
00147     }
00148 }


android_base_controller
Author(s):
autogenerated on Fri Aug 28 2015 10:04:47