compare.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 import sys
3 import math
4 import numpy as np
5 import matplotlib.pyplot as plt
6 from scipy.interpolate import interp1d
7 
8 import rosbag
9 import rospy
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
15 
16 
17 def resolve_id(bag):
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
22 
24 
25  id = resolve_id(bag)
26 
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
32 
34 
35  id = resolve_id(bag)
36 
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:
39  tt = msg.header.stamp
40  if status.status == 3 and status.goal_id.id == id:
41  return msg.header.stamp
42 
43  return tt
44 
45 
46 jorder = { 'shoulder_pan_joint': 1,
47  'shoulder_lift_joint' : 2,
48  'elbow_joint': 3,
49  'wrist_1_joint' : 4,
50  'wrist_2_joint' : 5,
51  'wrist_3_joint' : 6 }
52 
53 
54 def plot_data(data, plots = [], style = '-'):
55  data = np.array(data)
56 
57  for i in range(6):
58  index = i + 1 #jorder[msg.name[i]]
59 
60  plt.subplot(3, 2, index)
61 
62  plt.plot(data[:, 0], data[:, i+1], ls = style, hold=True)
63  # plt.ylabel(msg.name[i])
64  plt.xlabel('time')
65 
66 
67 
68  print(data[:,0])
69 
70 def plot_js(bag, start, end, plots = [], style = '-'):
71 
72  data = []
73 
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()
77 
78  row = np.append(time_stamp, msg.position)
79  data.append(row)
80 
81  data = np.array(data)
82 
83  for i in range(6):
84  index = jorder[msg.name[i]]
85 
86  plt.subplot(3, 2, index)
87 
88  plt.plot(data[:, 0], data[:, i+1], ls = style, hold=True)
89  plt.ylabel(msg.name[i])
90  plt.xlabel('time')
91 
92  # print(data[:,0])
93 
94  # print(row)
95 
96  # for i in range(len(msg.name)):
97  # jdict[msg.name[i]] = i
98 
99  # print(jdict)
100 
101 
102 
103 
104 def extract_data(bag, start, end):
105 
106  data = []
107 
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()
111 
112  row = np.append(time_stamp, msg.position)
113  data.append(row)
114 
115  return np.array(data)
116 
117 def find_error(t_a, f_a, t_b, f_b, offset = 0., resolution = rospy.Duration(0.01)):
118 
119  dt = resolution.to_sec()
120 
121  # t_a += offset
122 
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)
126 
127  # print(t_a)
128  # print(min_t, max_t)
129  # print(t_new - offset)
130  # print(t_new)
131 
132  y_new_a = f_a(t_new - offset)
133  y_new_b = f_b(t_new)
134 
135  # data_a = np.concatenate((t_new.reshape(len(t_new),1), y_new_a), axis=1)
136  # data_b = np.concatenate((t_new.reshape(len(t_new),1), y_new_b), axis=1)
137 
138 
139  # plot_data(data_a)
140  # plot_data(data_b)
141  # plt.draw()
142 
143 
144 
145  error = (sum(sum(abs(y_new_a-y_new_b)**2))) / len(t_new)
146  # print(error)
147  # print("###########")
148 
149  return error
150 
151 
152 
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.)):
154 
155  data_a = extract_data(bag_a, start_a, end_a)
156  data_b = extract_data(bag_b, start_b, end_b)
157 
158  t_a = data_a[:,0]
159  y_a = data_a[:,1:]
160 
161  t_b = data_b[:,0]
162  y_b = data_b[:,1:]
163 
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")
166 
167  print(t_a)
168  print(t_b)
169  # return
170 
171  offsets = np.arange(-off_range.to_sec(), off_range.to_sec(), resolution.to_sec())
172 
173  min_error = 1000000000000.
174  best_offset = 0.
175 
176  for offset in offsets:
177  error = find_error( t_a,
178  f_a,
179  t_b,
180  f_b,
181  offset,
182  resolution)
183 
184  if error < min_error:
185  min_error = error
186  best_offset = offset
187 
188  # print("offset", offset)
189  # if offset > 0.:
190  # plt.show()
191 
192  return rospy.Duration(best_offset)
193 
194 
195 
196 
197 def main(args):
198  # print(len(args))
199  if len(args) < 3:
200  print("Error: Too few arguments")
201 
202  bag_a = rosbag.Bag(args[1])
203  bag_b = rosbag.Bag(args[2])
204 
205  id_a = resolve_id(bag_a)
206  start_a = resolve_start_time(bag_a)
207  end_a = resolve_end_time(bag_a)
208 
209  id_b = resolve_id(bag_b)
210  start_b = resolve_start_time(bag_b)
211  end_b = resolve_end_time(bag_b)
212 
213  # offset = rospy.Duration(0.17)
214  offset = rospy.Duration(0.60)
215 
216  # plt.ion()
217 
218  offset = find_offset(bag_a, start_a, end_a, bag_b, start_b, end_b)
219  print(offset)
220 
221  # print(start_b, end_b)
222 
223  plot_js(bag_a, start_a, end_a)
224  plot_js(bag_b, start_b + offset, end_b + offset)#, style='-.')
225 
226  # find_error(bag_a, start_a, end_a, bag_b, start_b, end_b)
227 
228  mng = plt.get_current_fig_manager()
229  mng.full_screen_toggle()
230 
231  plt.show()
232  # print(id)
233  # print(start)
234  # print(end)
235  # print((end-start).secs)
236 
237  # for topic, msg, t in bag.read_messages(topics=['/follow_joi', 'numbers']):
238  # print msg
239  bag.close()
240 
241 if __name__ == '__main__':
242  try:
243  main(sys.argv)
244  except rospy.ROSInterruptException:
245  pass
def plot_data(data, plots=[], style='-')
Definition: compare.py:54
def resolve_end_time(bag)
Definition: compare.py:33
def plot_js(bag, start, end, plots=[], style='-')
Definition: compare.py:70
def resolve_start_time(bag)
Definition: compare.py:23
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.))
Definition: compare.py:153
def find_error(t_a, f_a, t_b, f_b, offset=0., resolution=rospy.Duration(0.01))
Definition: compare.py:117
def extract_data(bag, start, end)
Definition: compare.py:104
def resolve_id(bag)
Definition: compare.py:17
def main(args)
Definition: compare.py:197


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