ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question
0

Mapping ros::Publishers to different dynamically generated topic names

asked 2016-02-11 10:31:01 -0500

altella gravatar image

updated 2016-02-11 13:17:33 -0500

lucasw gravatar image

Hello all;

I am programming a tcp server to connect with another machine not using ROS. The main idea is to generate a topic for each connection, using the port value as identifier of the connection. So I use an std::map to pair ros::publisher and topic name:

std::map<std::string, ros::Publisher> comms_map;

When a new connection is received:

void addNewConnection(int socket, struct sockaddr_in addr)
{
    CONNECTION aux;
    aux.socket= socket;
    aux.port= addr.sin_port;
    aux.addr= addr.sin_addr.s_addr;
    connections.push_back(aux);

    //create the new publisher
    std::string topic_name = getTopicName(aux.port); //Creates the topic name "/tcp_server_data_pNUMPORT"
    ros::Publisher pub_aux = nh->advertise<std_msgs::String>(topic_name.c_str(), 1);
    comms_map.insert(std::pair<std::string, ros::Publisher>(topic_name,pub_aux));

    ROS_INFO("Added connection -> socket: %d port: %u addr: %ld",aux.socket, aux.port, aux.addr);
}

So, when I receive data, I try to publish over the correct ros::Publisher, using this code:

void publishMsgToTopic(int socket, char *buffer)
{
    //publish buffer to topic tcp_server_data_pXXXXXX
    std_msgs::String msg;
    unsigned long addr;
    unsigned short port;

    findConnection(socket,addr,port);
    std::string topic_name = getTopicName(port); 

    ROS_INFO("publishMsgToTopic -> socket %d port: %u addr: %ld. Topic: %s Message: %s",
                    socket, port, addr, (char *)(topic_name.c_str()), buffer);

    msg.data = std::string(buffer);

    ros::Publisher aux = comms_map.find(topic_name)->second;
    //also tested:
    // ros::Publisher aux = comms_map[topic_name];
    // ros::Publisher aux = comms_map.at(topic_name);

    aux.publish(msg);
}

Although the topic is created, and I do not receive any error, the topic does not publish any data ! Everything else in the process has been tested and works. IMPORTANT: each connection is attended in a child (fork process)

Could anyone give a hand about this?

Thank you very much in advance

edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted
0

answered 2016-02-11 11:57:13 -0500

gvdhoorn gravatar image

updated 2016-02-11 11:59:18 -0500

problem with publish in fork() process is probably relevant here.


Edit: you probably have looked into it, but would something like rosbridge work? Quote from the package description: "Rosbridge provides a JSON API to ROS functionality for non-ROS programs".

edit flag offensive delete link more

Comments

Thank you a lot, I will have a look t othe fork() problem. I have already looked at rosbrigde, but the client is a very limited machine, not capable of constructing JSON files...

altella gravatar image altella  ( 2016-02-12 01:40:59 -0500 )edit
0

answered 2020-03-08 21:10:59 -0500

vaspo gravatar image

It needs a second to generate a new topic. Adding a sleep after this line:

ros::Publisher pub_aux = nh->advertise<std_msgs::String>(topic_name.c_str(), 1);
sleep(3);

I used to generate new topics and add them to a std::map on ros-kinetic. In my case it works.

edit flag offensive delete link more

Question Tools

2 followers

Stats

Asked: 2016-02-11 10:31:01 -0500

Seen: 688 times

Last updated: Mar 08 '20