pr2_joint_space.cpp
Go to the documentation of this file.
00001 #include <reactive_grasping_pr2/pr2_joint_space.h>
00002 
00003 namespace pr2_joint_space {
00004         
00005         RightArmModel::RightArmModel(ros::NodeHandle &n, std::string frame) : n_(n) {
00006 
00007                 // set constants
00008                 h = 1.f;
00009                 c_u   = 4.f;
00010                 c_xT = 0.3f;
00011                 gain = 1.0f;
00012                 num_joints = 7;
00013                 
00014                 // setup services
00015                 ros::service::waitForService("/pr2_right_arm_kinematics/get_ik_solver_info");
00016                 ros::service::waitForService("/pr2_right_arm_kinematics/get_fk");
00017                 ros::service::waitForService("/pr2_right_arm_kinematics/get_ik");
00018 
00019                 fk_info_client_ = 
00020                         n_.serviceClient<kinematics_msgs::GetKinematicSolverInfo>
00021                         ("/pr2_right_arm_kinematics/get_ik_solver_info");
00022                 fk_client_ = n_.serviceClient
00023                         <kinematics_msgs::GetPositionFK>("/pr2_right_arm_kinematics/get_fk");
00024                 ik_client_ = n_.serviceClient
00025                         <kinematics_msgs::GetPositionIK>("/pr2_right_arm_kinematics/get_ik");
00026 
00027                 // and the joint control scheme
00028                 controlled_joints_.resize(num_joints);
00029                 controlled_joints_[0] = "r_shoulder_pan_joint";
00030                 controlled_joints_[1] = "r_shoulder_lift_joint";
00031                 controlled_joints_[2] = "r_upper_arm_roll_joint";
00032                 controlled_joints_[3] = "r_elbow_flex_joint";
00033                 controlled_joints_[4] = "r_forearm_roll_joint";
00034                 controlled_joints_[5] = "r_wrist_flex_joint";
00035                 controlled_joints_[6] = "r_wrist_roll_joint";
00036 
00037                 root_frame = frame;
00038                 external_goal_.setZero(3);
00039                 res_.setZero(3);
00040 
00041                 if(fk_info_client_.call(request, response)) {
00042                         for(unsigned int i=0; 
00043                             i< response.kinematic_solver_info.joint_names.size(); i++) {
00044                                        
00045                                 ROS_INFO("Joint: %d %s", i,
00046                                           response.kinematic_solver_info.joint_names[i].c_str());
00047                         }
00048                 }
00049                 else {
00050                         
00051                         ROS_ERROR("Could not call fk_info service");
00052                 }
00053 
00054                 // get a joint state mapping 
00055                 joint_mapping_.resize(num_joints);
00056                 for(unsigned int i=0; i < controlled_joints_.size(); ++i) {
00057                         for(unsigned int j=0; 
00058                             j< response.kinematic_solver_info.joint_names.size(); j++) {
00059                                 if (response.kinematic_solver_info.joint_names[j].compare(controlled_joints_[i]) == 0) {
00060                                         joint_mapping_[i] = j;
00061                                 }
00062                         }
00063                 }
00064         }
00065 
00066 
00067         void RightArmModel::fk(const VectorT &x, VectorT &res) {
00068                 int offset = 0;
00069                 // define the service messages
00070                 fk_request.header.frame_id = root_frame;
00071                 //fk_request.header.stamp = ros::Time::now();
00072                 fk_request.fk_link_names.resize(1);
00073                 fk_request.fk_link_names[0] = "r_wrist_roll_link";
00074 
00075                 fk_request.robot_state.joint_state.position.resize
00076                         (response.kinematic_solver_info.joint_names.size());
00077                 fk_request.robot_state.joint_state.name =  controlled_joints_;
00078                 for(unsigned int i=0; i < joint_mapping_.size(); ++i) {
00079                         fk_request.robot_state.joint_state.position[i] = x(i+offset);
00080                 }
00081                 
00082                 if(fk_client_.call(fk_request, fk_response)) {
00083                         if(fk_response.error_code.val == fk_response.error_code.SUCCESS) {
00084                                 res(0) = fk_response.pose_stamped[0].pose.position.x - external_goal_(0);
00085                                 res(1) = fk_response.pose_stamped[0].pose.position.y - external_goal_(1);
00086                                 res(2) = fk_response.pose_stamped[0].pose.position.z - external_goal_(2);
00087                         }
00088                         else {
00089                                 ROS_ERROR("Forward kinematics failed");
00090                         }
00091                 }
00092                 else {
00093                         ROS_ERROR("Forward kinematics service call failed");
00094                 }
00095         }
00096 
00097         void RightArmModel::ik(const VectorT &pos, VectorT &x) {
00098                 ik_request.timeout = ros::Duration(2.0);
00099                 ik_request.ik_request.ik_link_name = "r_wrist_roll_link";
00100 
00101                 ik_request.ik_request.pose_stamped.header.frame_id = root_frame;
00102                 // TODO fix usage of pos
00103                 ik_request.ik_request.pose_stamped.pose.position.x = external_goal_(0);
00104                 ik_request.ik_request.pose_stamped.pose.position.y = external_goal_(1);
00105                 ik_request.ik_request.pose_stamped.pose.position.z = external_goal_(2);
00106 
00107                 ik_request.ik_request.pose_stamped.pose.orientation.x = 0.0;
00108                 ik_request.ik_request.pose_stamped.pose.orientation.y = 0.0;
00109                 ik_request.ik_request.pose_stamped.pose.orientation.z = -0.7;
00110                 ik_request.ik_request.pose_stamped.pose.orientation.w = -0.7;
00111                 ik_request.ik_request.ik_seed_state.joint_state.position.resize(response.kinematic_solver_info.joint_names.size());
00112                 ik_request.ik_request.ik_seed_state.joint_state.name = controlled_joints_;
00113                 std::cout << response.kinematic_solver_info.joint_names.size() << " " << response.kinematic_solver_info.limits.size() << std::endl;
00114                 for(unsigned int i=0; i< response.kinematic_solver_info.joint_names.size(); i++) {
00115                         ik_request.ik_request.ik_seed_state.joint_state.position[i] = (response.kinematic_solver_info.limits[i].min_position + response.kinematic_solver_info.limits[i].max_position)/2.0;
00116                         //x(i);
00117                 }
00118                 if(ik_client_.call(ik_request, ik_response)) {
00119                         if(ik_response.error_code.val == ik_response.error_code.SUCCESS)
00120                                 for(unsigned int i=0; i < ik_response.solution.joint_state.name.size(); i ++)
00121                                         x(i) = ik_response.solution.joint_state.position[i];
00122                         else
00123                                 ROS_ERROR("Inverse kinematics failed");
00124                 }
00125                 else {
00126                         ROS_ERROR("Inverse kinematics service call failed");
00127                 }
00128         }
00129         
00130         void RightArmModel::dynamics(const VectorT &x, const VectorT &u, VectorT &xNext) {
00131                 //int n = x.size();
00132                 //std::cout << xNext.size() << " " << x.size() << " nj " << num_joints << std::endl;
00133                 xNext.setZero(x.size());
00134                 xNext.tail(num_joints) = x.tail(num_joints) + u;
00135                 xNext.block(0, 0, num_joints, 1) = x.block(0, 0, num_joints, 1) + gain * xNext.tail(num_joints);
00136         }
00137         
00138         
00139         void RightArmModel::dynamicsD(const VectorT &x, const VectorT &u, Deriv &d) {
00140                 int n = x.size();
00141                 int m = u.size();
00142 
00143                 d.fx.setIdentity(n, n);
00144                 for(int i = 0; i < m; ++i) {
00145                         d.fx(i, i) = 1.;
00146                         d.fx(i, i + m) = 1.;
00147                 }
00148 
00149                 d.fu.setZero(n, m);
00150                 for(int i = 0; i < m; ++i) {
00151                         d.fu(i, i) = 1.;
00152                         d.fu(i+m, i) = 1.;
00153                 }
00154         
00155                 // TODO
00156                 d.fxx.resize(n);
00157                 d.fxu.resize(n);
00158                 d.fuu.resize(n);
00159                 for (int i = 0; i < n; ++i) {
00160                         d.fxx[i].setZero(n,n);
00161                         d.fxu[i].setZero(n,m);
00162                         d.fuu[i].setZero(m,m);
00163                 }
00164                 
00165         }
00166         
00167 
00168 
00169         float RightArmModel::cost(const VectorT &x, const VectorT &u) {
00170                 // cost on distance to goal state
00171                 float goalCost = c_xT * (x - goal_).squaredNorm(); 
00172                 // cost that constrains the velocity
00173                 float totalCost = goalCost + std::pow(c_u, h) * x.tail(7).squaredNorm();
00174                 return totalCost;
00175         }
00176 
00177         void RightArmModel::setInternalGoal(const VectorT &g) {
00178                 goal_ = g;
00179         }
00180 
00181         void RightArmModel::setExternalGoal(const VectorT &g) {
00182                 external_goal_ = g;
00183         }
00184 
00185 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Properties Friends Defines


reactive_grasping_pr2
Author(s): Jost Tobias Springenberg, Jan Wuelfing
autogenerated on Wed Dec 26 2012 16:26:42