How to get the position of certain object in gazebo and publish it on a topic to use it in px4 or ROS?

I am trying to get the position of Aruco Marker in the gazebo and publish it on the topic so that I can use it for different processes. Can anyone help me to sort out the solution? I found a similar question in the links below……

But I can not apply the solution given in the link above. Maybe the answers are ambiguous or it’s me who couldn’t get it? Can someone guide me on how can I get the position and publish it on a topic so I can use it outside the gazebo e.g., PX4 or ROS?

@David.Wright What is exactly the issue you are having?

There isn’t any issue, i didn’t knew how could I get the position of Aruco marker from Gazebo world in a ROS node which is communicating with PX4. I have found a way to get it done.


I know this is not stackoverflow, but you can answer your own question and flag it answered, for future reference :wink: .

You can use fiducials package. It will give you relative position of the marker with respect to the camera.

I have done a few steps to get the position of the drone in the gazebo world with respect to the origin and then get that position into my ROS node (C++).

  1. Create a service in my case the code is “ros::ServiceClient aruco_position_client = nh.serviceClient<gazebo_msgs::GetModelState>
  2. Create object of type GetModelState “gazebo_msgs::GetModelState objstate;”
  3. use the service “objstate.request.model_name = (std::string) (“put your model name here”)”;
    4.Now you can extract the position of the marker and publish it.

Note: the position is of w.r.t origin.

In response to @fulano, I have written steps to get position w.r.t origin but now i want to get position of camera w.r.t marker.

Now I am working on extracting the position of marker w.r.t camera. I have found some solution in python. But I couldn’t implement it in C++ till now. As for python I just need to add the name of both models and that’s it. But it’s not working in C++.

for python, the commands are like:

" model_coordinates = rospy.ServiceProxy(’/gazebo/get_model_state’, GetModelState)
while true:
resp_coordinates = model_coordinates(‘table’, ‘iris’)
dx = resp_coordinates.pose.position.x
dy = resp_coordinates.pose.position.y
dz = resp_coordinates.pose.position.z

I tried to do same for my C++ in my command objstate.request.model_name = (std::string) (“table”,“iris”);

but I am getting the position of Iris w.r.t origin. @netakoi @Jaeyoung-Lim Can you see if am doing something wrong?


Couldn’t solve the problem, still stuck on the same issue, @netakoi @Jaeyoung-Lim any suggestions?

Can you tell me more detail about your objective & constraints?
Because there are various ways to achieve relative position between camera and Aruco markers.
One of the easiest ways is using fiducials package as I mentioned. It will directly publish you the information you want in ROS envrionment.
For getting the relative position without ROS, it’s implemented in OpenCV library. You can simply use it.

I got the position of aruco marker with respect to iris drone. I can share my code where I am getting the position of aruco and iris with respect to the origin and also w.r.t to eachother. See the code below:

indent preformatted text by 4 spaces

ros::ServiceClient aruco_position_client = nh.serviceClient<gazebo_msgs::GetModelState>

gazebo_msgs::GetModelState objstate;

geometry_msgs::Pose aruco_pose;

objstate.request.model_name = “iris”;
objstate.request.relative_entity_name = “aruco_visual_marker_7”; //Add this line in order to have a relative position ,so here we are getting position of iris w.r.t marker.Ignore it to get position w.r.origin

if (( && objstate.response.success)){

  //ROS_INFO("This is position of Aruco Marker");
  aruco_pose = objstate.response.pose;
  std::cout<<"The Position of aruco marker is : "<<aruco_pose<<"\n";



indent preformatted text by 4 spaces

Now just create a topic of type geometry_msgs::Pose and publish the aruco_pose value on it .

So, you do have marker’s position w.r.t origin and iris’ position w.r.t to origin, right?

Correct, w.r.t origin. @fulano