53 """Read the entire file. 63 """Read the motion samples. 72 raise SyntaxError(
"Syntax error in line %d: 'MOTION' expected, got '%s' instead"%(self.
linenr, tok))
77 raise SyntaxError(
"Syntax error in line %d: 'Frames:' expected, got '%s' instead"%(self.
linenr, tok))
84 raise SyntaxError(
"Syntax error in line %d: 'Frame Time:' expected, got '%s' instead"%(self.
linenr, tok))
87 raise SyntaxError(
"Syntax error in line %d: 'Frame Time:' expected, got 'Frame %s' instead"%(self.
linenr, tok))
94 for i
in range(frames):
98 raise SyntaxError(
"Syntax error in line %d: %d float values expected, got %d instead"%(self.
linenr, self.
_numchannels, len(a)))
99 values = map(
lambda x: float(x), a)
105 """Read the skeleton hierarchy. 109 raise SyntaxError(
"Syntax error in line %d: 'HIERARCHY' expected, got '%s' instead"%(self.
linenr, tok))
113 raise SyntaxError(
"Syntax error in line %d: 'ROOT' expected, got '%s' instead"%(self.
linenr, tok))
116 self._nodestack.append(self.
_root)
121 """Read the data for a node. 131 raise SyntaxError(
"Syntax error in line %d: '{' expected, got '%s' instead"%(self.
linenr, tok))
140 elif tok==
"CHANNELS":
145 if tok
not in [
"Xposition",
"Yposition",
"Zposition",
146 "Xrotation",
"Yrotation",
"Zrotation"]:
147 raise SyntaxError(
"Syntax error in line %d: Invalid channel name: '%s'"%(self.
linenr, tok))
154 self._nodestack.append(node)
159 self._nodestack.append(node)
164 self._nodestack.pop()
167 raise SyntaxError(
"Syntax error in line %d: Unknown keyword '%s'"%(self.
linenr, tok))
172 """Return the next token which must be an int. 179 raise SyntaxError(
"Syntax error in line %d: Integer expected, got '%s' instead"%(self.
linenr, tok))
183 """Return the next token which must be a float. 190 raise SyntaxError(
"Syntax error in line %d: Float expected, got '%s' instead"%(self.
linenr, tok))
194 """Return the next token.""" 209 """Return the next line. 211 Empty lines are skipped. If the end of the file has been 212 reached, a StopIteration exception is thrown. The return 213 value is the next line containing data (this will never be an 221 s = self.fhandle.readline()
229 """Populate the token list from the content of s. 237 BVHReader.__init__(self, filename)
238 self.
br = tf.TransformBroadcaster()
257 self.all_motions.append(values)
263 num_channels = len(root.channels)
268 mat_rot = numpy.array([ [1.,0.,0.,0.],
273 for channel
in root.channels:
275 if(channel ==
"Xposition"):
278 elif(channel ==
"Yposition"):
281 elif(channel ==
"Zposition"):
284 elif(channel ==
"Xrotation"):
287 theta = math.radians(xrot)
290 mat_x_rot = numpy.array([ [1.,0.,0.,0.],
294 mat_rot = numpy.matmul(mat_rot, mat_x_rot)
296 elif(channel ==
"Yrotation"):
299 theta = math.radians(yrot)
302 mat_y_rot = numpy.array([ [ c,0., s,0.],
306 mat_rot = numpy.matmul(mat_rot, mat_y_rot)
308 elif(channel ==
"Zrotation"):
311 theta = math.radians(zrot)
314 mat_z_rot = numpy.array([ [ c,-s,0.,0.],
318 mat_rot = numpy.matmul(mat_rot, mat_z_rot)
332 temp_rot = tf.transformations.quaternion_from_matrix(mat_rot)
334 self.br.sendTransform(temp_trans, temp_rot, rospy.Time.now(), root.name, parent_frame)
336 for each_child
in root.children:
341 rate = rospy.Rate(1/self.
dt)
343 while not rospy.is_shutdown():
349 if rospy.is_shutdown():
356 parser = argparse.ArgumentParser(
"python BVHBroadcaster.py")
357 parser.add_argument(
'bvh_file', help=
"A path to bvh file that you want to broadcast")
358 parser.add_argument(
'base_frame', help=
"An existing frame in rviz on which the skeleton will be loaded")
359 parser.add_argument(
'-n',
'--name', help=
"Node name, default: BVHBroadcaster", default=
"BVHBroadcaster")
360 parser.add_argument(
'-l',
'--loop', help=
"Loop broadcasting", action=
"store_true")
361 return parser.parse_args()
364 rospy.init_node(args.name)
367 rospy.loginfo(
"Broadcasting bvh file (%s) on frame %s"%(args.bvh_file, args.base_frame))
369 rospy.loginfo(
"Loop")
371 rospy.loginfo(
"Only once")
372 bvh_test.broadcast(loop=args.loop)
373 rospy.loginfo(
"Broadcasting done")
376 rospy.init_node(
"BVHBroadcaster")
377 file_name =
"/home/mingfei/Documents/projects/RobotManipulationProject/mocap/62/62_07.bvh" 379 bvh_test.broadcast(loop=
True)
381 if __name__ ==
"__main__":
def broadcast(self, loop=False)
def onHierarchy(self, root)
def __init__(self, filename, root_frame)
def onMotion(self, frames, dt)
def onFrame(self, values)
def onMotion(self, frames, dt)
def onHierarchy(self, root)
def __init__(self, filename)
def onFrame(self, values)
def __init__(self, root=False)
def createTokens(self, s)
def broadcastRootJoint(self, root, parent_frame)