Ask Your Question
1

Spawn Gazebo Model Editor Model with ROS Service

asked 2021-07-15 03:31:18 -0500

amoo29 gravatar image

Hello!

is there a way to spawn a model in Gazebo created in the Gazebo Model Editor in the Terminal? Like with a Rosrun/Rosservice etc. command?

Path to the models: /home/user/model_editor_models/RACETRACK_xxx

Two Files in these folders:

model.config

model.sdf

Context / The reason I need this: I am using Matlab to control ROS & Gazebo and I have 17 different Racetracks, all built in the Model Editor. I have a predefined trajectory that my car follows and I'm subscribing to the image topic with Matlab to process the camera images. I already wrote a Matlab script that does everything other than spawning the racetracks by itself. I'd like to make an additional for-loop for spawning the racetracks so that I have a fully automated script, which I can run overnight. Right now I have to change the Racetracks manually.

I am using Gazebo 11.6

(I already asked this question in Gazebo Answers but didn't get an answer.)

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
1

answered 2021-07-30 08:40:56 -0500

amoo29 gravatar image

Okay I found the answer by myself.

There is a ROS SERVICE called /gazebo/spawn_sdf_model. We need some parameters for this service and I wrote a MATLAB .m test file to spawn and remove all models I need (for test purposes).

If you need to write a Python Script for that, check out this answer: https://answers.ros.org/question/3370...

Heres my MATLAB script:

% initlize ROS in MATLAB
rosinit;
disp("Initilizing ROS, wait please ...")
pause(2)
disp("Connected to ROS Master\n\n")

% List with all file name numbers
model_num_list = ["001", "002","003","004"];
% iterate through every track
for n_track = 1:length(model_num_list)
    % model name
    me_track = "MA_" + string(model_num_list(n_track));
    % path to model editor model sdf
    sdf_path = "/home/amo/model_editor_models/" + me_track + "/model.sdf";
    % read sdf as textfile
    sdf_table = table2array(readtable(sdf_path, 'FileType', "text"));
    % we need to send the sdf in string format --> empty string
    sdf_string = "";
    % iterate through every line of the sdf
    for n_sdf = 1:length(sdf_table)
        % attach line to string
        sdf_string = strcat(sdf_string, sdf_table{n_sdf}, "\n");
    end
    % initiate spawn service
    track_spawn_client = rossvcclient('/gazebo/spawn_sdf_model');
    track_spawn_service = rosmessage(track_spawn_client);
    % Parameters of service
    track_spawn_service.ModelName = me_track; % model name
    track_spawn_service.ModelXml = sdf_string; % created string of sdf
    track_spawn_service.RobotNamespace = "/" + me_track; % namespace of robot
    track_spawn_service.ReferenceFrame = 'world'; % referenceframe
    response = call(track_spawn_client,track_spawn_service); % call service


    input("ENTER to remove Track")

    %%% REMOVE SPAWNED TRACK %%%
    % initiate spawn service
    track_remove_client = rossvcclient('/gazebo/delete_model');
    track_remove_service = rosmessage(track_remove_client);
    % Parameters of service
    track_remove_service.ModelName = me_track; % model name
    call(track_remove_client, track_remove_service); % call service
end
edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

2 followers

Stats

Asked: 2021-07-15 03:31:18 -0500

Seen: 25 times

Last updated: Jul 30