LaserScanPublisher.java
Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2011 Google Inc.
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
00005  * use this file except in compliance with the License. You may obtain a copy of
00006  * the License at
00007  *
00008  * http://www.apache.org/licenses/LICENSE-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
00012  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
00013  * License for the specific language governing permissions and limitations under
00014  * the License.
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       // Select only the configured range measurements and convert from
00111       // millimeters to meters.
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 }


rosjava_extras
Author(s): Damon Kohler
autogenerated on Thu Aug 27 2015 14:53:42