Quantcast
Channel: ROS Answers: Open Source Q&A Forum - RSS feed
Viewing all articles
Browse latest Browse all 1441

Moveit classes inside a class

$
0
0
Hi I am trying to wrap MoveIt classes inside another class (which is a backend in a library). I am following the tutorial in http://docs.ros.org/kinetic/api/moveit_tutorials/html/doc/pr2_tutorials/kinematics/src/doc/kinematic_model_tutorial.html to figure out how to use all kinematics. I managed to make the example work with a custom robot. The problem is that when I wrap it inside of a class it seems that is not working. The same piece of code works if I put it in the main.cpp file, but not if I call it from a method of a class. When using the code in the main.cpp it shows the pose of the direct kinematic and it performs the inverse kinematic but when I use it in the class the pose if the direct kinematic is a 4x4 identity matrix and it says that it cannot find the IK (After of each fragment of code there is an example of output) By now, the class do not store any class member, it is just using the same piece of code. I cannot guess what could be happening. Could it be something scope dependent? This works: **main.cpp** #include #include #include #include #include #include int main(int _argc, char **_argv) { ros::init(_argc, _argv, "asas"); ros::AsyncSpinner spinner(1); spinner.start(); std::string mArmPrefix = ""; ros::NodeHandle n; ros::Publisher mJointPublisher = n.advertise(mArmPrefix + "/joint_states", 1000); robot_model_loader::RobotModelLoader robot_model_loader("/robot_description"); robot_model::RobotModelPtr kinematic_model = robot_model_loader.getModel(); ROS_INFO("Model frame: %s", kinematic_model->getModelFrame().c_str()); robot_state::RobotStatePtr kinematic_state(new robot_state::RobotState(kinematic_model)); kinematic_state->setToDefaultValues(); const robot_state::JointModelGroup *joint_model_group = kinematic_model->getJointModelGroup("arm4dof"); const std::vector&joint_names = joint_model_group->getVariableNames(); while (ros::ok()) { std::vector joint_values; kinematic_state->setToRandomPositions(joint_model_group); kinematic_state->copyJointGroupPositions(joint_model_group, joint_values); for (std::size_t i = 0; i < joint_names.size(); ++i) { ROS_INFO("Joint %s: %f", joint_names[i].c_str(), joint_values[i]); } ROS_INFO_STREAM("Current state is " << (kinematic_state->satisfiesBounds() ? "valid" : "not valid")); /* Enforce the joint limits for this state and check again*/ kinematic_state->enforceBounds(); ROS_INFO_STREAM("Current state is " << (kinematic_state->satisfiesBounds() ? "valid" : "not valid")); kinematic_state->copyJointGroupPositions(joint_model_group, joint_values); const Eigen::Affine3d &end_effector_state = kinematic_state->getGlobalLinkTransform("arm_2"); /* Print end-effector pose. Remember that this is in the model frame */ ROS_INFO_STREAM("Translation: " << end_effector_state.translation()); ROS_INFO_STREAM("Rotation: " << end_effector_state.rotation()); bool found_ik = kinematic_state->setFromIK(joint_model_group, end_effector_state, 10, 0.1); // Now, we can print out the IK solution (if found): if (found_ik) { std::cout << "Inverse kinematic" << std::endl; kinematic_state->copyJointGroupPositions(joint_model_group, joint_values); for (std::size_t i = 0; i < joint_names.size(); ++i) { ROS_INFO("Joint %s: %f", joint_names[i].c_str(), joint_values[i]); } } else { ROS_INFO("Did not find IK solution"); } std::this_thread::sleep_for(std::chrono::milliseconds(3000)); } ros::shutdown(); return 0; } Example output: [ INFO] [1519129629.774489067]: Loading robot model 'arm4DoF'... [ INFO] [1519129629.793275887]: Model frame: /base_link [ INFO] [1519129629.793342125]: Joint arm_0_bottom_joint: -1.844674 [ INFO] [1519129629.793351311]: Joint arm_1_joint: 1.262605 [ INFO] [1519129629.793359813]: Joint arm_2_joint: -2.574378 [ INFO] [1519129629.793372435]: Current state is valid [ INFO] [1519129629.793383459]: Current state is valid [ INFO] [1519129629.793437553]: Translation: 0.43813 -1.55953 -0.990671 [ INFO] [1519129629.793535933]: Rotation: -0.962729 -0.0692766 -0.261444 -0.270467 0.24659 0.930613 -9.61075e-12 0.966641 -0.256137 Inverse kinematic [ INFO] [1519129629.793729052]: Joint arm_0_bottom_joint: -1.844674 [ INFO] [1519129629.793739007]: Joint arm_1_joint: 1.262605 [ INFO] [1519129629.793747039]: Joint arm_2_joint: -2.574378 But this does not work: **Arm.h** #include #include #include #include #include #include #include #include class Arm { public: void run(); }; **Arm.cpp** #include #include #include #include #include #include #include void Arm::run() { std::string mArmPrefix = ""; ros::NodeHandle n; std::vector joint_values; ros::Publisher mJointPublisher = n.advertise(mArmPrefix + "/joint_states", 1000); robot_model_loader::RobotModelLoader robot_model_loader("/robot_description"); robot_model::RobotModelPtr kinematic_model = robot_model_loader.getModel(); ROS_INFO("Model frame: %s", kinematic_model->getModelFrame().c_str()); robot_state::RobotStatePtr kinematic_state(new robot_state::RobotState(kinematic_model)); kinematic_state->setToDefaultValues(); const robot_state::JointModelGroup *joint_model_group = kinematic_model->getJointModelGroup("arm4dof"); const std::vector&joint_names = joint_model_group->getVariableNames(); while (ros::ok()) { kinematic_state->setToRandomPositions(joint_model_group); kinematic_state->copyJointGroupPositions(joint_model_group, joint_values); for (std::size_t i = 0; i < joint_names.size(); ++i) { ROS_INFO("Joint %s: %f", joint_names[i].c_str(), joint_values[i]); } ROS_INFO_STREAM("Current state is " << (kinematic_state->satisfiesBounds() ? "valid" : "not valid")); /* Enforce the joint limits for this state and check again*/ kinematic_state->enforceBounds(); ROS_INFO_STREAM("Current state is " << (kinematic_state->satisfiesBounds() ? "valid" : "not valid")); kinematic_state->copyJointGroupPositions(joint_model_group, joint_values); const Eigen::Affine3d &end_effector_state = kinematic_state->getGlobalLinkTransform("arm_2"); /* Print end-effector pose. Remember that this is in the model frame */ ROS_INFO_STREAM("Translation: " << end_effector_state.translation()); ROS_INFO_STREAM("Rotation: " << end_effector_state.rotation()); sensor_msgs::JointState j_msg; bool found_ik = kinematic_state->setFromIK(joint_model_group, end_effector_state, 10, 0.1); // Now, we can print out the IK solution (if found): if (found_ik) { std::cout << "Inverse kinematic" << std::endl; kinematic_state->copyJointGroupPositions(joint_model_group, joint_values); for (std::size_t i = 0; i < joint_names.size(); ++i) { ROS_INFO("Joint %s: %f", joint_names[i].c_str(), joint_values[i]); } } else { ROS_INFO("Did not find IK solution"); } std::this_thread::sleep_for(std::chrono::milliseconds(3000)); std::cout << "-------------------------------------" << std::endl; } } **main.cpp** int main(int _argc, char **_argv){ ros::init(_argc, _argv, "asas"); ros::AsyncSpinner spinner(1); spinner.start(); Arm arm; arm.run(); } Example output: [ INFO] [1519132193.015645909]: Loading robot model 'arm4DoF'... [ INFO] [1519132193.034682569]: Model frame: /base_link [ INFO] [1519132193.034757365]: Joint arm_0_bottom_joint: -2.803085 [ INFO] [1519132193.034770917]: Joint arm_1_joint: -0.145080 [ INFO] [1519132193.034783361]: Joint arm_2_joint: -2.677995 [ INFO] [1519132193.034806116]: Current state is valid [ INFO] [1519132193.034823085]: Current state is valid [ INFO] [1519132193.034909581]: Translation: 0 0 0 [ INFO] [1519132193.035004678]: Rotation: 1 0 0 0 1 0 0 0 1 [ INFO] [1519132194.045864989]: Did not find IK solution

Viewing all articles
Browse latest Browse all 1441

Trending Articles