ParameterServerTestNode.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 org.ros;
00018 
00019 import org.apache.commons.logging.Log;
00020 import org.ros.concurrent.CancellableLoop;
00021 import org.ros.message.MessageFactory;
00022 import org.ros.namespace.GraphName;
00023 import org.ros.namespace.NameResolver;
00024 import org.ros.node.AbstractNodeMain;
00025 import org.ros.node.ConnectedNode;
00026 import org.ros.node.parameter.ParameterTree;
00027 import org.ros.node.topic.Publisher;
00028 
00029 import java.util.List;
00030 import java.util.Map;
00031 
00038 public class ParameterServerTestNode extends AbstractNodeMain {
00039 
00040   @Override
00041   public GraphName getDefaultNodeName() {
00042     return GraphName.of("rosjava/parameter_server_test_node");
00043   }
00044 
00045   @SuppressWarnings("rawtypes")
00046   @Override
00047   public void onStart(ConnectedNode connectedNode) {
00048     final Publisher<std_msgs.String> pub_tilde =
00049         connectedNode.newPublisher("tilde", std_msgs.String._TYPE);
00050     final Publisher<std_msgs.String> pub_string =
00051         connectedNode.newPublisher("string", std_msgs.String._TYPE);
00052     final Publisher<std_msgs.Int64> pub_int =
00053         connectedNode.newPublisher("int", std_msgs.Int64._TYPE);
00054     final Publisher<std_msgs.Bool> pub_bool =
00055         connectedNode.newPublisher("bool", std_msgs.Bool._TYPE);
00056     final Publisher<std_msgs.Float64> pub_float =
00057         connectedNode.newPublisher("float", std_msgs.Float64._TYPE);
00058     final Publisher<rosjava_test_msgs.Composite> pub_composite =
00059         connectedNode.newPublisher("composite", rosjava_test_msgs.Composite._TYPE);
00060     final Publisher<rosjava_test_msgs.TestArrays> pub_list =
00061         connectedNode.newPublisher("list", rosjava_test_msgs.TestArrays._TYPE);
00062 
00063     ParameterTree param = connectedNode.getParameterTree();
00064 
00065     Log log = connectedNode.getLog();
00066     MessageFactory topicMessageFactory = connectedNode.getTopicMessageFactory();
00067 
00068     final std_msgs.String tilde_m = topicMessageFactory.newFromType(std_msgs.String._TYPE);
00069     tilde_m.setData(param.getString(connectedNode.resolveName("~tilde").toString()));
00070     log.info("tilde: " + tilde_m.getData());
00071 
00072     GraphName paramNamespace = GraphName.of(param.getString("parameter_namespace"));
00073     GraphName targetNamespace = GraphName.of(param.getString("target_namespace"));
00074     log.info("parameter_namespace: " + paramNamespace);
00075     log.info("target_namespace: " + targetNamespace);
00076     NameResolver resolver = connectedNode.getResolver().newChild(paramNamespace);
00077     NameResolver setResolver = connectedNode.getResolver().newChild(targetNamespace);
00078 
00079     final std_msgs.String string_m = topicMessageFactory.newFromType(std_msgs.String._TYPE);
00080     string_m.setData(param.getString(resolver.resolve("string")));
00081     log.info("string: " + string_m.getData());
00082     final std_msgs.Int64 int_m = topicMessageFactory.newFromType(std_msgs.Int64._TYPE);
00083     int_m.setData(param.getInteger(resolver.resolve("int")));
00084     log.info("int: " + int_m.getData());
00085 
00086     final std_msgs.Bool bool_m = topicMessageFactory.newFromType(std_msgs.Bool._TYPE);
00087     bool_m.setData(param.getBoolean(resolver.resolve("bool")));
00088     log.info("bool: " + bool_m.getData());
00089     final std_msgs.Float64 float_m = topicMessageFactory.newFromType(std_msgs.Float64._TYPE);
00090     float_m.setData(param.getDouble(resolver.resolve("float")));
00091     log.info("float: " + float_m.getData());
00092 
00093     final rosjava_test_msgs.Composite composite_m =
00094         topicMessageFactory.newFromType(rosjava_test_msgs.Composite._TYPE);
00095     Map composite_map = param.getMap(resolver.resolve("composite"));
00096     composite_m.getA().setW((Double) ((Map) composite_map.get("a")).get("w"));
00097     composite_m.getA().setX((Double) ((Map) composite_map.get("a")).get("x"));
00098     composite_m.getA().setY((Double) ((Map) composite_map.get("a")).get("y"));
00099     composite_m.getA().setZ((Double) ((Map) composite_map.get("a")).get("z"));
00100     composite_m.getB().setX((Double) ((Map) composite_map.get("b")).get("x"));
00101     composite_m.getB().setY((Double) ((Map) composite_map.get("b")).get("y"));
00102     composite_m.getB().setZ((Double) ((Map) composite_map.get("b")).get("z"));
00103 
00104     final rosjava_test_msgs.TestArrays list_m = topicMessageFactory.newFromType(rosjava_test_msgs.TestArrays._TYPE);
00105     // only using the integer part for easier (non-float) comparison
00106     @SuppressWarnings("unchecked")
00107     List<Integer> list = (List<Integer>) param.getList(resolver.resolve("list"));
00108     int[] data = new int[list.size()];
00109     for (int i = 0; i < list.size(); i++) {
00110       data[i] = list.get(i);
00111     }
00112     list_m.setInt32Array(data);
00113 
00114     // Set parameters
00115     param.set(setResolver.resolve("string"), string_m.getData());
00116     param.set(setResolver.resolve("int"), (int) int_m.getData());
00117     param.set(setResolver.resolve("float"), float_m.getData());
00118     param.set(setResolver.resolve("bool"), bool_m.getData());
00119     param.set(setResolver.resolve("composite"), composite_map);
00120     param.set(setResolver.resolve("list"), list);
00121 
00122     connectedNode.executeCancellableLoop(new CancellableLoop() {
00123       @Override
00124       protected void loop() throws InterruptedException {
00125         pub_tilde.publish(tilde_m);
00126         pub_string.publish(string_m);
00127         pub_int.publish(int_m);
00128         pub_bool.publish(bool_m);
00129         pub_float.publish(float_m);
00130         pub_composite.publish(composite_m);
00131         pub_list.publish(list_m);
00132         Thread.sleep(100);
00133       }
00134     });
00135   }
00136 }


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