Using MC_DigitalCamSwitch_HighPrecision
The example BasicMotion_DigitalCamSwitch_HighPrecision.project
is located in the installation directory CODESYS under ..\CODESYS SoftMotion\Examples
.
This example shows how to use the outputs of SMC_DigitalCamSwitch_HighPrecision
to control digital outputs with high time resolution. The EL2258 and EL2252 terminals from Beckhoff are used as an example.
Note
The device descriptions from Beckhoff are not included in the example. You can download these directly from the vendor and install them in the device repository. To run the example, you need an EK1100 bus coupler, an EL2258 terminal, and an EL2252 terminal.
Important
This example was developed and tested according to Revision 0017 of EL2258 and Revision 0020 of EL2252. It serves only as an example. We do not guarantee the correctness. The source code must be tested before use. If necessary, it must be modified to other revisions of the terminals.
Structure of the application
The application consists of a Main
program, the DigitalCamSwitch_EL2258
and DigitalCamSwitch_EL2252
function blocks, and an EK1100 bus coupler with the EL2258 and EL2252 output terminals.
The DigitalCamSwitch_EL2258
and DigitalCamSwitch_EL2252
function blocks are the essential parts of the example. They show how to use the Events
output of SMC_DigitalCamSwitch_HighPrecision
to program digital outputs. The function blocks should be understood as examples which can be adapted to other hardware.
DigitalCamSwitch_EL2258
The function block is initialized on a rising edge at the
Enable
input. In theSTATE_INIT_0
andSTATE_INIT_1
states, theOutputBufferReset
signals of the EL2258 terminal are written and theaLastEventIds
array is initialized. The ID of the last processed event for each track is saved in this array. This prevents an event from being transferred to the terminal multiple times.Once initialized (
STATE_ACTIVE
), all tracks are processed in turn, and the following logic is performed for each track:Check whether the EL2258 terminal has already accepted the last commanded events (
ChannelsIn[channel].OutputOrderFeedback = ChannelsOut[channel].OutputOrderCounter
).Loop through all events of the track. Only those events whose
ToggleEventId
is greater than the last ID processed are considered further. (QueryEventId_GreaterThan(event^.ToggleEventId, aLastEventIds[channel])
)See section: "Note on the event order".
Convert the
Duration
of the event into an EtherCAT timestamp and corresponding write of theChannelsOut[channel].OutputEventState
andChannelsOut[channel].OutputEventTime
outputs.Abort the processing after a maximum of 5 events.
Notify the terminal that new events exists if at least one event has been found (
ChannelsOut[channel].OutputOrderCounter := ChannelsOut[channel].OutputOrderCounter + 1
).
Note on the event order
The events are returned by the SMC_DigitalCamSwitch_HighPrecision
function block for each track in an array. The SwitchNumber
and a ToggleEventId
are returned for each event. This ToggleEventId
is unique and ascending for each track.
For more information, see: SMC_CAMSWITCH_TOGGLE_EVENT.
In the example, the aLastEventIds[trackNo]
array stores for each track which ToggleEventId
was last transferred to the terminal in a previous function block call. Because the ToggleEventIds
are in ascending order, the next call will know exactly which events still need to be transferred.
Because it is not known at which
ToggleEventId
the events start, avalid
flag is saved in theEventId
data type in addition to the ID. For theEventId_GreaterThan
comparison, thisvalid
flag is taken into account accordingly.The
ToggleEventIds
have the 32-bit data typeUDINT
and can therefore, in principle, overflow with a respectively long runtime or high switching frequency. The value then goes from 2^32-1 back to 0. The comparison functionEventId_GreaterThan
also takes 32-bit overflows into account.
PDO Mapping EL2258
The ChannelsOut
and ChannelsIn
outputs of the DigitalCamSwitch_EL2258
function block are connected to the respective I/O channels of the EL2258 terminal. The following image shows the mappings for Channel 1. Channel 2 is similar; only when accessing ChannelsOut
and ChannelsIn
the index is 2, not 1.

DigitalCamSwitch_EL2252
The basic principle is identical to DigitalCamSwitch_EL2258
. The difference is in how the signals are written because the EL2252 terminal can program only one active event.
First, the function block is initialized in
STATE_INIT
.Then
STATE_CHECK_FOR_EVENT
checks whether events from theSMC_DigitalCamSwitch_HighPrecision
function block are pending.If an event is available, then the event must be programmed within two cycles:
The outputs and the EtherCAT timestamp are written in the first cycle. The
Activate
output is set to 0.In the second cycle, the
Activate
output is set to 3 inSTATE_ACTIVATE_EVENT_IN_EL2252
. This activates the event in the terminal.
In
STATE_WAIT_UNTIL_THE_INPUTS_MATCH_THE_OUTPUTS
, the system then waits until the event has been executed. The signals from theFeedback
input of the terminal are used for this.
PDO Mapping EL2252

Main
In the main program, four switches are configured (two on each track). The tracks also have different on/off compensations (OnComp
, OffComp
):

The program includes the necessary function block calls to activate a forecast for the axis and to move it constantly at 180°/s (*), as well as for SMC_DigitalCamSwitch_HighPrecision
, DigitalCamSwitch_EL2258
, and DigitalCamSwitch_EL2252
. Before starting the movement, the fieldbus must be fully powered up. Otherwise the events cannot be transmitted to the terminal.
(*) Constant movement was chosen to keep the example simple and clear. However, the SMC_DigitalCamSwitch_HighPrecision
function block works with all types of movements. For example, it also provides exact timestamps during an acceleration phase or with cams.
Commissioning
Build the application and download it to a controller.
Start the application.