FakeLaserDevice.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 org.ros.message.Time;
00020 
00024 public class FakeLaserDevice implements LaserScannerDevice {
00025 
00026   private static final int SCAN_PUBLISH_FREQUENCY = 10;
00027 
00028   private RepeatingScanGeneratorThread scanGeneratorThread;
00029   private int numberOfRangeValues;
00030 
00031   private class RepeatingScanGeneratorThread extends Thread {
00032     private LaserScanListener listener;
00033     private int frequency;
00034 
00035     public RepeatingScanGeneratorThread(int frequency, LaserScanListener listener) {
00036       this.listener = listener;
00037       this.frequency = frequency;
00038     }
00039 
00040     @Override
00041     public void run() {
00042       try {
00043         while (!Thread.currentThread().isInterrupted()) {
00044           listener.onNewLaserScan(makeFakeScan());
00045           Thread.sleep((long) (1000f / frequency));
00046         }
00047       } catch (InterruptedException e) {
00048         // Cancelable
00049       }
00050     }
00051 
00052     public void cancel() {
00053       interrupt();
00054     }
00055   }
00056 
00057   public FakeLaserDevice(int numberOfRangeValues) {
00058     this.numberOfRangeValues = numberOfRangeValues;
00059   }
00060 
00061   @Override
00062   public void startScanning(LaserScanListener listener) {
00063     if (scanGeneratorThread != null) {
00064       scanGeneratorThread.cancel();
00065     }
00066     scanGeneratorThread = new RepeatingScanGeneratorThread(SCAN_PUBLISH_FREQUENCY, listener);
00067     scanGeneratorThread.start();
00068   }
00069 
00070   @Override
00071   public void shutdown() {
00072     if (scanGeneratorThread != null) {
00073       scanGeneratorThread.cancel();
00074     }
00075   }
00076 
00077   @Override
00078   public LaserScannerConfiguration getConfiguration() {
00079     return new FakeLaserScannerConfiguration();
00080   }
00081 
00082   public LaserScan makeFakeScan() {
00083     int[] fakeRangeMeasurements = new int[numberOfRangeValues];
00084     for (int i = 0; i < numberOfRangeValues; i++) {
00085       fakeRangeMeasurements[i] = 0;
00086     }
00087     return new LaserScan(new Time(), fakeRangeMeasurements);
00088   }
00089 }


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