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

publishing a message from the subscriber

asked 2012-11-22 00:57:41 -0500

joseescobar60 gravatar image

Hello,

I created a node that has a subscriber and a publisher, the thing is that i need to deliver to the publisher the message that the subscriber is reading, when i do rosrun i obtain:

  jose@ubuntu:~/prueba$ rosrun prueba segunda
segunda: /usr/include/boost/smart_ptr/shared_ptr.hpp:412: boost::shared_ptr<T>::reference boost::shared_ptr<T>::operator*() const [with T = const std_msgs::Float64_<std::allocator<void> >, boost::shared_ptr<T>::reference = const std_msgs::Float64_<std::allocator<void> >&]: Assertion `px != 0' failed.
Abortado (`core' generado)

the code of my node is the next one:

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


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

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

  ros::init(argc, argv, "segunda");
  ros::NodeHandle n;

   /////SUSCRIPTOR/////

  ros::Subscriber sub = n.subscribe("indice", 50, indiceCallback);
  //ros::spinOnce();

   /////PUBLICADOR/////

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

  int count = 0;
  while (ros::ok())
  {
    //std_msgs::Float64::ConstPtr msg;
   // msg = mensaje;
    pub.publish(mensaje);
    ros::spinOnce();
    loop_rate.sleep();
    ++count;
  }

  return 0;
}
edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
3

answered 2012-11-22 01:18:54 -0500

Lorenz gravatar image

Before a message has been received, your global const pointer will not contain a valid reference which means you cannot use it for publishing. Before calling publish, you need to check if the pointer is valid:

if(mensaje) {
  pub.publish(mensaje);
}
edit flag offensive delete link more

Question Tools

Stats

Asked: 2012-11-22 00:57:41 -0500

Seen: 822 times

Last updated: Nov 22 '12