Skip to content

Commit 551c50f

Browse files
authored
Merge pull request #249 from fmessmer/fix/fjt_emulation_melodic
[melodic] fix fjt emulation
2 parents 931d96a + adc3236 commit 551c50f

File tree

1 file changed

+23
-23
lines changed

1 file changed

+23
-23
lines changed

cob_hardware_emulation/scripts/emulation_follow_joint_trajectory.py

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -15,23 +15,30 @@ def __init__(self):
1515

1616
params = rospy.get_param('~')
1717
self.joint_names = params['joint_names']
18-
19-
action_name = "joint_trajectory_controller/follow_joint_trajectory"
20-
21-
self.as_fjta = actionlib.SimpleActionServer(action_name, FollowJointTrajectoryAction, execute_cb=self.fjta_cb, auto_start = False)
22-
self.pub_joint_states = rospy.Publisher("joint_states", JointState, queue_size=1)
18+
# fixed frequency to control the granularity of the sampling resolution
19+
self.sample_rate_hz = rospy.get_param("~sample_rate_hz", 10)
20+
# duration from one sample to the next
21+
self.sample_rate_dur_secs = (1.0 / float(self.sample_rate_hz))
22+
# rospy loop rate
23+
self.sample_rate = rospy.Rate(self.sample_rate_hz)
24+
25+
# joint_states publisher
2326
js = JointState()
2427
js.name = copy.deepcopy(self.joint_names)
2528
js.position = [0]*len(js.name)
2629
js.velocity = [0]*len(js.name)
2730
js.effort = [0]*len(js.name)
2831
self.joint_states = js
29-
30-
self.as_fjta.start()
32+
self.pub_joint_states = rospy.Publisher("joint_states", JointState, queue_size=1)
33+
self.js_timer = rospy.Timer(rospy.Duration(self.sample_rate_dur_secs), self.publish_joint_states)
3134

3235
# reset service
3336
self.service_reset_fjta = rospy.Service("reset_joint_states", Trigger, self.reset_cb)
3437

38+
# follow_joint_trajectory action
39+
action_name = "joint_trajectory_controller/follow_joint_trajectory"
40+
self.as_fjta = actionlib.SimpleActionServer(action_name, FollowJointTrajectoryAction, execute_cb=self.fjta_cb, auto_start = False)
41+
self.as_fjta.start()
3542
rospy.loginfo("Emulation running for action %s of type FollowJointTrajectoryAction"%(action_name))
3643

3744
def reset_cb(self, req):
@@ -71,6 +78,11 @@ def fjta_cb(self, goal):
7178
self.as_fjta.set_aborted()
7279
return
7380

81+
point_time_delta = point.time_from_start - time_since_start_of_previous_point
82+
if point_time_delta.to_sec() < self.sample_rate_dur_secs:
83+
rospy.logwarn("current trajectory point has time_delta smaller than sample_rate: {} < {}! Skipping".format(point_time_delta.to_sec(), self.sample_rate_dur_secs))
84+
continue
85+
7486
# we need to resort the positions array because moveit sorts alphabetically but all other ROS components sort in the URDF order
7587
positions_sorted = []
7688
for joint_name in self.joint_names:
@@ -83,12 +95,6 @@ def fjta_cb(self, goal):
8395
# linear interpolation of the given trajectory samples is used
8496
# to compute smooth intermediate joints positions at a fixed resolution
8597

86-
# fixed frequency to control the granularity of the sampling resolution
87-
sample_rate_hz = 10
88-
# duration from one sample to the next
89-
sample_rate_dur_secs = (1.0 / float(sample_rate_hz))
90-
# rospy loop rate
91-
sample_rate = rospy.Rate(sample_rate_hz) # 10Hz for now
9298
# upper bound of local duration segment
9399
t1 = point.time_from_start - time_since_start_of_previous_point
94100
# compute velocity as the fraction of distance from prev point to next point in trajectory
@@ -124,9 +130,9 @@ def fjta_cb(self, goal):
124130
self.joint_states.position = interpolated_positions
125131

126132
# sleep until next sample update
127-
sample_rate.sleep()
133+
self.sample_rate.sleep()
128134
# increment passed time
129-
latest_time_from_start += rospy.Duration(sample_rate_dur_secs)
135+
latest_time_from_start += rospy.Duration(self.sample_rate_dur_secs)
130136

131137
# ensure that the goal and time point is always exactly reached
132138
latest_time_from_start = point.time_from_start
@@ -143,18 +149,12 @@ def fjta_cb(self, goal):
143149
rospy.logerr("received unexpected joint names in goal")
144150
self.as_fjta.set_aborted()
145151

146-
def publish_joint_states(self):
152+
def publish_joint_states(self, event):
147153
msg = copy.deepcopy(self.joint_states)
148154
msg.header.stamp = rospy.Time.now() # update to current time stamp
149155
self.pub_joint_states.publish(msg)
150156

151157
if __name__ == '__main__':
152158
rospy.init_node('emulation_follow_joint_trajectory')
153-
154159
emulation_follow_joint_trajectory = EmulationFollowJointTrajectory()
155-
156-
# updating the joint states: 10Hz
157-
joint_states_pub_rate = rospy.Rate(10)
158-
while not rospy.is_shutdown():
159-
emulation_follow_joint_trajectory.publish_joint_states()
160-
joint_states_pub_rate.sleep()
160+
rospy.spin()

0 commit comments

Comments
 (0)