pycram.utils#
Implementation of helper functions and classes for internal usage only.
Functions: _block – wrap multiple statements into a single block.
Classes: GeneratorList – implementation of generator list wrappers.
Classes#
Color codes which can be used to highlight Text in the Terminal. For example, |
|
Implementation of generator list wrappers. |
|
A context manager for doing a "deep suppression" of stdout and stderr in |
|
A helper that can be used to define properties of a class like the built-in ones but does not require the class |
Functions#
Get the pose a link would be in if the given joint configuration would be applied to the object. |
|
|
Get rays from min and max bounds as an array of start and end 3D points. |
|
Yield successive n-sized chunks from lst. |
|
Convert axis-angle to quaternion. |
Adjust the given cam_pose orientation such that it is facing the target_pose, which partly depends on the |
|
|
Get the quaternion between the camera and the target. |
|
Transform a vector using a pose. |
Apply a quaternion to a pose. |
|
|
Get the quaternion between two vectors. |
Get the axis and angle between two vectors. |
|
|
Convert a quaternion from WXYZ to XYZW format. |
|
Convert a quaternion from XYZW to WXYZ format. |
|
Convert a quaternion from WXYZ to XYZW format. |
|
Convert a quaternion from XYZW to WXYZ format. |
|
|
|
Checks if the given object is iterable. |
|
Lazily generate the cartesian product of the iterables. |
Translate a pose along a given 3d vector (axis) by a given distance. The axis is given in the local coordinate |
Module Contents#
- pycram.utils.link_pose_for_joint_config(obj: semantic_digital_twin.world_description.world_entity.Body, joint_config: typing_extensions.Dict[str, float]) pycram.datastructures.pose.PoseStamped#
Get the pose a link would be in if the given joint configuration would be applied to the object. This is done by using the respective object in the prospection world and applying the joint configuration to this one. After applying the joint configuration the link position is taken from there.
- Parameters:
obj – The body for which the pose should be calculated
joint_config – Dict with the goal joint configuration
- Returns:
The pose of the link after applying the joint configuration
- pycram.utils.get_rays_from_min_max(min_bound: typing_extensions.Sequence[float], max_bound: typing_extensions.Sequence[float], step_size_in_meters: float = 0.01) numpy.ndarray#
Get rays from min and max bounds as an array of start and end 3D points. Note: The rays are not steped in the x direction as the rays are cast parallel to the x-axis.
Example: >>> min_bound = [0, 0, 0] >>> max_bound = [1, 2, 3] >>> rays = get_rays_from_min_max(min_bound, max_bound, 1) >>> rays.shape (6, 3, 2) >>> rays array([ [[0. , 1. ],
[0. , 0. ], [0. , 0. ]],
- [[0. , 1. ],
[0. , 0. ], [1.5, 1.5]],
- [[0. , 1. ],
[0. , 0. ], [3. , 3. ]],
- [[0. , 1. ],
[2. , 2. ], [0. , 0. ]],
- [[0. , 1. ],
[2. , 2. ], [1.5, 1.5]],
- [[0. , 1. ],
[2. , 2. ], [3. , 3. ]] ])
- Parameters:
min_bound – The minimum bound of the rays, a sequence of 3 floats.
max_bound – The maximum bound of the rays, a sequence of 3 floats.
step_size_in_meters – The step size in meters between the rays.
- Returns:
The rays as an array of shape (n, 3, 2) where n is number of rays, 3 is because each point has x, y, and z,
and 2 is for the start and end points of the rays.
- pycram.utils.chunks(lst: typing_extensions.List | numpy.ndarray, n: int) Iterator[typing_extensions.List]#
Yield successive n-sized chunks from lst.
- Parameters:
lst – The list from which chunks should be yielded
n – Size of the chunks
- Returns:
A list of size n from lst
- class pycram.utils.bcolors#
Color codes which can be used to highlight Text in the Terminal. For example, for warnings. Usage: Firstly import the class into the file. print(f’{bcolors.WARNING} Some Text {bcolors.ENDC}’)
- HEADER = '\x1b[95m'#
- OKBLUE = '\x1b[94m'#
- OKCYAN = '\x1b[96m'#
- OKGREEN = '\x1b[92m'#
- WARNING = '\x1b[93m'#
- FAIL = '\x1b[91m'#
- ENDC = '\x1b[0m'#
- BOLD = '\x1b[1m'#
- UNDERLINE = '\x1b[4m'#
- class pycram.utils.GeneratorList(generator: typing_extensions.Callable)#
Implementation of generator list wrappers.
Generator lists store the elements of a generator, so these can be fetched multiple times.
Methods: get – get the element at a specific index. has – check if an element at a specific index exists.
- _generated = []#
- get(index: int = 0)#
Get the element at a specific index or raise StopIteration if it doesn’t exist.
Arguments: index – the index to get the element of.
- has(index: int) bool#
Check if an element at a specific index exists and return True or False.
Arguments: index – the index to check for.
- pycram.utils.axis_angle_to_quaternion(axis: typing_extensions.List, angle: float) typing_extensions.Tuple#
Convert axis-angle to quaternion.
- Parameters:
axis – (x, y, z) tuple representing rotation axis.
angle – rotation angle in degree
- Returns:
The quaternion representing the axis angle
- class pycram.utils.suppress_stdout_stderr#
Bases:
objectA context manager for doing a “deep suppression” of stdout and stderr in Python, i.e. will suppress all prints, even if the print originates in a compiled C/Fortran sub-function.
This will not suppress raised exceptions, since exceptions are printed to stderr just before a script exits, and after the context manager has exited (at least, I think that is why it lets exceptions through). Copied from https://stackoverflow.com/questions/11130156/suppress-stdout-stderr-print-from-python-functions
- null_fds#
- save_fds#
- __enter__()#
- __exit__(*_)#
- pycram.utils.adjust_camera_pose_based_on_target(cam_pose: pycram.datastructures.pose.PoseStamped, target_pose: pycram.datastructures.pose.PoseStamped, camera_description: pycram.robot_description.CameraDescription) pycram.datastructures.pose.PoseStamped#
- Adjust the given cam_pose orientation such that it is facing the target_pose, which partly depends on the
front_facing_axis of the that is defined in the camera_description.
- Parameters:
cam_pose – The camera pose.
target_pose – The target pose.
camera_description – The camera description.
- Returns:
The adjusted camera pose.
- pycram.utils.get_quaternion_between_camera_and_target(cam_pose: pycram.datastructures.pose.PoseStamped, target_pose: pycram.datastructures.pose.PoseStamped, camera_description: pycram.robot_description.CameraDescription) numpy.ndarray#
Get the quaternion between the camera and the target.
- Parameters:
cam_pose – The camera pose.
target_pose – The target pose.
camera_description – The camera description.
- Returns:
The quaternion between the camera and the target.
- pycram.utils.transform_vector_using_pose(vector: typing_extensions.Sequence, pose) numpy.ndarray#
Transform a vector using a pose.
- Parameters:
vector – The vector.
pose – The pose.
- Returns:
The transformed vector.
- pycram.utils.apply_quaternion_to_pose(pose: pycram.datastructures.pose.PoseStamped, quaternion: numpy.ndarray) pycram.datastructures.pose.PoseStamped#
Apply a quaternion to a pose.
- Parameters:
pose – The pose.
quaternion – The quaternion.
- Returns:
The new pose.
- pycram.utils.get_quaternion_between_two_vectors(v1: numpy.ndarray, v2: numpy.ndarray) numpy.ndarray#
Get the quaternion between two vectors.
- Parameters:
v1 – The first vector.
v2 – The second vector.
- Returns:
The quaternion between the two vectors.
- pycram.utils.get_axis_angle_between_two_vectors(v1: numpy.ndarray, v2: numpy.ndarray) typing_extensions.Tuple[numpy.ndarray, float]#
Get the axis and angle between two vectors.
- Parameters:
v1 – The first vector.
v2 – The second vector.
- Returns:
The axis and angle between the two vectors.
- pycram.utils.wxyz_to_xyzw(wxyz: typing_extensions.List[float]) typing_extensions.List[float]#
Convert a quaternion from WXYZ to XYZW format.
- pycram.utils.xyzw_to_wxyz(xyzw: typing_extensions.List[float]) typing_extensions.List[float]#
Convert a quaternion from XYZW to WXYZ format.
- Parameters:
xyzw – The quaternion in XYZW format.
- pycram.utils.wxyz_to_xyzw_arr(wxyz: numpy.ndarray) numpy.ndarray#
Convert a quaternion from WXYZ to XYZW format.
- Parameters:
wxyz – The quaternion in WXYZ format.
- pycram.utils.xyzw_to_wxyz_arr(xyzw: numpy.ndarray) numpy.ndarray#
Convert a quaternion from XYZW to WXYZ format.
- Parameters:
xyzw – The quaternion in XYZW format.
- class pycram.utils.ClassPropertyDescriptor(fget, fset=None)#
A helper that can be used to define properties of a class like the built-in ones but does not require the class to be instantiated.
- fget#
- fset = None#
- __get__(obj, klass=None)#
- __set__(obj, value)#
- setter(func)#
- pycram.utils.classproperty(func)#
- pycram.utils.is_iterable(obj: typing_extensions.Any) bool#
Checks if the given object is iterable.
- Parameters:
obj – The object that should be checked
- Returns:
True if the object is iterable, False otherwise
- pycram.utils.lazy_product(*iterables: typing_extensions.Iterable, iter_names: typing_extensions.List[str] = None) typing_extensions.Iterable[typing_extensions.Tuple]#
Lazily generate the cartesian product of the iterables.
- Parameters:
iterables – Iterable of iterables to construct product for.
iter_names – Optional names for the iterables for better error messages.
- Returns:
Iterable of tuples in the cartesian product.
- pycram.utils.translate_pose_along_local_axis(pose: pycram.datastructures.pose.PoseStamped, axis: typing_extensions.List | numpy.ndarray, distance: float) pycram.datastructures.pose.PoseStamped#
Translate a pose along a given 3d vector (axis) by a given distance. The axis is given in the local coordinate frame of the pose. The axis is normalized and then scaled by the distance.
- Parameters:
pose – The pose that should be translated
axis – The local axis along which the translation should be performed
distance – The distance by which the pose should be translated
- Returns:
The translated pose