1
$\begingroup$

I am trying to spawn my robot model using rospy in my python interface to Gazebo. In the launch file, I spawn the robot using:

<node
name="urdf_spawner"
pkg="gazebo_ros"
type="spawn_model"
respawn="false"
output="screen"
ns="$(arg robot_name)"
args="-urdf -model $(arg robot_model) -param robot_description -z 1.015"/>

What would be the python way of doing the same? I'd be happy to share more details if needed. Thanks in advance!

$\endgroup$

2 Answers 2

0
$\begingroup$

I haven't tried it myself but found something similar. Give it a try and let me know as well if it works for you. Good luck!

This is the code that they used.

import roslaunch

package = 'rqt_gui'
executable = 'rqt_gui'
node = roslaunch.core.Node(package, executable)

launch = roslaunch.scriptapi.ROSLaunch()
launch.start()

process = launch.launch(node)
print(process.is_alive())
process.stop()

In this, they used rqt_gui but not gazebo. I have a feeling that you need to use OS module to run gazebo using python but not sure how.

$\endgroup$
1
  • 1
    $\begingroup$ Thanks for the reply! I ended up launching the ROS node using a launch file only, and defined a function in Python which simply sends the command to the terminal to launch this file (providing the path and launch file name as the arguments) - hence being able to initialize the node whenever needed in my script! $\endgroup$
    – rl4haai
    Aug 2, 2022 at 15:34
0
$\begingroup$

For resetting the robot to the initial pose - I was able to fix this by using

import rospy
from gazebo_msgs.srv import SetModelConfiguration

set_model = rospy.ServiceProxy("/gazebo/set_model_configuration", SetModelConfiguration)

joint_names = ['waist', 'shoulder', 'elbow', 'wrist_angle', 'left_finger', 'right_finger'] #example
joint_positions = [0.0, 0.0, 0.0, 0.0, 0.0195, -0.0195]

set_model.call(model_name=robot_name,
               urdf_param_name=robot_description,
               joint_names=joint_names,
               joint_positions=joint_positions)

Note, it is important to pause the Gazebo physics before calling set_model and unpause once done.

When launching the robot for the first time in the Gazebo world, I use the gazebo_ros package to spawn the robot using a node of type spawn_model and pass the desired spawn coordinates.

$\endgroup$
2
  • $\begingroup$ Please ask a new self contained question instead of trying to get a follow up answer here. $\endgroup$
    – Tully
    Aug 4, 2022 at 18:43
  • 1
    $\begingroup$ Thanks! My doubt got resolved as to why this worked without the need of sending the spawn coordinates when resetting the robot. I realized I am not restarting the simulation (which would require me to spawn the robot again), but only resetting the world and robot pose to the initial pose configuration (which is where the above-mentioned Gazebo Service came handy)! $\endgroup$
    – rl4haai
    Aug 8, 2022 at 14:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.