LoginSignup
11
13

More than 5 years have passed since last update.

Pepper開発に向けて、Pythonを理解する。-Pythonボックス自作関数編-

Posted at

前書き

PepperやNAOの開発をするにあたって、Pythonの知識は必須といえるでしょう。
そこで、前回に引き続きPythonボックス、特に関数を自作して使うことができるようになることを目標にします。
また、Choreographeを扱う方でpythonをもともと使っていた方とあまり会ったこと無いので、そのような方でもpythonボックスを改良できるように説明を書きたいです。

開発環境

機種名 : MacBook Pro
OS : Yosemite 10.10.3
プロセッサ名 : Intel Core i5 2.6 GHz
メモリ : 16 GB
グラフィック : Intel Iris 1536MB
Choreographe : 2.3.1

python2.7

本題

サクッと本題に行きましょう。

前提

基本的に、プロジェクトは以下のとおりです。

スクリーンショット 2015-07-17 16.37.53.png

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

    def onLoad(self):
        #put initialization code here
        pass

    def onUnload(self):
        #put clean-up code here
        pass

    def onInput_onStart(self):
        #アウトプットが必要なので、self.onStopped()を有効にする
        self.onStopped() #activate the output of the box
        #pass #必要ないのでコメントアウト

    def onInput_onStop(self):
        self.onUnload() #it is recommended to reuse the clean-up as the box is stopped
        self.onStopped() #activate the output of the box

ここからいじり始めていきます。

関数

引数なし関数作成

まずは簡単な関数を作ってみます。

def testFunc(self):
    pass

このような感じでいいかと思います。

def testFunc(self):

これは、僕の中では引数なし関数と呼んでいます。
self変数はどの関数でも渡される変数なので必ず書きましょう。

pass

これは空っぽの関数を示すものです。

引数なし関数呼び出し

def onInput_onStart(self):
    self.testFunc() #このように自クラスの◯◯というメソッドという指定の仕方をします。
    self.onStopped() #activate the output of the box

引数あり関数作成

まずは簡単な関数を作ってみます。

def testFunc2(self,a,b):
    c=a*b
    self.logger.info(c)

このような感じでいいかと思います。

def testFunc(self):

これは、僕の中では引数なし関数と呼んでいます。
self変数はどの関数でも渡される変数なので必ず書きましょう。

引数あり関数呼び出し

def onInput_onStart(self):
    self.testFunc2(10,20) #このように自クラスの◯◯というメソッドという指定の仕方をします。
    self.onStopped() #activate the output of the box

出力結果

200

感想

ひとまず、この辺りで終了します。

11
13
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
13