1+ from dmp_lib import DMPs_discrete
2+ from nav_msgs .msg import Path
3+ from geometry_msgs .msg import PoseStamped
4+ from transformations import pose_to_list , list_to_pose # TODO dependency baxter_commander
5+ from tf .transformations import quaternion_about_axis
6+ import numpy as np
7+ import rospy
8+
9+ # dmp = DMPs_discrete(dmps=2, bfs=bfs)
10+ # print len(np.array([path1, path2])
11+ # dmp.imitate_path(y_des=np.array([path1, path2]))
12+ # # change the scale of the movement
13+ # dmp.goal[0] = 3; dmp.goal[1] = 2
14+
15+ # y_track,dy_track,ddy_track = dmp.rollout()
16+
17+ class DiscreteTaskSpaceTrajectory (object ):
18+ NUMBER_STABLE_STATES = 50
19+
20+ def __init__ (self , init_path , bfs = 10 ):
21+ assert isinstance (path , Path )
22+ self .init_path = init_path
23+ self ._dmp = DMPs_discrete (dmps = 7 , bfs = bfs )
24+ self ._dmp .imitate_path (self ._path_to_y_des (init_path , self .NUMBER_STABLE_STATES ))
25+
26+ if self .init_path .header .frame_id == '' :
27+ self .init_path .header .frame_id = self .init_path .poses [0 ].header .frame_id
28+
29+ def _path_to_y_des (self , path , nss ):
30+ y_des = []
31+ for pose_s in path .poses :
32+ assert pose_s .header .frame_id == self .init_path .header .frame_id
33+ pose = [val for sublist in pose_to_list (pose_s ) for val in sublist ] # flatten to [x, y, z, x, y, z, w]
34+ y_des .append (pose )
35+
36+ # Repeat the last point (stable state) n times to avoid brutal cuts due to asymptotic approach
37+ for n in range (nss ):
38+ y_des .append (y_des [- 1 ])
39+
40+ return np .array (y_des ).transpose ()
41+
42+ def rollout (self , goal ):
43+ assert isinstance (goal , PoseStamped )
44+ self ._dmp .goal = [val for sublist in pose_to_list (goal ) for val in sublist ]
45+ y_track , dy_track , ddy_track = self ._dmp .rollout ()
46+
47+ path = Path ()
48+ for y in y_track :
49+ path .poses .append (list_to_pose ([[y [0 ], y [1 ], y [2 ]], [y [3 ], y [4 ], y [5 ], y [6 ]]],
50+ frame_id = self .init_path .header .frame_id ))
51+ path .header .stamp = rospy .Time .now ()
52+ path .header .frame_id = self .init_path .header .frame_id
53+ return path
54+
55+ if __name__ == '__main__' :
56+ rospy .init_node ("test_dmp_traj_discrete" )
57+ path = Path ()
58+ for i in range (100 ):
59+ quat = quaternion_about_axis (i * 0.00628 , (1 , 0 , 0 ))
60+ pose = list_to_pose ([[i / 100. , i / 100. , i / 100. ], quat ])
61+ pose .header .stamp = i / 10.
62+ path .poses .append (pose )
63+
64+ goal = list_to_pose ([[- 10 , - 10 , - 10 ], [0.707 , 0 , 0 , 0.707 ]])
65+ rollout = DiscreteTaskSpaceTrajectory (path ).rollout (goal )
66+ print rollout
0 commit comments