control_plugin.cpp
Go to the documentation of this file.
00001 /*
00002  * Copyright 2017 Fraunhofer Institute for Manufacturing Engineering and Automation (IPA)
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License");
00005  * you may not use this file except in compliance with the License.
00006  * You may obtain a copy of 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,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  */
00016 
00017 
00018 #include "GeomController.h"
00019 #include "WheelControllerBase.h"
00020 
00021 #include <dynamic_reconfigure/server.h>
00022 #include <cob_omni_drive_controller/SteerCtrlConfig.h>
00023 
00024 namespace cob_omni_drive_controller
00025 {
00026 
00027 class WheelController : public WheelControllerBase< GeomController<hardware_interface::VelocityJointInterface, UndercarriageCtrl> >
00028 {
00029 public:
00030     virtual bool init(hardware_interface::VelocityJointInterface* hw, ros::NodeHandle &root_nh, ros::NodeHandle& controller_nh){
00031 
00032         wheel_params_t wheel_params;
00033         if(!parseWheelParams(wheel_params, controller_nh) || !GeomController::init(hw, wheel_params)) return false;
00034 
00035         pos_ctrl_.init(wheel_params, controller_nh);
00036 
00037         return setup(root_nh, controller_nh);
00038     }
00039     virtual void update(const ros::Time& time, const ros::Duration& period){
00040 
00041         updateState();
00042         pos_ctrl_.try_configure(*geom_);
00043 
00044         updateCtrl(time, period);
00045 
00046         for (unsigned i=0; i<wheel_commands_.size(); i++){
00047             steer_joints_[i].setCommand(wheel_commands_[i].dVelGearSteerRadS);
00048             drive_joints_[i].setCommand(wheel_commands_[i].dVelGearDriveRadS);
00049         }
00050 
00051     }
00052     class PosCtrl {
00053     public:
00054         PosCtrl() : updated(false) {}
00055         void try_configure(UndercarriageCtrl &ctrl){
00056             boost::recursive_mutex::scoped_try_lock lock(mutex);
00057             if(lock && updated){
00058                 ctrl.configure(pos_ctrl_params);
00059                 updated = false;
00060             }
00061         }
00062         void init(const wheel_params_t &params, const ros::NodeHandle &nh){
00063             boost::recursive_mutex::scoped_lock lock(mutex);
00064             pos_ctrl_params.resize(params.size());
00065             reconfigure_server_axes_.clear();
00066 
00067             reconfigure_server_.reset( new dynamic_reconfigure::Server<SteerCtrlConfig>(mutex,ros::NodeHandle(nh, "default/steer_ctrl")));
00068             reconfigure_server_->setCallback(boost::bind(&PosCtrl::setForAll, this, _1, _2)); // this writes the default into pos_ctrl_params
00069             {
00070                 SteerCtrlConfig config;
00071                 copy(config, params.front().pos_ctrl);
00072                 reconfigure_server_->setConfigDefault(config);
00073             }
00074 
00075             for(size_t i=0; i< pos_ctrl_params.size(); ++i) {
00076                 boost::shared_ptr<dynamic_reconfigure::Server<SteerCtrlConfig> > dr(new dynamic_reconfigure::Server<SteerCtrlConfig>(mutex, ros::NodeHandle(nh, params[i].geom.steer_name)));
00077                 SteerCtrlConfig config;
00078                 copy(config, params[i].pos_ctrl);
00079                 dr->setConfigDefault(config);
00080                 dr->updateConfig(config);
00081                 dr->setCallback(boost::bind(&PosCtrl::setForOne, this, i, _1, _2));  // this writes the joint-specific config into pos_ctrl_params
00082                 reconfigure_server_axes_.push_back(dr);
00083             }
00084         }
00085     private:
00086         static void copy(PosCtrlParams &params, const SteerCtrlConfig &config){
00087             params.dSpring = config.spring;
00088             params.dDamp = config.damp;
00089             params.dVirtM = config.virt_mass;
00090             params.dDPhiMax = config.d_phi_max;
00091             params.dDDPhiMax = config.dd_phi_max;
00092         }
00093         static void copy(SteerCtrlConfig &config, const PosCtrlParams &params){
00094             config.spring = params.dSpring;
00095             config.damp = params.dDamp;
00096             config.virt_mass = params.dVirtM;
00097             config.d_phi_max = params.dDPhiMax;
00098             config.dd_phi_max = params.dDDPhiMax;
00099         }
00100         // these function don't get locked
00101         void setForAll(SteerCtrlConfig &config, uint32_t /*level*/) {
00102             ROS_INFO("configure all steers: s: %lf, d: %lf, m: %lf, v: %lf, a: %lf", config.spring, config.damp, config.virt_mass, config.d_phi_max, config.dd_phi_max);
00103             for(size_t i=0; i< pos_ctrl_params.size(); ++i) {
00104                 copy(pos_ctrl_params[i], config);
00105                 if(!reconfigure_server_axes_.empty()){
00106                     reconfigure_server_axes_[i]->setConfigDefault(config);
00107                     reconfigure_server_axes_[i]->updateConfig(config);
00108                 }
00109             }
00110             updated = true;
00111         }
00112         void setForOne(size_t i, SteerCtrlConfig &config, uint32_t /*level*/) {
00113             ROS_INFO("configure steer %d: s: %lf, d: %lf, m: %lf, v: %lf, a: %lf", (int)i, config.spring, config.damp, config.virt_mass, config.d_phi_max, config.dd_phi_max);
00114             copy(pos_ctrl_params[i], config);
00115             updated = true;
00116         }
00117         std::vector<PosCtrlParams> pos_ctrl_params;
00118         boost::recursive_mutex mutex; // dynamic_reconfigure::Server calls the callback from the setCallback function
00119         bool updated;
00120         boost::scoped_ptr< dynamic_reconfigure::Server<SteerCtrlConfig> > reconfigure_server_;
00121         std::vector<boost::shared_ptr< dynamic_reconfigure::Server<SteerCtrlConfig> > > reconfigure_server_axes_; // TODO: use unique_ptr
00122     } pos_ctrl_;
00123 
00124 };
00125 
00126 }
00127 
00128 PLUGINLIB_EXPORT_CLASS( cob_omni_drive_controller::WheelController, controller_interface::ControllerBase)


cob_omni_drive_controller
Author(s): Christian Connette, Mathias Lüdtke
autogenerated on Thu Jun 6 2019 21:19:19