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

Call to publish() on an invalid Publisher

asked 2016-04-16 10:25:52 -0600

jeping123456 gravatar image

updated 2021-08-22 13:27:51 -0600

Mike Scheutzow gravatar image

i rolslaunch the file of one node but got the error message as following.

[FATAL] [1460819328.350552093]: ASSERTION FAILED
    file = /opt/ros/indigo/include/ros/publisher.h
    line = 102
    cond = false
    message = 
[FATAL] [1460819328.350660121]: Call to publish() on an invalid Publisher
[FATAL] [1460819328.350700112]:

i am doubt that publisher.h has some error, but it is OK when i rolslaunch anothee file of another node.

edit retag flag offensive close merge delete

Comments

Please format console copy/pastes with the Preformatted text button (the one with 101010 on it). Otherwise the error text gets filtered, and it's not visible.

gvdhoorn gravatar image gvdhoorn  ( 2016-04-16 12:27:01 -0600 )edit

In my case, the issue was caused by calling the shutdown() method from the publisher

ignacio gravatar image ignacio  ( 2021-05-18 09:31:14 -0600 )edit

4 Answers

Sort by » oldest newest most voted
10

answered 2016-04-18 00:23:03 -0600

djsw gravatar image

Has the publisher been initialised? This is an error I recently ran into, and from memory, this error will be thrown if you try to publish on a publisher that has been declared as a class variable, but not initialised either in the constructor or in the function body. If it's initialised in the constructor and the instance of the class is in a different scope to the call to publish(), that will also cause this error (say if you construct your instance in your main loop but try to call publish from a subscriber callback).

edit flag offensive delete link more

Comments

Thanks! This helped.

ai-shwarya gravatar image ai-shwarya  ( 2016-12-05 02:29:46 -0600 )edit
2

answered 2016-04-18 00:44:05 -0600

dmngu9 gravatar image

I had this problem before. In my case, I call a function that has ros publisher in it before declare pub = n.advertise ... in main()

Maybe this will help you

edit flag offensive delete link more
1

answered 2018-12-21 03:21:09 -0600

Farid gravatar image

updated 2018-12-21 03:22:35 -0600

Publisher must be initialized. The following sample code to publish IMU message might be helpful:

void pubIMU(double tsIMU, double gyro_x, double gyro_y, double gyro_z, double acc_x, double acc_y, double acc_z)
{
       sensor_msgs::Imu imuMsg;

//  imuMsg.header.stamp = ros::Time::now();

    imuMsg.header.frame_id = "imu"; //

    imuMsg.angular_velocity.x = gyro_x;
    imuMsg.angular_velocity.y = gyro_y;
    imuMsg.angular_velocity.z = gyro_z;

    imuMsg.linear_acceleration.x = acc_x;
    imuMsg.linear_acceleration.y = acc_y;
    imuMsg.linear_acceleration.z = acc_z;

    pub_imu.publish(imuMsg);
}
void readCSV(const string &strPath2Dataset)
{
       // some function to extract info from a sample csv file:
       // ....
       // ....
       // ....

       pubIMU(timeStampIMU[i], gyro_X[i], gyro_Y[i], gyro_Z[i], acc_X[i], acc_Y[i], acc_Z[i]);
       // ^^^^^real function I'd like to publish^^^^^, ****modify based on your own need!!!
}

int main(int argc, char **argv)
{
    ros::init(argc, argv, "imuPub");
    ros::NodeHandle nh;

    // all your publisher must be defined in the very beginning:
    pub_imu     = nh.advertise      <sensor_msgs::Imu>                  ("/imu_raw"     ,1);
    pub_cam     = nh.advertise      <geometry_msgs::PoseStamped>        ("/cam_pose"    ,1);
    pub_path    = nh.advertise      <nav_msgs::Path>                    ("/cam_path"    ,1);

    while(ros::ok())
    {
        readCSV("test.csv");
        ros::spinOnce();
    }
    return 0;
}
edit flag offensive delete link more

Comments

I tried the same. Still facing the error. Can you help me?

raghav gravatar image raghav  ( 2021-10-27 04:36:37 -0600 )edit
0

answered 2021-08-21 20:59:24 -0600

KhalidOwlWalid gravatar image

I recently had the same problem and dumb me forgot to initialize my publisher, my solution is as follows:

#include <geometry_msgs/Twist.h>
#include <my_custom_srv_msg_pkg/MyCustomServiceMessage.h>
#include <ros/ros.h>

class MoveBB8 {
private:
  ros::Publisher pub;
  ros::NodeHandle nh;

ros::ServiceServer move_bb8_in_circle_custom;
  geometry_msgs::Twist twist;

public:

MoveBB8();
  bool circle_custom_callback(
      my_custom_srv_msg_pkg::MyCustomServiceMessage::Request &request,
      my_custom_srv_msg_pkg::MyCustomServiceMessage::Response &response);
};

MoveBB8::MoveBB8()
    : move_bb8_in_circle_custom{
          nh.advertiseService("/move_bb8_in_circle_custom",
                              &MoveBB8::circle_custom_callback, this)} {
  pub = nh.advertise<geometry_msgs::Twist>("cmd_vel", 10);     // I forgot to initialize this when I first got the error
}

bool MoveBB8::circle_custom_callback(
    my_custom_srv_msg_pkg::MyCustomServiceMessage::Request &request,
    my_custom_srv_msg_pkg::MyCustomServiceMessage::Response &response) {
  ROS_INFO("My callback has been called");
  ROS_INFO("Request duration is %d", request.duration);

  const int size = 1;
  ros::Publisher *pubs[size];

  pubs[0] = &pub;
  for (int i = 0; i < size; ++i) {
    twist.linear.x = 1;
    pubs[i]->publish(twist);
  }

  return true;
}

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

  ros::init(argc, argv, "move_bb8_in_circle_node");

  MoveBB8 move_bb8_in_circle;

  ros::spin();

  return 0;
}
edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2016-04-16 10:25:52 -0600

Seen: 13,636 times

Last updated: Aug 21 '21