-
Notifications
You must be signed in to change notification settings - Fork 320
/
move_robot.py
executable file
·86 lines (78 loc) · 3.18 KB
/
move_robot.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#! /usr/bin/env python
"""Publishes joint trajectory to move robot to given pose"""
import rospy
from trajectory_msgs.msg import JointTrajectory
from trajectory_msgs.msg import JointTrajectoryPoint
from std_srvs.srv import Empty
import argparse
import time
def argumentParser(argument):
""" Argument parser """
parser = argparse.ArgumentParser(description='Drive robot joint to command position')
parser.add_argument('kinova_robotType', metavar='kinova_robotType', type=str, default='j2n6a300',
help='kinova_RobotType is in format of: [{j|m|r|c}{1|2}{s|n}{4|6|7}{s|a}{2|3}{0}{0}]. eg: j2n6a300 refers to jaco v2 6DOF assistive 3fingers. Please be noted that not all options are valided for different robot types.')
#args_ = parser.parse_args(argument)
argv = rospy.myargv()
args_ = parser.parse_args(argv[1:])
prefix = args_.kinova_robotType
nbJoints = int(args_.kinova_robotType[3])
nbfingers = int(args_.kinova_robotType[5])
return prefix, nbJoints, nbfingers
def moveJoint (jointcmds,prefix,nbJoints):
topic_name = '/' + prefix + '/effort_joint_trajectory_controller/command'
pub = rospy.Publisher(topic_name, JointTrajectory, queue_size=1)
jointCmd = JointTrajectory()
point = JointTrajectoryPoint()
jointCmd.header.stamp = rospy.Time.now() + rospy.Duration.from_sec(0.0);
point.time_from_start = rospy.Duration.from_sec(5.0)
for i in range(0, nbJoints):
jointCmd.joint_names.append(prefix +'_joint_'+str(i+1))
point.positions.append(jointcmds[i])
point.velocities.append(0)
point.accelerations.append(0)
point.effort.append(0)
jointCmd.points.append(point)
rate = rospy.Rate(100)
count = 0
while (count < 50):
pub.publish(jointCmd)
count = count + 1
rate.sleep()
def moveFingers (jointcmds,prefix,nbJoints):
topic_name = '/' + prefix + '/effort_finger_trajectory_controller/command'
pub = rospy.Publisher(topic_name, JointTrajectory, queue_size=1)
jointCmd = JointTrajectory()
point = JointTrajectoryPoint()
jointCmd.header.stamp = rospy.Time.now() + rospy.Duration.from_sec(0.0);
point.time_from_start = rospy.Duration.from_sec(5.0)
for i in range(0, nbJoints):
jointCmd.joint_names.append(prefix +'_joint_finger_'+str(i+1))
point.positions.append(jointcmds[i])
point.velocities.append(0)
point.accelerations.append(0)
point.effort.append(0)
jointCmd.points.append(point)
rate = rospy.Rate(100)
count = 0
while (count < 500):
pub.publish(jointCmd)
count = count + 1
rate.sleep()
if __name__ == '__main__':
try:
rospy.init_node('move_robot_using_trajectory_msg')
prefix, nbJoints, nbfingers = argumentParser(None)
#allow gazebo to launch
time.sleep(5)
# Unpause the physics
rospy.wait_for_service('/gazebo/unpause_physics')
unpause_gazebo = rospy.ServiceProxy('/gazebo/unpause_physics', Empty)
resp = unpause_gazebo()
if (nbJoints==6):
#home robots
moveJoint ([0.0,2.9,1.3,4.2,1.4,0.0],prefix,nbJoints)
else:
moveJoint ([0.0,2.9,0.0,1.3,4.2,1.4,0.0],prefix,nbJoints)
moveFingers ([1,1,1],prefix,nbfingers)
except rospy.ROSInterruptException:
print "program interrupted before completion"