Gazebo | Ignition | Community
Ask Your Question

When plugin publishes message, gazebo crashes with "terminate called after throwing an instance of 'gazebo::common::Exception'"

asked 2016-09-14 16:46:44 -0500

FriendlyFire gravatar image

Basically, I have a model plugin to control a robot, and an outside program running more sophisticated code. I want these to communicate. However, whenever the plugin attempts to publish a message, gazebo crashes with the above error. Otherwise everything works fine.

The outside program can successfully subscribe to gazebo topics like "/world_stats", and can also publish successfully to my plugin. Here is the code for the transport stuff.

int main (int _argc, char **_argv)
  gazebo::client::setup(_argc, _argv);

  gazebo::transport::NodePtr node(new gazebo::transport::Node());

  std::cout << "Publishing to " << "~/forecaster/results" << std::endl;
  gazebo::transport::PublisherPtr forecastPublisher = node->Advertise<gazebo::msgs::Vector3d>("~/forecaster/results");

  std::cout << "Subscribing to " << "~/forecaster/commands" << std::endl;
  gazebo::transport::SubscriberPtr commandSubscriber = node->Subscribe("~/forecaster/commands", &Callback);

  // Listen to Gazebo world_stats topic
  //gazebo::transport::SubscriberPtr sub = node->Subscribe("~/world_stats", cb);

  std::cout << "Waiting for connection." << std::endl;
  std::cout << "Connection found." << std::endl;

  while (true) {
    ignition::math::Vector3d vect(5, 7, 9);
    gazebo::msgs::Vector3d msg;
    gazebo::msgs::Set(&msg, vect);


The relevant part of my plugin code is below. The pointers are defined as normal in a head file. Even using the exact same simple Vector3d message code that my other program publishes successfully with, it doesn't work. If I simply comment out the line with "commandPublisher->Publish(command_msg);" it will run. It clears the WaitForConnection() hang up as soon as I run my other program just fine.

void Robot::Load(physics::ModelPtr _model, sdf::ElementPtr _sdf)
  // ... other code ...
  // Setup message stuff
  node = transport::NodePtr(new transport::Node());

  std::cout << "Publishing to " << "~/forecaster/commands" << std::endl;
  commandPublisher = node->Advertise<msgs::Int>("~/forecaster/commands");

  std::cout << "Subscribing to " << "~/forecaster/results" << std::endl;
  forecastSubscriber = node->Subscribe("~/forecaster/results", &Robot::Callback, this);

  std::cout << "Waiting for connection." << std::endl;
  std::cout << "Connection found." << std::endl;

void Robot::OnUpdate(const common::UpdateInfo &_info)
  //other code
   printf("Attempting to publish message...\n");
   ignition::math::Vector3d vect(0, 4, 5);
   msgs::Vector3d command_msg;
   msgs::Set(&command_msg, vect);
   printf("Message published.\n");

Frankly I have no idea what's going. The error message is incredibly vague, I am publishing a message the exact same way that works in my program, and it seems like both the program and plugin succesfuly subscribe to each others published topics.

Is there anything special you have to do to publish messages inside a plugin? I haven't included things like headers, Cmake, or all that for brevity but I can add them if needed.

Thanks for any help.

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted

answered 2016-09-14 17:23:36 -0500

chapulina gravatar image

You're trying to publish a message of type msgs::Vector3d, but your publisher advertises msgs::Int. The message types should be the same.

It looks to me that this could be handled more gracefully by Gazebo, with a clearer error message.

edit flag offensive delete link more


Well that's embarrassing. Thanks a lot for the help, I guess sometimes when you've stared at code to long you stop actually reading it.

FriendlyFire gravatar imageFriendlyFire ( 2016-09-14 22:42:01 -0500 )edit

Had the exact same problem, thank you. Too bad after 3 years this still wasn't handled better.

tommy gravatar imagetommy ( 2018-09-13 07:37:53 -0500 )edit

If you catch the exception and print its error string, I suspect it will say [Invalid message type]( I'm not a fan of exceptions myself, but I think that's how they're meant to be used ;)

chapulina gravatar imagechapulina ( 2018-09-13 09:52:11 -0500 )edit

Question Tools

1 follower


Asked: 2016-09-14 16:46:44 -0500

Seen: 1,968 times

Last updated: Sep 14 '16