joint_trajectory_generator.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 
3 import sys
4 import actionlib
5 import argparse
6 import numpy as np
7 import rospy
8 from trajectory_msgs.msg import JointTrajectory,JointTrajectoryPoint
9 from control_msgs.msg import FollowJointTrajectoryAction, FollowJointTrajectoryGoal
10 
12  def __init__(self, time=1.0, step=0.01, freq=1.0, joint_num=1, topic='/joint_trajectory', action='/joint_trajectroy_action'):
13  self.time = time
14  self.step = step
15  self.freq = freq
16  self.joint_names = ['joint_'+str(n) for n in range(joint_num)]
17 
18  self.pub = rospy.Publisher(topic, JointTrajectory, queue_size=10)
19  self.client = actionlib.SimpleActionClient(
20  action, FollowJointTrajectoryAction)
21 
22  def update(self):
23  points = []
24  tick = 0
25  self.phase = np.random.uniform(np.pi)
26  while tick <= self.time:
27  x = 2*np.pi*self.freq*tick + self.phase
28  positions = [np.sin(x)]*len(self.joint_names)
29  velocities = [np.cos(x)]*len(self.joint_names)
30  accelerations = [-np.sin(x)]*len(self.joint_names)
31  points.append(JointTrajectoryPoint(positions=positions,
32  velocities=velocities,
33  accelerations=accelerations,
34  time_from_start=rospy.Time.from_sec(tick)))
35  tick += self.step
36  msg = JointTrajectory(joint_names=self.joint_names, points=points)
37  msg.header.stamp = rospy.Time.now()
38  self.pub.publish(msg)
39 
40  # joint_trajectroy_action
41  goal = FollowJointTrajectoryGoal()
42  goal.trajectory = msg
43  self.client.send_goal(goal)
44 
45 if __name__ == "__main__":
46  rospy.init_node('joint_trajectory_generator')
47 
48  parser = argparse.ArgumentParser()
49  parser.add_argument('--time-step', type=float, help='Time step between each trajectory point')
50  parser.add_argument('--topic', type=str, default="/joint_trajectory", help='Topic name to be published')
51  parser.add_argument('--action', type=str, default="/joint_trajectory_action", help='Action name to be called')
52  parser.add_argument('--joint-num', type=int, default=1, help='Number of joints')
53  sys.argv = rospy.myargv()
54  args = parser.parse_args()
55 
56  generator = JointTrajectoryGenerator(joint_num=args.joint_num,
57  topic=args.topic,
58  action=args.action)
59 
60  rate = rospy.Rate(1)
61  while not rospy.is_shutdown():
62  generator.update()
63  rate.sleep()
def __init__(self, time=1.0, step=0.01, freq=1.0, joint_num=1, topic='/joint_trajectory', action='/joint_trajectroy_action')


rqt_joint_trajectory_plot
Author(s): Ryosuke Tajima
autogenerated on Mon Nov 2 2020 03:13:10