Skip to main content

Implementation of a Path-Preprocessing Function Block

See the CNC14_PathPreprocessing.project sample project in the installation directory of CODESYS under ..\CODESYS SoftMotion\Examples.

The sample project shows how to implement a new path-preprocessing function block.

Path preprocessing is generally used to modify the path which is read from the G-code. This allows functions such as tool radius correction or corner smoothing to be implemented. However, it is also possible to integrate specific functions for a particular machine or application as a path-preprocessing function block.

For more information about path preprocessing, see the following: Path Preprocessing and Queue Sizes.

For an example of how to use a path-preprocessing function block, see the following: CNC Example 03: Performing Path Preprocessing Online.

Limiting the tangent change

The example shows a 2D cutting application. A knife should be used to cut along a path defined by G-code. The SMC_TRAFO_GantryCutter2 transformation is used to determine the angle of the knife based on the current tangent of the path.

The challenge is that the knife must not rotate too quickly, otherwise the cut will not be clean. The limitation of the angular velocity of the knife is solved in the sample project by means of the newly created path-preprocessing function block LimitTangentVelocity. It changes the feed rate on the path so that the change velocity of the tangent does not exceed a configurable maximum value.

The G-code is a simple rectangle with smoothed corners.

_sm_img_example_preprocessing_function_block1.png

The trace shows the result of the execution: The velocity of the C-axis, which corresponds to the rotational velocity of the knife, is displayed in orange. It is limited to 45°/s as specified.

_sm_img_example_preprocessing_function_block2.png

Structure of the application

The structure is typical for CNC applications. The G-code is read in the background task, and path preprocessing also takes place here. The interpolation is performed in the bus task.

The LimitTangentVelocity function block shows the necessary steps and states for a path-preprocessing function block.

The basic principle is that the function block reads, processes, and then writes path elements (type SMC_GEOINFO) from the input queue (poqDataIn) to the output queue (poqDataOut). The elements are removed from the input queue.

The logic for changing the feed rate is located in lines 102–138. The auxiliary function ComputeMaxCurvature calculates the maximum curvature which may occur on a given path element.

// Our velocity limitation comes here. This modifies an element from the
// input queue and copies the modified element to the output queue.
// Finally, the element is removed from the input queue

m_geo := pgeo^; // Copy the element
 // Note: the feature flags set in the G-Code with G38/G39 can be queried
 // by reading pgo^.dwFeatureFlags, like this:
 IF (SHR(m_geo.dwFeatureFlags, featureFlag) AND 1) = 1 THEN
     // feature is turned on

     ok := ComputeMaxCurvature(m_geo, kappa=> kappa_max);
     IF NOT ok THEN
         // Curvature cannot be computed
         m_state := STATE_ERROR;
         ErrorID := SMC_INVALID_PARAMETER;
         OnExit();
         RETURN;
     END_IF

     // Compute maximum allowed path velocity based on maximum curvature
     // and maximum allowed angular velocity
     IF kappa_max = 0 THEN
         // No curvature, no limitation necessary
         vel := m_geo.dVel;
     ELSE
         vel := m_maxAngularVelocity_rad / kappa_max;
     END_IF

     IF velMin < 0 OR vel < velMin THEN
         velMin := vel;
     END_IF

     // Set new maximum velocity for the element
     m_geo.dVel := MIN(m_geo.dVel, vel);
 ELSE
     // feature is turned off
 END_IF

Commissioning

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

  2. Start the application.

  3. Open the trace and download it to the controller.

  4. Write the value TRUE to the variables Path.bExecute and PLC_PRG.bStart.

  5. You can track the processing of the G-code in the trace.