[[Tutorial]]

Up:[[Tutorial]]     Previous:[[テスト実行]]     Next:[[人間型エージェントの操作]]
----
#contents

*Creating our first simulation :A falling virtual object . [#c6d40ee5]
*物体が落下する仮想空間の作成 [#c6d40ee5]
ここでは物体を落下させる動力学シミュレーションを行う新しい仮想空間を作成します。
**ディレクトリ構造 [#fea2aba6]
インストールしたパッケージのディレクトリ構造は以下のようになります。

In this simulation dynamics is added to a virtual object which causes it to fall down due to gravity . The virtual object used here is the model of penguin.

**Directory Structure  [#fea2aba6]

Here we represent the structure of the directories of the Sigverse package that is installed on your system .

sigverse-<version>
 sigverse-<version>
       |
       |---bin
       |---include
       |     +----sigverse       
       +---shared 
             +----sigverse
                     +----data
                     |     |----xml
                     |     +----shape
                     |----etc
                     |----jar
                     +----samples  
まず自分の好きな場所(たとえばホームディレクトリ)にディレクトリを作りそこを作業場所とします。
※このチュートリアルではsigverse-<version>/binに新しくディレクトリを作りそこを作業場所とします。

First step is to create a directory that will be your workspace.
**エージェントコントローラの作成 [#b9411488]
簡単なエージェントコントローラを作成します。
エージェントとは仮想世界で自律動作を行う物体のことを言います。
まずSIGVerseの sigverse-<version>/binに移動します。
 $ cd ~/sigverse-<version>/bin
ここに新しいディレクトリを作成します。

※In this tutorial , we set up our workspace in the directory  the sigverse-<version> / bin.

**Creating an agent controller [#b9411488]

***What is controller ? [#x6bb25d1]

This section has to be written.

***Create a simple agent controller. [#g8b3d053]

An agent is an object upon which the autonomous operation is performed in the virtual world. We create our agent controller in the workspace directory that will created in  sigverse-<version>/bin

 $ mkdir NewWorld 
 $ mkdir NewWorld
 $ cd NewWorld

Creating our agent controller named MoveController.cpp using some editor e.g Vi or Emacs . We chose emacs . 

emacsまたはviなどのエディタを使ってMoveController.cppファイルを作成します。
 $ emacs MoveController.cpp

MoveController.cpp
 
#highlight(cpp){{
 #include "Controller.h"
 #include "Logger.h"
 
 //Declare MoveController as a sublclass of Controller.

 //ControllerのサブクラスMoveControllerの宣言します。
 class MoveController : public Controller {
 public:
 
  // Declare "onAction" for the use of periodic processing

   //定期的な処理を行うonActionの利用を宣言します。
   double onAction(ActionEvent&);
 };
 
 
 double MoveController::onAction(ActionEvent &evt) {
   return 5.0;      // return the time , when onAction will be called next time
   return 5.0;      //次にonActionが呼ばれるまでの時間を返します。
 }
 
 // Return an instance of itself to Sigverse

 //自身のインスタンスをSIGVerseに返します。
 extern "C" Controller * createController() {
      return new MoveController;
 }
}}
※ファイル保存時にコードシステムを聞かれたら今後すべてutf-8を指定してください。

基本的にコントローラはContorollerクラスを継承して作成します。
これは何もしないコントローラです。5秒ごとに何もしない関数onActionが呼び出されます。

※ファイル保存時にコードシステムを聞かれたら今後すべてutf-8を指定してください。( could not translate )
**コンパイル [#hf0f4b09]
次に作成したコードのコンパイルを行うためのmakefileを作成します。
// emacs make.sh

Basically, the agent controller creates a class named "MoveController" by inheriting Contoroller class .  onAction function is called every 5 seconds does nothing. 
//make.sh
// #!/bin/sh
 
// #SIGVerseソースの場所を指定します。
// export SIG_SRC="/home/<username>/sigverse-201003/include/sigverse"

**Compiling [#hf0f4b09]
// #コンパイルを行います。
// make clean
// make

Create a makefile for compiling the code we have created.


 $ emacs Makefile

Makefile
 #Specifying the location of Sigverse header files
 #SIGVerseヘッダファイルの場所指定
 SIG_SRC  = /home/<username>/sigverse-<version>/include/sigverse
 
 #Specify the object file
 #オブジェクトファイルの指定
 OBJS     = MoveController.so
 
 all: $(OBJS)
 
 #Compile
 #コンパイルを行います。
 ./%.so: ./%.cpp
         g++ -DCONTROLLER -DNDEBUG -DUSE_ODE -DdDOUBLE -I$(SIG_SRC) -I$(SIG_SRC)/comm/controller  -fPIC -shared -o $@   $<

※<username> and 、<version> has to be adjusted to your username and sigverse you are using . To remind yourself the <version> of your Sigverse please look at the directory sigverse-<version> .
※<username>はユーザー名、<version>はバージョン番号に置き換えます。
コマンド行のg++の前のスペースはTabですので注意してください。

Note :  In front of 'g++ -DCONTROLLER' line please remove the whitespaces and insert a TAB. otherwise you may get the following error 

--missing separator (did you mean TAB instead of 8 spaces?).  Stop. 

Run make 

makeを実行します。
 $ make
MoveController.soが作成されていればコンパイル成功です。

On successful make MoveController.so file should be created , which we can be verfied using "ls" command .

 $ ls
 Makefile  MoveController.cpp  MoveController.so
 
**Creating a world file [#j3bf1bf8]
**世界ファイルの作成 [#j3bf1bf8]
次に仮想世界の設定を記述した世界ファイルを作成します。世界ファイルでは仮想世界に登場するエージェントの設定を行います。

*** What is a world file ? [#dc85d86d]

World file describes the configuration of the virtual world including the configuration of agent . We describe the configuration in XML format.

The location of world files is : sigverse-<version>/share/sigverse/data/xml

世界ファイルをsigverse-<version>/share/sigverse/data/xmlに作成します。
 $ cd ~/sigverse-<version>/share/sigverse/data/xml
 $ emacs NewWorld.xml
NewWorld.xml

#highlight(xml){{

<?xml version="1.0" encoding="utf8"?>
 <world name="myworld1">
 
 <!--Set Gravity-->
 <!--重力の設定-->
   <gravity x="0.0" y="-9.8" z="0.0"/>  
  
 <!--Create an instance of the agent seToy_D -->
 <!--エージェントToy_Dのインスタンス作成-->
   <instanciate class="seToy_D.xml">
 
 <!--Naming the agent as Toy_D -->
 <!--エージェント名-->
         <set-attr-value name="name" value="Toy_D"/>  
 
 <!--Specifying the language of agent controller as C++-->
 <!--C++言語の指定-->
         <set-attr-value name="language" value="c++"/> 
 
 <!--Specify the created Agent Controller and its location -->
 <!--作成したコントローラの指定-->
         <set-attr-value name="implementation"
 value="./NewWorld/MoveController.so"/>
 
 <!--Set the flag for Dynamics calculation as True-->
 <!--動力学演算フラグ-->
         <set-attr-value name="dynamics" value="true"/>
  
 <!--Set the initial position of the agent (x,y,z)-->
 <!--エージェントの最初の位置(x,y,z)-->
         <set-attr-value name="x" value="0.0"/>
         <set-attr-value name="y" value="18.0"/>
         <set-attr-value name="z" value="5.0"/>
 
 <!--specify the mass of the agent-->
 <!--エージェントの質量設定-->
         <set-attr-value name="mass" value="1.0"/>
 
  </instanciate>
</world>
}}


The world file reads thefile seToy_D.xml as an agent. Shape files of various agents and entities (x3d, wrl files) are placed in sigverse-<version>/share/sigverse/data/shapes. The xml file is prepared to use the shapes. We specify the programming language of the  agent controller and attached it to the agent in world file. The flag to simulate "dynamics" was set to "true" .
ここでは動力学シミュレーションを行うため"dynamics"を"true"に設定しました。また、エージェントとしてseToy_D.xmlというファイルを読み込むように設定しています。さまざまなエージェントやエンティティの形状ファイル(x3d,wrlファイル)やそれに付随したxmlファイルがあらかじめ用意されているので、それを用います。



Specifying the controller and its location within workspace directory
作成したコントローラの指定は以下のように設定しました。
#highlight(xml:firstline[18]){{
        <set-attr-value name="implementation" value="./NewWorld/MoveController.so"/>
}}
作成したコントローラのパスを指定するときはシグバースを実行する場所(sigserver.shがある場所)からの相対パス、または絶対パスを指定します。

The script sigserver.sh is used to run the simulation and it placed in sigverse-<version>/bin directory . Since our workspace directory "NewWorld" is located in sigverse-<version>/bin , we have written the relative path for the agent controller.    You may need to write the full path depending on the placement of your workspace directory.
これで準備は完了です。

**シミュレーション開始[#i190b28d]
それではSIGVerseを起動してみます。
sigverseを起動するにはシグバースインストール先の~/sigverse-<version>/binにある
シェルスクリプトsigserver.shを使って実行します。

**Starting the si[#i190b28d]
わざわざディレクトリを移動するのが面倒な場合は

The shell script sigserver.sh is located at sigverse-<version>/bin . You reduce the need of changing directories , we can add it to our path.

 $ export PATH=$PATH:/home/<username>/sigverse-<version>/bin
でシェルスクリプトがあるディレクトリのパスを通しておきます。
また、作業場所にこのファイルをコピーして使用することもできます。

Moving to the directory where execution is located

 $ cd ~/sigverse-<version>/bin
 $ ./sigserver.sh -w NewWorld.xml -p 9001
       :
       :
 [SYS]  waiting for connection...
 [SYS]  Controller attached to "Toy_D"
 [SYS]  127.0.0.1 connected
 [SYS]  Toy_D : dataport
 [SYS]  127.0.0.1 connected

この時-wオプションで作成した世界ファイルを指定します。(何も指定しないときはMyWorld.xmlが読み込まれます。)

Since the agent controller is specified in the xml file you just created, the agent "Toy_D" starts automatically with attached controller.  
xmlファイルの中で今回作成したコントローラを指定したので、SIGVerseの起動と同時に自動的にエージェント"Toy_D"にアタッチされました。
次にSIGViewerを起動して接続するホスト名とポート番号を入力し、Connect to SimServerボタンをクリックしてサーバに接続します。

Open Sigviewer and enter the host name and port number to connect to  the Simserver . Please use the button "Connect to SimServer" by single click.


#ref(toy_1.jpg)

エージェントToy_Dが世界ファイルで記述した位置(0.0, 18.0, 5.0)でスタンバイしている様子が確認できます。SIGVerseではy方向が高さになります。

World position for the agent Toy_D is described in the file as (0.0, 18.0, 5.0) . You can verify it . The height is in they direction  in SIGVerse.
次にSIM_CTRL_CMDの枠の中がSTARTとなっている状態でSendボタンを押してシミュレーションを開始します。

Start the simulation by using the framework of SIM_CTRL_CMD and the menu option START and press the button "Send" 
シミュレーションが開始し、エージェント(toy_D)が地面に落下して弾んでいるのが確認できます。

#ref(toy_2.jpg)
*エージェントの移動 [#z54adf9a]
次にエージェントに力を与えて移動させます。MoveController.cppを修正します。

The simulation starts, you can confirm that the agent (toy_D) falls to the ground and bounces.

*Move agents[#z54adf9a]

We make some changes in the agent controller MoveController.cpp so that agent can move upon application of force.

 $ cd NewWorld
 $ emacs MoveController.cpp

Add the following code in agent controller.
onActionの中に以下のコードを追加します。

MoveController.cpp

#highlight(cpp:firstline[13]){{
 double MoveController::onAction(ActionEvent &evt) {
   return 5.0;      //returns the time for onAction to be called next.
   return 5.0;      //次にonActionが呼ばれるまでの時間を返します。
 }
}}

     ↓
#highlight(cpp:firstline[13]){{
 double MoveController::onAction(ActionEvent &evt) {
   SimObj *obj = getObj(myname());  //obtaining handle to the agent
   obj->setForce(0,0,300);         //apply the force 300[N] in Z direction
   return 5.0;      //returns the time for onAction to be called next.
   SimObj *obj = getObj(myname());  //自分自身の取得
   obj->setForce(0,0,300);         //z軸方向に300[N]の力を加える
   return 5.0;      //次にonActionが呼ばれるまでの時間を返します。
 }
}}


Compile and Run


コンパイルして実行します。
 $ make
 $ cd ..
 $ ./sigserver.sh -w NewWorld.xml -p 9001

If you check SIGViewer, you can see agent moving in the z-direction where force is applied to the agent once every five seconds .
SIGViewerで確認すると、5秒に一回エージェントがz方向に力が加えられて、移動しているのがわかります。

#ref(toy_3.jpg)

It would be a fun exercise to use the following functions as well.
力を与える関数setForce以外にも速度を設定するsetVelocityやトルクを設定するsetTorqueなどもあるのでいろいろ試してみると面白いと思います。

--setForce
--setVelocity
--setTorque
*物理演算用の形状、大きさ、位置の設定 [#n6779cdb]
物理演算で用いるオブジェクトの形状は見た目の形状と異なり、sphere,cube, cylinderで近似されています。デフォルトでは見た目と大体同じ形状、大きさに設定されていますが、これらを設定ファイルで修正することができます。

*Physics for form, size and position settings [#n6779cdb]

We have several visual forms representing shapes of objects like sphere ,cube , cylinder etc . But the shape of the objects for the application of physics is approximated by shape of cylinder. The default is roughly the same shape and appearance, but the size is set, and  you can fix them in the configuration file.

**Fixed configuration file[#ofb6b204]

First, edit the configuration file of the object.

**設定ファイル修正 [#ofb6b204]
まず、オブジェクトの設定ファイルを編集します。
 $ cd ~/sigverse-<version>/share/sigverse/data/xml
 $ emacs seToy_D.xml

in seToy_D.xml

seToy_D.xmlの中の
  <body filename="dummy-body.xml"/>
の下に以下のいずれかを追加します。

please add under the following keywords

***box [#b90fa015]

#highlight(xml:nogutter){{
 <!--set the Box Shape。-->
 <!--形状をboxに設定します。-->
  <simpleShape type="box">
 <!--The position of the box shape is set。-->
 <!--物理演算形状の位置(見た目の形状からのずれ)を設定します。-->
    <position x="0" y="0" z="0"/>
 <!--The size of the box shape is set-->
 <!--物理演算用の形状のサイズを設定します。-->
    <size sx="10" sy="10" sz="10"/>
  </simpleShape>
}}
一辺が10の立方体に設定しました。



***sphere [#f0404294]
sphereに設定する場合は以下のように設定します。

setting the sphere shape below

#highlight(xml:nogutter){{
  <simpleShape type="sphere">
    <position x="0" y="0" z="0"/>
    <size r="10"/>
  </simpleShape>
}}
半径10の球に設定しました。


***cylinder [#z94a7687]

#highlight(xml:nogutter){{
  <simpleShape type="cylinder">
    <position x="0" y="0" z="0"/>
    <size r="3" h="10"/>
  </simpleShape>
}}
※2011/10時点ではシリンダ形状の代わりにカプセル型形状を作ります。

Radius of the bottom is 3 and height of the cylinder is 10.

**On run[#r537a2f8]
底面の円の半径3、円筒の高さ10のシリンダーに設定しました。

When you run the agent controllers ( first that makes agent fall and the second that applies force on agent to move ) , please please try to see the difference in behaviors .
**実行 [#r537a2f8]
再度シグバースを実行してエージェントが落下した時や、力を加えた時の動きの違いを確認してみてください。

*Units [#c72a1e70]
*単位系 [#c72a1e70]
SIGVerseでは単位系は角度(ラジアン)以外は特に決まっていませんが、このチュートリアルでは世界標準の国際(SI)単位系を用います。

The units in SIGVerse: angle (in radians . For all the quantities used in this tutorial world standard international (SI) system of units used.
|量|名称|記号|
|長さ|メートル|m|
|質量|キログラム|kg|
|時間|秒|s|
|角度|ラジアン|rad|
|速度|メートル毎秒|m/s|
|加速度|メートル毎秒毎秒|m/s^2|
|力|ニュートン|N|
|トルク|ニュートン・メートル|Nm|

|Amount|Name|Sign|
|Length|metres|m|
|Weight|kilogram|kg|
|Time|seconds|s|
|Angle|radians|rad|
|Velocity|meters per second|m/s|
|Acceleration|meters per second per second|m/s^2|
|Force|Newton|N|
|Torque|Newton meter|Nm|

Up:[[Tutorial]]     Previous:[[テスト実行]]     Next:[[人間型エージェントの操作]]

[[English version>Samples/Control of humanoid agent]]

#highlight(end)


Front page   New List of pages Search Recent changes   Help   RSS of recent changes