5 import matplotlib.pyplot
as plt
6 from scipy.interpolate
import interp1d
10 from std_msgs.msg
import String
11 from sensor_msgs.msg
import JointState
12 from trajectory_msgs.msg
import JointTrajectory
13 from trajectory_msgs.msg
import JointTrajectoryPoint
14 from control_msgs.msg
import FollowJointTrajectoryActionGoal
18 for topic, msg, t
in bag.read_messages(topics=[
'/arm_controller/follow_joint_trajectory/status',
'/follow_joint_trajectory/status']):
19 for status
in msg.status_list:
20 if status.status == 1:
21 return status.goal_id.id
27 for topic, msg, t
in bag.read_messages(topics=[
'/arm_controller/follow_joint_trajectory/status',
'/follow_joint_trajectory/status']):
28 for status
in msg.status_list:
29 return msg.header.stamp
30 if status.status == 1
and status.goal_id.id == id:
31 return msg.header.stamp
37 for topic, msg, t
in bag.read_messages(topics=[
'/arm_controller/follow_joint_trajectory/status',
'/follow_joint_trajectory/status']):
38 for status
in msg.status_list:
40 if status.status == 3
and status.goal_id.id == id:
41 return msg.header.stamp
46 jorder = {
'shoulder_pan_joint': 1,
47 'shoulder_lift_joint' : 2,
60 plt.subplot(3, 2, index)
62 plt.plot(data[:, 0], data[:, i+1], ls = style, hold=
True)
70 def plot_js(bag, start, end, plots = [], style = '-'):
74 for topic, msg, t
in bag.read_messages(topics=[
'/joint_states']):
75 if msg.header.stamp >= start
and msg.header.stamp < end:
76 time_stamp = (msg.header.stamp - start).to_sec()
78 row = np.append(time_stamp, msg.position)
84 index = jorder[msg.name[i]]
86 plt.subplot(3, 2, index)
88 plt.plot(data[:, 0], data[:, i+1], ls = style, hold=
True)
89 plt.ylabel(msg.name[i])
108 for topic, msg, t
in bag.read_messages(topics=[
'/joint_states']):
109 if msg.header.stamp >= start
and msg.header.stamp <= end:
110 time_stamp = (msg.header.stamp - start).to_sec()
112 row = np.append(time_stamp, msg.position)
115 return np.array(data)
117 def find_error(t_a, f_a, t_b, f_b, offset = 0., resolution = rospy.Duration(0.01)):
119 dt = resolution.to_sec()
123 min_t = max(np.min(t_a + offset), np.min(t_b))
124 max_t = min(np.max(t_a + offset), np.max(t_b))
125 t_new = np.arange(min_t, max_t, dt)
132 y_new_a = f_a(t_new - offset)
145 error = (sum(sum(abs(y_new_a-y_new_b)**2))) / len(t_new)
153 def find_offset(bag_a, start_a, end_a, bag_b, start_b, end_b, resolution = rospy.Duration(0.01), off_range = rospy.Duration(2.)):
164 f_a = interp1d(t_a, y_a, axis = 0, fill_value =
"extrapolate")
165 f_b = interp1d(t_b, y_b, axis = 0, fill_value =
"extrapolate")
171 offsets = np.arange(-off_range.to_sec(), off_range.to_sec(), resolution.to_sec())
173 min_error = 1000000000000.
176 for offset
in offsets:
184 if error < min_error:
192 return rospy.Duration(best_offset)
200 print(
"Error: Too few arguments")
214 offset = rospy.Duration(0.60)
218 offset =
find_offset(bag_a, start_a, end_a, bag_b, start_b, end_b)
224 plot_js(bag_b, start_b + offset, end_b + offset)
228 mng = plt.get_current_fig_manager()
229 mng.full_screen_toggle()
241 if __name__ ==
'__main__':
244 except rospy.ROSInterruptException:
def plot_data(data, plots=[], style='-')
def resolve_end_time(bag)
def plot_js(bag, start, end, plots=[], style='-')
def resolve_start_time(bag)
def find_offset(bag_a, start_a, end_a, bag_b, start_b, end_b, resolution=rospy.Duration(0.01), off_range=rospy.Duration(2.))
def find_error(t_a, f_a, t_b, f_b, offset=0., resolution=rospy.Duration(0.01))
def extract_data(bag, start, end)