Up:[[Tutorial]]     Previous:[[Humanoid agent operations]]     Next:[[Vision sensor]]

----
#contents

* Exchange of messages between agents. [#ia7882aa]

※This tutorial is valid only for later than v2.1.0.


** Sending and receiving messages [#d353e783]

*** Controller of message sender [#zd064119]

Create a controller for sending messages.

 $ cd ~/MyWorld
 $ emacs SendController.cpp

#gist(sigverse-git/4ec290ead83ed03833efd8cd5791531f);

This controller send a message "Hello!" to the robot agent every second.

If you want to send a message to all of the agents in the world, you should use broadcastMsg instead of sendMsg.

Old
#highlight(cpp:firstline[38]){{
 sendMsg("robot_000",msg);
}}
New
#highlight(cpp:firstline[38]){{
 broadcastMsg(msg);
}}

*** Controller to receive messages [#i2790ab2]

Create a controller to receive messages.

 $ emacs RecvController.cpp


#gist(sigverse-git/dfd3d5c6d9d64180ca9eb8ffba54d544);



This controller put the agent's hand up if a message "Hello!!" comes.
The agent will put the hand down every 0.5 second.


Compiling the controller.

 $ ./sigmake.sh SendController.cpp RecvController.cpp


*** Creating a worldfile [#w395fedb]

 $ emacs MessageWorld.xml

#gist(sigverse-git/008dee0218d42c41388f7e02a2fe09c5);

In this controller, a human avator will send a message to a humanoid agent.


*** Execution [#tb38ac14]

 $ sigserver.sh -w ./MessageWorld.xml 

First, the avatar agent put a hand down and turn back by onInit function.
Then, you can see that the agent put it's hand up every second with receiving a message.

#ref(./message_1.PNG,40%)


** Specification of message reachable area [#a22b41bf]

This part explains how to specify the maximum length of the reachable area of the message sending function.

*** Controller of text message sender [#we283cc2]

Create another controller to send messages.

 $ emacs SendController2.cpp 

SendController2.cpp
#highlight(cpp){{
#include "Controller.h"
#include "Logger.h"
#include "ControllerEvent.h"

#define PI 3.141592
#define DEG2RAD(DEG) ( (PI) * (DEG) / 180.0 )
#define ARY_SIZE(ARY) ( (int)(sizeof(ARY)/sizeof(ARY[0])) )
#gist(sigverse-git/8a057f168978dd46bb2c48ae5c912a11);

class SendController : public Controller
{
public:
  void onInit(InitEvent &evt);
  double onAction(ActionEvent &evt);
};

void SendController::onInit(InitEvent &evt)
{
  SimObj *my = getObj(myname());

  // Putting the both hands and rotate the body 180[deg]
  my->setJointAngle("LARM_JOINT2", DEG2RAD(-90));
  my->setJointAngle("RARM_JOINT2", DEG2RAD(90));
  my->setAxisAndAngle(0, 1.0, 0, DEG2RAD(180));
}

double SendController::onAction(ActionEvent &evt)
{
  SimObj *my = getObj(myname());

  Vector3d vec;
  my->getPosition(vec);

  // Moving 20 to z-axis
  my->setPosition( vec.x(), vec.y(), vec.z() + 20 );

  // Creating a message
  std::string msg = "Hello!!";

  // Broadcasting the message to all of the agents within 3-meter radius from the agent
  broadcastMsg(msg, 300.0);

  //
  return 1.0;
}

extern "C"  Controller * createController ()
{
  return new SendController;
}
}}

The second argument of the broadcastMsg, 300, means that the maximum distance of the message reachable area is 3 meters.
If you omit the secnod argument, the message will be reached to all of the agents irrespective of the distance.

This agent keeps on sending a message and moving to z-axis.



*** Compiling [#k7a9b5fc]

 $ ./sigmake.sh SendController2.cpp

*** Modification of the world file [#s62dc619]

 $ emacs MessageWorld.xml

Modify the MessageWorld.xml to change the controller for sending agent

Old
#highlight(xml:firstline[13]){{
       <set-attr-value name="implementation" value="./SendController.so"/>
}}

New
#highlight(xml:firstline[13]){{        
       <set-attr-value name="implementation" value="./SendController2.so"/>
}}
 
*** Execution [#m24d78ab]

 $ sigserver.sh -w ./MessageWorld.xml

#ref(./message_3.PNG,40%)

You can see the agent sending messages with backing away from the receiver.
Please check how far the message reaches to the receiver.


** Sending message to the SIGViewer [#sa02f588]
If the broadcastMsg is used, the message will be reached to all of the agents, SIGViewer and SIGVerse services.
The SIGViewer will show the received message as log message.

You can send messages to the SIGViewer using sendMsg.
In this case, you should specify a service name which is determined when the SIGViewer connected to the SIGverse server.
If the service name is "SIGViewer", the following modification will change the sending target from the agent to the SIGViewer.

SendController.so

Old
#highlight(cpp:firstline[33]){{
 sendMsg("robot_000",msg);
}}
New
#highlight(cpp:firstline[33]){{
 sendMsg("SIGViewer",msg);
}}

#highlight(end)

Up:[[Tutorial]]     Previous:[[Humanoid agent operations]]     Next:[[Vision sensor]]

#counter

Front page   Edit Diff Backup Upload Copy Rename Reload   New List of pages Search Recent changes   Help   RSS of recent changes