ros_gazebo_gym.common.helpers

Contains several helper functions that are used in the several environments.

Module Contents

Classes

DummyFile

Dummy file class to redirect stderr to.

Functions

model_state_msg_2_link_state_dict(link_state_msgs)

Converts the a gazebo_msgs/ModelState

pose_msg_2_pose_dict(pose_msg)

Create a pose dictionary {x, y, z, rx, ry, rz, rw} out of a

lower_first_char(string)

De-capitalize the first letter of a string.

wrap_space_around(text)

Wrap one additional space around text if it is not already present.

to_pascal_case(text)

Convert a string to pascal case.

to_snake_case(text)

Convert a string to snake case.

list_2_human_text(input_list[, separator, end_separator])

Function converts a list of values into human readable sentence.

split_dict(input_dict, *args)

Split a dictionary into smaller dictionaries based on the keys.

split_bounds_dict(bounds_dict)

Splits the bounding region dictionary into two separate bounding dictionaries,

gripper_width_2_finger_joints_positions(input_dict, joints)

Replaces a 'gripper_width' key in a dictionary with corresponding finger joint

split_pose_dict(pose_dict)

Splits a pose dictionary into two separate pose dictionaries, one for the

shallow_dict_merge(*args[, order])

Given several dicts, merge them into a new dict as a shallow copy.

flatten_list(input_list)

Function used to flatten a list containing sublists. It does this by calling

deep_update(d[, u, fixed])

Updates a dictionary recursively (i.e. deep update). This function takes a update

is_sublist(lst1, lst2)

Checks whether lst1 is a sublist of lst2.

remove_dict_none_values(input_dict)

Removes all the None values from a dictionary.

has_invalid_type(variable, variable_types[, ...])

Validates whether a variable or its attributes has an invalid type.

action_server_exists(topic_name)

Checks whether a topic contains an action server

find_gazebo_model_path(model_name, models_directory_path)

Finds the path of the sdf or urdf file that belongs to a given

get_orientation_euler(quaternion)

Converts pose (position, orientation) to euler angles.

quaternion_norm(quaternion)

Calculates the norm of a quaternion.

normalize_quaternion(quaternion)

Normalizes a given quaternion.

suppress_stderr()

Suppresses the stderr output of a code block.

Converts the a gazebo_msgs/ModelState message into a state dictionary. Contrary to the original ModelState message, in the model state dictionary the poses and twists are grouped per link/model.

Parameters:

link_state_msgs (gazebo_msgs.msg.ModelState) – A ModelState message.

Returns:

A model_state dictionary.

Return type:

dict

ros_gazebo_gym.common.helpers.pose_msg_2_pose_dict(pose_msg)[source]

Create a pose dictionary {x, y, z, rx, ry, rz, rw} out of a geometry_msgs.msg.Pose message.

Parameters:

pose_msg (geometry_msgs.msg.Pose) – A pose message

Returns:

Dictionary that contains the pose.

Return type:

dict

ros_gazebo_gym.common.helpers.lower_first_char(string)[source]

De-capitalize the first letter of a string.

Parameters:

string (str) – The input string.

Returns:

The de-capitalized string.

Return type:

str

Note

This function is not the exact opposite of the capitalize function of the standard library. For example, capitalize(‘abC’) returns Abc rather than AbC.

ros_gazebo_gym.common.helpers.wrap_space_around(text)[source]

Wrap one additional space around text if it is not already present.

Parameters:

text (str) – Text

Returns:

Text with extra spaces around it.

Return type:

str

ros_gazebo_gym.common.helpers.to_pascal_case(text)[source]

Convert a string to pascal case.

Parameters:

text (str) – Text

Returns:

Text in pascal case.

Return type:

str

ros_gazebo_gym.common.helpers.to_snake_case(text)[source]

Convert a string to snake case.

Parameters:

text (str) – Text

Returns:

Text in snake case.

Return type:

str

ros_gazebo_gym.common.helpers.list_2_human_text(input_list, separator=', ', end_separator=' & ')[source]

Function converts a list of values into human readable sentence.

Example

Using this function a list of 4 items [item1, item2, item3, item4] becomes item2, item3 & item4.

Parameters:

input_list (list) – A input list.

Returns:

A human readable string that can be printed.

Return type:

str

ros_gazebo_gym.common.helpers.split_dict(input_dict, *args)[source]

Split a dictionary into smaller dictionaries based on the keys.

Example

split_dict_list = split_dict(
    input_dict, ["first_dict_key1", "first_dict_key2"],
    ["second_dict_key1", "second_dict_key2"]
)
Parameters:
  • input_dict (dict) – Input dictionary.

  • *args (list) – Lists containing the keys you want to have in the successive dictionaries.

Returns:

A list containing the splitted dictionaries.

Return type:

list

ros_gazebo_gym.common.helpers.split_bounds_dict(bounds_dict)[source]

Splits the bounding region dictionary into two separate bounding dictionaries, one for the ee_pose and one fore the joint_pose.

Parameters:

bounds_dict (dict) – Original bounds dictionary.

Returns:

tuple containing:

  • dict: ee_pose bounding region dictionary.

  • dict: joint_pose bounding region dictionary.

Return type:

(tuple)

ros_gazebo_gym.common.helpers.gripper_width_2_finger_joints_positions(input_dict, joints)[source]

Replaces a ‘gripper_width’ key in a dictionary with corresponding finger joint position keys.

Parameters:
  • input_dict (dict) – The dictionary that contains the ‘gripper_Width’.

  • joints (list) – The available finger joints.

Returns:

The new dictionary that contains the finger joint positions.

Return type:

dict

ros_gazebo_gym.common.helpers.split_pose_dict(pose_dict)[source]

Splits a pose dictionary into two separate pose dictionaries, one for the ee_pose and one fore the joint_pose.

Parameters:

bounding_region (dict) – Original bounds dictionary.

Returns:

tuple containing:

  • dict: ee_pose bounding region dictionary.

  • dict: joint_pose dictionary.

Return type:

(tuple)

ros_gazebo_gym.common.helpers.shallow_dict_merge(*args, order=None)[source]

Given several dicts, merge them into a new dict as a shallow copy.

Parameters:
  • args (dict) – The input dictionaries.

  • order (list) – The order in which you want to have the keys.

Returns:

The new merged dictionary.

Return type:

dict

ros_gazebo_gym.common.helpers.flatten_list(input_list)[source]

Function used to flatten a list containing sublists. It does this by calling itself recursively.

Parameters:

input_list (list) – A list containing strings or other lists.

Returns:

The flattened list.

Return type:

list

ros_gazebo_gym.common.helpers.deep_update(d, u=None, fixed=False, **kwargs)[source]

Updates a dictionary recursively (i.e. deep update). This function takes a update dictionary and/or keyword arguments. When a keyword argument is supplied, the key-value pair is changed if it exists somewhere in the dictionary.

Parameters:
  • d (dict) – Dictionary you want to update.

  • u (dict, optional) – The update dictionary.

  • fixed (bool, optional) – Whether you want the input dictionary to be fixed (i.e. only change keys that are already present). Defaults to False.

  • **kwargs – Keyword arguments used for creating the dictionary keys.

Returns:

The updated dictionary.

Return type:

dict

See also

Based on the answer given by @alex-martelli on this stackoverflow question.

ros_gazebo_gym.common.helpers.is_sublist(lst1, lst2)[source]

Checks whether lst1 is a sublist of lst2.

Parameters:
  • lst1 (list) – List 1.

  • lst2 (list) – List 2.

Returns:

Boolean specifying whether lst1 is a sublist of lst2.

Return type:

bool

ros_gazebo_gym.common.helpers.remove_dict_none_values(input_dict)[source]

Removes all the None values from a dictionary.

Parameters:

input_dict (dict) – The input dictionary.

Returns:

The dictionary without the None values.

Return type:

dict

ros_gazebo_gym.common.helpers.has_invalid_type(variable, variable_types, items_types=None, depth=0)[source]

Validates whether a variable or its attributes has an invalid type.

Parameters:
  • variable (object) – The variable you want to check.

  • variable_types (tuple) – The type the variable can have.

  • items_types (tuple) – The types the dictionary or list values can have.

Returns:

tuple containing:

  • bool: A bool specifying whether a type was invalid.

  • int: The maximum depth at which the type was invalid.

  • type: The types that were invalid.

Return type:

(tuple)

ros_gazebo_gym.common.helpers.action_server_exists(topic_name)[source]

Checks whether a topic contains an action server is running.

Parameters:

topic_name (str) – Action server topic name.

Returns:

Boolean specifying whether the action service exists.

Return type:

bool

ros_gazebo_gym.common.helpers.find_gazebo_model_path(model_name, models_directory_path, extension='')[source]

Finds the path of the sdf or urdf file that belongs to a given model_name. This is done by searching in the models_directory_path folder. If no file was found the model file path is returned empty.

Parameters:
  • model_name (str) – The name of the model for which you want to find the path.

  • models_directory_path (str) – The path of the folder that contains the gazebo models. extension (str, optional): The model path extension. Defaults to "" meaning that the function will first look for a sdf file and if that is not found it will look for a urdf file.

Returns:

tuple containing:

  • str: The path where the sdf or urdf model file can be found.

  • str: Extension of the model file.

Return type:

(tuple)

ros_gazebo_gym.common.helpers.get_orientation_euler(quaternion)[source]

Converts pose (position, orientation) to euler angles.

Parameters:

quaternion (geometry_msgs.Pose) – Input quaternion.

Returns=:
EulerAngles: Object containing the

yaw (z), pitch (y) and roll (x) euler angles.

ros_gazebo_gym.common.helpers.quaternion_norm(quaternion)[source]

Calculates the norm of a quaternion.

Parameters:

Quaternion (geometry_msgs.msg.Quaternion) – A quaternion.

Returns:

The norm of the quaternion.

Return type:

float

ros_gazebo_gym.common.helpers.normalize_quaternion(quaternion)[source]

Normalizes a given quaternion.

Parameters:

quaternion (geometry_msgs.msg.Quaternion) – A quaternion.

Returns:

The normalized quaternion.

Return type:

geometry_msgs.msg.Quaternion

class ros_gazebo_gym.common.helpers.DummyFile[source]

Bases: object

Dummy file class to redirect stderr to.

write(x)[source]

Writes the given string to the dummy file.

ros_gazebo_gym.common.helpers.suppress_stderr()[source]

Suppresses the stderr output of a code block.

Example

with suppress_stderr():
    # Code block that will not print stderr.
    sys.stderr.write("This will not be printed.")