Issues creating srv and msg files in ROS service from python client and C++ server

asked 2019-07-29 00:01:25 -0500

Tawfiq Chowdhury gravatar image

I need to send a PoseStamped from python client to C++ server to perfrom a pick. Here is my python code:

#!/usr/bin/env python

import sys
import copy
import rospy
import roscpp
import moveit_commander
import moveit_msgs.msg
import geometry_msgs.msg
from geometry_msgs.msg import PoseStamped
import tf
import roslib
import traceback

def Pose_Send_Client(pose_source):
    rospy.wait_for_service('Pose_Send')
    try:
      Pose_Send = rospy.ServiceProxy('Pose_Send', PoseSend)
      grasping = Pose_Send(pose_source)
    except rospy.ServiceException, e:
     print "Service call failed: %s"%e


def move_group_python_interface_tutorial():

  pose_source = geometry_msgs.msg.Pose()
  pose_source.orientation.x = 0.5
  pose_source.orientation.y = 0.5
  pose_source.orientation.z = 0.5
  pose_source.orientation.w = 0.5
  pose_source.position.x = 0.68
  pose_source.position.y = -0.01
  pose_source.position.z = 1.1
  Pose_Send_Client(pose_source)

if __name__=='__main__':
  try:
    move_group_python_interface_tutorial()
  except rospy.ROSInterruptException:
    pass

Here is the C++ code:

#include <ros/ros.h>
#include "pr2_package/PoseSend.h"

// MoveIt!
#include <moveit/planning_scene_monitor/planning_scene_monitor.h>
#include <moveit/move_group_interface/move_group.h>
#include <geometric_shapes/solid_primitive_dims.h>

static const std::string ROBOT_DESCRIPTION="robot_description";

void openGripper(trajectory_msgs::JointTrajectory& posture){
  posture.joint_names.resize(6);
  posture.joint_names[0] = "r_gripper_joint";
  posture.joint_names[1] = "r_gripper_motor_screw_joint";
  posture.joint_names[2] = "r_gripper_l_finger_joint";
  posture.joint_names[3] = "r_gripper_r_finger_joint";
  posture.joint_names[4] = "r_gripper_r_finger_tip_joint";
  posture.joint_names[5] = "r_gripper_l_finger_tip_joint";

  posture.points.resize(1);
  posture.points[0].positions.resize(6);
  posture.points[0].positions[0] = 1;
  posture.points[0].positions[1] = 1.0;
  posture.points[0].positions[2] = 0.477;
  posture.points[0].positions[3] = 0.477;
  posture.points[0].positions[4] = 0.477;
  posture.points[0].positions[5] = 0.477;
}

void closedGripper(trajectory_msgs::JointTrajectory& posture){
  posture.joint_names.resize(6);
  posture.joint_names[0] = "r_gripper_joint";
  posture.joint_names[1] = "r_gripper_motor_screw_joint";
  posture.joint_names[2] = "r_gripper_l_finger_joint";
  posture.joint_names[3] = "r_gripper_r_finger_joint";
  posture.joint_names[4] = "r_gripper_r_finger_tip_joint";
  posture.joint_names[5] = "r_gripper_l_finger_tip_joint";

  posture.points.resize(1);
  posture.points[0].positions.resize(6);
  posture.points[0].positions[0] = 0;
  posture.points[0].positions[1] = 0;
  posture.points[0].positions[2] = 0.002;
  posture.points[0].positions[3] = 0.002;
  posture.points[0].positions[4] = 0.002;
  posture.points[0].positions[5] = 0.002;
}


void pick(moveit::planning_interface::MoveGroup &group, geometry_msgs::PoseStamped p)
{

  std::vector<moveit_msgs::Grasp> grasps;
  moveit_msgs::Grasp g;
  g.pre_grasp_approach.direction.vector.x = 1.0;
  g.pre_grasp_approach.direction.header.frame_id = "r_wrist_roll_link";
  g.pre_grasp_approach.min_distance = 0.2;
  g.pre_grasp_approach.desired_distance = 0.4;

  g.post_grasp_retreat.direction.header.frame_id = "base_footprint";
  g.post_grasp_retreat.direction.vector.z = 1.0;
  g.post_grasp_retreat.min_distance = 0.1;
  g.post_grasp_retreat.desired_distance = 0.25;
  g.grasp_pose = p;

  openGripper(g.pre_grasp_posture);

  closedGripper(g.grasp_posture);

  grasps.push_back(g);
  group.pick("part", grasps);
}


void serv(pr2_package::PoseSend::Request &req){

  geometry_msgs::PoseStamped p
  p=(geometry_msgs::PoseStamped)req.a;
  pick(p);

}

int main(int argc, char **argv)
{
  ros::init (argc, argv, "right_arm_pick_place");
  ros::AsyncSpinner spinner(1);
  spinner.start();

  ros::NodeHandle nh;
  // ros::Publisher pub_co = nh.advertise<moveit_msgs::CollisionObject>("collision_object", 10);
  // ros::Publisher pub_aco = nh.advertise<moveit_msgs::AttachedCollisionObject>("attached_collision_object", 10);

  ros::WallDuration(1.0).sleep();

  moveit::planning_interface::MoveGroup group("right_arm");
  group.setPlanningTime(45.0);

  ros::WallDuration(1.0).sleep();

  ros::ServiceServer service = nh.advertiseService("Pose_Send", serv);

  //pick(group, p);

  ros::WallDuration(1.0).sleep();

  //place(group);

  ros::waitForShutdown();
  return 0 ...
(more)
edit retag flag offensive close merge delete

Comments

do you have those lines included into your package.xml?

 <build_depend>message_generation</build_depend>
  <run_depend>message_runtime</run_depend>
artemiialessandrini gravatar image artemiialessandrini  ( 2019-12-16 05:31:09 -0500 )edit

Hi, do you manage to open/close the gripper with this code on the PR2 using moveit ?

Madcreator gravatar image Madcreator  ( 2020-02-07 02:57:34 -0500 )edit

@Madcreator ,yes, I did.

Tawfiq Chowdhury gravatar image Tawfiq Chowdhury  ( 2020-02-14 12:45:00 -0500 )edit

Then could you please answer your question and mark it as answered?

artemiialessandrini gravatar image artemiialessandrini  ( 2020-04-06 06:31:40 -0500 )edit