MessagesBenchmark.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.node.AbstractNodeMain;
00026 import org.ros.node.ConnectedNode;
00027 import org.ros.node.topic.Publisher;
00028 
00029 import java.util.concurrent.TimeUnit;
00030 import java.util.concurrent.atomic.AtomicInteger;
00031 
00035 public class MessagesBenchmark extends AbstractNodeMain {
00036 
00037   private final AtomicInteger counter;
00038 
00039   private Time time;
00040 
00041   public MessagesBenchmark() {
00042     counter = new AtomicInteger();
00043   }
00044 
00045   @Override
00046   public GraphName getDefaultNodeName() {
00047     return GraphName.of("messages_benchmark");
00048   }
00049 
00050   @Override
00051   public void onStart(final ConnectedNode connectedNode) {
00052     connectedNode.executeCancellableLoop(new CancellableLoop() {
00053       @Override
00054       protected void loop() throws InterruptedException {
00055         geometry_msgs.TransformStamped message =
00056             connectedNode.getTopicMessageFactory()
00057                 .newFromType(geometry_msgs.TransformStamped._TYPE);
00058         message.getHeader().setFrameId("world");
00059         message.setChildFrameId("turtle");
00060         message.getHeader().setStamp(connectedNode.getCurrentTime());
00061         message.getTransform().getRotation().setW(Math.random());
00062         message.getTransform().getRotation().setX(Math.random());
00063         message.getTransform().getRotation().setY(Math.random());
00064         message.getTransform().getRotation().setZ(Math.random());
00065         message.getTransform().getTranslation().setX(Math.random());
00066         message.getTransform().getTranslation().setY(Math.random());
00067         message.getTransform().getTranslation().setZ(Math.random());
00068         counter.incrementAndGet();
00069       }
00070     });
00071 
00072     time = connectedNode.getCurrentTime();
00073     final Publisher<std_msgs.String> statusPublisher =
00074         connectedNode.newPublisher("status", std_msgs.String._TYPE);
00075     final Rate rate = new WallTimeRate(1);
00076     final std_msgs.String status = statusPublisher.newMessage();
00077     connectedNode.executeCancellableLoop(new CancellableLoop() {
00078       @Override
00079       protected void loop() throws InterruptedException {
00080         Time now = connectedNode.getCurrentTime();
00081         Duration delta = now.subtract(time);
00082         if (delta.totalNsecs() > TimeUnit.NANOSECONDS.convert(5, TimeUnit.SECONDS)) {
00083           double hz = counter.getAndSet(0) * 1e9 / delta.totalNsecs();
00084           status.setData(String.format("%.2f Hz", hz));
00085           statusPublisher.publish(status);
00086           time = now;
00087         }
00088         rate.sleep();
00089       }
00090     });
00091   }
00092 }


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