Home | Tutorials | Wiki | Issues
Ask Your Question
0

SITL Gazebo, Reset model using code

asked 2018-12-17 11:07:31 -0500

shrit gravatar image

Hello, I am looking to reset the model position, I found that it is possible using the Edit panel in gazebo. I have tried to write several code to reset the mode automatically from the outside even being inspired by the source code of gazebo itself. However, the code I have written does not do anything. I do not know if you have an idea why it does not working. Should the code be implemented as a plugin?? or I missed something ?

         /*  C++ Standard library include */                                                                                                                                                                               
# include <cstdlib>                                                                                                                                                                                               
# include <string>                                                                                                                                                                                                
# include <future>                                                                                                                                                                                                
# include <chrono>                                                                                                                                                                                                     
# include <thread>                                                                                                                                                                                                
# include <vector>                                                                                                                                                                                                

/*  locale defined include */                                                                                                                                                                                     
# include "gazebo.hh"                                                                                                                                                                                             

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

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

  /*  start resetting the world  */                                                                                                                                                                               

  gazebo::transport::PublisherPtr reset_pub;                                                                                                                                                                      

  gazebo::transport::PublisherPtr  userCmdPub;                                                                                                                                                                    

  node->Init("iris.world");                                                                                                                                                                                       
  node->GetTopicNamespace();                                                                                                                                                                                      

  //  ~/world_control                                                                                                                                                                                             

  reset_pub = node->Advertise<gazebo::msgs::WorldControl>("~/world_control");                                                                                                                                     

  userCmdPub = node->Advertise<gazebo::msgs::UserCmd>("~/user_cmd");                                                                                                                                              


  gazebo::msgs::WorldControl msg;                                                                                                                                                                                 

  //  w_ctrl.reset(); //->set_all(true); //I believe this is equal to indicating a world reset                                                                                                                    
  msg.mutable_reset()->set_all(false);                                                                                                                                                                            
  msg.mutable_reset()->set_time_only(false);                                                                                                                                                                      

  msg.mutable_reset()->set_model_only(true);                                                                                                                                                                      

  gazebo::msgs::UserCmd userCmdMsg;                                                                                                                                                                               
  userCmdMsg.set_description("Reset models");                                                                                                                                                                     
  userCmdMsg.set_type(gazebo::msgs::UserCmd::WORLD_CONTROL);                                                                                                                                                      
  userCmdMsg.mutable_world_control()->CopyFrom(msg);                                                                                                                                                              
  userCmdPub->Publish(userCmdMsg);                                                                                                                                                                                

  reset_pub->Publish(msg);                                                                                                                                                                                        

}
edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted
0

answered 2018-12-21 02:56:43 -0500

nzlz gravatar image

A good way of resetting the simulation is to make use of the /gazebo/reset_world service provided by gazebo_ros_packages.

Also I see you are using the SITL plugin.. maybe you could find some useful code related to the erlecopter in gym-gazebo. Check folders gym_gazebo/envs/erlecopter and gym-gazebo/examples/erlecopter/. Note that this is not under maintenance anymore, but you can just take a look.

edit flag offensive delete link more
0

answered 2018-12-21 05:07:55 -0500

shrit gravatar image

Thanks for your answer,

Actually, It is not possible to reset the worlds, due to the way the PX4 code works. If I reset the world using the the button inside gazebo Edit panel, the entire simulation is going to stop.

The only possible solution is to reset the model position after landing the quad copters.

I am going to look at the implementation of the reset world by gazebo ros package, and get some inspiration to reset the model positions. I you know any possible way to reset the model, please let me know.

Thanks for your answers,

edit flag offensive delete link more

Comments

I checked the code at gym-gazebo and APM is just launched again, from scratch. You could use delete_model and spawn_urdf_model / spawn_sdf_model gazebo services every time. It would be much quicker for sure. Or also you could manually change the Pose of the robot.

nzlz gravatar imagenzlz ( 2018-12-24 07:37:03 -0500 )edit

Hello, I understand what you mean. However, I am in a different case, I do not use python, neither ROS, the above is an example to be inserted in a bigger project. Thus, it will be complicated to use ROS. Do you have any idea how to do it with out these dependencies. Thanks for your answers, best regards

shrit gravatar imageshrit ( 2019-01-07 09:27:42 -0500 )edit
Login/Signup to Answer

Question Tools

1 follower

Stats

Asked: 2018-12-17 11:07:31 -0500

Seen: 245 times

Last updated: Dec 21 '18