Difference between revisions of "Category:HamsterAPICPPTest"

From cogniteam
Jump to: navigation, search
(Created page with "== Tests in C++ == How our C++ tests works ? before all test we have to be connected to the Robot so we beginning with the connection. It is necessary to create an object h...")
 
 
(6 intermediate revisions by the same user not shown)
Line 17: Line 17:
 
Example :
 
Example :
 
<syntaxhighlight lang="bash" line='line'>
 
<syntaxhighlight lang="bash" line='line'>
HamsterClientParameters params = new HamsterClientParameters();
+
HamsterAPI::HamsterClientParameters params7;
params.port = 8101;
+
params7.port = 8107;
params.base_address = "10.0.2";
+
params7.base_address = "10.0.2";
Hamster hamster = new Hamster(1,params);
+
// Init the agent ID and the port
 +
HamsterAPI::Hamster hamster7(7,params7);
 
</syntaxhighlight>
 
</syntaxhighlight>
  
Line 41: Line 42:
  
 
<syntaxhighlight lang="bash" line='line'>
 
<syntaxhighlight lang="bash" line='line'>
@Test
+
try
public void positionCompareTopics(){
+
{
}  
+
// define pose like get pose from the robot and print it
 +
HamsterAPI::Pose pose = hamster.getPose();
 +
HamsterAPI::Log::i("Client",pose.toString());
 +
}
 +
catch(const HamsterAPI::HamsterError & message_error)
 +
{
 +
std::stringstream stream;
 +
stream << "Pose: " << message_error.what();
 +
HamsterAPI::Log::i("Client", stream.str());
 +
}
 
</syntaxhighlight>
 
</syntaxhighlight>
  
Line 50: Line 60:
 
The purpose of this test is to verify that the positioning of the robot makes sense.
 
The purpose of this test is to verify that the positioning of the robot makes sense.
  
<syntaxhighlight lang="bash" line='line'>
+
 
@Test
 
public void speedAndPosition(){
 
}
 
</syntaxhighlight>
 
  
 
We have created a function that recovers speed and position as long as we are connected to the robot.
 
We have created a function that recovers speed and position as long as we are connected to the robot.
 
The aim of this test is to verify the speed variations of the robots with respect to the position.
 
The aim of this test is to verify the speed variations of the robots with respect to the position.
  
<syntaxhighlight lang="bash" line='line'>
+
 
@Test
+
 
public void navigationTest(){
 
}
 
</syntaxhighlight>
 
  
 
We created a function for the robot to navigate from point A to point B.
 
We created a function for the robot to navigate from point A to point B.
Line 70: Line 73:
  
 
<syntaxhighlight lang="bash" line='line'>
 
<syntaxhighlight lang="bash" line='line'>
Pose initial = new pose;  
+
HamsterAPI::Pose pose;
initial.setX(4.0f);
+
pose.setX(7.03);
initial.setY(2.0f);
+
pose.setY(1.35);
initial.setHeading(120.0f);
+
pose.setHeading(-135.0);
hamster.setInitialPose(initial);
+
hamster.setInitialPose(pose);
 
</syntaxhighlight>
 
</syntaxhighlight>
  
Line 80: Line 83:
  
 
<syntaxhighlight lang="bash" line='line'>
 
<syntaxhighlight lang="bash" line='line'>
Goal goal = new goal;  
+
HamsterAPI::Goal goal;
goal.setX(4.0f);
+
goal.setX(3.0);
goal.setY(2.0f);
+
goal.setY(-2.0);
goal.setHeading(120.0f);
+
goal.setHeading(-135.0);
 +
goal.setGoalType(HamsterAPI::GT_GLOBAL_GOAL);
 +
//print all information about the goal
 +
HamsterAPI::Log::i("Client", goal.toString());
 +
 
 +
// Thread --> sleep 1s
 +
boost::this_thread::sleep(boost::posix_time::milliseconds(1000));
 +
 
 +
// ask to the hamster  to navigate forward the goal
 
hamster.navigateTo(goal);
 
hamster.navigateTo(goal);
 
</syntaxhighlight>
 
</syntaxhighlight>
Line 112: Line 123:
 
We first run the ReceiveDataMess program before the SendDataMessage program because we need the receiver connected to be able to send a message to it.
 
We first run the ReceiveDataMess program before the SendDataMessage program because we need the receiver connected to be able to send a message to it.
  
=== ReceiveDataMess ===
+
=== ReceiveDataMessage ===
 
<syntaxhighlight lang="bash" line='line'>
 
<syntaxhighlight lang="bash" line='line'>
try
+
*
 +
* HamsterAPIClientDataMessageReceiver.cpp
 +
*
 +
*  Created on: jun 21, 2016
 +
*      Author: Ilan
 +
*
 +
 
 +
#include <hamster_api_client_cpp/Hamster.h>
 +
#include <boost/date_time.hpp>
 +
#include <boost/thread.hpp>
 +
#include "IFDEF.h"
 +
 
 +
#ifdef RECEIVER
 +
 
 +
int main(int argc, char ** argv)
 +
{
 +
try
 +
{
 +
HamsterAPI::HamsterClientParameters params;
 +
params.port = 8108;
 +
params.base_address = "10.0.2";
 +
 
 +
// Init hamster with the agent id number
 +
HamsterAPI::Hamster hamster(8,params);
 +
 
 +
while(hamster.isConnected())
 
{
 
{
// Init the hamster with agent ID number and port
+
try
HamsterClientParameters params = new HamsterClientParameters();
 
params.port = 8108;
 
params.base_address = "10.0.2";
 
Hamster hamster = new Hamster(8,params);
 
 
 
      while(hamster.isConnected())
 
 
{
 
{
Log.i("Client", "Waiting for Sender to send msg");
+
HamsterAPI::Log::i("Client", "Waiting for Sender to send msg");// print
  
 
while(!hamster.hasDataMessages())
 
while(!hamster.hasDataMessages())
{
+
// Thread --> sleep 1
try
+
boost::this_thread::sleep(boost::posix_time::milliseconds(1000));
{
+
 
Thread.sleep(1000);
+
HamsterAPI::Log::i("Client", "Got msg"); // print
}
 
catch (InterruptedException e)  
 
{
 
continue;
 
}
 
}
 
                                // print GOT msg
 
Log.i("Client", "Got msg");
 
  
DataMessage msg = hamster.popLatestDataMessage();
+
HamsterAPI::DataMessage msg = hamster.popLatestDataMessage();
Log.i("Client", "" + msg.getSourceAgentID() + "=" + msg.getStringData()); //print ID source = message ex: 7=salut
+
HamsterAPI::Log::i("Client", boost::lexical_cast<std::string>(msg.getSourceAgentID()) + "=" + msg.getStringData());
  
// create new message to answer to the sender
+
// define message of receiver : receiver_msg
DataMessage receiver_msg = new DataMessage("coucou",8,7);
+
HamsterAPI::DataMessage receiver_msg("coucou",8,7);
Log.i("Client", "Sending Data Message"); // print "sending Data Message"
+
HamsterAPI::Log::i("Client", "Sending Data Message");//print
Log.i("Client", receiver_msg.toString()); // print information about the message
+
HamsterAPI::Log::i("Client", receiver_msg.toString());//print the receiver_msg
 +
// send the message from the receiver to the sender
 
hamster.sendDataMessage(receiver_msg);
 
hamster.sendDataMessage(receiver_msg);
                        }
+
}
      }
+
// Message Issue or Connection Issue
       
+
catch(const HamsterAPI::HamsterError & message_error)
// Connection Issue
+
{
catch(HamsterError connection_error)
+
HamsterAPI::Log::i("Client", message_error.what());
{
+
}
Log.i("Client",connection_error.what());
 
 
}
 
}
          Log.i("Client",connection_error.what());
+
}
                }
+
// Connection Issue
 +
catch(const HamsterAPI::HamsterError & connection_error)
 +
{
 +
HamsterAPI::Log::i("Client",connection_error.what());
 +
  }
 +
}
 +
 
 +
#endif
 +
 
 
</syntaxhighlight>
 
</syntaxhighlight>
  
 
=== SendDataMessage ===
 
=== SendDataMessage ===
 
<syntaxhighlight lang="bash" line='line'>
 
<syntaxhighlight lang="bash" line='line'>
try{
+
*
// Init the hamster with agent ID number and port
+
* HamsterAPIClientDataMessageSender.cpp
HamsterClientParameters params = new HamsterClientParameters();
+
*
params.port = 8107;
+
*  Created on: jun 21, 2016
params.base_address = "10.0.2";
+
*      Author: Ilan
Hamster hamster = new Hamster(7,params);
+
*
 +
 
 +
#include <hamster_api_client_cpp/Hamster.h>
 +
#include <boost/date_time.hpp>
 +
#include <boost/thread.hpp>
 +
#include "IFDEF.h"
 +
 
 +
#ifdef SENDER
 +
 
 +
int main(int argc, char ** argv)
 +
{
 +
try
 +
{
 +
HamsterAPI::HamsterClientParameters params;
 +
params.port = 8107;
 +
params.base_address = "10.0.2";
  
    while(hamster.isConnected()){
+
// Init hamster with the agent id number
 +
HamsterAPI::Hamster hamster(7,params);
  
// define a new message "Salut" send by 7 to 8
+
while(hamster.isConnected())
DataMessage sender_msg = new DataMessage("Salut",7,8);
+
{
Log.i("Client", "Sending Data Message");
+
try
Log.i("Client", sender_msg.toString());// print "Sending data message" on the console
 
hamster.sendDataMessage(sender_msg);// we print all information about the message : "Salut" send by 7 to 8
 
 
Log.i("Client", "Waiting for Receiver to send msg");
 
 
while(!hamster.hasDataMessages())
 
 
{
 
{
try
+
// define the message from the sender : sender_msg
{
+
HamsterAPI::DataMessage sender_msg("Salut",7,8);
Thread.sleep(1000);
+
HamsterAPI::Log::i("Client", "Sending Data Message");//print
}
+
HamsterAPI::Log::i("Client", sender_msg.toString());// print sender_msg
catch (InterruptedException e)  
+
hamster.sendDataMessage(sender_msg); // send message
{
+
 
continue;
+
HamsterAPI::Log::i("Client", "Waiting for Receiver to send msg");// print
}
+
 
 +
while(!hamster.hasDataMessages())
 +
// Thread --> sleep
 +
boost::this_thread::sleep(boost::posix_time::milliseconds(1000));
 +
 
 +
HamsterAPI::Log::i("Client", "Got msg"); // print
 +
 
 +
HamsterAPI::DataMessage msg = hamster.popLatestDataMessage();
 +
HamsterAPI::Log::i("Client", boost::lexical_cast<std::string>(msg.getSourceAgentID()) + "=" + msg.getStringData());
 +
}
 +
// Message Issue or Connection Issue
 +
catch(const HamsterAPI::HamsterError & message_error)
 +
{
 +
HamsterAPI::Log::i("Client", message_error.what());
 
}
 
}
 +
}
 +
}
 +
// Connection Issue
 +
catch(const HamsterAPI::HamsterError & connection_error)
 +
{
 +
HamsterAPI::Log::i("Client",connection_error.what());
 +
}
 +
}
  
Log.i("Client", "Got msg"); // print "got message "
+
#endif
  
DataMessage msg = hamster.popLatestDataMessage();
 
Log.i("Client", "" + msg.getSourceAgentID() + "=" + msg.getStringData()); // print source ID message = message ex : 8=coucou
 
  }
 
  }
 
                // Connection Issue
 
              catch(HamsterError connection_error)
 
              {
 
          Log.i("Client",connection_error.what());
 
              }
 
  }
 
 
</syntaxhighlight>
 
</syntaxhighlight>
  
 +
For launch correctly our communication between two Hamster we create a file IFDEF.h to launch the receiver before the sender.
 +
<syntaxhighlight lang="bash" line='line'>
 +
*
 +
* IFDEF.h
 +
*
 +
*  Created on: Jun 21, 2017
 +
*      Author: cogniteam
 +
*
 +
 +
#ifndef IFDEF_H_
 +
#define IFDEF_H_
 +
 +
#define RECEIVER 1
 +
#define SENDER 1
 +
 +
#endif /* IFDEF_H_ */
 +
 +
</syntaxhighlight>
 
=== TwoHamsterTwoGoal ===
 
=== TwoHamsterTwoGoal ===
  
 
<syntaxhighlight lang="bash" line='line'>
 
<syntaxhighlight lang="bash" line='line'>
public class TwoHamsterTwoGoalGlobal {
+
/*
 +
* 2Goal2HamsterGlobal.cpp
 +
*
 +
*  Created on: Jun 22, 2017
 +
*      Author: Ilan
 +
*/
 +
 
 +
 
 +
#include <hamster_api_client_cpp/Hamster.h>
 +
#include <boost/date_time.hpp>
 +
#include <boost/thread.hpp>
 +
 
 +
int main(int argc, char ** argv){
 +
 
 +
 
 +
try{
 +
HamsterAPI::HamsterClientParameters params7;
 +
params7.port = 8107;
 +
params7.base_address = "10.0.2";
 +
// Init the agent ID and the port
 +
HamsterAPI::Hamster hamster7(7,params7);
  
public static void main(String[] args) {
+
HamsterAPI::HamsterClientParameters params8;
// TODO Auto-generated method stub
+
params8.port = 8108;
+
params8.base_address = "10.0.2";
tryThe purpose of this test is to make two hamster go towards a definite goal one after the other.
+
// Init the agent ID and the port
As long as the first did not reach its goal the second robot does not go towards are objectives.
+
HamsterAPI::Hamster hamster8(8,params8);
For this, first we initialize the positions of each robot and then we impose the goal for each of them.
+
 
{
+
// Define a new position and set X, Y, and heading
// Init hamster with the agent id number and port
+
HamsterAPI::Pose pose;
HamsterClientParameters params7 = new HamsterClientParameters();
+
pose.setX(7.03);
// set port for agent 7
+
pose.setY(1.35);
params7.port = 8107;
+
pose.setHeading(-135.0);
// set the base address
+
// Def pose like the initial position for hamster 7
params7.base_address = "10.0.2";
+
hamster7.setInitialPose(pose);
Hamster hamster7 = new Hamster(7,params7);
+
pose.setX(6.02);
+
pose.setY(1.98);
HamsterClientParameters params8 = new HamsterClientParameters();
+
// set new X, and Y and define the new pose like the initial position of hamster 8
params8.port = 8108;
+
hamster8.setInitialPose(pose);
params8.base_address = "10.0.2";
+
 
Hamster hamster8 = new Hamster(8,params8);
+
// thread --> sleep during 2s
+
boost::this_thread::sleep(boost::posix_time::milliseconds(2000));
if(hamster7.isConnected() && hamster8.isConnected())
+
 
{
+
// define new goal and set X, Y, heading and goal Type
// if hamster 7 is connected
+
HamsterAPI::Goal goal;
// DEF Initial position hamster 7 & 8
+
goal.setX(3.0);
Pose p = new Pose();
+
goal.setY(-2.0);
p.setX(5.96f);
+
goal.setHeading(-135.0);
p.setY(2.06f);
+
goal.setGoalType(HamsterAPI::GT_GLOBAL_GOAL);
p.setHeading(-135.0f);
+
//print all information about the goal
hamster7.setInitialPose(p);
+
HamsterAPI::Log::i("Client", goal.toString());
p.setX(7.1f);
+
 
p.setY(1.30f);
+
// Thread --> sleep 1s
hamster8.setInitialPose(p);
+
boost::this_thread::sleep(boost::posix_time::milliseconds(1000));
+
 
+
// ask to the hamster 7 to navigate forward the goal
try {
+
hamster7.navigateTo(goal);
Thread.sleep(500);
+
 
} catch (InterruptedException e) {
+
while(hamster7.isConnected()){
// TODO Auto-generated catch block
+
// during Hamster 7 is connected
e.printStackTrace();
+
// define state like the navigation state all the time
}
+
// if the status is still in progress so we continue
//just for know if i come inside this if
+
// if the status is reach so we break the loop
//System.out.print("---------- Tu passe par la ???? \n");
+
// if the status is fail so we return 1
// goal and navigation for hamster 7
+
HamsterAPI::NavigationState state = hamster7.getNavigationState();
+
if(state.getStatus() == HamsterAPI::NS_IN_PROGRESS)
Goal g = new Goal();
+
continue;
// POSITION OF THE GOAL
+
else if(state.getStatus() == HamsterAPI::NS_REACHED_GOAL)
g.setX(3.55f);
+
{
g.setY(-2.44f);
+
HamsterAPI::Log::i("Client", "Reached Goal!");
g.setHeading(135.0f);
+
break;
// GOAL TYPE IS GLOBAL
+
}
g.setGoalType(GoalType.GT_GLOBAL_GOAL);
+
else
//tell to hamster 7 navigate from initial pose to goal
+
{
hamster7.navigateTo(g);
+
HamsterAPI::Log::i("Client", "Goal Failed.");
+
return 1;
}
+
}
 
// status of navigation hamster 7
 
 
while(hamster7.isConnected())
 
{
 
// during hamster 7 is connected get the navigation state  
 
NavigationState state = hamster7.getNavigationState();
 
// if the state is in progress, the navigation continue
 
if(state.getStatus() == NavigationStatus.NS_IN_PROGRESS)
 
continue;
 
else if(state.getStatus() == NavigationStatus.NS_REACHED_GOAL)
 
{
 
// if the navigation state is reach so the goal is reach and we disconnect the hamster
 
Log.i("Client", "Reached Goal : Hamster 7 !");
 
break;
 
 
}
 
}
else
+
// hamster 7 is disconnect
{
+
hamster7.disconnect();
// else the goal failed and we print it
+
 
Log.i("Client", "Goal Failed : Hamster 7 ");
+
// Thread --> sleep 1s
break;
+
boost::this_thread::sleep(boost::posix_time::milliseconds(1000));
 +
 
 +
if(!hamster7.isConnected()){
 +
// if the hamster 7 is disconnect
 +
// define a new goal and we set X, Y, Heading and goal type
 +
HamsterAPI::Goal goal1;
 +
goal1.setX(4.76);
 +
goal1.setY(-2.46);
 +
goal1.setHeading(-135.0);
 +
goal1.setGoalType(HamsterAPI::GT_GLOBAL_GOAL);
 +
 
 +
// we print all information about goal1
 +
HamsterAPI::Log::i("Client", goal1.toString());
 +
 
 +
//Thread --> 1s
 +
boost::this_thread::sleep(boost::posix_time::milliseconds(1000));
 +
 
 +
// and ask to hamster 8 to navigate forward goal1
 +
hamster8.navigateTo(goal1);
 +
 
 
}
 
}
 +
 +
while(hamster8.isConnected()){
 +
// during Hamster 8 is connected
 +
// define state like the navigation state all the time
 +
// if the status is still in progress so we continue
 +
// if the status is reach so we break the loop
 +
// if the status is fail so we return 1
 +
 +
HamsterAPI::NavigationState state = hamster8.getNavigationState();
 +
if(state.getStatus() == HamsterAPI::NS_IN_PROGRESS)
 +
continue;
 +
else if(state.getStatus() == HamsterAPI::NS_REACHED_GOAL)
 +
{
 +
HamsterAPI::Log::i("Client", "Reached Goal!");
 +
break;
 +
}
 +
else
 +
{
 +
HamsterAPI::Log::i("Client", "Goal Failed.");
 +
return 1;
 +
}
 +
}
 +
// disconnect hamster 8
 +
hamster8.disconnect();
 +
 
}
 
}
+
catch(const HamsterAPI::HamsterError & connection_error){
// deconnection hamster 7
+
HamsterAPI::Log::i("Client",connection_error.what());
hamster7.disconnect();
 
 
if(!hamster7.isConnected() && hamster8.isConnected())
 
{
 
// if goal hamster 7 is reach  or failed and hamster 8 is conected
 
// define goal for hamster 8 and navigate to the position
 
 
//just for know if i come inside this if
 
//System.out.print("---------- Tu passe par la ? ");
 
 
 
Goal g = new Goal();
 
// set the  position of the goal for hamster 8
 
g.setX(4.01f);
 
g.setY(-0.948f);
 
g.setHeading(135.0f);
 
//define type of goal for hamster 8 : global
 
g.setGoalType(GoalType.GT_GLOBAL_GOAL);
 
//tell to hamster 8 navigate from initial pose to goal
 
hamster8.navigateTo(g);
 
}
 
 
 
  
try {
 
Thread.sleep(500);
 
} catch (InterruptedException e) {
 
// TODO Auto-generated catch block
 
e.printStackTrace();
 
}
 
 
while(hamster8.isConnected())
 
{
 
// status of navigation Hamster 8
 
NavigationState state = hamster8.getNavigationState();
 
// if it's in progress so the hamster continue the navigation
 
if(state.getStatus() == NavigationStatus.NS_IN_PROGRESS)
 
continue;
 
else if(state.getStatus() == NavigationStatus.NS_REACHED_GOAL)
 
{
 
// if the navigation state is reach so the goal is reach and we disconnect the hamster
 
Log.i("Client", "Reached Goal : Hamster 8 !");
 
break;
 
}
 
else
 
{
 
// else the goal failed and we print it
 
Log.i("Client", "Goal Failed : Hamster 8 ");
 
break;
 
 
}
 
}
 
// disconnection Hamster 8
 
hamster8.disconnect();
 
}
 
// Connection Issue
 
catch(HamsterError connection_error)
 
{
 
Log.i("Client",connection_error.what());
 
}
 
 
}
 
}
 
}
 
}

Latest revision as of 08:47, 24 July 2017

Tests in C++

How our C++ tests works ?

before all test we have to be connected to the Robot so we beginning with the connection.

It is necessary to create an object hamster and it use all information about the Hamster class. Then we use we create a new HamsterClientParameters. It use to initialize the port and the base address of the Robot. and now we use the object Hamster that we created before with two parameters :

1- number agent of the robot

2- the name of the HamsterClientParameters that we created before.

We put this little function in a try catch of function with throws exception because if we can't be connected to the robot all the test after will failed.

Example : <syntaxhighlight lang="bash" line='line'> HamsterAPI::HamsterClientParameters params7; params7.port = 8107; params7.base_address = "10.0.2"; // Init the agent ID and the port HamsterAPI::Hamster hamster7(7,params7); </syntaxhighlight>

Test with simulation

Before launch our test we have to launch our simulation software Gazebo.

<syntaxhighlight lang="bash" line='line'> cd hamster_ws source devel/setup.bash </syntaxhighlight>

and then launch Gazebo :

<syntaxhighlight lang="bash" line='line'> roslaunch hamster_api_server_cpp hamster_api_simulation_server_and_gazbo_and_algs.launch </syntaxhighlight>

For the tests we created a test file where each test corresponds to a function

<syntaxhighlight lang="bash" line='line'> try { // define pose like get pose from the robot and print it HamsterAPI::Pose pose = hamster.getPose(); HamsterAPI::Log::i("Client",pose.toString()); } catch(const HamsterAPI::HamsterError & message_error) { std::stringstream stream; stream << "Pose: " << message_error.what(); HamsterAPI::Log::i("Client", stream.str()); } </syntaxhighlight>

We have created a function to get the position of the robot and print it as long as we are connected. In a second time we start the topic on ROS and we compare the values. The purpose of this test is to verify that the positioning of the robot makes sense.


We have created a function that recovers speed and position as long as we are connected to the robot. The aim of this test is to verify the speed variations of the robots with respect to the position.



We created a function for the robot to navigate from point A to point B. To be done:
-First define the point A as the initial points and therefore define the initial position.

<syntaxhighlight lang="bash" line='line'> HamsterAPI::Pose pose; pose.setX(7.03); pose.setY(1.35); pose.setHeading(-135.0); hamster.setInitialPose(pose); </syntaxhighlight>

- In second time set goal to reach with position

<syntaxhighlight lang="bash" line='line'> HamsterAPI::Goal goal; goal.setX(3.0); goal.setY(-2.0); goal.setHeading(-135.0); goal.setGoalType(HamsterAPI::GT_GLOBAL_GOAL); //print all information about the goal HamsterAPI::Log::i("Client", goal.toString());

// Thread --> sleep 1s boost::this_thread::sleep(boost::posix_time::milliseconds(1000));

// ask to the hamster to navigate forward the goal hamster.navigateTo(goal); </syntaxhighlight>

Between the initial position and the goal we have to insert a thread.sleep().

Test with robots

To make the tests on the robots we use a software allowing us to know all the parameters of the robot such as the position on a map for example. To launch this software type the following command lines : <syntaxhighlight lang="bash" line='line'>

export ROS_MASTER_URI=http://10.0.2.1:11311

</syntaxhighlight> The address after http correspond to the robot's base address.

Now do an ifconfig and find the ip address that has just been added. <syntaxhighlight lang="bash" line='line'> ROS_IP=10.0.2. your address </syntaxhighlight>

And then launch rviz. <syntaxhighlight lang="bash" line='line'> rviz </syntaxhighlight>


To check the communication between two hamster we have developed two programs one for the sending of data and the other for the reception and the answer.
We first run the ReceiveDataMess program before the SendDataMessage program because we need the receiver connected to be able to send a message to it.

ReceiveDataMessage

<syntaxhighlight lang="bash" line='line'>

  • HamsterAPIClientDataMessageReceiver.cpp
  • Created on: jun 21, 2016
  • Author: Ilan
  1. include <hamster_api_client_cpp/Hamster.h>
  2. include <boost/date_time.hpp>
  3. include <boost/thread.hpp>
  4. include "IFDEF.h"
  1. ifdef RECEIVER

int main(int argc, char ** argv) { try { HamsterAPI::HamsterClientParameters params; params.port = 8108; params.base_address = "10.0.2";

// Init hamster with the agent id number HamsterAPI::Hamster hamster(8,params);

while(hamster.isConnected()) { try { HamsterAPI::Log::i("Client", "Waiting for Sender to send msg");// print

while(!hamster.hasDataMessages()) // Thread --> sleep 1 boost::this_thread::sleep(boost::posix_time::milliseconds(1000));

HamsterAPI::Log::i("Client", "Got msg"); // print

HamsterAPI::DataMessage msg = hamster.popLatestDataMessage(); HamsterAPI::Log::i("Client", boost::lexical_cast<std::string>(msg.getSourceAgentID()) + "=" + msg.getStringData());

// define message of receiver : receiver_msg HamsterAPI::DataMessage receiver_msg("coucou",8,7); HamsterAPI::Log::i("Client", "Sending Data Message");//print HamsterAPI::Log::i("Client", receiver_msg.toString());//print the receiver_msg // send the message from the receiver to the sender hamster.sendDataMessage(receiver_msg); } // Message Issue or Connection Issue catch(const HamsterAPI::HamsterError & message_error) { HamsterAPI::Log::i("Client", message_error.what()); } } } // Connection Issue catch(const HamsterAPI::HamsterError & connection_error) { HamsterAPI::Log::i("Client",connection_error.what());

 }

}

  1. endif

</syntaxhighlight>

SendDataMessage

<syntaxhighlight lang="bash" line='line'>

*
* HamsterAPIClientDataMessageSender.cpp
*
*  Created on: jun 21, 2016
*      Author: Ilan
*
  1. include <hamster_api_client_cpp/Hamster.h>
  2. include <boost/date_time.hpp>
  3. include <boost/thread.hpp>
  4. include "IFDEF.h"
  1. ifdef SENDER

int main(int argc, char ** argv) { try { HamsterAPI::HamsterClientParameters params; params.port = 8107; params.base_address = "10.0.2";

// Init hamster with the agent id number HamsterAPI::Hamster hamster(7,params);

while(hamster.isConnected()) { try { // define the message from the sender : sender_msg HamsterAPI::DataMessage sender_msg("Salut",7,8); HamsterAPI::Log::i("Client", "Sending Data Message");//print HamsterAPI::Log::i("Client", sender_msg.toString());// print sender_msg hamster.sendDataMessage(sender_msg); // send message

HamsterAPI::Log::i("Client", "Waiting for Receiver to send msg");// print

while(!hamster.hasDataMessages()) // Thread --> sleep boost::this_thread::sleep(boost::posix_time::milliseconds(1000));

HamsterAPI::Log::i("Client", "Got msg"); // print

HamsterAPI::DataMessage msg = hamster.popLatestDataMessage(); HamsterAPI::Log::i("Client", boost::lexical_cast<std::string>(msg.getSourceAgentID()) + "=" + msg.getStringData()); } // Message Issue or Connection Issue catch(const HamsterAPI::HamsterError & message_error) { HamsterAPI::Log::i("Client", message_error.what()); } } } // Connection Issue catch(const HamsterAPI::HamsterError & connection_error) { HamsterAPI::Log::i("Client",connection_error.what()); } }

  1. endif

</syntaxhighlight>

For launch correctly our communication between two Hamster we create a file IFDEF.h to launch the receiver before the sender. <syntaxhighlight lang="bash" line='line'>

* IFDEF.h
*
*  Created on: Jun 21, 2017
*      Author: cogniteam
*
  1. ifndef IFDEF_H_
  2. define IFDEF_H_
  1. define RECEIVER 1
  2. define SENDER 1
  1. endif /* IFDEF_H_ */

</syntaxhighlight>

TwoHamsterTwoGoal

<syntaxhighlight lang="bash" line='line'> /*

* 2Goal2HamsterGlobal.cpp
*
*  Created on: Jun 22, 2017
*      Author: Ilan
*/


  1. include <hamster_api_client_cpp/Hamster.h>
  2. include <boost/date_time.hpp>
  3. include <boost/thread.hpp>

int main(int argc, char ** argv){


try{ HamsterAPI::HamsterClientParameters params7; params7.port = 8107; params7.base_address = "10.0.2"; // Init the agent ID and the port HamsterAPI::Hamster hamster7(7,params7);

HamsterAPI::HamsterClientParameters params8; params8.port = 8108; params8.base_address = "10.0.2"; // Init the agent ID and the port HamsterAPI::Hamster hamster8(8,params8);

// Define a new position and set X, Y, and heading HamsterAPI::Pose pose; pose.setX(7.03); pose.setY(1.35); pose.setHeading(-135.0); // Def pose like the initial position for hamster 7 hamster7.setInitialPose(pose); pose.setX(6.02); pose.setY(1.98); // set new X, and Y and define the new pose like the initial position of hamster 8 hamster8.setInitialPose(pose);

// thread --> sleep during 2s boost::this_thread::sleep(boost::posix_time::milliseconds(2000));

// define new goal and set X, Y, heading and goal Type HamsterAPI::Goal goal; goal.setX(3.0); goal.setY(-2.0); goal.setHeading(-135.0); goal.setGoalType(HamsterAPI::GT_GLOBAL_GOAL); //print all information about the goal HamsterAPI::Log::i("Client", goal.toString());

// Thread --> sleep 1s boost::this_thread::sleep(boost::posix_time::milliseconds(1000));

// ask to the hamster 7 to navigate forward the goal hamster7.navigateTo(goal);

while(hamster7.isConnected()){ // during Hamster 7 is connected // define state like the navigation state all the time // if the status is still in progress so we continue // if the status is reach so we break the loop // if the status is fail so we return 1 HamsterAPI::NavigationState state = hamster7.getNavigationState(); if(state.getStatus() == HamsterAPI::NS_IN_PROGRESS) continue; else if(state.getStatus() == HamsterAPI::NS_REACHED_GOAL) { HamsterAPI::Log::i("Client", "Reached Goal!"); break; } else { HamsterAPI::Log::i("Client", "Goal Failed."); return 1; } } // hamster 7 is disconnect hamster7.disconnect();

// Thread --> sleep 1s boost::this_thread::sleep(boost::posix_time::milliseconds(1000));

if(!hamster7.isConnected()){ // if the hamster 7 is disconnect // define a new goal and we set X, Y, Heading and goal type HamsterAPI::Goal goal1; goal1.setX(4.76); goal1.setY(-2.46); goal1.setHeading(-135.0); goal1.setGoalType(HamsterAPI::GT_GLOBAL_GOAL);

// we print all information about goal1 HamsterAPI::Log::i("Client", goal1.toString());

//Thread --> 1s boost::this_thread::sleep(boost::posix_time::milliseconds(1000));

// and ask to hamster 8 to navigate forward goal1 hamster8.navigateTo(goal1);

}

while(hamster8.isConnected()){ // during Hamster 8 is connected // define state like the navigation state all the time // if the status is still in progress so we continue // if the status is reach so we break the loop // if the status is fail so we return 1

HamsterAPI::NavigationState state = hamster8.getNavigationState(); if(state.getStatus() == HamsterAPI::NS_IN_PROGRESS) continue; else if(state.getStatus() == HamsterAPI::NS_REACHED_GOAL) { HamsterAPI::Log::i("Client", "Reached Goal!"); break; } else { HamsterAPI::Log::i("Client", "Goal Failed."); return 1; } } // disconnect hamster 8 hamster8.disconnect();

} catch(const HamsterAPI::HamsterError & connection_error){ HamsterAPI::Log::i("Client",connection_error.what());

} }

</syntaxhighlight>

The purpose of this test is to make two hamster go towards a definite goal one after the other. As long as the first did not reach his goal the second robot does not go towards his objectives.
For this, first we initialize the positions of each robot and then we impose the goal for each of them.

Subcategories

This category has only the following subcategory.