Skip to content

Commit ac49f0c

Browse files
author
Kai Thouard
committed
Go To Pose Logic Complete (includes debugging print statements)
1 parent a614187 commit ac49f0c

File tree

5 files changed

+87
-45
lines changed

5 files changed

+87
-45
lines changed

CMakeLists.txt

+3
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ find_package(catkin REQUIRED COMPONENTS
2424
pluginlib
2525
tf2
2626
sensor_msgs
27+
moveit_commander
2728
)
2829

2930
find_package(gazebo REQUIRED)
@@ -56,6 +57,8 @@ catkin_package(
5657
gazebo_ros
5758
moveit_core
5859
moveit_visual_tools
60+
moveit_ros_planning
61+
moveit_commander
5962
moveit_ros_planning_interface
6063
tf2_geometry_msgs
6164
visualization_msgs

include/panda_moveit/pick_and_place.hpp

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include <tf2_eigen/tf2_eigen.h>
1313
#include <sensor_msgs/JointState.h>
1414
#include <geometry_msgs/Pose.h>
15+
#include <boost/algorithm/string/join.hpp>
1516

1617
namespace pnp
1718
{

package.xml

+2
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
<build_depend>tf2</build_depend>
3838
<build_depend>visualization_msgs</build_depend>
3939
<build_depend>sensor_msgs</build_depend>
40+
<build_depend>moveit_commander</build_depend>
4041

4142

4243

@@ -61,6 +62,7 @@
6162
<exec_depend>pluginlib</exec_depend>
6263
<exec_depend>moveit_core</exec_depend>
6364
<exec_depend>moveit_commander</exec_depend>
65+
<exec_depend>moveit_ros_planning</exec_depend>
6466
<exec_depend>moveit_ros_planning_interface</exec_depend>
6567
<exec_depend>moveit_ros_perception</exec_depend>
6668
<exec_depend>moveit_visual_tools</exec_depend>

scripts/pick_and_place.py

+36-8
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,11 @@ class PickAndPlace(object):
3131

3232
def __init__(self):
3333
super(PickAndPlace, self).__init__()
34+
rospy.init_node("pnp_pipeline")
3435

3536
# Initialize moveit_commander and ROS node
3637
moveit_commander.roscpp_initialize(sys.argv)
37-
rospy.init_node("pnp_pipeline")
38-
38+
3939
self.pose_point_pub = rospy.Publisher(
4040
"pose_point", visualization_msgs.msg.Marker, queue_size=10
4141
)
@@ -151,7 +151,7 @@ def go_to_joint_state(self, joint_goal) -> None:
151151
self.arm.stop()
152152

153153
def go_to_pose_target(
154-
self, translation: list = None, rotation: list = None, relative: bool = False
154+
self, translation: list, rotation: list, relative: bool = False
155155
) -> None:
156156
"""Go to a specified pose, the pose is selected by the user as the desired location
157157
of the palm of the hand. The orientation is specified by the user as a list of Euler angles
@@ -171,30 +171,50 @@ def go_to_pose_target(
171171
)
172172
# add the translation to the homogeneous transformation matrix
173173
homogeneous_mat_arm[:3, 3] = translation[:3] # w^T_ee
174+
# print the homogeneous transformation matrix for debugging purposes
175+
#print("homogeneous_mat_arm: ", homogeneous_mat_arm)
176+
177+
174178
# create a homogeneous transformation matrix for the end effector with no rotation
175179
homogeneous_trans_end_effector = translation_matrix( # palm^T_ee <- thus this gets inversed
176180
[0, 0, end_effector_palm_length]
177181
)
182+
183+
# print the homogeneous transformation matrix for debugging purposes
184+
#print("homogenous translation end effector matrix: ", homogeneous_trans_end_effector)
185+
186+
178187
# multiply the homogeneous transformation matrix of the arm by the inverse of the homogeneous transformation matrix of the end effector
179188
homogeneous_mat = np.dot( # w^T_ee * (palm^T_ee)^-1 = w^T_palm
180189
homogeneous_mat_arm, np.linalg.inv(homogeneous_trans_end_effector)
181190
)
191+
192+
# print the homogeneous transformation matrix for debugging purposes
193+
#print("homogeneous_mat: ", homogeneous_mat)
194+
195+
182196
# quaternion_from_euler using input rotation values
183197
quaternion = quaternion_from_euler(
184198
rotation_rads[0], rotation_rads[1], rotation_rads[2], axes="szyz"
185199
)
200+
201+
186202
# create message types for the pose target
187203
orientation = Quaternion(
188204
quaternion[0], quaternion[1], quaternion[2], quaternion[3]
189205
)
190206
# this is the position of the palm and the index retrieves the x,y,z values from the
191207
# homogeneous transformation matrix
192208
position = Point(
193-
homogeneous_mat[0, 3], homogeneous_mat[1, 3], homogeneous_mat[2, 3]
209+
homogeneous_mat[0, 3], homogeneous_mat[1, 3], homogeneous_mat[2, 3] # type: ignore
194210
)
195211
# Set the target pose message
196212
pose_target = Pose(position, orientation)
197213
self.add_pose_arrow(position, rotation_rads[2])
214+
215+
# print the pose target for debugging purposes
216+
#print("pose_target: ", pose_target)
217+
198218
input("[PROGRESS] Pose Target Set, Press Enter to Continue...")
199219
self.arm.set_pose_target(pose_target)
200220

@@ -251,6 +271,9 @@ def attach_rod(self) -> None:
251271
## FLOW METHODS
252272
def pick_rod(self) -> None:
253273
rod_position = self.get_rod_position()
274+
# print the rod position for debugging purposes
275+
print("rod_position: ", rod_position)
276+
254277
self.go_to_pose_target(
255278
[
256279
round(rod_position[0], 2),
@@ -301,6 +324,10 @@ def add_pose_arrow(self, desired_position: Point, z_rotation: float) -> None:
301324
desired_position,
302325
Quaternion(quaternion[0], quaternion[1], quaternion[2], quaternion[3]),
303326
)
327+
328+
# print the desired pose for debugging purposes
329+
print("desired_pose: ", desired_pose)
330+
304331
marker.pose = desired_pose
305332
# Publish the marker
306333
self.pose_point_pub.publish(marker)
@@ -314,10 +341,8 @@ def remove_pose_arrow(self) -> None:
314341
self.pose_point_pub.publish(marker)
315342

316343

317-
def main():
344+
def main(pick_and_place):
318345
try:
319-
# initialize the node
320-
pick_and_place = PickAndPlace()
321346
# retrieve critical parameters and set up the scene
322347
home_joint_pos = pick_and_place.arm.get_current_joint_values()
323348
print(
@@ -345,4 +370,7 @@ def main():
345370

346371

347372
if __name__ == "__main__":
348-
main()
373+
# initialize the pick and place node
374+
pick_and_place = PickAndPlace()
375+
# run the main function
376+
main(pick_and_place)

src/pick_and_place.cpp

+45-37
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
#include "panda_moveit/pick_and_place.hpp"
1+
#include "../include/panda_moveit/pick_and_place.hpp"
2+
23
namespace pnp
34
{
45
// The init function
@@ -7,7 +8,7 @@ namespace pnp
78
pose_point_pub = nh.advertise<visualization_msgs::Marker>("pose_point", 10);
89

910
// set arm planning time
10-
const double PLANNING_TIME = 5.0;
11+
const double PLANNING_TIME = 15.0;
1112
move_group_interface_arm.setPlanningTime(PLANNING_TIME);
1213

1314
// Raw pointers are frequently used to refer to the planning group for improved performance.
@@ -21,43 +22,24 @@ namespace pnp
2122
ROS_INFO_NAMED("pnp", "Planning frame: %s", move_group_interface_arm.getPlanningFrame().c_str());
2223

2324
std::vector<std::string> linkNames = move_group_interface_arm.getLinkNames();
24-
std::string linkNamesArm;
25-
for (const auto &link : linkNames)
26-
{
27-
linkNamesArm += link + ", ";
28-
}
25+
std::string linkNamesArm = boost::algorithm::join(linkNames, ", ");
2926
ROS_INFO_NAMED("pnp", "Arm links: %s", linkNamesArm.c_str());
3027

3128
// ROS_INFO_NAMED the arm joint names
3229
std::vector<std::string> jointNamesArm = move_group_interface_arm.getJoints();
33-
std::string jointNamesArmString;
34-
for (const auto &joint : jointNamesArm)
35-
{
36-
jointNamesArmString += joint + ", ";
37-
}
30+
std::string jointNamesArmString = boost::algorithm::join(jointNamesArm, ", ");
3831
ROS_INFO_NAMED("pnp", "Arm joint names: %s", jointNamesArmString.c_str());
3932

4033
// ROS_INFO_NAMED the gripper joint names
4134
std::vector<std::string> jointNamesGripper = move_group_interface_gripper.getJoints();
42-
std::string jointNamesGripperString;
43-
for (const auto &joint : jointNamesGripper)
44-
{
45-
jointNamesGripperString += joint + ", ";
46-
}
47-
35+
std::string jointNamesGripperString = boost::algorithm::join(jointNamesGripper, ", ");
4836
ROS_INFO_NAMED("pnp", "Gripper joints names: %s", jointNamesGripperString.c_str());
4937

5038
// We can get a list of all the groups in the robot:
5139
std::vector<std::string> planningGroups = move_group_interface_arm.getJointModelGroupNames();
52-
std::string planningGroupsString;
53-
for (const auto &groups : planningGroups)
54-
{
55-
planningGroupsString += groups + ", ";
56-
}
40+
std::string planningGroupsString = boost::algorithm::join(planningGroups, ", ");
5741
ROS_INFO_NAMED("pnp", "Available Planning Groups: %s", planningGroupsString.c_str());
58-
// Add a delay before calling getCurrentJointValues()
59-
ros::Duration(1.0).sleep();
60-
42+
6143
// using getJointStates to get the current joint values put in a block to print the error
6244
home_joint_values = move_group_interface_arm.getCurrentJointValues();
6345
std::string jointValuesString;
@@ -67,8 +49,6 @@ namespace pnp
6749
}
6850
ROS_INFO_NAMED("pnp", "Current joint values: %s", jointValuesString.c_str());
6951

70-
// add a sleep
71-
// ros::Duration(1000.0).sleep();
7252
}
7353

7454
void PickandPlace::createCollisionObject(std::string id, std::vector<double> dimensions, std::vector<double> position, double rotation_z)
@@ -168,13 +148,22 @@ namespace pnp
168148
homogeneous_mat_arm(1, 3) = translation[1];
169149
homogeneous_mat_arm(2, 3) = translation[2];
170150

151+
// print the homogeneous matrix of the arm
152+
ROS_INFO_STREAM_NAMED("pnp", "homogeneous_mat_arm: \n" << homogeneous_mat_arm);
153+
171154
// Create a homogeneous transformation matrix for the end effector with no rotation
172155
Eigen::Matrix4d homogeneous_trans_end_effector = Eigen::Matrix4d::Identity();
173156
homogeneous_trans_end_effector(2, 3) = end_effector_palm_length;
174157

158+
// print the homogeneous matrix of the end effector
159+
ROS_INFO_STREAM_NAMED("pnp", "homogeneous_translation_end_effector: \n" << homogeneous_trans_end_effector);
160+
175161
// Multiply the homogeneous transformation matrix of the arm by the inverse of the homogeneous transformation matrix of the end effector
176162
Eigen::Matrix4d homogeneous_mat = homogeneous_mat_arm * homogeneous_trans_end_effector.inverse();
177163

164+
// print the homogeneous matrix of the adjusted homogenous matrix
165+
ROS_INFO_STREAM_NAMED("pnp", "homogeneous_mat: \n" << homogeneous_mat);
166+
178167
// Create a quaternion from euler angles
179168
tf2::Quaternion quaternion;
180169
// get a quaternion from the rotation matrix R
@@ -184,10 +173,7 @@ namespace pnp
184173

185174
// Create message types for the pose target
186175
geometry_msgs::Quaternion orientation;
187-
orientation.x = quaternion.x();
188-
orientation.y = quaternion.y();
189-
orientation.z = quaternion.z();
190-
orientation.w = quaternion.w();
176+
orientation = tf2::toMsg(quaternion);
191177

192178
geometry_msgs::Point position;
193179
position.x = homogeneous_mat(0, 3);
@@ -202,7 +188,11 @@ namespace pnp
202188
// add pose arrow
203189
add_pose_arrow(pose_target.position, rotation_rads[2]);
204190

205-
// set the target pose
191+
192+
// print the target pose
193+
ROS_INFO_STREAM_NAMED("pnp", "pose_target: \n" << pose_target);
194+
195+
// set the pose target
206196
move_group_interface_arm.setPoseTarget(pose_target);
207197

208198
// print target pose successfully set
@@ -212,10 +202,8 @@ namespace pnp
212202
bool success = (move_group_interface_arm.plan(plan) == moveit::planning_interface::MoveItErrorCode::SUCCESS);
213203

214204
// print if the arm was able to move to the target pose
215-
ROS_INFO_NAMED("pnp", "Visualizing plan 1 (pose goal) %s", success ? "" : "FAILED");
205+
ROS_INFO_NAMED("pnp", "Moving to pose target %s", success ? "" : "FAILED");
216206

217-
// execute the plan
218-
move_group_interface_arm.move();
219207
}
220208

221209
void PickandPlace::add_pose_arrow(geometry_msgs::Point desired_position, float z_rotation)
@@ -240,6 +228,17 @@ namespace pnp
240228
geometry_msgs::Pose Pose;
241229
Pose.position = desired_position;
242230
Pose.orientation = tf2::toMsg(quaternion);
231+
232+
// print the desired pose for debugging
233+
ROS_INFO_STREAM_NAMED("pnp", "desired pose: \n"
234+
<< "x: " << Pose.position.x << "\n"
235+
<< "y: " << Pose.position.y << "\n"
236+
<< "z: " << Pose.position.z << "\n"
237+
<< "x: " << Pose.orientation.x << "\n"
238+
<< "y: " << Pose.orientation.y << "\n"
239+
<< "z: " << Pose.orientation.z << "\n"
240+
<< "w: " << Pose.orientation.w);
241+
243242
marker.pose = Pose;
244243

245244
// Publish the marker
@@ -286,6 +285,15 @@ namespace pnp
286285
// print rod position
287286
ROS_INFO_NAMED("pnp", "Rod position: %f, %f, %f", rod_position[0], rod_position[1], rod_position[2]);
288287

289-
ros::Duration(2.0).sleep();
288+
// set pose target
289+
set_pose_target(rod_position, {45, 90, 45});
290+
291+
// execute the plan
292+
move_group_interface_arm.move();
293+
294+
// sleep for 10000 seconds for debugging
295+
ros::Duration(10000).sleep();
296+
297+
290298
}
291299
};

0 commit comments

Comments
 (0)