run.py
Go to the documentation of this file.
1 from timeout import timeout
2 import rospy
3 import matplotlib.pyplot as plt
4 rospy.init_node('tester')
5 
6 
7 
8 # def f(t):
9 # # @timeout(t)
10 # def h():
11 # rospy.sleep(20)
12 # h()
13 
14 # f(1)
15 
16 def plot_dual(config):
17  plt.clf()
18  data_sim = config['/simulation/joint_states/position']
19  data_real = config['/real/joint_states/position']
20  plot(data_sim)
21  plot(data_real)
22  plt.draw()
23  plt.show()
24 
25 
26 def plot(data, style = '-'):
27 
28  plt.ion()
29 
30  for i in range(data.shape[1]):
31  col = data[:, i]
32  plt.subplot(3, 2, i+1)
33 
34  plt.plot(col, ls = style)#, hold=True)
35  # plt.ylabel(key)
36  plt.xlabel('time')
37 
38  plt.pause(0.05)
39 
40 def error(x, y, axis = 0):
41  mserr = ((x - y) ** 2).mean(axis=axis)
42  # print(mserr)
43  mserr = sum(mserr)
44  return mserr
45 
46 
47 def shift(x, y, i):
48  if i>=0:
49  x_new = x[i:]
50  y_new = y[:len(y)-i]
51  else:
52  x_new = x[0:len(x)+i]
53  y_new = y[-i:]
54  return x_new, y_new
55 
56 
57 def find_best_error(x, y, offset, axis = 0):
58  min_err = float("inf")
59  min_index = 0
60  for i in range(-offset, offset+1):
61  x_new, y_new = shift(x, y, i)
62  # print "################"
63  # print len(x_new)
64  # print len(y_new)
65 
66  err = error(x_new, y_new, axis)
67  print err, i
68  if err < min_err:
69  min_err = err
70  min_index = i
71 
72  print min_err, min_index
73 
74  return min_err, min_index
75 
76 
77 def f(config, axis = 0):
78  # print(config)
79  sim_pos = config['/simulation/joint_states/position']
80  sim_pos[:,[2,1,0,3,4,5]] = sim_pos[:,[0,1,2,3,4,5]]
81 
82 
83  real_pos = config['/real/joint_states/position']
84 
85  mserr, offset = find_best_error(sim_pos, real_pos, len(sim_pos)/4)
86 
87  sim_pos, real_pos = shift(sim_pos, real_pos, offset)
88 
89  config['/simulation/joint_states/position'] = sim_pos
90  config['/real/joint_states/position'] = real_pos
91 
92  # plot(sim_pos)
93  # plot(real_pos)
94  # plt.draw()
95  # plt.show()
96 
97 
98  plot_dual(config)
99 
100  # rospy.sleep(50)
101 
102  # mserr = ((sim_pos - real_pos) ** 2).mean(axis=axis)
103  # mserr = sum(mserr)
104  # print(mserr)
105  return mserr
106 
107 
108 from dynamic_tuner import *
109 params = rospy.get_param("/optimizable_params")
110 values = rospy.get_param("/objective_values")
111 
112 print params, values
113 
114 src_topic = '/follow_joint_trajectory/goal'
115 dst_topic = '/arm_controller/follow_joint_trajectory/goal'
116 
117 src_bag = 'exp-real-7.bag'
118 dst_bag = 'test_sim.bag'
119 
120 start = {'/follow_joint_trajectory/status/status_list[0]/status':1, '/arm_controller/follow_joint_trajectory/feedback/status/status':1}
121 end = {'/arm_controller/follow_joint_trajectory/result':None, '/follow_joint_trajectory/result':None}
122 
123 dyn = dynamic_tuner(params, values, src_bag, dst_bag, src_topic, dst_topic, objective = f, start_signal = start, end_signal = end)
124 
125 dyn.tune_params()
126 
127 
128 # bag = start_simulation(src_bag, dst_bag, src_topic, dst_topic)
129 
130 """
131 from dynamic_tuner import *
132 start_simulation('exp-real-7.bag', 'test_sim.bag', '/follow_joint_trajectory/goal', '/arm_controller/follow_joint_trajectory/goal', {'/follow_joint_trajectory/goal':None, '/arm_controller/follow_joint_trajectory/goal':None}, {'/arm_controller/follow_joint_trajectory/result':None, '/follow_joint_trajectory/result':None})
133 
134 
135 from dynamic_tuner import *
136 start_simulation('exp-real-7.bag', 'test_sim.bag', '/follow_joint_trajectory/goal', '/arm_controller/follow_joint_trajectory/goal', {'/follow_joint_trajectory/status/status_list[0]/status':1, '/arm_controller/follow_joint_trajectory/feedback/status/status':1}, {'/arm_controller/follow_joint_trajectory/result':None, '/follow_joint_trajectory/result':None})
137 
138 
139 
140 sim_bag = rosbag.Bag('test_sim.bag', 'r')
141 bag = restamp(sim_bag, output_bag_file = 'sim-restamped.bag', prefix = SIM_PREFIX, start_signal = start_signal, end_signal = end_signal)
142 
143 
144 dyn._optimizer.optimize()
145 
146 dyn._optimizer.set_params_config({'/arm_controller/gains/elbow_joint/p':100001})
147 desc = dyn.get_params_desc(['/arm_controller/gains/elbow_joint/p', '/arm_controller/gains/elbow_joint/i', '/arm_controller/gains/wrist_3_joint/d'])
148 """
def find_best_error(x, y, offset, axis=0)
Definition: run.py:57
def error(x, y, axis=0)
Definition: run.py:40
def plot_dual(config)
Definition: run.py:16
def f(config, axis=0)
Definition: run.py:77
def plot(data, style='-')
Definition: run.py:26
def shift(x, y, i)
Definition: run.py:47
def restamp(bag, output_bag_file='restamped.bag', values=[], prefix="", start_signal={}, end_signal={})


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