ℹ️ Select 'Choose Exercise', or randomize 'Next Random Exercise' in selected language.

Choose Exercise:
Timer 00:00
WPM --
Score --
Acc --
Correct chars --

Coordinate Transformation for Rotated Workpiece

CNC Fanuc

Goal -- WPM

Ready
Exercise Algorithm Area
1#define PI 3.1415926535
2
3/*
4* Macro to transform workpiece coordinates based on a rotation angle.
5* This is useful when the workpiece is fixtured at an angle relative to the machine axes.
6*/
7Sub TransformWorkpieceCoordinates(angleDegrees As Float, inputX As Float, inputY As Float, inputZ As Float, ByRef outputX As Float, ByRef outputY As Float, ByRef outputZ As Float)
8Dim angleRadians As Float
9Dim cosAngle As Float
10Dim sinAngle As Float
11
12' --- Input Validation ---
13' Angle validation: Normalize angle to be within 0-360 degrees.
14angleDegrees = Modulo(angleDegrees, 360.0)
15If angleDegrees < 0 Then
16angleDegrees = angleDegrees + 360.0
17End If
18
19' If angle is 0 or 360, no transformation is needed.
20If angleDegrees = 0.0 Then
21outputX = inputX
22outputY = inputY
23outputZ = inputZ
24MsgBox "Rotation angle is 0 degrees. No transformation applied."
25Return
26End If
27
28' Convert angle from degrees to radians for trigonometric functions.
29angleRadians = angleDegrees * (PI / 180.0)
30
31' Calculate cosine and sine of the angle.
32cosAngle = Cos(angleRadians)
33sinAngle = Sin(angleRadians)
34
35' --- Apply 2D Rotation Transformation (around Z-axis) ---
36' The standard 2D rotation matrix is:
37' | cos(theta) -sin(theta) |
38' | sin(theta) cos(theta) |
39' Applied to [x, y]^T
40
41outputX = inputX * cosAngle - inputY * sinAngle
42outputY = inputX * sinAngle + inputY * cosAngle
43
44' Z-axis is typically unaffected by rotation around Z.
45outputZ = inputZ
46
47MsgBox "Coordinates transformed. Original (X,Y,Z): (" + Format(inputX) + "," + Format(inputY) + "," + Format(inputZ) + ")"
48MsgBox "Transformed (X,Y,Z): (" + Format(outputX) + "," + Format(outputY) + "," + Format(outputZ) + ")"
49
50End Sub
51
52/*
53* Helper function to calculate the modulo of two floats.
54*/
55Function Modulo(numerator As Float, denominator As Float) As Float
56' Basic modulo implementation for floats.
57Modulo = numerator - Floor(numerator / denominator) * denominator
58End Function
59
60/*
61* Helper function to get the floor of a float.
62*/
63Function Floor(value As Float) As Float
64' Placeholder for floor function.
65Floor = Int(value) ' Assuming Int truncates towards zero, adjust if needed.
66End Function
67
68/*
69* Helper function to display a message box.
70*/
71Sub MsgBox(message As String)
72Print "[MESSAGE] " + message
73End Sub
74
75/*
76* Helper function to format a float for display.
77*/
78Function Format(value As Float) As String
79Format = "" + value
80End Function
81
82/*
83* Main program to demonstrate coordinate transformation.
84*/
85Sub Main()
86Dim rotationAngle As Float
87Dim originalX As Float, originalY As Float, originalZ As Float
88Dim transformedX As Float, transformedY As Float, transformedZ As Float
89
90Print "--- Test Case 1: Simple Rotation ---"
91rotationAngle = 90.0
92originalX = 10.0
93originalY = 0.0
94originalZ = 5.0
95TransformWorkpieceCoordinates(rotationAngle, originalX, originalY, originalZ, transformedX, transformedY, transformedZ)
96' Expected: X=0, Y=10, Z=5
97
98Print "\n--- Test Case 2: Rotation by 45 degrees ---"
99rotationAngle = 45.0
100originalX = 10.0
101originalY = 10.0
102originalZ = 2.0
103TransformWorkpieceCoordinates(rotationAngle, originalX, originalY, originalZ, transformedX, transformedY, transformedZ)
104' Expected: X=0, Y=14.14, Z=2
105
106Print "\n--- Test Case 3: Zero Rotation ---"
107rotationAngle = 0.0
108originalX = 20.0
109originalY = 30.0
110originalZ = 10.0
111TransformWorkpieceCoordinates(rotationAngle, originalX, originalY, originalZ, transformedX, transformedY, transformedZ)
112' Expected: X=20, Y=30, Z=10
113
114Print "\n--- Test Case 4: Rotation by 360 degrees ---"
115rotationAngle = 360.0
116originalX = 15.0
117originalY = 25.0
118originalZ = 8.0
119TransformWorkpieceCoordinates(rotationAngle, originalX, originalY, originalZ, transformedX, transformedY, transformedZ)
120' Expected: X=15, Y=25, Z=8
121
122Print "\n--- Test Case 5: Negative Angle ---"
123rotationAngle = -90.0
124originalX = 10.0
125originalY = 0.0
126originalZ = 5.0
127TransformWorkpieceCoordinates(rotationAngle, originalX, originalY, originalZ, transformedX, transformedY, transformedZ)
128' Expected: X=0, Y=-10, Z=5
129
130Print "\n--- Test Case 6: Angle greater than 360 ---"
131rotationAngle = 450.0 // Equivalent to 90 degrees
132originalX = 10.0
133originalY = 0.0
134originalZ = 5.0
135TransformWorkpieceCoordinates(rotationAngle, originalX, originalY, originalZ, transformedX, transformedY, transformedZ)
136' Expected: X=0, Y=10, Z=5
137
138End Sub
Algorithm description viewbox

Coordinate Transformation for Rotated Workpiece

Algorithm description:

This macro applies a 2D rotation transformation to workpiece coordinates, which is essential when a part is fixtured at an angle on the CNC machine. It takes the rotation angle (in degrees) and the original X, Y, and Z coordinates as input. The macro calculates the new X and Y coordinates based on standard rotation formulas, leaving Z unchanged. This allows the programmer to define coordinates in the part's natural orientation and have the machine automatically convert them to machine coordinates, simplifying complex setups.

Algorithm explanation:

The `TransformWorkpieceCoordinates` macro takes an angle in degrees and input X, Y, Z coordinates. It first validates and normalizes the angle to be within 0-360 degrees. If the angle is 0 or 360, it directly assigns input to output coordinates, avoiding unnecessary calculations. Otherwise, it converts the angle to radians and computes its cosine and sine. The core transformation uses the 2D rotation matrix: `outputX = inputX * cos(angle) - inputY * sin(angle)` and `outputY = inputX * sin(angle) + inputY * cos(angle)`. The Z-coordinate remains unchanged. Helper functions `Modulo` and `Floor` are used for angle normalization. The time complexity is O(1) as it involves a fixed number of arithmetic and trigonometric operations. Space complexity is O(1) for its local variables.

Pseudocode:

FUNCTION TransformWorkpieceCoordinates(angleDegrees, inputX, inputY, inputZ, OUT outputX, OUT outputY, OUT outputZ):
  angleDegrees = Modulo(angleDegrees, 360.0)
  IF angleDegrees < 0 THEN angleDegrees += 360.0

  IF angleDegrees == 0.0 THEN
    outputX = inputX
    outputY = inputY
    outputZ = inputZ
    RETURN
  END IF

  angleRadians = angleDegrees * (PI / 180.0)
  cosAngle = COS(angleRadians)
  sinAngle = SIN(angleRadians)

  outputX = inputX * cosAngle - inputY * sinAngle
  outputY = inputX * sinAngle + inputY * cosAngle
  outputZ = inputZ
END FUNCTION