from controller import Motion

  • Motion
  • Motion Functions
  • new() & delete()
  • play(), stop(), setLoop(), setReverse()
  • isOver(), getDuration(), getTime(), setTime()
  • Action Editor


Motion

这篇文章相当于官方Reference Manual 的Motion函数部分的翻译

Motion Functions

  • new()
  • delete()
  • play()
  • stop()
  • setLoop()
  • setReverse()
  • isOver()
  • getDuration()
  • getTime()
  • setTime()

new() & delete()

获取并释放运动文件。

from controller import Motion

class Motion:
	def __init__(self, filename):
	def __del__(self):
	def isValid(self):

使用motion = Motion('filename')语句可以读取filename指定的.motion文件。
如果可以读取文件,语法正确并包含至少一个姿势和一个关节位置,则该函数返回一个 WbMotionRef ,可用作Motion中其他函数的参数。
在停止模式下创建动作,必须调用play()函数才能开始播放。
在python中,可以调用isValid()函数在构造函数之后检测错误情况。如果isValid()函数返回false,则Motion应显式删除对象。(读取文件错误,没有读到动作,所以要删除这个motion)
例如:

motion = Motion('filename')
if not motion.isValid():
	print('could not load file:', filename)
	motion.__del__()

play(), stop(), setLoop(), setReverse()

from controller import Motion

class Motion:
    def play(self):
    def stop(self):
    def setLoop(self, loop):
    def setReverse(self, reverse):

play()函数开始播放指定的动作。当robot.step()函数启动时,play()函数也被激活。如果要播放文件并等待其终止,可以:

def motionPlaySync(WbMotionRef):
	motion.play()
	while True:
		robot.step(timestep)
		if motion.isOver():
			break

同一机器人可以同时播放多个运动文件,但是,如果两个运动文件具有共同的关节,则行为是不确定的。
stop()功能可以中断动作的播放,此时机器人会保留当前的位置。中断后,可以调用play()函数恢复播放。
setLoop()函数指定运动的循环模式。如果循环模式为True,机器人会重复运动。
setReverse()函数指定运动的反向模式。如果反向模式为True,则运动文件向后播放。当motion在播放时,反向模式可以改变,机器人会从当前的位置反向运动。
默认情况下,循环模式和反向模式都为False

isOver(), getDuration(), getTime(), setTime()

from controller import Motion

class Motion:
    def isOver(self):
    def getDuration(self):
    def getTime(self):
    def setTime(self, time):

当播放位置到达运动文件的末尾时,isOver()函数返回True,这意味着最后的动作被setPosition()函数送到了Motor节点。但是此时机器人的运动可能并未到达这个位置,可能被卡住了。
当机器人处于循环模式时,这个函数总是返回False。当机器人处于反向模式时,isOver()函数返回True,此时播放位置正在文件的开头。
getDuration()函数以毫秒为单位返回运动文件的总持续时间。
getTime()函数以毫秒为单位返回当前播放位置。
setTime()函数允许及时更改播放位置。允许向前后者向后跳过。不论是正在播放还是停止播放,都可以更改时间位置。最小值为0(运动开始),最大值为getDuration()返回的值(运动结束)。以毫秒为单位。

Action Editor

可以双击在运动过程中的机器人,会弹出robot window。在这个窗口中有一个action editor选项卡,可以打开.motion文件,并让机器人跟随运动。
也可以使用这个功能创建.motion文件,新增动作。但是要比较熟悉每一个Motor节点的名称和角度。