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

publisher and subscriber in the same node

asked 2012-11-21 02:15:36 -0600

joseescobar60 gravatar image

updated 2012-11-22 22:15:52 -0600

Hello, i have a subscriber that reads the values of a flex sensor that rosserial_arduino is reading, and have another file with a publisher that writes values in a topic of the shadow hand i need to send the flex sensor values to shadow hand, the question is, can i create only one .cpp file with the subscriber and the publisher there?

already i have this two files:

Subscriber:

#include <ros/ros.h>
#include <std_msgs/Float32.h>

void indiceCallback(const std_msgs::Float32::ConstPtr& msg)
{
  ROS_INFO("Valor indice: [%f]", msg->data);
}


int main(int argc, char **argv)
{
  ros::init(argc, argv, "indice");
  ros::NodeHandle n;
  ros::Subscriber sub = n.subscribe("indice", 500, indiceCallback);
  ros::spin();

  return 0;
}

Publisher file:

#include "ros/ros.h"
#include "std_msgs/Float64.h"

#include <sstream>

int main(int argc, char **argv)
{
  ros::init(argc, argv, "prueba");
  ros::NodeHandle n;
  ros::Publisher pub = n.advertise<std_msgs::Float64>("/sh_ffj3_mixed_position_velocity_controller/command", 1000);
  ros::Rate loop_rate(10);


  int count = 0;
  while (ros::ok())
  {
    std_msgs::Float64 msg;
    msg.data = 1.5;
    ROS_INFO("%f", msg.data);
    pub.publish(msg);
    ros::spinOnce();
    loop_rate.sleep();
    ++count;
  }


  return 0;
}

As you can see the publisher file publish the data value, but there this value i change manually, i need to put there the value that i have reading in the subscriber.

Thank you a lot people!!

I already do this:

#include "ros/ros.h"
#include "std_msgs/Float64.h"
#include "std_msgs/Float32.h"

#include <sstream>

void indiceCallback(const std_msgs::Float64::ConstPtr& msg)
{
  ROS_INFO("Valor indice: [%f]", msg->data);
}

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

  ros::init(argc, argv, "indice");
  ros::NodeHandle n;
  ros::Subscriber sub = n.subscribe("indice", 500, indiceCallback);
  ros::spin();


  ros::init(argc, argv, "prueba");
  //ros::NodeHandle n;
  ros::Publisher pub = n.advertise<std_msgs::Float64>("/sh_ffj3_mixed_position_velocity_controller/command", 500);
  ros::Rate loop_rate(10);


  int count = 0;
  while (ros::ok())
  {
    std_msgs::Float64 msg;
    //msg.data
    ROS_INFO("%f", msg.data);
    pub.publish(msg);
    ros::spinOnce();
    loop_rate.sleep();
    ++count;
  }


  return 0;
}

When i run everything the subscriber shows to me what is listening, now i need to deliver this vlues to the publisher because i need to send this values to other topic. and if i do

rostopic echo /sh_ffj3_mixed_position_velocity_controller/command

it shoot me:

WARNING: no messages received and simulated time is active.
Is /clock being published?

Any Idea??

///////////////////////////////////Edit

#include "ros/ros.h"
#include "std_msgs/Float64.h"

#include <sstream>

void indiceCallback(const std_msgs::Float64::ConstPtr& msg)
{
  ROS_INFO("Valor indice: [%f]", msg->data);
}

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

  ros::init(argc, argv, "indice");
  ros::NodeHandle n;
  ros::Subscriber sub = n.subscribe("indice", 500, indiceCallback);
  ros::spin();


  ros::Publisher pub = n.advertise<std_msgs::Float64>("/sh_ffj3_mixed_position_velocity_controller/command", 500);
  ros::Rate loop_rate(10);
  ros::spinOnce();

  int count = 0;
  while (ros::ok())
  {
    std_msgs::Float64 msg;
    //msg.data
    ROS_INFO("%f", msg.data);
    pub.publish(msg);
    ros::spinOnce();
    loop_rate.sleep();
    ++count;
  }


  return 0;
}

EDIT///////////////////////////////////////////////////////////

the code is like that:

#include "ros/ros.h"
#include "std_msgs/Float64.h"
#include <sstream>

void indiceCallback(const std_msgs::Float64::ConstPtr& msg)
{
  ROS_INFO("Valor indice: [%f]", msg->data);
}

int main ...
(more)
edit retag flag offensive close merge delete

Comments

You only need the spinOnce in the main while loop.

dornhege gravatar image dornhege  ( 2012-11-21 06:10:28 -0600 )edit
1

OK, it already works, but i need to take the value that i reading with the subscriber and deliver this value to the publisher, how can i do that??

joseescobar60 gravatar image joseescobar60  ( 2012-11-21 23:01:45 -0600 )edit
1

You added a a different question that is not really related to your original one. It's better to create a new question for that. In your case, the problem is that you are trying to access a pointer that hasn't been initialized yet in your main loop. Add an if(mensaje) { ... } around your publish.

Lorenz gravatar image Lorenz  ( 2012-11-22 00:47:13 -0600 )edit

Thank you everybody, the problem was solved, i did what Lorenz said, thank you!!

joseescobar60 gravatar image joseescobar60  ( 2012-11-22 22:15:02 -0600 )edit

3 Answers

Sort by » oldest newest most voted
5

answered 2012-11-21 04:13:56 -0600

Lorenz gravatar image

You are getting a warning that clock is not being published. This indicates that the parameter use_sim_time is set to true but there is no time source. This will cause your call to sleep to block forever and subscriber callbacks are not handled. Get rid of the parameter with

rosparam delete use_sim_time

or start a simulator to provide time.

edit flag offensive delete link more

Comments

i did it, but the response is that the warning don't appear and the terminal remains freeze:

joseescobar60 gravatar image joseescobar60  ( 2012-11-21 04:17:50 -0600 )edit

And don't call ros::init twice.

Lorenz gravatar image Lorenz  ( 2012-11-21 04:18:50 -0600 )edit

i let the code like is in the last edit, and the response is the same, subscriber shows to me what he is readieng but the publisher is dead

joseescobar60 gravatar image joseescobar60  ( 2012-11-21 04:25:07 -0600 )edit

The call to ros::spin will block so your program will never enter the publish loop. Get rid of the call to ros::spin and only keep ros::spinOnce.

Lorenz gravatar image Lorenz  ( 2012-11-21 04:25:36 -0600 )edit

in the last edit i shows the answer....

joseescobar60 gravatar image joseescobar60  ( 2012-11-21 04:34:23 -0600 )edit

Do you know how can i take the message that i'm reading and deliver that message to the publisher, i need the topic /sh_ffj3_mixed_position_velocity_controller/command take the message that i'm subscribing.

joseescobar60 gravatar image joseescobar60  ( 2012-11-21 23:19:18 -0600 )edit

Either use a global variable (considered bad style) or use a class that contains your subscriber, publisher and the publish loop and use a member variable.

Lorenz gravatar image Lorenz  ( 2012-11-21 23:22:04 -0600 )edit

how can i create a global variable of the type Float64?

joseescobar60 gravatar image joseescobar60  ( 2012-11-21 23:57:16 -0600 )edit
3

answered 2012-11-22 21:06:30 -0600

updated 2012-11-22 21:07:28 -0600

Of course you can have a publisher and subscriber in the same node. The only thing to watch out for is that the publisher mustn't modify the message once it's published, because the subscriber will typically share the same message object (no copy).

Your code is fine, because you always create a new msg object each time you publish it, and never touch it after publishing.

edit flag offensive delete link more
3

answered 2012-11-21 02:50:42 -0600

dornhege gravatar image

Sure. Publisher and Subscriber in one node is a very common use case.

edit flag offensive delete link more

Comments

how? Do you have any example??

joseescobar60 gravatar image joseescobar60  ( 2012-11-21 02:51:42 -0600 )edit

Just copy the indiceCallback and subscribe line from your subscriber to your publisher node and that should be it.

dornhege gravatar image dornhege  ( 2012-11-21 03:36:19 -0600 )edit

that is already what i do, the indiceCallback outside of the main, and the subscriber inside of the main, but nothing happens

joseescobar60 gravatar image joseescobar60  ( 2012-11-21 03:43:59 -0600 )edit
1

who has given you 3 thumbs up with your ridiculous unhelpful answer?

Farid gravatar image Farid  ( 2018-09-13 12:24:18 -0600 )edit

Question Tools

2 followers

Stats

Asked: 2012-11-21 02:15:36 -0600

Seen: 18,933 times

Last updated: Nov 22 '12