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

Revision history [back]

click to hide/show revision 1
initial version

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/337065/what-is-the-correct-way-to-spawn-a-model-to-gazebo-using-a-python-script/

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