TransformBenchmark.java
Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2012 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 org.ros.rosjava_benchmarks;
00018 
00019 import org.ros.concurrent.CancellableLoop;
00020 import org.ros.concurrent.Rate;
00021 import org.ros.concurrent.WallTimeRate;
00022 import org.ros.message.Duration;
00023 import org.ros.message.Time;
00024 import org.ros.namespace.GraphName;
00025 import org.ros.namespace.NameResolver;
00026 import org.ros.node.AbstractNodeMain;
00027 import org.ros.node.ConnectedNode;
00028 import org.ros.node.topic.Publisher;
00029 import org.ros.rosjava_geometry.FrameTransformTree;
00030 
00031 import java.util.concurrent.TimeUnit;
00032 import java.util.concurrent.atomic.AtomicInteger;
00033 
00037 public class TransformBenchmark extends AbstractNodeMain {
00038 
00039   private final AtomicInteger counter;
00040 
00041   private Time time;
00042 
00043   public TransformBenchmark() {
00044     counter = new AtomicInteger();
00045   }
00046 
00047   @Override
00048   public GraphName getDefaultNodeName() {
00049     return GraphName.of("transform_benchmark");
00050   }
00051 
00052   @Override
00053   public void onStart(final ConnectedNode connectedNode) {
00054     final geometry_msgs.TransformStamped turtle1 =
00055         connectedNode.getTopicMessageFactory().newFromType(geometry_msgs.TransformStamped._TYPE);
00056     turtle1.getHeader().setFrameId("world");
00057     turtle1.setChildFrameId("turtle1");
00058     final geometry_msgs.TransformStamped turtle2 =
00059         connectedNode.getTopicMessageFactory().newFromType(geometry_msgs.TransformStamped._TYPE);
00060     turtle2.getHeader().setFrameId("world");
00061     turtle2.setChildFrameId("turtle2");
00062     final FrameTransformTree frameTransformTree = new FrameTransformTree();
00063     connectedNode.executeCancellableLoop(new CancellableLoop() {
00064       @Override
00065       protected void loop() throws InterruptedException {
00066         updateTransform(turtle1, connectedNode, frameTransformTree);
00067         updateTransform(turtle2, connectedNode, frameTransformTree);
00068         frameTransformTree.transform("turtle1", "turtle2");
00069         counter.incrementAndGet();
00070       }
00071     });
00072 
00073     time = connectedNode.getCurrentTime();
00074     final Publisher<std_msgs.String> statusPublisher =
00075         connectedNode.newPublisher("status", std_msgs.String._TYPE);
00076     final Rate rate = new WallTimeRate(1);
00077     final std_msgs.String status = statusPublisher.newMessage();
00078     connectedNode.executeCancellableLoop(new CancellableLoop() {
00079       @Override
00080       protected void loop() throws InterruptedException {
00081         Time now = connectedNode.getCurrentTime();
00082         Duration delta = now.subtract(time);
00083         if (delta.totalNsecs() > TimeUnit.NANOSECONDS.convert(5, TimeUnit.SECONDS)) {
00084           double hz = counter.getAndSet(0) * 1e9 / delta.totalNsecs();
00085           status.setData(String.format("%.2f Hz", hz));
00086           statusPublisher.publish(status);
00087           time = now;
00088         }
00089         rate.sleep();
00090       }
00091     });
00092   }
00093 
00094   private void updateTransform(geometry_msgs.TransformStamped transformStamped,
00095       ConnectedNode connectedNode, FrameTransformTree frameTransformTree) {
00096     transformStamped.getHeader().setStamp(connectedNode.getCurrentTime());
00097     transformStamped.getTransform().getRotation().setW(Math.random());
00098     transformStamped.getTransform().getRotation().setX(Math.random());
00099     transformStamped.getTransform().getRotation().setY(Math.random());
00100     transformStamped.getTransform().getRotation().setZ(Math.random());
00101     transformStamped.getTransform().getTranslation().setX(Math.random());
00102     transformStamped.getTransform().getTranslation().setY(Math.random());
00103     transformStamped.getTransform().getTranslation().setZ(Math.random());
00104     frameTransformTree.update(transformStamped);
00105   }
00106 }


rosjava_core
Author(s):
autogenerated on Wed Aug 26 2015 16:06:49