moveit_sim_hw_interface.cpp
Go to the documentation of this file.
00001 /*********************************************************************
00002  * Software License Agreement (BSD License)
00003  *
00004  *  Copyright (c) 2015, University of Colorado, Boulder
00005  *  All rights reserved.
00006  *
00007  *  Redistribution and use in source and binary forms, with or without
00008  *  modification, are permitted provided that the following conditions
00009  *  are met:
00010  *
00011  *   * Redistributions of source code must retain the above copyright
00012  *     notice, this list of conditions and the following disclaimer.
00013  *   * Redistributions in binary form must reproduce the above
00014  *     copyright notice, this list of conditions and the following
00015  *     disclaimer in the documentation and/or other materials provided
00016  *     with the distribution.
00017  *   * Neither the name of the Univ of CO, Boulder nor the names of its
00018  *     contributors may be used to endorse or promote products derived
00019  *     from this software without specific prior written permission.
00020  *
00021  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00022  *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00023  *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00024  *  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00025  *  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00026  *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00027  *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00028  *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00029  *  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00030  *  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00031  *  ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00032  *  POSSIBILITY OF SUCH DAMAGE.
00033  *********************************************************************/
00034 
00035 /* Author: Dave Coleman <dave@dav.ee>
00036    Desc:   Simulates a robot using ros_control controllers with a default position loaded from MoveIt!
00037 */
00038 
00039 #include <moveit_sim_controller/moveit_sim_hw_interface.h>
00040 
00041 // ROS parameter loading
00042 #include <rosparam_shortcuts/rosparam_shortcuts.h>
00043 
00044 namespace moveit_sim_controller
00045 {
00046 MoveItSimHWInterface::MoveItSimHWInterface(ros::NodeHandle& nh, urdf::Model* urdf_model)
00047   : ros_control_boilerplate::SimHWInterface(nh, urdf_model), name_("moveit_sim_hw_interface")
00048 {
00049   // Load rosparams
00050   ros::NodeHandle rpnh(nh_, name_);
00051   std::size_t error = 0;
00052   error += !rosparam_shortcuts::get(name_, rpnh, "joint_model_group", joint_model_group_);
00053   error += !rosparam_shortcuts::get(name_, rpnh, "joint_model_group_pose", joint_model_group_pose_);
00054   rosparam_shortcuts::shutdownIfError(name_, error);
00055 }
00056 
00057 void MoveItSimHWInterface::init()
00058 {
00059   // Call parent class version of this function
00060   SimHWInterface::init();
00061 
00062   // Load the loader
00063   robot_model_loader_.reset(new robot_model_loader::RobotModelLoader(ROBOT_DESCRIPTION));
00064 
00065   // Load default joint values
00066   loadDefaultJointValues();
00067 
00068   ROS_INFO_STREAM_NAMED(name_, "MoveItSimHWInterface Ready.");
00069 }
00070 
00071 void MoveItSimHWInterface::loadDefaultJointValues()
00072 {
00073   // Load the robot model
00074   robot_model::RobotModelPtr robot_model = robot_model_loader_->getModel();  // Get a shared pointer to the robot
00075 
00076   // Check for existance of joint model group
00077   if (!robot_model->hasJointModelGroup(joint_model_group_))
00078   {
00079     ROS_WARN_STREAM_NAMED(name_, "Unable to find joint model group '" << joint_model_group_
00080                           << "' for the fake controller manager");
00081     return;
00082   }
00083 
00084   moveit::core::JointModelGroup* jmg = robot_model->getJointModelGroup(joint_model_group_);
00085 
00086   // Load a robot state
00087   moveit::core::RobotState robot_state(robot_model);
00088 
00089   // First set whole robot default values to ensure there are no 'nan's
00090   robot_state.setToDefaultValues();
00091 
00092   // Attempt to set pose
00093   if (!robot_state.setToDefaultValues(jmg, joint_model_group_pose_))
00094   {
00095     ROS_WARN_STREAM_NAMED(name_, "Unable to find pose " << joint_model_group_pose_ << " for the fake controller "
00096                                                                                       "manager");
00097     return;
00098   }
00099   ROS_INFO_STREAM_NAMED(name_, "Set joints to pose " << joint_model_group_pose_);
00100 
00101   for (std::size_t i = 0; i < joint_names_.size(); ++i)
00102   {
00103     const moveit::core::JointModel* jm = robot_state.getJointModel(joint_names_[i]);
00104 
00105     // Error check
00106     if (!jm)
00107     {
00108       ROS_WARN_STREAM_NAMED(name_, "Unable to find joint model group: " << joint_names_[i]);
00109       continue;
00110     }
00111     if (jm->getVariableCount() != 1)
00112     {
00113       ROS_WARN_STREAM_NAMED(name_, "Fake joint controller does not currently accept more than 1 "
00114                                    "variable per joint");
00115       continue;
00116     }
00117 
00118     // Set position from SRDF
00119     joint_position_[i] = robot_state.getJointPositions(jm)[0];
00120     joint_position_command_[i] = joint_position_[i];
00121 
00122     if (std::isnan(joint_position_[i]))
00123     {
00124       ROS_ERROR_STREAM_NAMED(name_, "NaN found");
00125       std::cout << std::endl;
00126       std::cout << "i: " << i << " name: " << jm->getName() << std::endl;
00127       std::cout << "joint_model_group: " << jmg->getName() << std::endl;
00128       std::cout << "getVariableCount(): " << jm->getVariableCount() << std::endl;
00129       std::cout << "joint_position_[i]: " << joint_position_[i] << std::endl;
00130       std::cout << "joint_position_command_[i]: " << joint_position_command_[i] << std::endl;
00131       robot_state.printStateInfo();
00132       exit(-1);
00133     }
00134   }
00135 }
00136 
00137 }  // namespace moveit_sim_controller


moveit_sim_controller
Author(s): Dave Coleman
autogenerated on Fri Apr 22 2016 04:05:01