Skip to main content

ConvertAngle

Short summary

This function can be used to convert angles from one angular unit into another. The value will change corresponding to the conversion. Example: π radians = 180° = 10800' = 648000" = 200 gradians = 0.5 turns

Return: CNM_RecipeHandling.Angle: a copy of the given angle converted into the desired unit

Parameters

NameTypeCommentKind
angleToConvertAnglethe angle that should be convertedinout
convertToAngleUnitthe angular unit it should be converted toinput

Code

Declaration

FUNCTION ConvertAngle : Angle
VAR_IN_OUT CONSTANT
(* the angle that should be converted *)
angleToConvert :Angle;
END_VAR
VAR_INPUT
(* the angular unit it should be converted to *)
convertTo :AngleUnit := AngleUnit.RADIANS;
END_VAR
VAR
factor :LREAL;
END_VAR
VAR CONSTANT
PI :LREAL := 3.14159265358979323846264338327950288;
(* angle conversions related to radians (base unit) *)
ANGLES : ARRAY[AngleUnit.RADIANS..AngleUnit.TURNS] OF LREAL := [
1.0, // RADIANS (base unit)
PI / 180.0, // DEGREES to radians
PI / 10800.0, // ARC_MINUTES to radians
PI / 648000.0, // ARC_SECONDS to radians
PI / 200.0, // GRADIANS to radians
2.0 * PI // TURNS to radians
];
END_VAR

Implementation

factor := ANGLES[angleToConvert.unit] / ANGLES[convertTo];
ConvertAngle.unit := convertTo;
IF CheckValueIsANumber(angleToConvert.value) THEN
ConvertAngle.value := angleToConvert.value * factor;
ELSE
ConvertAngle.value := angleToConvert.value;
END_IF