-
Notifications
You must be signed in to change notification settings - Fork 2
Data Manager Example
##Manager Map
First, check the launcher file.
<launch>
<arg name="topic" default="/manager_test"/>
<node name="manager_test" pkg="cafer_core" type="manager_test_subscribe_node" ns="manager_ns">
<param name="topic" type="string" value="$(arg topic)"/>
</node>
<node name="managermap_example" pkg="cafer_core" type="managermap_example" ns="manager_ns" output="screen">
<param name="topic" type="string" value="$(arg topic)"/>
</node>
</launch>Here, we launch two nodes : one to publish messages and one to execute the manager. The launch file accept one parameter for the name of the topic.
The manager_test_subscribe_node is a simple publisher that publish message continuously.
Ok, let's look at the code of managermap_example.cpp.
//initiate the cafer core
cafer_core::init(argc, argv, "managermap_example");
std::string topic;
cafer_core::ros_nh->getParam("/manager_ns/managermap_example/topic",topic);
//create the data manager for the manager_test msg
cafer_core::ManagerMap<cafer_core::manager_test> manager("example", "example1");First, we initiate the cafer_core. Then we retrieve the name of the topic. Finally, we instantiate the ManagerMap with, as template argument, the type of message, and as argument his name and his description.
Then we can start to listen to the topic.
//listen to a specific topic
manager.listen_to(topic);
//listen until manager have 10 messages in his data set
while (manager.data_size() < 10) {
ros::spinOnce();
}Here, we use simply the listen_to method to tell to the manager to subscribe to the topic. And we retrieve 10 messages.
Finally, you can get the messages by random access with the method get, and search by id with the search method, or delete one message by his id.
cafer_core::manager_test msg;
//and get it with the random access getter
msg = manager.get();
ROS_INFO_STREAM( "Message info :\n"
<< " id : " << msg.header.seq << std::endl
<< " timestamp : " << msg.header.stamp << std::endl
<< " description : " << msg.description << std::endl
<< " content :" << msg.content << std::endl);
//you can search the first message arrived
int i = 0;
while(!manager.search(i,msg))
i++;
ROS_INFO_STREAM( "Message info :\n"
<< " id : " << msg.header.seq << std::endl
<< " timestamp : " << msg.header.stamp << std::endl
<< " description : " << msg.description << std::endl
<< " content :" << msg.content << std::endl);
//you can remove it
manager.remove(i);
if(!manager.search(i,msg))
ROS_INFO_STREAM("the message with id " << i << " doesn't exist");The search method return true if the message exist, false if not.
##Manager Queue
This example is very close from the ManagerMap example. First, take a look at the launch file.
<launch>
<arg name="topic" default="/manager_test"/>
<node name="manager_test" pkg="cafer_core" type="manager_test_subscribe_node" ns="manager_ns">
<param name="topic" type="string" value="$(arg topic)"/>
</node>
<node name="managerqueue_example" pkg="cafer_core" type="managerqueue_example" ns="manager_ns" output="screen">
<param name="topic" type="string" value="$(arg topic)"/>
</node>
</launch>Like the previous example, we launch two nodes : one to publish messages and one to execute the manager. The launch file accept one parameter for the name of the topic. The manager_test_subscribe_node is a simple publisher that publish message continuously.
First, initiate the cafer_core and the ManagerQueue.
//initiate the cafer core
cafer_core::init(argc, argv, "managerqueue_example");
std::string topic;
cafer_core::ros_nh->getParam("/manager_ns/managerqueue_example/topic",topic);
//create the data manager for the manager_test msg
cafer_core::ManagerQueue<cafer_core::manager_test> manager("example", "example1");Then, tell the manager to listen to the topic.
//listen to a specific topic
manager.listen_to(topic);
//listen until manager have 10 messages in his data set
while (manager.data_size() < 10) {
ros::spinOnce();
}Finally, you can get the first message added to the manager with the get message. When you get a message, it is directly removed. So, if you call a second time the method get, you will get the second message added to the manager.
//and get the first message arrived
msg = manager.get();
ROS_INFO_STREAM( "Message info :\n"
<< " id : " << msg.header.seq << std::endl
<< " timestamp : " << msg.header.stamp << std::endl
<< " description : " << msg.description << std::endl
<< " content :" << msg.content << std::endl);
//and get the second message arrived
msg = manager.get();
ROS_INFO_STREAM( "Message info :\n"
<< " id : " << msg.header.seq << std::endl
<< " timestamp : " << msg.header.stamp << std::endl
<< " description : " << msg.description << std::endl
<< " content :" << msg.content << std::endl);