In the context of my simulation, I need to programmatically get the position of robots and objects in the world (in global coordinate system). How can this be done?
I'm using Webots R2023a.
Robotics Stack Exchange is a question and answer site for professional robotic engineers, hobbyists, researchers and students. It only takes a minute to sign up.Sign up to join this community
There are different functions depending whether this information must be accessed in a normal controller, in a Supervisor controller or in a physics plugin. All the functions described below will return the 3D position in meters and expressed in the global (world) coordinate system.
If one is not available already, you need to first add a GPS node to your robot. You can get a reference to this device using the wb_robot_get_device function, enable it with wb_gps_enable and retrieve the values with wb_gps_get_values. Note that the GPS's resolution field must be 0 (the default), otherwise the results will be noisy. It should also be noted that the GPS can be placed on a robot's part (arm, foot, etc.) to get the world/global coordinates of that particular part.
To get the 3D position of any Transform (or derived) node using the Supervisor API you can use the wb_supervisor_node_get_position function.
Another possible approach of getting the 3D position of any Transform (or derived) node placed at the root of the Scene Tree (the nodes visible when the Scene Tree is completely collapsed), is to use the wb_supervisor_field_get_sf_vec3f to read the value of the
translation field of that object.
After adding a physics plugin to your world (under the WorldInfo node), you can retrieve the information directly from the physics engine by using ODE's
dBodyGetPosition function. Note that this function returns the position of the center of mass of the body, which may be different from the center of the Solid.
Lastly, the position of a robot can also be approximated by using odometry or SLAM techniques. This is usually more realistic because most robots don't have a GPS and therefore have no mean of precisely determining their position.
Disclaimer: I'm a software developer of Webots