Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
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
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
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
00075 if (connectedNode == null) {
00076 throw new DynamicReconfigureException("Node not connected !!");
00077 }
00078
00079
00080 this.configInstance = config;
00081 this.node = connectedNode;
00082 this.config = this.configInstance.getCurrentConfig();
00083
00084 this.description = this.configInstance.makeConfigDescription();
00085
00086
00087 this.copyFromParameterServer();
00088 this.setCallback(callback);
00089
00090
00091 this.publisherDescription = this.node.newPublisher("~parameter_descriptions", ConfigDescription._TYPE);
00092 this.publisherDescription.setLatchMode(true);
00093 this.publisherDescription.publish(description);
00094
00095
00096 this.publisherUpdate = this.node.newPublisher("~parameter_updates", Config._TYPE);
00097 this.publisherUpdate.setLatchMode(true);
00098 this.changeConfig(this.config, 0);
00099
00100
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
00115 this.configInstance.merge(config);
00116
00117
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
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
00234 try {
00235 Thread.sleep(1);
00236 } catch (InterruptedException e) {
00237
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
00268
00269
00270
00271
00272
00273 return level;
00274 }
00275
00280 protected void clamp(Config config) {
00281
00282
00283
00284
00285
00286
00287
00288
00289
00290
00291 }
00292 }