0
$\begingroup$

Rosanswers logo

I'm using ROS in my project and I need to send one message from time to time. I have this function:

void RosNetwork::sendMessage(string msg, string channel) {
    _mtx.lock();
    ros::Publisher chatter_pub = _n.advertise<std_msgs::String>(channel.c_str(),10);
    ros::Rate loop_rate(10);
    std_msgs::String msgToSend;
    msgToSend.data = msg.c_str();
    chatter_pub.publish(msgToSend);
    loop_rate.sleep();
    cout << "Message Sent" << endl;
    _mtx.unlock();
}

And I have this in python:

def callbackFirst(data):
    #rospy.loginfo(rospy.get_caller_id() + "I heard %s", data.data)
    print("Received message from first filter")

def callbackSecond(data):
    #rospy.loginfo(rospy.get_caller_id() + "I heard %s", data.data)
    print("Received message from second filter")

def listener():
    rospy.Subscriber("FirstTaskFilter", String, callbackFirst)
    print("subscribed to FirstTaskFilter")
    rospy.Subscriber("SecondTaskFilter", String, callbackSecond)
    print("subscribed to SecondTaskFilter")
    rospy.spin()

The listener is a thread in python. I get to the function sendMessage (I see in the terminal "Message Sent" a lot of times) but I don't see that the python script receives the message.

Update: I tested the python callback with rostopic pub /FirstTaskFilter std_msgs/String "test" and this works perfectly.

Any thought?


Originally posted by jdorfsman on ROS Answers with karma: 13 on 2015-08-04

Post score: 0


Original comments

Comment by mgruhler on 2015-08-04:
Well, what is the topic of the Publisher? Check with rosnode info <YOUR_PUBLISHER_NODE> which topics it sends. I guess this is a simple remapping issue...

You can also visualize this with rosrun rqt_graph rqt_graph.

$\endgroup$

1 Answer 1

0
$\begingroup$

Rosanswers logo

You should use advertise() only once at startup, not every time you want to send a message.


Originally posted by Sebastian Kasperski with karma: 1658 on 2015-08-04

This answer was ACCEPTED on the original site

Post score: 1


Original comments

Comment by jdorfsman on 2015-08-04:
Thanks, I did that and that solved it.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.