gen_traj.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 import math
3 import numpy as np
4 import random
5 import string
6 
7 import rospy
8 from std_msgs.msg import String
9 from sensor_msgs.msg import JointState
10 from trajectory_msgs.msg import JointTrajectory
11 from trajectory_msgs.msg import JointTrajectoryPoint
12 from control_msgs.msg import FollowJointTrajectoryActionGoal
13 
14 def js_callback(msg):
15  global last_js
16  last_js = msg
17 
18 
19 def gen_traj(joint_limits = np.array([[-math.pi, math.pi]] * 6),
20  vel_limit = 0.1,
21  acc_mod = 0.1,
22  time_step = 0.1,
23  action_step = 10,
24  duration = 10):
25 
26  traj = JointTrajectory()
27  traj.joint_names = [ 'shoulder_pan_joint',
28  'shoulder_lift_joint',
29  'elbow_joint',
30  'wrist_1_joint',
31  'wrist_2_joint',
32  'wrist_3_joint' ]
33 
34  traj.header.frame_id = '/world'
35 
36  t = 0.
37  iters = 0
38 
39  init_pose = np.array(last_js.position) ##### TODO: remove the dependency on global pose ####
40  init_pose[0:3] = init_pose[2::-1]
41 
42  pos = np.array([0.] * 6)
43  vel = np.array([0.] * 6)
44  acc = np.array([0.] * 6)
45 
46  while t < duration:
47 
48  # print("############################")
49  # print(t)
50  # print(pos)
51  # print(vel)
52  # print(acc)
53 
54  point = JointTrajectoryPoint()
55  point.positions = pos + init_pose
56  point.velocities = [0.] * 6
57  point.accelerations = [0.] * 6
58  point.time_from_start = rospy.Duration(t)
59 
60  traj.points.append(point)
61 
62  pos += vel * time_step
63  pos = np.minimum(pos, joint_limits[:,1])
64  pos = np.maximum(pos, joint_limits[:,0])
65 
66  vel += acc * time_step
67  vel = np.minimum(vel, vel_limit)
68  vel = np.maximum(vel, -vel_limit)
69 
70  # print (iters % action_step)
71 
72 
73  if iters % action_step == 0:
74  acc = np.random.rand(6) - 0.5
75  acc = acc / np.linalg.norm(acc)
76  acc *= acc_mod
77 
78 
79  t += time_step
80  iters += 1
81 
82  point = JointTrajectoryPoint()
83  point.positions = init_pose
84  point.velocities = [0.] * 6
85  point.accelerations = [0.] * 6
86  point.time_from_start = rospy.Duration(duration + 3)
87 
88  traj.points.append(point)
89 
90  return traj
91 
92 
93 
94 def main():
95 
96  # pub = rospy.Publisher('/if_any', String, queue_size=10)
97  traj_pub = rospy.Publisher('/arm_controller/follow_joint_trajectory/goal', FollowJointTrajectoryActionGoal, queue_size=10)
98  rospy.init_node('generate_trajectory', anonymous=True)
99  rospy.Subscriber("/joint_states", JointState, js_callback)
100 
101  rospy.sleep(0.5)
102  while 'last_js' not in globals() and not rospy.is_shutdown(): pass
103 
104  joint_limits = np.array([[-math.pi, math.pi]] * 6)
105  # joint_limits[0:2, :] = 0.
106  vel_limit = 1.0
107  acc = 10.0
108  time_step = 0.1
109 
110  traj = gen_traj(joint_limits = joint_limits, action_step = 10, duration = 10, acc_mod = 10., vel_limit = 0.5, time_step = 0.01)
111 
112  goal = FollowJointTrajectoryActionGoal()
113  goal.goal.trajectory = traj
114  # goal.header.stamp = rospy.Time.now()
115  # goal.goal_id.stamp = rospy.Time.now()
116  # goal.goal_id.id = ''.join(random.choice(string.ascii_uppercase + string.digits) for _ in range(10))
117 
118  # print(goal)
119 
120  import rosbag
121 
122  bag = rosbag.Bag('test.bag', 'w')
123 
124  try:
125  bag.write('/follow_joint_trajectory/goal', goal)
126  finally:
127  bag.close()
128 
129  traj_pub.publish(goal)
130 
131 
132 
133 if __name__ == '__main__':
134  try:
135  main()
136  except rospy.ROSInterruptException:
137  pass
def js_callback(msg)
Definition: gen_traj.py:14
def gen_traj(joint_limits=np.array([[-math.pi, math.pi]]*6), vel_limit=0.1, acc_mod=0.1, time_step=0.1, action_step=10, duration=10)
Definition: gen_traj.py:24


dyn_tune
Author(s):
autogenerated on Mon Jun 10 2019 13:03:17