Moving the axis via the PLC
| |
This example requires simulation axes - using real axes poses a risk of injury! This example creates an axis movement. When using real axes, there is a risk of injury from the movement of the axes.
|
Requirements
- A TwinCAT project with an MC project has been created;
see Creating an MC project. - A simulation axis was created in the MC project;
see Create axis object. - A PLC project was created in the TwinCAT project.
Including the PLC library
- 1. Add the Tc3_Mc3Ptp library to the PLC project. To do this, right-click on References in the PLC project and select Add library…
- 2. In the Add Library dialog, select the Tc3_Mc3Ptp library and click OK to confirm.
- The Tc3_Mc3Ptp library has been added to the PLC project. The library's function blocks and data types can now be used.
Writing a PLC program
All necessary data types and function blocks for a simple PLC motion program with MC3 are contained in the Tc3_Mc3Ptp library.
AXIS_REF
For each axis one instance of the data type AXIS_REF is required, which is the interface between PLC and MC3. It contains all the information about the axis that is given to the MC function blocks as a reference.
MC_Power
The MC_Power function block is used to enable the axis and specify its direction of motion.
MC_SetOverride
The MC_SetOverride function block can be used to change the axis's velocity override. By default, the factor is set to 1 (=100%), so changing the factor is optional.
MC_Reset
The function block MC_Reset can be used to reset an error on an axis.
MC_MoveRelative
The MC_MoveRelative function block can be used to move the axis by a specified distance.
Simple programming
- 3. Open MAIN(PRG) under POUs in the PLC project.
- 4. Insert the following declarations into MAIN(PRG).
PROGRAM MAIN
VAR
axis : AXIS_REF;
power : MC_Power;
setOverride : MC_SetOverride;
moveRelative : MC_MoveRelative;
reset : MC_Reset;
stop : MC_Stop;
enableAxis : BOOL;
enableOverride : BOOL;
executeStop : BOOL;
executeReset : BOOL;
executeMove : BOOL;
velFactor : LREAL := 1; // adapt override factor if necessary(1 corresponds to 100 %)
distance : LREAL := ???; // ToDo: set to a reachable position
velocity : LREAL := ???; // ToDo: set velocity for move
END_VAR- 5. Insert the following program code into MAIN(PRG).
power(
Axis := axis,
Enable := enableAxis,
EnablePositive := enableAxis,
EnableNegative := enableAxis,
Status => ,
Error => ,
ErrorId => );
setOverride(
Axis := axis,
Enable := enableOverride,
VelocityFactor := velFactor,
Enabled=> ,
Error=> ,
ErrorId=> );
stop(
Axis := axis,
Execute := executeStop, // The command is executed with a positive edge.
Deceleration := , // If the value is <= 0, the deceleration
// parameterized with the last Move command is used.
Jerk := , // If the value is <= 0, the jerk parameterized
// with the last Move command is used.
Done => ,
Busy => ,
Active => ,
CommandAborted => ,
Error => ,
ErrorId => );
reset(
Axis := axis,
Execute := executeReset, // The command is executed with a positive edge.
Done => ,
Busy => ,
Error => ,
ErrorId => );
moveRelative(
Axis := axis,
Execute := executeMove, // The command is executed with a positive edge.
Distance := distance,
Velocity := velocity,
Acceleration := LREAL_CONST.DEFAULT, // If the value is LREAL_CONST.DEFAULT,
// the standard acceleration from the axis configuration is used.
Deceleration := LREAL_CONST.DEFAULT, // If the value is LREAL_CONST.DEFAULT,
// the standard deceleration from the axis configuration is used.
Jerk := LREAL_CONST.DEFAULT, // If the value is LREAL_CONST.DEFAULT,
// the standard jerk from the axis configuration is applied.
BufferMode := Tc3_Mc3Base.EBufferMode.Buffered,
Done => ,
Busy => ,
Active => ,
CommandAborted => ,
Error => ,
ErrorId => );- 6. When calling
moveRelative, adjust the distance and dynamics to match your actual axis if you are not using a simulation axis. - 7. Build the PLC project.
- The
axisinstance of theAXIS_REFshould now be displayed among the PLC instances in the Solution Explorer. - 8. Link the PLC instance of the
AXIS_REFto the axis object in the MC project (see Link axis object). - 9. Activate the TwinCAT project
. Log in to the PLC
and start it
. - 10. Check that the distance specified for
moveRelativecan be traveled safely with the specified dynamics. Adjust them as needed. - 11. If you are sure that the movement specified for
moveRelativedoes not pose any danger, you can enable the controller for the axis by setting theenableAxisvariable online toTRUE. - 12. Check whether the controller was successfully enabled (
power.Status = TRUE) and follow the instructions regarding the axis's operational readiness. You can then activate the motion command withexecuteMove. - You can enable continuous writing of the override by setting
setOverride.Enable := TRUE. The override factor is adjusted viavelFactor. - Via
stop.Execute = TRUEthe motion command can be stopped prematurely if required. - If an axis error occurs, it can be reset via
reset.Execute := TRUE.
