trafo_example.py
Go to the documentation of this file.
1 #
2 # Transform example
3 #
4 # This python script demonstrates how a transform by a 6D pose (x,y,z,roll,pitch,yaw) is computed.
5 # See coordinate_transforms.md for more details.
6 #
7 
8 import math
9 import numpy as np
10 
11 # Converts roll (rotation about X-axis), pitch (rotation about Y-axis), yaw (rotation about Z-axis)
12 # to a 3x3 rotation matrix. Note: This function uses the "ZYX" notation, i.e. it compute the rotation
13 # by rotation = rot_z * rot_y * rot_x
14 # Input angles roll, pitch, yaw in radians.
15 def eulerZYXToRot3x3(roll, pitch, yaw):
16  # roll: rotation about x axis
17  rot_x = np.array([
18  [1.0, 0.0, 0.0],
19  [0.0, math.cos(roll), -math.sin(roll)],
20  [0.0, math.sin(roll), math.cos(roll)]], np.float64)
21  # pitch: rotation about y axis
22  rot_y = np.array([
23  [math.cos(pitch), 0.0, math.sin(pitch)],
24  [0.0, 1.0, 0.0],
25  [-math.sin(pitch), 0.0, math.cos(pitch)]], np.float64)
26  # yaw: rotation about z axis
27  rot_z = np.array([
28  [math.cos(yaw), -math.sin(yaw), 0.0],
29  [math.sin(yaw), math.cos(yaw), 0.0],
30  [0.0, 0.0, 1.0]], np.float64)
31  # 3x3 rotation matrix
32  rot_3x3 = rot_z @ rot_y @ rot_x
33  return rot_3x3;
34 
35 # Converts roll (rotation about X-axis), pitch (rotation about Y-axis), yaw (rotation about Z-axis)
36 # to quaternion (x, y, z, w). Note: This function uses the "ZYX" notation, i.e. it compute the rotation
37 # by rotation = rot_z * rot_y * rot_x
38 # Input angles roll, pitch, yaw in radians.
39 def eulerZYXToQuaternion(roll, pitch, yaw):
40  cy = math.cos(yaw * 0.5);
41  sy = math.sin(yaw * 0.5);
42  cp = math.cos(pitch * 0.5);
43  sp = math.sin(pitch * 0.5);
44  cr = math.cos(roll * 0.5);
45  sr = math.sin(roll * 0.5);
46  x = sr * cp * cy - cr * sp * sy;
47  y = cr * sp * cy + sr * cp * sy;
48  z = cr * cp * sy - sr * sp * cy;
49  w = cr * cp * cy + sr * sp * sy;
50  return x, y, z, w
51 
52 # Example: rotation by roll = 5, pitch = -10, yaw = 15 degree
53 roll, pitch, yaw = 5, -10, 15
54 rot3x3 = eulerZYXToRot3x3(np.deg2rad(roll), np.deg2rad(pitch), np.deg2rad(yaw))
55 quat = eulerZYXToQuaternion(np.deg2rad(roll), np.deg2rad(pitch), np.deg2rad(yaw))
56 print("roll = {}, pitch = {}, yaw = {}:\nrot3x3 = {}\nquaternion = {}".format(roll, pitch, yaw, rot3x3, quat))
57 # Example output:
58 # roll = 5, pitch = -10, yaw = 15:
59 # rot3x3 = [[ 0.95125124 -0.2724529 -0.14453543]
60 # [ 0.254887 0.95833311 -0.12895841]
61 # [ 0.17364818 0.08583165 0.98106026]]
62 # quaternion = (0.05444693224342944, -0.08065606284759969, 0.1336748975829986, 0.9862358505202384)
trafo_example.eulerZYXToQuaternion
def eulerZYXToQuaternion(roll, pitch, yaw)
Definition: trafo_example.py:39
roswrap::console::print
ROSCONSOLE_DECL void print(FilterBase *filter, void *logger, Level level, const char *file, int line, const char *function, const char *fmt,...) ROSCONSOLE_PRINTF_ATTRIBUTE(7
Don't call this directly. Use the ROS_LOG() macro instead.
trafo_example.eulerZYXToRot3x3
def eulerZYXToRot3x3(roll, pitch, yaw)
Definition: trafo_example.py:15


sick_scan_xd
Author(s): Michael Lehning , Jochen Sprickerhof , Martin Günther
autogenerated on Fri Oct 25 2024 02:47:12