-
Notifications
You must be signed in to change notification settings - Fork 0
/
streaming.py
283 lines (235 loc) · 8.44 KB
/
streaming.py
1
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
277
278
279
280
281
282
283
import bpy
import roslibpy
import threading
from queue import Queue
from .utils import Singleton, props
from mathutils import Matrix, Euler, Quaternion
import math
# FROM ranimation_manager
# Single action names that match single bone
props['faceStreaming'] = False
props.add('robotIP', "Robot IP address", '10.0.0.10')
HEAD_BONES = {
'BrowOuterLeft' : 'brow.L',
'BrowInnerLeft' : 'brow.inner.L',
'BrowCenter' : 'brow.C',
'BrowInnerRight' : 'brow.inner.R',
'BrowOuterRight' : 'brow.R',
'UpperLidLeft' : 'eyelid_UP_L',
'UpperLidRight' : 'eyelid_UP_R',
'LowerLidLeft' : 'eyelid_LO_L',
'LowerLidRight' : 'eyelid_LO_R',
'CheekSquintLeft' : 'cheek_L',
'CheekSquintRight' : 'cheek_R',
'SneerLeft': 'nostril_R',
'SneerRight': 'nostril_L',
'UpperLipLeft' : 'lip_U_L',
'UpperLipCenter' : 'lip_U',
'UpperLipRight' : 'lip_U_R',
'LowerLipLeft' : 'lip_D_L',
'LowerLipCenter' : 'lip_D',
'LowerLipRight' : 'lip_D_R',
'LipsStretchLeft' : 'mouth_C_L',
'LipsStretchRight': 'mouth_C_R',
'FrownLeft' : 'mouth_D_L',
'FrownRight' : 'mouth_D_R',
'SmileLeft' : 'mouth_U_L',
'SmileRight' : 'mouth_U_R',
'Jaw' : 'chin',
}
class ROSManager(metaclass=Singleton):
def __init__(self):
self.command_queue = Queue()
self.publishers = {}
self.ros = None
self.thread = threading.Thread(target=self.worker_thread, daemon=True)
self.thread.start()
def connect(self, url, publishers):
self.command_queue.put(('connect', (url, publishers)))
def publish(self, topic_name, data):
self.command_queue.put(('publish', (topic_name, data)))
def disconnect(self):
self.command_queue.put(('disconnect', None))
def is_connected(self):
return self.ros and self.ros.is_connected
def worker_thread(self):
while True:
command, args = self.command_queue.get()
if command == 'connect':
self._connect(*args)
elif command == 'publish':
self._publish(*args)
elif command == 'disconnect':
self._disconnect()
def _connect(self, url, publishers):
self.ros = roslibpy.Ros(url)
self.ros.run()
for pub in publishers:
topic_type, topic = publishers[pub]
self.publishers[pub] = roslibpy.Topic(self.ros, topic, topic_type)
def _publish(self, topic_name, data):
if topic_name in self.publishers:
self.publishers[topic_name].publish(roslibpy.Message(data))
def _disconnect(self):
try:
self.ros.close()
except Exception:
pass
class ExternalStream(metaclass=Singleton):
def __init__(self):
self.streaming = False
self.bones = bpy.data.objects['deform'].pose.bones
def get_pose(self):
pose = {}
for key, bone in HEAD_BONES.items():
# Default case
j = 1
if key == 'Jaw':
j = 2
pose[key] = min(1, max(-1,(self.bones[bone].location[j] * 25)))
head = self.getHeadData()
eyes = self.getEyesData()
neck = self.getNeckData()
print(neck)
pose.update(head)
pose.update(eyes)
pose.update(neck)
return pose
'''
These are copied from head data export functions in roscom package
'''
def getHeadData(self):
bones = self.bones
rhead = bones['DEF-head'].matrix @ Matrix.Rotation(math.pi/2, 4, 'X')
rneck = bones['DEF-neck'].matrix @ Matrix.Rotation(-math.pi/2, 4, 'X')
rneck.invert()
# I think this is the correct order for the neck rotations.
q = (rneck @ rhead).to_euler('XYZ')
# q = (rhead * rneck).to_quaternion()
return {
'HeadYaw': -q.z,
'HeadPitch': -q.x,
'HeadRoll': -q.y
}
# Same as head, but for the lower neck joint.
def getNeckData(self):
# Unused, as API only support HEAD joint
bones = self.bones
rneck = bones['DEF-neck'].matrix @ Matrix.Rotation(-math.pi/2, 4, 'X')
q = rneck.to_euler('XYZ')
return {
# 'NeckYaw': q.z, Not used
'NeckPitch': q.x,
'NeckRoll': q.y
}
# Gets Eye rotation angles:
# Pitch: down(negative) to up(positive)
# Yaw: left (negative) to right(positive)
def getEyesData(self):
bones = self.bones
head = (bones['DEF-head'].id_data.matrix_world @ bones['DEF-head'].matrix @ Matrix.Rotation(-math.pi/2, 4, 'X')).to_euler()
leye = bones['eye.L'].matrix.to_euler()
reye = bones['eye.R'].matrix.to_euler()
# Relative to head. Head angles are inversed.
leye_p = leye.x + head.x
leye_y = math.pi - leye.z if leye.z >= 0 else -(math.pi+leye.z)
reye_p = reye.x + head.x
reye_y = math.pi - reye.z if reye.z >= 0 else -(math.pi+reye.z)
# Add head target
leye_y += head.z
reye_y += head.z
# Current API do not support separate eye movements
return {
'EyesYaw': leye_y,
'EyesPitch': leye_p,
}
class FaceResetPoseButtonOperator(bpy.types.Operator):
bl_idname = "scene.face_reset_pose"
bl_label = "Reset Robot Pose"
def execute(self, context):
pose = ExternalStream().get_pose()
# Send an empty pose
tp = {
'names': list(pose.keys()),
'values': [0]*len(pose.keys())
}
ROSManager().publish('pose', tp)
return{'FINISHED'}
class FaceStopStreamButtonOperator(bpy.types.Operator):
bl_idname = "scene.face_stop_stream"
bl_label = "Stop Streaming"
def execute(self, context):
ExternalStream().streaming = False
return{'FINISHED'}
class FaceStreamPanel(bpy.types.Panel):
bl_space_type = 'VIEW_3D'
bl_region_type = 'UI'
bl_category = 'Sophia'
bl_idname = "VIEW3D_PT_face_stream_panel"
bl_label = "Face Stream"
def draw(self, context):
layout = self.layout
row = layout.row()
row.prop(bpy.context.scene, "robotIP")
row = layout.row()
if ROSManager().is_connected():
row.operator(FaceDisconnectROS.bl_idname)
else:
row.operator(FaceConnectROS.bl_idname)
if ROSManager().is_connected():
row.operator(FaceResetPoseButtonOperator.bl_idname)
row = layout.row()
if not ExternalStream().streaming:
row.operator(FaceStartStreamButtonOperator.bl_idname)
else:
row.operator(FaceStopStreamButtonOperator.bl_idname)
class FaceStartStreamButtonOperator(bpy.types.Operator):
bl_idname = "scene.face_start_stream"
bl_label = "Start Streaming to Robot"
def execute(self, context):
ExternalStream().streaming = True
if not props['faceStreaming']:
bpy.ops.wm.face_live_update_pose()
return {'FINISHED'}
class FaceConnectROS(bpy.types.Operator):
bl_idname = "scene.face_connect_ros"
bl_label = "Connect to robot"
def execute(self, context):
ROSManager().connect(f"ws://{context.scene.robotIP}:9090", {
'pose': ('hr_msgs/TargetPosture', '/hr/animation/set_state')
})
return {'FINISHED'}
class FaceDisconnectROS(bpy.types.Operator):
bl_idname = "scene.face_disconnect_ros"
bl_label = "Disconnect from robot"
def execute(self, context):
ROSManager().disconnect()
return {'FINISHED'}
class FaceStreaming(bpy.types.Operator):
""" Face Streaming callback on timer """
bl_label = "Start Face Stream"
bl_idname = 'wm.face_live_update_pose'
def modal(self, context, event):
if event.type == 'TIMER':
if ExternalStream().streaming:
pose = ExternalStream().get_pose()
# Send pose
tp = {
'names': list(pose.keys()),
'values': list(pose.values())
}
ROSManager().publish('pose', tp)
return {'PASS_THROUGH'}
def execute(self, context):
print("START STREAMING")
wm = context.window_manager
wm.modal_handler_add(self)
if not props['faceGlobalTimerStarted']:
bpy.ops.wm.face_global_timer()
props['faceStreaming'] = True
return {'RUNNING_MODAL'}
def cancel(self, context):
return {'CANCELLED'}
@classmethod
def poll(cls, context):
return True