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.rosjava.rosjava_extras.hokuyo;
00018
00019 import com.google.common.annotations.VisibleForTesting;
00020 import com.google.common.base.Preconditions;
00021
00022 import org.ros.namespace.GraphName;
00023 import org.ros.node.AbstractNodeMain;
00024 import org.ros.node.ConnectedNode;
00025 import org.ros.node.Node;
00026 import org.ros.node.parameter.ParameterTree;
00027 import org.ros.node.topic.Publisher;
00028
00032 public class LaserScanPublisher extends AbstractNodeMain {
00033
00034 private final LaserScannerDevice laserScannerDevice;
00035
00036 private Publisher<sensor_msgs.LaserScan> publisher;
00037
00042 public LaserScanPublisher(LaserScannerDevice laserScannerDevice) {
00043 this.laserScannerDevice = laserScannerDevice;
00044 }
00045
00046 @Override
00047 public GraphName getDefaultNodeName() {
00048 return GraphName.of("android_hokuyo/laser_scan_publisher");
00049 }
00050
00051 @Override
00052 public void onStart(ConnectedNode connectedNode) {
00053 ParameterTree params = connectedNode.getParameterTree();
00054 final String laserTopic = params.getString("~laser_topic", "laser");
00055 final String laserFrame = params.getString("~laser_frame", "laser");
00056 publisher =
00057 connectedNode.newPublisher(connectedNode.resolveName(laserTopic),
00058 sensor_msgs.LaserScan._TYPE);
00059 laserScannerDevice.startScanning(new LaserScanListener() {
00060 @Override
00061 public void onNewLaserScan(LaserScan scan) {
00062 sensor_msgs.LaserScan message =
00063 toLaserScanMessage(laserFrame, scan, publisher.newMessage());
00064 publisher.publish(message);
00065 }
00066 });
00067 }
00068
00069 @Override
00070 public void onShutdownComplete(Node node) {
00071 laserScannerDevice.shutdown();
00072 }
00073
00074 @VisibleForTesting
00075 Publisher<sensor_msgs.LaserScan> getPublisher() {
00076 return publisher;
00077 }
00078
00096 @VisibleForTesting
00097 sensor_msgs.LaserScan toLaserScanMessage(String laserFrame, LaserScan scan,
00098 sensor_msgs.LaserScan result) {
00099 LaserScannerConfiguration configuration = laserScannerDevice.getConfiguration();
00100 result.setAngleIncrement(configuration.getAngleIncrement());
00101 result.setAngleMin(configuration.getMinimumAngle());
00102 result.setAngleMax(configuration.getMaximumAngle());
00103 int numberOfConfiguredRanges = configuration.getLastStep() - configuration.getFirstStep() + 1;
00104 Preconditions.checkState(numberOfConfiguredRanges <= scan.getRanges().length, String.format(
00105 "Number of scans in configuration does not match received range measurements (%d > %d).",
00106 numberOfConfiguredRanges, scan.getRanges().length));
00107 float[] ranges = new float[numberOfConfiguredRanges];
00108 for (int i = 0; i < numberOfConfiguredRanges; i++) {
00109 int step = i + configuration.getFirstStep();
00110
00111
00112 ranges[i] = (float) (scan.getRanges()[step] / 1000.0);
00113 }
00114 result.setRanges(ranges);
00115 result.setTimeIncrement(configuration.getTimeIncrement());
00116 result.setScanTime(configuration.getScanTime());
00117 result.setRangeMin((float) (configuration.getMinimumMeasurment() / 1000.0));
00118 result.setRangeMax((float) (configuration.getMaximumMeasurement() / 1000.0));
00119 result.getHeader().setFrameId(laserFrame);
00120 result.getHeader().setStamp(scan.getTime());
00121 return result;
00122 }
00123 }