contact_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  print "single plot %d" % data.shape[1]
29 
30  plt.ion()
31 
32  for i in range(data.shape[1]):
33  col = data[:, i]
34  plt.subplot(3, 2, i+1)
35  print "plottting data %d" % i
36  print col
37 
38  plt.plot(col, ls = style)#, hold=True)
39  # plt.ylabel(key)
40  plt.xlabel('time')
41 
42  plt.pause(0.05)
43 
44 def error(x, y, axis = 0):
45  mserr = ((x - y) ** 2).mean(axis=axis)
46  # print(mserr)
47  mserr = sum(mserr)
48  return mserr
49 
50 
51 def shift(x, y, i):
52  if i>=0:
53  x_new = x[i:]
54  y_new = y[:len(y)-i]
55  else:
56  x_new = x[0:len(x)+i]
57  y_new = y[-i:]
58  return x_new, y_new
59 
60 
61 def find_best_error(x, y, offset, axis = 0):
62  min_err = float("inf")
63  min_index = 0
64  for i in range(-offset, offset+1):
65  x_new, y_new = shift(x, y, i)
66  # print "################"
67  # print len(x_new)
68  # print len(y_new)
69 
70  err = error(x_new, y_new, axis)
71  print err, i
72  if err < min_err:
73  min_err = err
74  min_index = i
75 
76  print min_err, min_index
77 
78  return min_err, min_index
79 
80 
81 def f(config, axis = 0):
82 
83  return 0.
84 
85  print "the config is:"
86  print(config)
87  sim_pos = config['/simulation/joint_states/position']
88  # sim_pos[:,[2,1,0,3,4,5]] = sim_pos[:,[0,1,2,3,4,5]]
89 
90 
91  real_pos = config['/real/joint_states/position']
92 
93  sim_pos = sim_pos[:,[1,3,4,5,6,8]]
94  real_pos = real_pos[:,[1,3,4,5,6,8]]
95 
96  mserr, offset = find_best_error(sim_pos, real_pos, len(sim_pos)/4)
97 
98  sim_pos, real_pos = shift(sim_pos, real_pos, offset)
99 
100  config['/simulation/joint_states/position'] = sim_pos
101  config['/real/joint_states/position'] = real_pos
102 
103  print "before plot"
104 
105  # plot(sim_pos)
106  # plot(real_pos)
107  # plt.draw()
108  # plt.show()
109 
110 
111  plot_dual(config)
112 
113  print "after plot"
114 
115  # rospy.sleep(50)
116 
117  # mserr = ((sim_pos - real_pos) ** 2).mean(axis=axis)
118  # mserr = sum(mserr)
119  print(mserr)
120  return mserr
121 
122 
123 from dynamic_tuner import *
124 # params = rospy.get_param("/optimizable_params")
125 # values = rospy.get_param("/objective_values")
126 
127 values = [
128  '/simulation/sensor_states/contact[10]',
129  '/simulation/sensor_states/contact[6]',
130  '/simulation/sensor_states/contact[1]',
131  '/simulation/sensor_states/contact[8]',
132 
133  '/real/sensor_states/contact[3]',
134  '/real/sensor_states/contact[2]',
135  '/real/sensor_states/contact[4]',
136  '/real/sensor_states/contact[0]',
137  '/real/sensor_states/contact[1]',
138 
139  '/simulation/sensor_states',
140  '/real/sensor_states',
141 
142  '/real/commands',
143 
144  '/simulation/haptix_deka_controller/command',
145  '/real/haptix_deka_controller/command'
146  ]
147 
148 print "###"
149 # print params
150 
151 # print params, values
152 
153 src_topic = '/haptix_deka_controller/command'
154 dst_topic = '/haptix_deka_controller/command'
155 
156 # src_bag = 'exp-rand-01.bag'
157 src_bag = 'exp-contacts.bag'
158 dst_bag = 'test_sim.bag'
159 
160 # start = {'/haptix_deka_controller/follow_joint_trajectory/status/status_list[0]/status':1, '/haptix_deka_controller/follow_joint_trajectory/feedback/status/status':1}
161 # end = {'/haptix_deka_controller/follow_joint_trajectory/result':None, '/haptix_deka_controller/follow_joint_trajectory/result':None}
162 
163 start = {}
164 end = {}
165 
166 print "ready to start"
167 
168 # dyn = dynamic_tuner(params, values, src_bag, dst_bag, src_topic, dst_topic, objective = f, start_signal = start, end_signal = end)
169 
170 
171 print "starting..."
172 # dyn.tune_params()
173 
174 
175 bag = start_simulation(src_bag, dst_bag, src_topic, dst_topic, values = values)
176 
177 """
178 from dynamic_tuner import *
179 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})
180 
181 
182 from dynamic_tuner import *
183 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})
184 
185 
186 
187 sim_bag = rosbag.Bag('test_sim.bag', 'r')
188 bag = restamp(sim_bag, output_bag_file = 'sim-restamped.bag', prefix = SIM_PREFIX, start_signal = start_signal, end_signal = end_signal)
189 
190 
191 dyn._optimizer.optimize()
192 
193 dyn._optimizer.set_params_config({'/arm_controller/gains/elbow_joint/p':100001})
194 desc = dyn.get_params_desc(['/arm_controller/gains/elbow_joint/p', '/arm_controller/gains/elbow_joint/i', '/arm_controller/gains/wrist_3_joint/d'])
195 """
def plot(data, style='-')
Definition: contact_run.py:26
def plot_dual(config)
Definition: contact_run.py:16
def shift(x, y, i)
Definition: contact_run.py:51
def start_simulation(real_bag_file, sim_bag_file, src_topic, dst_topic, values=[], start_signal={}, end_signal={})
def error(x, y, axis=0)
Definition: contact_run.py:44
def find_best_error(x, y, offset, axis=0)
Definition: contact_run.py:61
def f(config, axis=0)
Definition: contact_run.py:81
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