I want to control a quadrotor with a Python script and run the simulation as fast as my laptop can, not only in real time. I've modified my world and now the simulation runs with 7-10 real time factor.

My problem is that after acting I want the physics to run for a determinate amount of simulation time, but if I do time.sleep(steptime) the sleep is in real time, which is 7-10 times simulation time. So I need to know the real time factor in Python to divide the sleep time. Is there a way to get it?

  • $\begingroup$ I would suggest to log machine time at begininng of the program and keep track of the simulation time, you can then compare them at each iteration $\endgroup$
    – N. Staub
    Commented Aug 20, 2018 at 18:45
  • $\begingroup$ Thanks N. Staub, your solution is not perfect, but good enougth $\endgroup$
    – kNo
    Commented Aug 22, 2018 at 12:18

1 Answer 1


@kNo the proper way to use time in ROS is generally to use the ros::Time abstraction and the /clock topic. This way you can use the same program with simulated and real time. Your simulator program should be the one publishing on /clock. Trying to access the real time factor is unnecessarily complicated.

  • $\begingroup$ How can make my python script sleep for 5 simulation time seconds using ros::Time and /clock? $\endgroup$
    – kNo
    Commented Aug 23, 2018 at 9:30
  • 1
    $\begingroup$ wiki.ros.org/rospy/Overview/Time#Sleeping_and_Rates : rospy.sleep(5.) (Assuming your simulator is publishing the simulation time on /clock) $\endgroup$
    – al-dev
    Commented Aug 23, 2018 at 10:28
  • $\begingroup$ But this will stop simulation for 5 seconds in simulation time, not my script. I want my code to stop for 5 seconds in simulation time. I'm following this tutorial: linkedin.com/pulse/using-openai-gym-ros-ricardo-tellez There, the controller chooses an action, sends it to ROS, unpauses the simulation, sleeps for a time and pauses ROS again. It only works if ROS works in real time. $\endgroup$
    – kNo
    Commented Aug 24, 2018 at 7:42
  • $\begingroup$ @kNo sorry I am getting confused. You say: " I want my code to stop for 5 seconds in simulation time. " This is exactly what rospy.sleep(5.) does. Could you give us some sample code to understand what you are trying to achieve? $\endgroup$
    – al-dev
    Commented Aug 24, 2018 at 10:00
  • $\begingroup$ The pausing of the simulation in the openai gym tutorial is achieved by calling self.gazebo.pauseSim() not by the invocation of the rospy.sleep(). $\endgroup$
    – Tully
    Commented Aug 26, 2021 at 21:37

Your Answer

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

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