Skip to main content

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 works as follows:
  1. The function block is initialized on a rising edge at the Enable input. In the STATE_INIT_0 and STATE_INIT_1 states, the OutputBufferReset signals of the EL2258 terminal are written and the aLastEventIds 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.

  2. Once initialized (STATE_ACTIVE), all tracks are processed in turn, and the following logic is performed for each track:

    1. Check whether the EL2258 terminal has already accepted the last commanded events (ChannelsIn[channel].OutputOrderFeedback = ChannelsOut[channel].OutputOrderCounter).

    2. Loop through all events of the track. Only those events whose ToggleEventId is greater than the last ID processed are considered further. (Query EventId_GreaterThan(event^.ToggleEventId, aLastEventIds[channel]))

      See section: "Note on the event order".

    3. Convert the Duration of the event into an EtherCAT timestamp and corresponding write of the ChannelsOut[channel].OutputEventState and ChannelsOut[channel].OutputEventTime outputs.

    4. Abort the processing after a maximum of 5 events.

    5. 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.

. Two details still need to be considered:
  1. Because it is not known at which ToggleEventId the events start, a valid flag is saved in the EventId data type in addition to the ID. For the EventId_GreaterThan comparison, this valid flag is taken into account accordingly.

  2. The ToggleEventIds have the 32-bit data type UDINT 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 function EventId_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.

_sm_img_mapping_el2258.png

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.

  1. First, the function block is initialized in STATE_INIT.

  2. Then STATE_CHECK_FOR_EVENT checks whether events from the SMC_DigitalCamSwitch_HighPrecision function block are pending.

  3. 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 in STATE_ACTIVATE_EVENT_IN_EL2252. This activates the event in the terminal.

  4. In STATE_WAIT_UNTIL_THE_INPUTS_MATCH_THE_OUTPUTS, the system then waits until the event has been executed. The signals from the Feedback input of the terminal are used for this.

PDO Mapping EL2252

_sm_img_mapping_el2252.png

Main

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

_sm_img_digitalcamswitch_highprecision.png

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

  1. Build the application and download it to a controller.

  2. Start the application.