Example
This chapter shows an example for calculating an angle of inclination in a PLC program.
Requirement: the inclination measurement was configured as described in chapter Calculation in the controller.
Variable links in TwinCAT
- "AI Inputs Channel 1" > "Value" is linked to
ay
- "AI Inputs Channel 2" > "Value" is linked to
ax
- "AI Inputs Channel 3" > "Value" is linked to
az
Program code
PROGRAM MAIN
VAR
ax AT %I* : INT;
ay AT %I* : INT;
az AT %I* : INT;
Inclincation : LREAL;
END_VAR
IF (ay <> 0 OR az <> 0) THEN (* Prevent division by 0 *)
Inclincation := ATAN( ax / SQRT( ay * ay + az * az ) ) * 360 / ( 2 * 3.14 );
END_IF
(* Compensate zero crossing of az *)
IF az > 0 THEN
Inclincation := 180 - Inclincation;
END_IF
(* Insert offset to shift the measuring range from -90...270 to -180...+180 *)
IF Inclincation > 180 THEN
Inclincation := Inclincation - 360;
END_IF