Control Gripper using Python
Closed this issue · 3 comments
Dear all,
I am learning and find it difficult from the doc to get real understanding to control the Franka using Python and ROS.
following terminal command close the gripper
rostopic pub /franka_gripper/move/goal franka_gripper/MoveActionGoal "header:
seq: 0
stamp:
secs: 0
nsecs: 0
frame_id: ''
goal_id:
stamp:
secs: 0
nsecs: 0
id: ''
goal:
width: 0.0
speed: 1.0"
Now if I want to do similar thing using python. I have written following code in a package
import rospy
from std_msgs.msg import String
import franka_gripper.msg
def my_listener():
pub = rospy.Publisher('/franka_gripper/move/goal', franka_gripper.msg.MoveActionGoal, queue_size=10)
rospy.init_node('my_listener', anonymous=True)
rate = rospy.Rate(10) # 10hz
while not rospy.is_shutdown():
goal = franka_gripper.msg.MoveGoal(width=0.5, speed=1.0)
#hello_str = "hello world %s" % rospy.get_time()
#rospy.loginfo('Publishing')
pub.publish(goal)
rate.sleep()
if __name__ == '__main__':
try:
my_listener()
except rospy.ROSInterruptException:
pass
but I get following error
rosrun my_gripper my_listener.py
Traceback (most recent call last):
File "/home/panda/ros_catkin/src/my_gripper/scripts/my_listener.py", line 56, in <module>
my_listener()
File "/home/panda/ros_catkin/src/my_gripper/scripts/my_listener.py", line 51, in my_listener
pub.publish(goal)
File "/opt/ros/melodic/lib/python2.7/dist-packages/rospy/topics.py", line 879, in publish
data = args_kwds_to_message(self.data_class, args, kwds)
File "/opt/ros/melodic/lib/python2.7/dist-packages/rospy/msg.py", line 121, in args_kwds_to_message
raise TypeError("expected [%s] but got [%s]"%(data_class._slot_types[0], arg._type))
TypeError: expected [std_msgs/Header] but got [franka_gripper/MoveGoal]
The movement of the gripper is an action. While it should be possible to make it work with a publisher, you should consider making it an action client. You can read about them here
You can also take my comment in #256 as a starting point
Dear marcbone,
Thank you. I changed the code as following by going through your starting point;
import franka_gripper.msg
import rospy
import sys
# Brings in the SimpleActionClient
import actionlib
# Brings in the messages used by the grasp action, including the
# goal message and the result message.
def move_gripper():
# Creates the SimpleActionClient, passing the type of the action
client = actionlib.SimpleActionClient('/franka_gripper/move/goal', franka_gripper.msg.MoveActionGoal)
# Waits until the action server has started up and started
# listening for goals.
client.wait_for_server()
# Creates a goal to send to the action server.
goal = franka_gripper.msg.MoveActionGoal()
goal.width = 0.022
goal.speed = 1.0
# Sends the goal to the action server.
client.send_goal(goal)
# Waits for the server to finish performing the action.
client.wait_for_result()
# Prints out the result of executing the action
return client.get_result() # A GraspResult
if __name__ == '__main__':
try:
# Initializes a rospy node so that the SimpleActionClient can
# publish and subscribe over ROS.
rospy.init_node('junaid_gripper')
result = move_gripper()
print("Success: ",result.success)
print("Error message: ", result.error)
except rospy.ROSInterruptException:
print("program interrupted before completion", file=sys.stderr)
It still not working. Secondly, why my mouse cursor changes and i get control back of mouse only if kill the process. Kindly help.
I figured out my mistake. Thank you. Here is the solution for the community. You may close the issue.
import franka_gripper.msg
import rospy
import sys
# Brings in the SimpleActionClient
import actionlib
# Brings in the messages used by the action, including the
# goal message and the result message.
def move_gripper():
# Creates the SimpleActionClient, passing the type of the action
client = actionlib.SimpleActionClient('/franka_gripper/move', franka_gripper.msg.MoveAction)
# Waits until the action server has started up and started
# listening for goals.
client.wait_for_server()
# Creates a goal to send to the action server.
goal = franka_gripper.msg.MoveGoal(width=0.001, speed=1.0)
#goal.width = 0.022
#goal.speed = 1.0
# Sends the goal to the action server.
client.send_goal(goal)
# Waits for the server to finish performing the action.
client.wait_for_result()
# Prints out the result of executing the action
return client.get_result() # A move result
if __name__ == '__main__':
try:
# Initializes a rospy node so that the SimpleActionClient can
# publish and subscribe over ROS.
rospy.init_node('junaid_gripper')
result = move_gripper()
print("Success: ",result.success)
print("Error message: ", result.error)
except rospy.ROSInterruptException:
print("program interrupted before completion")