LoginSignup
11
12

More than 5 years have passed since last update.

Choregraphe標準のボックスを拡張する その1

Last updated at Posted at 2015-06-30

概要

Choregraphe標準のボックスには便利なものが多いですが、
ユースケースによってはそのままでは使えない場合が多々あります。
この記事では実際によく使うボックスの拡張を使用する例とともに紹介していきます。

リポジトリ

ここで紹介しているボックスと以前紹介したRandom Outボックスは
こちらのリポジトリにcblをあげています。

【PEPPER】InputしたらRandomでOutputするPythonBoxをつくる  http://qiita.com/hws-hitorobo/items/0a0a57199c24a25a602f

1. 値付きのWait

値を出力するボックスの後にWaitを入れたいときや、
値を入力するボックスの前にWaitを入れたいときなどに使用します。

class MyClass(GeneratedClass):
    def __init__(self):
        GeneratedClass.__init__(self, False)

    def onLoad(self):
        self.waiting = None
        self.p = None

    def onUnload(self):
        self.cancelWaiting()

    def triggerOutput(self):
        self.timerOutput()

    def cancelWaiting(self):
        if self.waiting:
            self.waiting.cancel()
        self.waiting = None

    def onInput_onStart(self, p):
        self.cancelWaiting()
        self.p = p

        import qi
        self.waiting = qi.async(self.triggerOutput, delay=int(self.getParameter("Timeout (s)") * 1000 * 1000))

    def onInput_onStop(self):
        if self.getParameter("Trigger timerOutput if cancelled") and self.waiting and self.waiting.isRunning():
            self.timerOutput(self.p)
        self.onUnload()

標準のWaitの入出力の型をダイナミックに設定して、
入力で入ってきた値をself.pに保存して、出力時にself.pを渡します。

ディレイでも同様です。

ちなみにWaitとDelayの違いですが、
WaitはInputごとにリセットされ、
DelayはInputの回数分Outputします。

2. 値付きOnly Once

ALMemoryからのイベントを排他制御するときなどに
よく使うOnly Onceですが、イベントの引数をそのまま次のボックスに渡したいときなどに重宝します。

class MyClass(GeneratedClass):
    def __init__(self):
        GeneratedClass.__init__(self, False)

    def onLoad(self):
        self.bFirstTime = True

    def onUnload(self):
        self.bFirstTime = True

    def onInput_onSignal(self, p):
        if( self.bFirstTime ):
            self.bFirstTime = False
            self.firstTime(p)

同じく入力と出力をダイナミックに変更します。

WaitやDelayと違いInputのメソッド内で完結するので、
プロパティを持つ必要がないです。

3. 値付きWait For Signals

上記の2つのケースの複数ボックスの処理を待機するバージョンです。

class MyClass(GeneratedClass):
    def __init__(self):
        GeneratedClass.__init__(self, False)

    def onLoad(self):
        self.ok = [False]*2
        self.p  = None

    def onUnload(self):
        #puts code for box cleanup here
        ""

    def onStart(self, nInput):
        self.ok[nInput-1] = True
        bOutput = True
        for bOk in self.ok:
            bOutput = bOutput and bOk
        if( bOutput ):
            self.ok = [False]*2
            self.signalsReceived(self.p)

    def onInput_signal1(self, p):
        self.p = p
        self.onStart(1)

    def onInput_signal2(self):
        self.onStart(2)

Wait For Signalsの場合もWaitと同じように一度self.pにsignal1の値を保存しています。


長くなってきたので、その2につづく

その2 http://qiita.com/hws-hitorobo/items/fcd72e3958cd59457f7b

11
12
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
11
12