Utils.java
Go to the documentation of this file.
00001 /*
00002 * Copyright (C) 2014 Mickael Gaillard. All rights reserved.
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.dynamic_reconfigure;
00018 
00019 import org.ros.message.MessageFactory;
00020 import org.ros.node.ConnectedNode;
00021 
00022 import dynamic_reconfigure.BoolParameter;
00023 import dynamic_reconfigure.Config;
00024 import dynamic_reconfigure.ConfigDescription;
00025 import dynamic_reconfigure.DoubleParameter;
00026 import dynamic_reconfigure.Group;
00027 import dynamic_reconfigure.GroupState;
00028 import dynamic_reconfigure.IntParameter;
00029 import dynamic_reconfigure.ParamDescription;
00030 import dynamic_reconfigure.StrParameter;
00031 
00038 public class Utils {
00039 
00040     public static final String GROUP_DEFAULT   = "Default";
00041     public static final String TYPE_INT        = "int";
00042     public static final String TYPE_BOOL       = "bool";
00043     public static final String TYPE_STR        = "str";
00044     public static final String TYPE_DBL        = "double";
00045 
00046     private MessageFactory factory;
00047     private ConfigDescription descConfig;
00048     private Config updateConfig;
00049     private final Group defaultGroup;
00050     private final GroupState activeGroupState;
00051 
00052     public Utils(ConnectedNode node, Config currentUpdateConfig) {
00053         this.updateConfig = currentUpdateConfig;
00054         this.factory = node.getTopicMessageFactory();
00055         this.descConfig = this.factory.newFromType(ConfigDescription._TYPE);
00056 
00057         // Group
00058         this.defaultGroup = this.factory.newFromType(Group._TYPE);
00059         this.defaultGroup.setId(0);
00060         this.defaultGroup.setName(GROUP_DEFAULT);
00061         this.descConfig.getGroups().add(this.defaultGroup);
00062 
00063         // GroupState
00064         this.activeGroupState = this.factory.newFromType(GroupState._TYPE);
00065         this.activeGroupState.setId(0);
00066         this.activeGroupState.setName(GROUP_DEFAULT);
00067         this.activeGroupState.setState(true);
00068     }
00069 
00070     public void updateField(String name, String type, String value) {
00071         Utils.encodeConfig(this.factory, name, type, value, this.updateConfig);
00072     }
00073 
00084     public void addField(
00085             String name,
00086             String type,
00087             int level,
00088             String description,
00089             Object defaultValue,
00090             int minValue,
00091             int maxValue) {
00092 
00093         ParamDescription descParam = null;
00094         for (ParamDescription param : this.defaultGroup.getParameters()) {
00095             if (param.getName().equals(name)) {
00096                 descParam = param;
00097                 break;
00098             }
00099         }
00100 
00101         if (descParam == null) {
00102             descParam = this.factory.newFromType(ParamDescription._TYPE);
00103             descParam.setName(name);
00104             this.defaultGroup.getParameters().add(descParam);
00105         }
00106 
00107         descParam.setDescription(description);
00108         descParam.setLevel(level);
00109         descParam.setType(type);
00110 
00111         // Update Description message
00112         Utils.encodeConfig(
00113                 this.factory,
00114                 name,
00115                 type,
00116                 defaultValue,
00117                 this.descConfig.getDflt());
00118         Utils.encodeConfig(
00119                 this.factory,
00120                 name,
00121                 "int",
00122                 minValue,
00123                 this.descConfig.getMin());
00124         Utils.encodeConfig(
00125                 this.factory,
00126                 name,
00127                 "int",
00128                 maxValue,
00129                 this.descConfig.getMax());
00130         // TODO group case...
00131 
00132         // Update Current config message
00133         Utils.encodeConfig(
00134                 this.factory,
00135                 name,
00136                 type,
00137                 defaultValue,
00138                 this.updateConfig);
00139     }
00140 
00141     public ConfigDescription makeConfigDescription() {
00142         return this.descConfig;
00143     }
00144 
00145     public Config makeConfigUpdate() {
00146         return this.updateConfig;
00147     }
00148 
00157     public static void encodeConfig(MessageFactory factory, String name, String type, Object value, Config current) {
00158 
00159         //Sorry but no inheritance in this case...
00160 
00161         if (TYPE_INT.equals(type)) {
00162             IntParameter defaultParam = null;
00163             for (IntParameter param : current.getInts()) {
00164                 if (param.getName().equals(name)) {
00165                     defaultParam = param;
00166                     break;
00167                 }
00168             }
00169 
00170             if (defaultParam == null) {
00171                 defaultParam = factory.newFromType(IntParameter._TYPE);
00172                 defaultParam.setName(name);
00173                 current.getInts().add(defaultParam);
00174             }
00175 
00176             defaultParam.setValue((Integer)value);
00177 
00178         } else
00179 
00180         if (TYPE_BOOL.equals(type)) {
00181             BoolParameter defaultParam = null;
00182             for (BoolParameter param : current.getBools()) {
00183                 if (param.getName().equals(name)) {
00184                     defaultParam = param;
00185                     break;
00186                 }
00187             }
00188 
00189             if (defaultParam == null) {
00190                 defaultParam = factory.newFromType(BoolParameter._TYPE);
00191                 defaultParam.setName(name);
00192                 current.getBools().add(defaultParam);
00193             }
00194 
00195             defaultParam.setValue((Boolean)value);
00196         } else
00197 
00198         if (TYPE_STR.equals(type)) {
00199             StrParameter defaultParam = null;
00200             for (StrParameter param : current.getStrs()) {
00201                 if (param.getName().equals(name)) {
00202                     defaultParam = param;
00203                     break;
00204                 }
00205             }
00206 
00207             if (defaultParam == null) {
00208                 defaultParam = factory.newFromType(StrParameter._TYPE);
00209                 defaultParam.setName(name);
00210                 current.getStrs().add(defaultParam);
00211             }
00212 
00213             defaultParam.setValue((String)value);
00214         } else
00215 
00216         if (TYPE_DBL.equals(type)) {
00217             DoubleParameter defaultParam = null;
00218             for (DoubleParameter param : current.getDoubles()) {
00219                 if (param.getName().equals(name)) {
00220                     defaultParam = param;
00221                     break;
00222                 }
00223             }
00224 
00225             if (defaultParam == null) {
00226                 defaultParam = factory.newFromType(DoubleParameter._TYPE);
00227                 defaultParam.setName(name);
00228                 current.getDoubles().add(defaultParam);
00229             }
00230 
00231             defaultParam.setValue((Double)value);
00232         }
00233     }
00234 }


rosjava_dynamic_reconfigure
Author(s): Mickaƫl Gaillard , Erwan Le Huitouze
autogenerated on Thu Jun 6 2019 22:01:52