postures.cpp
Go to the documentation of this file.
00001 /*
00002  * Copyright 2016 SoftBank Robotics Europe
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 // MoveIt!
00018 #include <moveit/move_group_interface/move_group.h>
00019 
00020 #include "romeo_moveit_actions/postures.hpp"
00021 
00022 namespace moveit_simple_actions
00023 {
00024 Posture::Posture(const std::string robot_name,
00025                  const std::string eef_name,
00026                  const std::string group_name)
00027 {
00028   //set joint for the head
00029   moveit::planning_interface::MoveGroup move_group_head("head");
00030   //get current
00031   move_group_head.getCurrentState()->copyJointGroupPositions(move_group_head.getCurrentState()->getRobotModel()->getJointModelGroup(move_group_head.getName()), pose_head_down_);
00032   if (pose_head_down_.size() > 1)
00033     if (robot_name == "nao")
00034       pose_head_down_[1] = 0.3;
00035     else
00036       pose_head_down_[1] = 0.35;
00037   pose_head_zero_.resize(pose_head_down_.size(), 0.0);
00038 
00039   //get the arm joints
00040   moveit::planning_interface::MoveGroup move_group_plan(group_name);
00041   std::vector<double> pose_arm_init;
00042   move_group_plan.getCurrentState()->copyJointGroupPositions(
00043               move_group_plan.getCurrentState()->getRobotModel()->getJointModelGroup(
00044                   move_group_plan.getName()), pose_arm_init);
00045 
00046   //initialize the left arm
00047   pose_arm_.resize(5);
00048   pose_arm_[0] = std::vector<double>(pose_arm_init);
00049   for (int i=1; i<pose_arm_.size(); ++i)
00050     pose_arm_[i].resize(pose_arm_[0].size(), 0.0);
00051   if (pose_arm_[0].size() >= 5)
00052   {
00053     //define the zero pose
00054     pose_arm_[0].assign(pose_arm_[0].size(), 0.0);
00055 
00056     //define the initial pose (arms down)
00057     pose_arm_[1][0] = 1.56;
00058     pose_arm_[1][1] = 0.14;
00059     pose_arm_[1][2] = -1.22;
00060     pose_arm_[1][3] = -0.52;
00061     pose_arm_[1][4] = -0.02;
00062     if (pose_arm_[1].size() >= 6)
00063     {
00064       if (robot_name == "romeo")
00065         pose_arm_[1][5] = 0.17;
00066       else
00067         pose_arm_[1][5] = 0.02;
00068     }
00069 
00070     //define the initial grasp pose
00071     pose_arm_[2][0] = 1.74;
00072     pose_arm_[2][1] = 0.75;
00073     pose_arm_[2][2] = -2.08;
00074     pose_arm_[2][3] = -1.15;
00075     pose_arm_[2][4] = -0.43;
00076     if (pose_arm_[2].size() >= 6)
00077     {
00078       if (robot_name == "romeo")
00079         pose_arm_[2][5] = 0.17;
00080       else
00081         pose_arm_[1][5] = 0.02;
00082     }
00083 
00084     //define another initial grasp pose
00085     pose_arm_[3][0] = 1.08;
00086     pose_arm_[3][1] = 0.66;
00087     pose_arm_[3][2] = -0.84;
00088     pose_arm_[3][3] = -0.61;
00089     pose_arm_[3][4] = -0.67;
00090     if (pose_arm_[3].size() >= 6)
00091     {
00092       if (robot_name == "romeo")
00093         pose_arm_[3][5] = 0.025;
00094       else
00095         pose_arm_[1][5] = 0.02;
00096     }
00097 
00098     //define another initial grasp pose
00099     pose_arm_[4][0] = 1.87;
00100     pose_arm_[4][1] = 0.56;
00101     pose_arm_[4][2] = -1.99;
00102     pose_arm_[4][3] = -1.05;
00103     pose_arm_[4][4] = -0.67;
00104     if (pose_arm_[4].size() >= 6)
00105       pose_arm_[4][5] = 0.02;
00106   }
00107 
00108   //initialize the right arm
00109   if (group_name.find("right") != std::string::npos)
00110     for (int i=0; i<pose_arm_.size(); ++i)
00111     {
00112       if (pose_arm_[i].size() >= 3)
00113       {
00114         pose_arm_[i][1] *= -1;
00115         pose_arm_[i][2] *= -1;
00116       }
00117       if (pose_arm_[i].size() >= 5)
00118       {
00119         pose_arm_[i][3] *= -1;
00120         if (robot_name != "pepper") //not for Pepper
00121           pose_arm_[i][4] *= -1;
00122       }
00123     }
00124 
00125   //get the joint of the eef
00126   std::vector<double> pose_hand;
00127   moveit::planning_interface::MoveGroup move_group_eef(eef_name);
00128   move_group_eef.getCurrentState()->copyJointGroupPositions(move_group_eef.getCurrentState()->getRobotModel()->getJointModelGroup(move_group_eef.getName()), pose_hand);
00129   pose_hand_.resize(2);
00130   pose_hand_[0].resize(pose_hand.size(), 0.0);
00131   pose_hand_[1].resize(pose_hand.size(), 0.8);
00132 }
00133 
00134 void Posture::initHandPose(const double &value, const int &pose)
00135 {
00136   if (pose <= pose_hand_.size())
00137   {
00138     pose_hand_[pose].assign(pose_hand_[pose].size(), value);
00139     if (pose_hand_[pose].size() > 0)
00140       pose_hand_[pose][0] = 0.0;
00141   }
00142 }
00143 
00144 bool Posture::poseHeadZero()
00145 {
00146   goToPose("head", &pose_head_zero_);
00147 }
00148 
00149 bool Posture::poseHeadDown()
00150 {
00151   goToPose("head", &pose_head_down_);
00152 }
00153 
00154 bool Posture::poseHand(const std::string &end_eff,
00155                        const std::string &group,
00156                        const int &pose_id)
00157 {
00158   //set the end-effector first
00159   goToPose(end_eff, &pose_hand_[0]);
00160   bool res = false;
00161   if (pose_id < pose_arm_.size())
00162     res = goToPose(group, &pose_arm_[pose_id]);
00163   return res;
00164 }
00165 
00166 bool Posture::poseHandOpen(const std::string &end_eff)
00167 {
00168   goToPose(end_eff, &pose_hand_[0]);
00169 }
00170 
00171 bool Posture::poseHandClose(const std::string &end_eff)
00172 {
00173   goToPose(end_eff, &pose_hand_[1]);
00174 }
00175 
00176 bool Posture::goToPose(const std::string group_name,
00177                        std::vector<double> *pose)
00178 {
00179   moveit::planning_interface::MoveGroup move_group(group_name);
00180 
00181   //get the current set of joint values for the group
00182   std::vector<double> joints;
00183   move_group.getCurrentState()->copyJointGroupPositions(
00184         move_group.getCurrentState()->getRobotModel()->
00185         getJointModelGroup(move_group.getName()), joints);
00186 
00187   //plan to the new joint space goal and visualize the plan
00188   if (joints.size() == pose->size())
00189   {
00190     //move_group.setApproximateJointValueTarget(*pose);
00191     move_group.setJointValueTarget(*pose);
00192 
00193     moveit::planning_interface::MoveGroup::Plan plan;
00194     bool success = move_group.plan(plan);
00195     sleep(1.0);
00196     if (success)
00197     {
00198       ROS_INFO("Action with the move_group %s", success?"":"FAILED");
00199       move_group.move();
00200       sleep(1.0);
00201 
00202       /*std::vector<double> joints_temp = move_group.getCurrentJointValues();
00203       std::cout << "Joint values of " << group_name << " ";
00204       for (int i=0; i< joints_temp.size(); ++i)
00205         std::cout << joints_temp[i] << " ";
00206       std::cout << std::endl;*/
00207     }
00208     return success;
00209   }
00210   else
00211     ROS_INFO_STREAM("Posture: Joint size is wrong for " << group_name
00212                     << ": " << joints.size() << " != " << pose->size());
00213   return false;
00214 }
00215 }


romeo_moveit_actions
Author(s):
autogenerated on Thu Jun 6 2019 21:57:24