Skip to content

Instantly share code, notes, and snippets.

@alifahrri
Last active November 20, 2018 18:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alifahrri/8bd97d6e80a66a7288d0664eddae6a28 to your computer and use it in GitHub Desktop.
Save alifahrri/8bd97d6e80a66a7288d0664eddae6a28 to your computer and use it in GitHub Desktop.
cheatsheet for rospy

get rospy.Time

now = rospy.Time.now()
now = rospy.get_rostime()
rospy.loginfo("Current time %i %i", now.secs, now.nsecs)

get time in seconds

now = rospy.get_time()
rospy.loginfo("Current time %s", now)

sleeper

# sleep for 10 seconds
rospy.sleep(10.)

# sleep for duration
d = rospy.Duration(10, 0)
rospy.sleep(d)

parameter

all_names = rospy.get_param_names()
global_name = rospy.get_param("/global_name")
relative_name = rospy.get_param("relative_name")
private_param = rospy.get_param('~private_name')    
default_param = rospy.get_param('default_param', 'default_value')

# fetch a group (dictionary) of parameters
gains = rospy.get_param('gains')
p, i, d = gains['P'], gains['I'], gains['D']

timer

def my_callback(event):
    print 'Timer called at ' + str(event.current_real)
rospy.Timer(rospy.Duration(2), my_callback)

service

rospy.wait_for_service('add_two_ints')
add_two_ints = rospy.ServiceProxy('add_two_ints', AddTwoInts)
try:
  resp1 = add_two_ints(x, y)
except rospy.ServiceException as exc:
  print("Service did not process request: " + str(exc))

tf

#type(pose) = geometry_msgs.msg.Pose
quaternion = (
    pose.orientation.x,
    pose.orientation.y,
    pose.orientation.z,
    pose.orientation.w)
euler = tf.transformations.euler_from_quaternion(quaternion)
roll = euler[0]
pitch = euler[1]
yaw = euler[2]
quaternion = tf.transformations.quaternion_from_euler(roll, pitch, yaw)
#type(pose) = geometry_msgs.msg.Pose
pose.orientation.x = quaternion[0]
pose.orientation.y = quaternion[1]
pose.orientation.z = quaternion[2]
pose.orientation.w = quaternion[3]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment