External setpoint generation
External setpoint generation can be used to implement a custom setpoint profile that is not available by default in TwinCAT MC3. This profile can be mathematically very simple or as complex as necessary. In external setpoint generation, the setpoints are generated in function blocks or TcCOM modules that implement the ITcMcExternalSetpointGenerator interface. This is called from the MC context.
The main input is ExternalSetpointGenerator of type ITcMcExternalSetpointGenerator. This interface defines methods that must be implemented (e.g., by a PLC FB). These methods are called by the MC task context (MET-Task) via a Callback mechanism. This mechanism forms the basis for decoupling the MC task and the PLC task during external setpoint generation. This basic operating principle is represented in the figure below.

With the MC3, it is possible to execute PLC and MC3 axes with different cycle times in different contexts when using external setpoint generation. Be aware of the update rate of the data used in the Callback methods when AllowOtherContext is enabled.
MC3 can check the calculated setpoints for consistency (from the previous cycle to the current cycle, taking into account the maximum dynamic parameters of the axis). To do so, set EnableDynamicChecks := TRUE.
The interpretation of the input variable InvokeId, which is of type XWORD, is customer-specific. For example, any numeric value or the address (pointer) to a customer-specific data structure can be passed. This InvokeId is passed as an input to every method included in the interface.
The Callback methods contained in the ITcMcExternalSetpointGenerator interface are called by the MC task context (MET task) in the order listed:
OnPreparation (invokeId : XWORD)- Is called directly when MC_ActivateExternalSetpointGeneration.Execute = TRUE
- For example, this method can be used to initialize setpoint generation.
OnStartPointInitialization (invokeId : XWORD, startDcTime : LINT, position : LREAL, velocity : LREAL, acceleration : LREAL)- Called shortly before the external setpoint generator becomes active and the next setpoint is generated by that generator for the first time.
- For example, this method can be used to initialize setpoint generation.
- The last setpoint (position and dynamics) and the corresponding timestamp of the previous command (which has completed by this point) are passed to this method. The next setpoint/cycle is calculated by the external setpoint generator.
ABSTRACT OnCyclicSetpointGeneration (invokeId : XWORD,
timeInGeneration : LREAL, position : REFERENCE TO LREAL,
velocity : REFERENCE TO LREAL, acceleration : REFERENCE TO LREAL)- This method must be implemented.
- The method is called cyclically when MC_ActivateExternalSetpointGeneration
.Active = TRUE. - This method is used to calculate the setpoint for the specified timestamp.
- The input variable
timeInGenerationrepresents the elapsed time while external setpoint generation is active. - Position, velocity, and acceleration must be set to the values for the current setpoint generation cycle.
OnDeactivation (invokeId : XWORD)- This method is intended for the final steps when external setpoint generation is terminated by another motion command.
- For example, it can be used to free up storage space that was allocated during external setpoint generation.
The FB MC_ExternalSetpointGenerator provides the API for the implementation of external setpoint generation from the PLC environment. The component diagram below represents the relationships between the modules. The generator itself is a TcCOM object that is created at runtime and registered in the TwinCAT system (TcBaseModuleRegistered). This Generator FB already implements methods for handling online change processes. As a core feature, it implements the ITcMcExternalSetpointGenerator (an extension of ITcUnknown).
