Rotation Problem using rotateX, rotateY, rotateZ

Need help, or want to share a macro? Post here!
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
Post Reply
doxley
Posts: 35
Joined: Tue Jun 18, 2019 6:55 pm

Rotation Problem using rotateX, rotateY, rotateZ

Post by doxley »

In a project I'm doing, I need to rotate an LCS such that the z-axis is aligned with the z-axis of a second LCS using two rotations along its primary axes. I'm attempting to do this using the Matrix.rotate(X/Y/Z) with the LCS positioned at the origin. There is obviously a misunderstanding on my part as I'm getting nothing like I intended.

In the example below, I'm using the yaw/pitch/roll angles. I realize that the second and third rotations would be applied to a rotated matrix, so the example would not work as shown (if all three rotations were attempted). However, I did presume that a rotation of only the yaw would return a matrix such that getYawPitchRoll would return a yaw angle equal to what I had applied (if I have misunderstood the XYZ association to YPR, I would still have expected one angle to match).

What I need is to determine the proper angles such that I can successively apply them to achieve the orientation of the z-axis I need (I need to use the rotations in this form to correspond to what is happening physically).

Code: Select all

doc = FreeCAD.ActiveDocument

# create a local coordinate system (at the origin) with an arbitrary rotation
lcs1 = doc.addObject('PartDesign::CoordinateSystem', "LCS_1")
rot = FreeCAD.Rotation(40, 62, 38)
lcs1.Placement.Rotation = rot

print(f"LCS1_YPR: {lcs1.Placement.Rotation.getYawPitchRoll()}")

lcs2 = doc.addObject('PartDesign::CoordinateSystem', "LCS_2")

yaw, pitch, roll = lcs1.Placement.Rotation.getYawPitchRoll()

# rotate the second coordinate system to match the orientation of the first
matrix = lcs2.Placement.Matrix
matrix.rotateZ(math.degrees(yaw))
# matrix.rotateX(math.degrees(pitch))       # would be applied to a rotated LCS
# matrix.rotateY(math.degrees(roll))
lcs2.Placement.Matrix = matrix

# print the final orientation of the second coordinate system 
print(f"LCS2_YPR: {lcs2.Placement.Rotation.getYawPitchRoll()}")
Any help/corrections/insight would be appreciated.

Thanks,
--Don

Code: Select all

OS: Ubuntu 22.10 (ubuntu:GNOME/ubuntu)
Word size of FreeCAD: 64-bit
Version: 0.20.1.
Build type: Release
Python 3.10.7, Qt 5.15.4, Coin 4.0.0, Vtk 9.1.0, OCC 7.6.3
Locale: English/United States (en_US)
Installed mods: 
  * Curves 0.5.2
  * Assembly4 0.12.3
  * Manipulator 1.4.9
  * fasteners 0.3.50
User avatar
Roy_043
Veteran
Posts: 8450
Joined: Thu Dec 27, 2018 12:28 pm

Re: Rotation Problem using rotateX, rotateY, rotateZ

Post by Roy_043 »

Change:

Code: Select all

matrix.rotateZ(math.degrees(yaw))
to:

Code: Select all

matrix.rotateZ(math.radians(yaw))
doxley
Posts: 35
Joined: Tue Jun 18, 2019 6:55 pm

Re: Rotation Problem using rotateX, rotateY, rotateZ

Post by doxley »

Thank you @Roy_043 - I changed that twice and still couldn't get it right!! :oops:

That still leaves me with the problem of determining the two rotations that I do need which can be applied successively, though I may be able to figure it out with this crazy mistake out of the way.

Thx,
--Don
User avatar
Roy_043
Veteran
Posts: 8450
Joined: Thu Dec 27, 2018 12:28 pm

Re: Rotation Problem using rotateX, rotateY, rotateZ

Post by Roy_043 »

The order is important:

Code: Select all

matrix.rotateX(math.radians(roll))
matrix.rotateY(math.radians(pitch))
matrix.rotateZ(math.radians(yaw))
doxley
Posts: 35
Joined: Tue Jun 18, 2019 6:55 pm

Re: [SOLVED] Rotation Problem using rotateX, rotateY, rotateZ

Post by doxley »

Yes, and I had the pitch and roll axes misassigned.

Thanks for the help - I was going nuts over stupid mistakes.

Cheers,
--Don
User avatar
onekk
Veteran
Posts: 6144
Joined: Sat Jan 17, 2015 7:48 am
Contact:

Re: [SOLVED] Rotation Problem using rotateX, rotateY, rotateZ

Post by onekk »

doxley wrote: Fri Mar 31, 2023 1:33 pm Yes, and I had the pitch and roll axes misassigned.

Thanks for the help - I was going nuts over stupid mistakes.

Cheers,
--Don
rotateX[YZ]() will modify the Matrix and the changes are "additive", so order is important, see the inverse order.

Regards

Carlo D.
GitHub page: https://github.com/onekk/freecad-doc.
- In deep articles on FreeCAD.
- Learning how to model with scripting.
- Various other stuffs.

Blog: https://okkmkblog.wordpress.com/
Post Reply