Server.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 package org.ros.dynamic_reconfigure.server;
00017 
00018 import org.ros.exception.ServiceException;
00019 import org.ros.node.ConnectedNode;
00020 import org.ros.node.service.ServiceResponseBuilder;
00021 import org.ros.node.service.ServiceServer;
00022 import org.ros.node.topic.Publisher;
00023 import org.ros.dynamic_reconfigure.DynamicReconfigureCallbackException;
00024 import org.ros.dynamic_reconfigure.DynamicReconfigureException;
00025 
00026 import dynamic_reconfigure.BoolParameter;
00027 import dynamic_reconfigure.Config;
00028 import dynamic_reconfigure.ConfigDescription;
00029 import dynamic_reconfigure.DoubleParameter;
00030 import dynamic_reconfigure.IntParameter;
00031 import dynamic_reconfigure.Reconfigure;
00032 import dynamic_reconfigure.ReconfigureRequest;
00033 import dynamic_reconfigure.ReconfigureResponse;
00034 import dynamic_reconfigure.StrParameter;
00035 
00042 public class Server<T extends BaseConfig> implements ServiceResponseBuilder<ReconfigureRequest, ReconfigureResponse> {
00043 
00044     // Current state & node engine
00046     private final transient ConnectedNode node;
00047 
00049     private final transient T configInstance;
00050 
00052     private transient Config config;
00054     private final transient ConfigDescription description;
00055 
00056     // Topics and Services
00058     private final transient Publisher<Config> publisherUpdate;
00060     private final transient Publisher<ConfigDescription> publisherDescription;
00062     private final transient ServiceServer<ReconfigureRequest, ReconfigureResponse> serviceReconfigure;
00063 
00065     private ReconfigureListener<T> callback;
00066 
00073     public Server(ConnectedNode connectedNode, T config, ReconfigureListener<T> callback) {
00074         // Assert
00075         if (connectedNode == null) {
00076             throw new DynamicReconfigureException("Node not connected !!");
00077         }
00078 
00079         // Init
00080         this.configInstance = config;
00081         this.node = connectedNode;
00082         this.config = this.configInstance.getCurrentConfig();
00083 
00084         this.description = this.configInstance.makeConfigDescription();
00085 
00086         // Restore configuration from last run.
00087         this.copyFromParameterServer();
00088         this.setCallback(callback);
00089 
00090         // Description.
00091         this.publisherDescription = this.node.newPublisher("~parameter_descriptions", ConfigDescription._TYPE);
00092         this.publisherDescription.setLatchMode(true);
00093         this.publisherDescription.publish(description);
00094 
00095         // Updater.
00096         this.publisherUpdate = this.node.newPublisher("~parameter_updates", Config._TYPE);
00097         this.publisherUpdate.setLatchMode(true);
00098         this.changeConfig(this.config, 0);
00099 
00100         // Service.
00101         this.serviceReconfigure = this.node.newServiceServer("~set_parameters", Reconfigure._TYPE, this);
00102     }
00103 
00110     private Config changeConfig(Config config, int level) {
00111         this.node.getLog().info("Update config !");
00112 
00113         if (this.configInstance != null) {
00114             //TODO Update config instance !!!
00115             this.configInstance.merge(config);
00116 
00117             // Notify callback config has change.
00118             Config configResult = this.callback
00119                     .onReconfigure(this.configInstance, level)
00120                         .getCurrentConfig();
00121 
00122             if (configResult == null) {
00123                 String msg = "Reconfigure callback should return a possibly updated configuration.";
00124                 this.node.getLog().error(msg);
00125                 throw new DynamicReconfigureCallbackException(msg);
00126             }
00127 
00128             this.config = configResult;
00129             // Save Configuration for next run.
00130             this.copyToParameterServer();
00131             this.publisherUpdate.publish(this.config);
00132         }
00133 
00134         return this.config;
00135     }
00136 
00138     protected void copyToParameterServer() {
00139         String rosParamNameString;
00140         this.node.getLog().info("Save to parameter server...");
00141 
00142         for (final BoolParameter param : this.config.getBools()) {
00143             rosParamNameString = "~" + param.getName();
00144             this.node.getLog().debug("save : " + rosParamNameString);
00145             this.node.getParameterTree().set(
00146                     rosParamNameString,
00147                     param.getValue());
00148         }
00149         for (final IntParameter param : this.config.getInts()) {
00150             rosParamNameString = "~" + param.getName();
00151             this.node.getLog().debug("save : " + rosParamNameString);
00152             this.node.getParameterTree().set(
00153                     rosParamNameString,
00154                     param.getValue());
00155         }
00156         for (final StrParameter param : this.config.getStrs()) {
00157             rosParamNameString = "~" + param.getName();
00158             this.node.getLog().debug("save : " + rosParamNameString);
00159             this.node.getParameterTree().set(
00160                     rosParamNameString,
00161                     param.getValue());
00162         }
00163         for (final DoubleParameter param : this.config.getDoubles()) {
00164             rosParamNameString = "~" + param.getName();
00165             this.node.getLog().debug("save : " + rosParamNameString);
00166             this.node.getParameterTree().set(
00167                     rosParamNameString,
00168                     param.getValue());
00169         }
00170     }
00171 
00173     protected void copyFromParameterServer() {
00174         String rosParamNameString;
00175         this.node.getLog().info("Load from parameter server...");
00176 
00177         for (final BoolParameter param : this.config.getBools()) {
00178             rosParamNameString = "~" + param.getName();
00179             if (this.node.getParameterTree().search(rosParamNameString) != null) {
00180                 this.node.getLog().debug("found : " + rosParamNameString);
00181                 param.setValue(
00182                         this.node.getParameterTree()
00183                             .getBoolean(rosParamNameString));
00184         }}
00185         for (final IntParameter param : this.config.getInts()) {
00186             rosParamNameString = "~" + param.getName();
00187             if (this.node.getParameterTree().search(rosParamNameString) != null) {
00188                 this.node.getLog().debug("found : " + rosParamNameString);
00189                 param.setValue(
00190                         this.node.getParameterTree()
00191                             .getInteger(rosParamNameString));
00192         }}
00193         for (final StrParameter param : this.config.getStrs()) {
00194             rosParamNameString = "~" + param.getName();
00195             if (this.node.getParameterTree().search(rosParamNameString) != null) {
00196                 this.node.getLog().debug("found : " + rosParamNameString);
00197                 param.setValue(
00198                         this.node.getParameterTree()
00199                             .getString(rosParamNameString));
00200         }}
00201         for (final DoubleParameter param : this.config.getDoubles()) {
00202             rosParamNameString = "~" + param.getName();
00203             if (this.node.getParameterTree().search(rosParamNameString) != null) {
00204                 this.node.getLog().debug("found : " + rosParamNameString);
00205                 param.setValue(
00206                         this.node.getParameterTree()
00207                         .getDouble(rosParamNameString));
00208         }}
00209     }
00210 
00215     protected void setCallback(ReconfigureListener<T> callback) {
00216         if (callback != null) {
00217             this.callback = callback;
00218         }
00219     }
00220 
00222     public void close() {
00223         this.serviceReconfigure.shutdown();
00224         this.publisherUpdate.shutdown();
00225         this.publisherDescription.shutdown();
00226     }
00227 
00228     @Override
00229     public void build(ReconfigureRequest request, ReconfigureResponse response) throws ServiceException {
00230         this.node.getLog().info("call service");
00231         this.config = this.changeConfig(request.getConfig(), 0);
00232         response.setConfig(this.config);
00233 //        encode_config(this.updateConfiguration(decode_config(req.config, self.type.config_description)))
00234         try {
00235             Thread.sleep(1);
00236         } catch (InterruptedException e) {
00237             // TODO Auto-generated catch block
00238             e.printStackTrace();
00239         }
00240     }
00241 
00248     public interface ReconfigureListener<T> {
00255         T onReconfigure(T config, int level);
00256     }
00257 
00264     protected int calcLevel(Config config1, Config config2) {
00265         int level = 0;
00266 
00267 //        for (Object param : extract_params(this.description)) {
00268 //            if (config1[param["name"]] != config2[param["name"]]) {
00269 //                level |= param["level"];
00270 //            }
00271 //        }
00272 
00273         return level;
00274     }
00275 
00280     protected void clamp(Config config) {
00281 //        for (Object param : extract_params(this.description)) {
00282 //            int maxVal = this.type.max[param["name"]];
00283 //            int minVal = this.type.min[param["name"]];
00284 //            int val = config[param["name"]];
00285 //            if (val > maxVal) {
00286 //                config[param["name"]] = maxVal;
00287 //            } else if (val < minVal) {
00288 //                config[param["name"]] = minVal;
00289 //            }
00290 //        }
00291     }
00292 }


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