demos.py 8.17 KB
Newer Older
Emmanuel PIGNAT's avatar
Emmanuel PIGNAT committed
1
#!/usr/bin/env python2.7
Emmanuel PIGNAT's avatar
Emmanuel PIGNAT committed
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
import numpy as np
import os
import matplotlib.pyplot as plt
from pbdlib.gui import Interactive
from termcolor import colored
import tkinter as tk
from tkFileDialog import asksaveasfilename
from matplotlib import gridspec
import pbdlib as pbd

class Robot(object):
	def __init__(self, T):
		self.x, self.dx, self.dt = np.array([0., 0.]), np.array([0., 0.]), 1. / T
		self.ddx, self.fsx, self.fx = np.array([0., 0.]), np.array([0.]), np.array([0., 0.])
		self.sensor_mode = 0

class InteractiveDemos(Interactive, Robot):
	"""
	GUI for recording demonstrations in 2D
	"""
	def	__init__(self, filename='test', path='', **kwargs):
		Interactive.__init__(self)

		self.path = os.path.dirname(pbd.__file__) + '/data/gui/' if path == '' else path
		
		self.fig = plt.figure(figsize=(15, 8), facecolor='white')
		self.bindings.update({
			'q': (self.save_demos, [], "save demos"),
			'c': (self.clear_demos, [], "clear demos"),
			'x': (self.clear_demos, [True], "clear last demos"),
			'i': ([self.incr_param, self.highlight_demos], [['current_demo'], []], "next demos"),
			'd': (self.clear_demos, [False, True], "clear selected demos"),
		})

		Robot.__init__(self, self.simulation_T)

		gs = gridspec.GridSpec(1, 2)

		self.filename = filename

		self.ax_x = plt.subplot(gs[0])
		self.ax_dx = plt.subplot(gs[1])

		self.set_events()
		self.set_plots()

		self.is_demonstrating = False
		self.velocity_mode = False

		self.curr_demo, self.curr_demo_dx = [], []
		self.curr_demo_obj, self.curr_demo_obj_dx = [], []

		self._current_demo = {'x': [], 'dx': []}

		self.curr_mouse_pos = None
		self.robot_pos = np.zeros(2)

		self.nb_demos = 0
		self.demos = {'x': [], 'dx': []}

		self.params.update({'current_demo': [0, 0, self.nb_demos]})

		self.loaded = False
		#
		# win = plt.gcf().canvas.manager.window
		#
		# win.lift()
		# win.attributes("-topmost", True)
		# win.attributes("-alpha", 0.4)


		try:
			self.demos = np.load(self.path + filename + '.npy')[()]
			self.nb_demos = self.demos['x'].__len__(); self.params['current_demo'][2] = self.nb_demos - 1
			print colored('Existing skill, demos loaded', 'green')
			self.replot_demos()
			self.fig.canvas.draw()
			self.loaded = True
		except:
			self.demos = {'x': [], 'dx': []}
			print colored('Not existing skill', 'red')

	def highlight_demos(self):
		data = self.demos['x'][self.params['current_demo'][0]]
		self.plots['current_demo'].set_data(data[:, 0], data[:, 1] )
		self.fig.canvas.draw()

	def plot_sensor_value(self, s, scale=1.):
		data = np.vstack([self.x + np.array([0., 1.]) * s * scale, self.x])
		data -= 5. * np.array([0., 1.])[None]

		self.plots['sensor_value'].set_data(data[:, 0], data[:, 1])

	def set_plots(self):
		self.plots.update({
			'robot_plot':self.ax_x.plot([], [], 'o-', mew=4, mec='orangered', ms=10, mfc='w')[0],
			'sensor_value':self.ax_x.plot([], [],ls=(0,(1,1)), lw=10)[0],
			'attractor_plot':self.ax_x.plot([], [], 'o-', mew=4, mec='teal', ms=10, mfc='w')[0],
			'obj_plot':self.ax_x.plot([], [], 'o-', mew=4, mec='steelblue', ms=10, mfc='w')[0],
			'current_demo': self.ax_x.plot([], [], lw=3, ls='--', color='orangered')[0],
			'current_demo_dx': self.ax_dx.plot([], [], lw=3, ls='--', color='orangered')[0]
		})

		for ax, lim in zip([self.ax_x, self.ax_dx], [100, 25]):
			ax.set_xlim([-lim, lim])
			ax.set_ylim([-lim, lim])

	def sim_dynamics(self, ffx, n_steps=10):
		if not self.velocity_mode:
			m = 1.0

			ddx = ffx/m
			self.x += self.dt / n_steps * self.dx + 0.5 * self.ddx * (
																	 self.dt / n_steps) ** 2
			self.dx += self.dt / n_steps * 0.5 * (self.ddx + ddx)
			self.dxx = np.copy(ddx)
		else:
			kp = 0.;
			kv = kp ** 0.5 * 2
			for i in range(50):
				ddx = kp * (self.curr_mouse_pos - self.dx)
				self.dx += self.dt * ddx
				self.x += self.dt * self.dx + (self.dt ** 2) / 2. * ddx


	def timer_event(self, event):
		if self.is_demonstrating:
			if self.curr_mouse_pos is None: self.pretty_print('Outside'); return

			# print self.x, self.dx
			kp = 400.;
			kv = kp ** 0.5 * 2

			n_steps = 10
			for i in range(n_steps):
				ffx = kp * (self.curr_mouse_pos - self.x) - kv * self.dx
				self.sim_dynamics(ffx)


			# self.curr_demo += [np.copy(self.x)]; self.curr_demo_dx += [np.copy(self.dx)]

			self._current_demo['x'] += [np.copy(self.x)]
			self._current_demo['dx'] += [np.copy(self.dx)]

	def move_event(self, event):
		self.curr_mouse_pos = None if None in [event.xdata, event.ydata] else np.array([event.xdata, event.ydata])

		if event.key == 'shift' or self.is_demonstrating:
			self.robot_pos = np.copy(self.curr_mouse_pos)

			if not self.is_demonstrating:
				self.plots['robot_plot'].set_data(self.robot_pos[0], self.robot_pos[1])
				self.fig.canvas.draw()

	def plot_timer_event(self, event):
		self.robot_pos = self.curr_mouse_pos if self.robot_pos is None else self.robot_pos
		self.plots['attractor_plot'].set_data(self.robot_pos[0], self.robot_pos[1])
		self.plots['robot_plot'].set_data(self.x[0], self.x[1])

		if self.is_demonstrating:
			curr_demo_arr = np.array(self._current_demo['x'])
			curr_demo_dx_arr = np.array(self._current_demo['dx'])

			self.plots['current_demo'].set_data(curr_demo_arr[:, 0],
												curr_demo_arr[:, 1])

			self.plots['current_demo_dx'].set_data(curr_demo_dx_arr[:, 0],
												curr_demo_dx_arr[:, 1])
			self.fig.canvas.draw()

	def click_event(self, event):
		if event.key is None:
			self.pretty_print('Demonstration started')
			self.velocity_mode = event.inaxes == self.ax_dx
			self.is_demonstrating = True
			if not self.velocity_mode:
				self.x = self.curr_mouse_pos
			else:

				self.x = self.demos['x'][-1][0] if self.nb_demos > 0 else np.array([0., 0.])
				self.dx = self.curr_mouse_pos


			[t.start() for t in [self.timer, self.plot_timer]]

	def release_event(self, event):
		if event.key is None:
			self.pretty_print('Demonstration finished')
			self.is_demonstrating = False
			self.finish_demo()

			[t.stop() for t in [self.timer, self.plot_timer]]

	def replot_demos(self):
		for i in range(self.nb_demos):
			data = self.demos['x'][i]
			self.plots['demo_%d' % i] = \
			self.ax_x.plot(data[:, 0], data[:, 1], lw=2, ls='--')[0]
			data = self.demos['dx'][i]
			self.plots['demo_dx_%d' % i] = \
			self.ax_dx.plot(data[:, 0], data[:, 1], lw=2, ls='--')[0]

	def clear_demos(self, last=False, selected=False):
		"""
		:param last: 	 [bool]
			Delete only last one
		"""
		if last or selected:
			idx = -1 if last else self.params['current_demo'][0]

			for s in self.demos:
				self.demos[s].pop(idx)

			for i in range(self.nb_demos):
				self.plots['demo_%d' % (i)].remove()
				self.plots['demo_dx_%d' % (i)].remove()

			self.nb_demos = len(self.demos['x']); self.params['current_demo'][2] = self.nb_demos - 1

			self.replot_demos()

			if selected:
				self.plots['current_demo'].set_data([], [])

			self.fig.canvas.draw()
		else:
			for i in range(self.nb_demos):
				self.plots['demo_%d' % i].remove()
				self.plots['demo_dx_%d' % i].remove()

			self.fig.canvas.draw()

			for s in self.demos:
				self.demos[s] = []
			self.nb_demos = 0; self.params['current_demo'][2] = self.nb_demos - 1


	def finish_demo(self):
		"""
		Called when finishing a demonstration to store the data
		:return:
		"""
		curr_demo_arr = np.array(self._current_demo['x'])
		curr_demo_dx_arr = np.array(self._current_demo['dx'])

		# self.demos['x'] += [curr_demo_arr]; self.demos['dx'] += [curr_demo_dx_arr]
		# self.curr_demo = []; self.curr_demo_dx = []

		for s in self._current_demo:
			self.demos[s] += [np.array(self._current_demo[s])]
			self._current_demo[s] = []

		self.plots['current_demo'].set_data([], []); self.plots['current_demo_dx'].set_data([], [])
		self.plots['demo_%d' % self.nb_demos] = self.ax_x.plot(curr_demo_arr[:, 0], curr_demo_arr[:, 1], lw=2, ls='--')[0]
		self.plots['demo_dx_%d' % self.nb_demos] = self.ax_dx.plot(curr_demo_dx_arr[:, 0], curr_demo_dx_arr[:, 1], lw=2, ls='--')[0]

		self.nb_demos += 1; self.params['current_demo'][2] = self.nb_demos - 1

		self.fig.canvas.draw()


	def save_demos(self):
		"""
		Saving demonstrations with filename prompt
		:return:
		"""
		root = tk.Tk(); root.withdraw()

		file_path = asksaveasfilename(initialdir=self.path, initialfile=self.filename + '.npy')

		self.pretty_print("Demonstrations saved as\n "+ file_path)

		np.save(file_path, self.demos)

		pass