// The person in charge of this page is Raghav
#contents

*Creating our first simulation :A falling virtual object . [#c6d40ee5]

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 .
Here we represent the structure of the directories of the SIGVerse package that is installed on your system .

sigverse-<version>
       |
       |---bin
       |---include
       |     +----sigverse       
       +---shared 
             +----sigverse
                     +----data
                     |     |----xml
                     |     +----shape
                     |----etc
                     |----jar
                     +----samples  

First step is to create a directory that will be your workspace.
The above directories have important files as core system. Please do not edit the files on the above tree structure.

※In this tutorial , we set up our workspace in the directory  the sigverse-<version> / bin.
First step is to create a directory that will be your working space.

**Creating an agent controller [#b9411488]

***What is controller ? [#x6bb25d1]
** Creating your working space [#v539ae53]

This section has to be written.
A workspace directory will be created by the following command named sigcreate.sh

***Create a simple agent controller. [#g8b3d053]
 $ cd ~/ 
 $ sigcreate.sh MyWorld

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
Makefile, controller sample file, world file and so on will be created in this working directory.

 $ mkdir NewWorld 
 $ cd NewWorld
*** Controller for entity [#k3be7ece]

Creating our agent controller named MoveController.cpp using some editor e.g Vi or Emacs . We chose emacs . 
A controller is one of the main programs which should be written by SIGVerse users.
This controller determines the behavior of agents, robots and objects in the virtual world.
These 'objects' are called as entities in the SIGVerse.

 $ emacs MoveController.cpp
Let's see the controller sample.

MoveController.cpp
 $ cd MyWorld 
 $ emacs ControllerSample.cpp

ControllerSample.cpp
 
#highlight(cpp){{
 #include "Controller.h"
 #include "Logger.h"
 
 //Declare MoveController as a sublclass of Controller.
#include "ControllerEvent.h"  
#include "Controller.h"  
#include "Logger.h"  
  
class MyController : public Controller {  
public:  
  void onInit(InitEvent &evt);  
  double onAction(ActionEvent&);  
  void onRecvMsg(RecvMsgEvent &evt); 
  void onCollision(CollisionEvent &evt); 
};  
  
void MyController::onInit(InitEvent &evt) {  
}  
  
double MyController::onAction(ActionEvent &evt) {  
  return 1.0;      
}  
  
void MyController::onRecvMsg(RecvMsgEvent &evt) {  
}  

 class MoveController : public Controller {
 public:
 
  // Declare "onAction" for the use of periodic processing

   double onAction(ActionEvent&);
 };
 
 
 double MoveController::onAction(ActionEvent &evt) {
   return 5.0;      // return the time , when onAction will be called next time
 }
 
 // Return an instance of itself to Sigverse

 extern "C" Controller * createController() {
      return new MoveController;
 }
void MyController::onCollision(CollisionEvent &evt) { 
}
  
extern "C" Controller * createController() {  
  return new MyController;  
}  
}}


The agent controller creates a class named "MyController" by inheriting Controller class.
In this case, onAction function is called every 1.0 seconds, which is specified as the return value of the onAction.
But, as you can see, this controller does nothing. 

Basically, the agent controller creates a class named "MoveController" by inheriting Contoroller class .  onAction function is called every 5 seconds does nothing. 

**Compiling [#hf0f4b09]
** Compiling the controller [#q60191ea]

Create a makefile for compiling the code we have created.
A shell script for the compilation has been automatically prepared in your working space. You can compile the controller by 

 $ ./sigmake.sh ControllerSample.cpp

 $ emacs Makefile
If the compilation succeeded, you can see ControllerSample.so.

Makefile
 #Specifying the location of Sigverse header files
 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> .

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

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]
** World file [#x8e5ecc7]

*** What is a world file ? [#dc85d86d]
World file describes the configuration of the virtual world. In the SIGVerse system, world file is described in XML format.
The following world file is already prepared in your working space.

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

The location of world files is : sigverse-<version>/share/sigverse/data/xml
WorldSample.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 -->
   <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++-->
         <set-attr-value name="language" value="c++"/> 
 
 <!--Specify the created Agent Controller and its location -->
         <set-attr-value name="implementation"
 value="./NewWorld/MoveController.so"/>
                         value="./ControllerSample.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)-->
         <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 actual world file doesn't have the above comments.)

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" .



Specifying the controller and its location within workspace directory
#highlight(xml:firstline[18]){{
        <set-attr-value name="implementation" value="./NewWorld/MoveController.so"/>
#highlight(xml:firstline[17]){{
        <set-attr-value name="implementation"
                       value="./ControllerSample.so"/>
}}

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.

Since your workspace directory is "MyWorld", the relative path of the controller (such as ./ControlerSample.so) could be use.
You may need to write the full path depending on the relation between the world file and controller file.

**Starting the simulation [#zafcd00a]

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.
** Starting the simulation [#zafcd00a]

 $ export PATH=$PATH:/home/<username>/sigverse-<version>/bin
The script sigserver.sh is used to run the simulation and it placed in sigverse-<version>/bin directory. But you can run the simulation from anywhere because the PATH for the simulation is already set.
Type the following command in your working directory.

Moving to the directory where execution is located

 $ cd ~/sigverse-<version>/bin
 $ ./sigserver.sh -w NewWorld.xml -p 9001
 $ sigserver.sh -w ./WorldSample.xml
       :
       :
 [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

Please specify a world file with -w option. If you don't use the -w option, MyWorld.xml will be used.
Please specify a world file with -w option. If you don't use the -w option, MyWorld.xml will be used as default world file.

Since the agent controller is specified in the xml file you just created, the agent "Toy_D" starts automatically with attached controller.  

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.
Start the SIGViewer and enter the host name and port number to connect to the SIGServer . Please use the button "Connect to SIGServer" by single click.


#ref(toy_1.jpg)


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.

Start the simulation by using the framework of SIM_CTRL_CMD and the menu option START and press the button "Send" 

#ref(toy_2.jpg)

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.
We make some changes in the agent controller ControllerSample.cpp so that agent can move upon application of force.

 $ cd NewWorld
 $ emacs MoveController.cpp
 $ cd ~/MyWorld
 $ emacs ControllerSample.cpp

Add the following code in agent controller.

MoveController.cpp
ControllerSample.cpp

#highlight(cpp:firstline[13]){{
 double MoveController::onAction(ActionEvent &evt) {
   return 5.0;      //returns the time for onAction to be called next.
 double MyController::onAction(ActionEvent &evt) {
   return 1.0;      //returns the time period for next call of the onAction.
 }
}}

     ↓
#highlight(cpp:firstline[13]){{
 double MoveController::onAction(ActionEvent &evt) {
 double MyController::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.
   obj->addForce(0,0,500);          //apply the force 500[N] in Z direction
   return 1.0;      //returns the time period for next call of the onAction.
 }
}}


Compile and Run
*** Compile and Run [#m0f672b0]

 $ ./sigmake.sh ControllerSample.cpp
 $ sigserver.sh -w ./WorldSample.xml 

 $ 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 .

#ref(toy_3.jpg)

It would be a fun exercise to use the following functions as well.
It would be a fun exercise to use other functions such as setLinearVelocity, setTorque, and so on.

--setForce
--setVelocity
--setTorque
* Physics for form, size and position settings [#n6779cdb]

*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]
** Modification of object shape file [#ef9fabb8]

First, edit the configuration file of the object.
First, copy the original object shape file to your working directory.

 $ cd ~/sigverse-<version>/share/sigverse/data/xml
$ cp ~/sigverse-<version>/share/sigverse/data/xml/seToy_D.xml .

Then, edit the configuration file of the object to observe the change.

 $ emacs seToy_D.xml

in seToy_D.xml

  <body filename="dummy-body.xml"/>

please add under the following keywords

***box [#b90fa015]

#highlight(xml:nogutter){{
 <!--set the Box Shape。-->
  <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>
}}



***sphere [#f0404294]

setting the sphere shape below

#highlight(xml:nogutter){{
  <simpleShape type="sphere">
    <position x="0" y="0" z="0"/>
    <size r="10"/>
  </simpleShape>
}}


***cylinder [#z94a7687]

#highlight(xml:nogutter){{
  <simpleShape type="cylinder">
    <position x="0" y="0" z="0"/>
    <size r="3" h="10"/>
  </simpleShape>
}}

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

**On run[#r537a2f8]

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 .

*Units [#c72a1e70]

The units in SIGVerse: angle (in radians . For all the quantities used in this tutorial world standard international (SI) system of units used.

|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|
|Amount      |Name       |Sign|
|Length      |centimeter |cm|
|Weight      |kilogram   |kg|
|Time        |seconds    |s|
|Angle       |radians    |rad|
|Velocity    |centimeters per second|cm/s|
|Acceleration|centimeters per second per second|cm/s^2|
|Force       |kilogram centimeters per second per second|kg cm/s^2|
|Torque      |kilogram centimeters square per second per second |kg cm^2/s^2|

Up:[[Tutorial]]     Previous:[[Tutorial/Execution of test]]     Next:[[Humanoid agent operations]]


#highlight(end)

#counter


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