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());
node->Init();
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;
forecastPublisher->WaitForConnection();
std::cout << "Connection found." << std::endl;
while (true) {
gazebo::common::Time::MSleep(100);
ignition::math::Vector3d vect(5, 7, 9);
gazebo::msgs::Vector3d msg;
gazebo::msgs::Set(&msg, vect);
forecastPublisher->Publish(msg);
}
gazebo::client::shutdown();
}
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());
node->Init();//world->GetName());
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;
commandPublisher->WaitForConnection();
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);
commandPublisher->Publish(command_msg);
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.