MC_MoveAbsolute() 4th Order Real-Time Trajectory Generation Function Algorithm and Implementation

Featured Application: The theoretical results and practical implementation of the trajectory generator presented in this article are the outcome of two research projects: The ﬁrst was issued by the Polish Ministry of Science and Higher Education and the second one was part of the European Union’s 7th Framework Programme (see Funding Section). Abstract: This paper presents the issue of generating motion trajectories in a digital servo drive in accordance with the PLCopen Motion Control standard. This standard does not limit the details of motion generation in the electromechanical systems, but indicates its interface and set of necessary parameters. Moreover, it is placed within a state machine, which allows the individual software elements to integrate with it seamlessly. This work discusses time-optimal point-to-point trajecto-ries, i.e., the initial and ﬁnal reference speeds are zero, and they are compliant with the MC_MoveAbsolute() function deﬁned in the PLCopen Motion Control standard. The smoothness of the resulting trajectory can be attributed to the use of a fourth order trajectory generator, which deﬁnes the bounds up to snap – the second derivative of acceleration. One of the aims of this article was to bridge the theoretical aspect of trajectory generation with the algorithms practical implementation, by the means of PLC code generation using the MATLAB/Simulink package.


Introduction
Servo drive systems are exploited widely in industries due to their efficiency, high-performance in motion control, and fast response times to dynamic reference signals [1,2].In order to comply with the technological requirements, modern automatic machines have to guarantee decent dynamics for position and velocity control of motors, these servo drives solutions are utilized in Computer Numerically Controlled (CNC) machining, robotics, factory automation, and many other areas.
The most common control scheme implemented in servo drives to control position and velocity is the cascade control algorithm [3].Its task is to regulate current, velocity, and position of the motor in separate closed control loops, in order to minimize overshoot and reduce the influence of external disturbances.
Nowadays, an increasing number of commercial control system manufacturers provide open architecture systems [4], meeting the need to constantly improve the servo drives functionality.This approach allows for design, testing, and integration of new control algorithms and techniques such as control laws, multi-sensor integration, or Artificial Intelligence (AI).
Simultaneously, evolution of the servo drive system programming languages is progressing.Currently, most of the commercial systems allow the user to implement custom code in the native Appl.Sci.2019, 9, 538 2 of 17 control structure.These modifications can be written in popular programming languages such as C, C++, or Structured Text (ST).
Automatic code generation [5] is one of the ideas which facilitates implementing the code in various industrial control platforms.It is based on text languages (C, ST) and allows to compile and transfer developed code or algorithms between devices of different classes and manufacturers with minimal effort on the programmer's part.
In the following work, generation of motion profiles (especially position) for servo drives is considered.This is a crucial aspect of applications that demand a high motion fidelity, e.g., CNC feed drives and multi-axis manipulators.In this case, constraining the acceleration profile obtained by the second order trajectory generator leads to oscillations whenever the movement direction changes (i.e., zero crossing of set velocity).This is the main reason for using third and higher order trajectory generators, for defining constraints on jerk or snap [6].
This paper is organized as follows: Section 1 serves as a brief introduction to control of digital servo drives; Section 2 is devoted to PLCopen Motion Control, particularly to one of its motion functions-MC_MoveAbsolute().Section 3 presents a step by step algorithm to find a motion profile with boundaries defined up to the fourth derivative of position, and the trajectory generator of the fourth order is described.This topic is expanded upon in Section 4, where a real-time calculation of the beforementioned profile is discussed.Finally, Section 5 puts these solutions in context of the Programmable Logic/Automation Controller (PLC/PAC) devices that are IEC 61131-3 compliant, while Section 6 summarizes the paper.

Trajectory Generation in Digital Servo Drives: PLCopen Motion Control
In References [6][7][8][9], trajectory generation algorithms used in motion solutions are presented.The dynamics of mechanical aspects of the systems are considered in order to develop a feedforward controller.The following work is further expanding the ideas presented in Reference [9], such that the mathematical foundation laid there could be implemented in a real-time system.
In Reference [10], the concept of utilizing the PLCopen Motion Control standard for controlling the digital servo drive of the CNC machine is given.In the PLCopen Motion Control standard, several states are defined that outline the operation of a digital servo drive.These states depict the actual status of the motion system, being the type of movement executed (if any), present errors, stopping, or awaiting commands from the control system.Of particular interest are the two states tied to the motion execution: (a) Discrete Motion -movement to a defined position; (b) Continuous Motion -movement without a setpoint (e.g., shaft rotating with set velocity).
The transition between particular states is achieved by calling related functions, e.g., MC_MoveAbsolute (absolute movement) or MC_MoveAdditive (additive moment in a specified direction).The entire state machine is presented in Figure 1.

Trajectory Generation in Digital Servo Drives: PLCopen Motion Control
In References [6][7][8][9], trajectory generation algorithms used in motion solutions are presented.The dynamics of mechanical aspects of the systems are considered in order to develop a feedforward controller.The following work is further expanding the ideas presented in Reference [9], such that the mathematical foundation laid there could be implemented in a real-time system.In Reference [10], the concept of utilizing the PLCopen Motion Control standard for controlling the digital servo drive of the CNC machine is given.In the PLCopen Motion Control standard, several states are defined that outline the operation of a digital servo drive.These states depict the actual

MC_MoveAbsolute() -Function Description
This function block starts a controlled movement to a specified absolute position.All of the parameters needed to start the movement are transferred on a rising edge of the Execute input (Figure 2).The axis changes to the Discrete Motion state after all of them have been successfully transferred.Before the function block can be used for a real axis, the axis must be homed.States in which this function block can be used: Standstill, Discrete Motion, and Continuous Motion.The transition between particular states is achieved by calling related functions, e.g., MC_MoveAbsolute (absolute movement) or MC_MoveAdditive (additive moment in a specified direction).The entire state machine is presented in Figure 1.

MC_MoveAbsolute() -Function Description
This function block starts a controlled movement to a specified absolute position.All of the parameters needed to start the movement are transferred on a rising edge of the Execute input (Figure 2).The axis changes to the Discrete Motion state after all of them have been successfully transferred.Before the function block can be used for a real axis, the axis must be homed.States in which this function block can be used: Standstill, Discrete Motion, and Continuous Motion.In the following figures, the timing diagrams for MC_MoveAbsolute() possible usage scenarios are shown.In Figure 3a, the first function block (MC_MoveAbsolute_0) stopped its movement before the second function block (MC_MoveAbsolute_1) was started.In Figure 3b, the second function block (MC_MoveAbsolute_1) was started before the first function block (MC_MoveAbsolute_0) completed its movement.In both cases, as seen in Figure 3a,b, the movements finish in the same position.In the following figures, the timing diagrams for MC_MoveAbsolute() possible usage scenarios are shown.In Figure 3a, the first function block (MC_MoveAbsolute_0) stopped its movement before the second function block (MC_MoveAbsolute_1) was started.In Figure 3b, the second function block (MC_MoveAbsolute_1) was started before the first function block (MC_MoveAbsolute_0) completed its movement.In both cases, as seen in Figure 3a,b, the movements finish in the same position.In the following figures, the timing diagrams for MC_MoveAbsolute() possible usage scenarios are shown.In Figure 3a, the first function block (MC_MoveAbsolute_0) stopped its movement before the second function block (MC_MoveAbsolute_1) was started.In Figure 3b, the second function block (MC_MoveAbsolute_1) was started before the first function block (MC_MoveAbsolute_0) completed its movement.In both cases, as seen in Figure 3a,b, the movements finish in the same position.

Fourth Order Position Profile Generation
In this work, the position profile will be understood as a setpoint trajectory with a sampling time specified for a real-time algorithm implementation.The following convention, adopted from [9], was used to denote the parameters: x -position, displacement (profile, m); v -velocity (profile, m/s); a -acceleration (profile, m/s 2 ); j -jerk (profile, m/s 3 ); d -derivative of jerk (profile, m/s 4 ); x, v, a, j, d -bound on |x|, |v|, |a|, |j|, |d|, respectively; t d , t j , t a , t v -time interval during in which |d|, |j|, |a|, |v| obtains its bound, respectively; T s -sampling time in seconds.
The fourth order position trajectory generation problem was considered within this document.One input was used to define the desired position (i.e., increment from an actual position to the new one) and another four input parameters described the movement profile including velocity, acceler-ation, jerk, and snap.These latter parameters described the boundaries which cannot be crossed while executing the movement.The generated trajectory was time-optimal, i.e., the position was reached in the shortest possible time.For simplicity we assumed that the shape of the profiles was symmetric no matter what the direction of movement was.
The direction of movement can be calculated based on the following equation: where the sd variable equals 1 if the movement direction is positive, or −1 when it is negative.While considering the real-time implementation with respect to sampling time of code execu-tion, it is necessary to round up calculated time values: Fourth order trajectory profiles are shown in Figure 4.
tion, it is necessary to round up calculated time values: Fourth order trajectory profiles are shown in Figure 4.In the following sections, calculations for the type of trajectory generation for MC_MoveAbsolute() are shown in an algorithmic way.In the following sections, calculations for the type of trajectory generation for MC_MoveAbsolute() are shown in an algorithmic way.

Calculation of Time t d
The following equations are used for calculation of time t d which covers the duration of a single movement section with applied snap boundary (i.e., its constrained value), as in Figure 4.
Rounding up (4) with respect to the sampling time T s : If the following Equation ( 7) holds true: Then: Appl.Sci.2019, 9, 538 6 of 17 Otherwise: This rounded up with respect to the sampling time (T s ) yields the following equation: Then: If the following Equation ( 12) holds true: Then: Otherwise: This rounded up with respect to the sampling time (T s ) yields the following equation: Then: If the following Equation (17) holds true: Then: Otherwise: This rounded up with respect to the sampling time (T s ) yields the following equation: Then: Time t d is derived from Equation (21) as: The next section shows how to evaluate value of time (t j ).

Calculation of Time t j
When time (t d ) is calculated, the very next step is to calculate the value of time (t j ), while taking into account movement constraints and sampling time rounding of calculated values.The value t j means the time of jerk (3rd order derivative of position), as shown in Figure 4. Derivation of time (t j ) starts from calculations: This rounded up with respect to the sampling time (T s ) yields the following: Then: If the following Equation (26) holds true: Then: Otherwise: Appl.Sci.2019, 9, 538 8 of 17 This rounded up with respect to the sampling time (T s ) yields the following: Then: If the following Equation (31) holds true: Then: Otherwise: This rounded up with respect to the sampling time (T s ) yields the following: Then: Time t j is derived from Equation (35) as: The following section considers calculations of acceleration phase time (t a ).

Calculation of Time t a
When times t d and t j are calculated, taking into account movement constraints and sam-pling time rounding of calculated values, the next step is to calculate value of time (t a ).This means the time of acceleration (second order derivative of position), as shown in Figure 4.The derivation of time (t a ) starts from the following calculations: And: This rounded up with respect to the sampling time (T s ) yields the following: Then: If the following Equation (41) holds true: Then: Otherwise: This rounded up with respect to the sampling time (T s ) yields the following: Then: Time t a is derived from Equation (45) as: The following section considers further calculations of the fourth order trajectory profile.

Final Calculation of Movement Profile Times and Direction
Final calculations (including time of constant velocity movement (t v )) start with: This rounded up with respect to the sampling time (T s ) yields the following: Then: Where sd is calculated in Equation ( 2).The column vector in Equation ( 49) is the final part of the fourth order trajectory profile generation.Now we have all the time values necessary to create the movement profile based on the parameters given by the user.This part of the algorithm is called the trajectory planner.The next part of the algorithm is called the profile generator.It is described in the following section and its purpose is to generate a movement profile out of the times t d , t j , t a , t v were calculated in previous sections.

Generation of Profile
The generation of a movement profile starts with the calculation of switching times t 0 . . .t 15 as shown in Figure 4: For simplicity, we can assume: Then Equation ( 50) can be rewritten in the following form: where S M4 describes the switching matrix for the fourth order trajectory profile generation.After these calculations, the following holds true as shown in Figure 5: Movement starts in time (t 0 ), while time moment (t 15 ) is the endpoint of the generated move-ment.

Execution of Trajectory Profile Generation
When the trajectory profile generation is executed (i.e., by the user) and output of the reference position trajectory has started, then the generation of snap profile is based on the following calculations.When: Then:

Execution of Trajectory Profile Generation
When the trajectory profile generation is executed (i.e., by the user) and output of the reference position trajectory has started, then the generation of snap profile is based on the following calcula-tions.
Input EN = TRUE means that the user wishes for execution of the trajectory profile calculation (and profiled movement generation) and information about the readiness of the module for calculations is passed from the input variable ready to the variable ENstart.The rising edge of the condition: enables determination of direction, i.e., calculation of variable dir: Equation ( 68) is used when we want to switch operation mode of the movement, i.e., motor/servo is mounted in an opposite direction.

Generation of Start Signal
Generation of the start signal that triggers Equation (56) and results in a new value of the t 16 variable (calculated based on Equation (52) and consequently Equation ( 53)) is connected with the equation: The same start variable is used for triggering new profile calculations (Equations ( 1)-( 53)).

Calculation of Execution Time
Calculation of trajectory profiling execution time (t exec ) is based on the following expressions: This finishes the algorithm of trajectory profile generation.The next step is to calculate actual values out of the process involving integrating snap signal values.It is shown in the next subsection.

Generation of Motion Profile Step
To output the motion profile of all the derivatives and the position itself, the following steps need to be executed in a real-time control platform:

•
Reference signal d k (derivative of jerk/snap) is the zero-order-hold sampled value of p d jerk variable.
• Reference signal j k (jerk) is the numerically integrated value of signal d k with respect to the sampling time (T s ): • Reference signal a k (acceleration) is the numerically integrated value of signal j k with respect to the sampling time (T s ): • Reference signal v k (velocity) is the numerically integrated value of signal a k with respect to the sampling time (T s ): • Reference signal x k (position) is the numerically integrated value of signal v k with respect to the sampling time (T s ): Appl.Sci.2019, 9, 538 14 of 17 These steps finish the trajectory generation procedure.

Implementation of Trajectory Generator According to IEC 61131-3
The proposed algorithm was prepared to be transferrable to real-time implementation (e.g., PLC/PAC), which could later be used in industrial control platforms.
The algorithm was divided into the following sections: • Plan Trajectory (Figure 6) -Equations ( 1)-( 49); • Generate profile (Figure 6) -Equations ( 50)-(71); while • Generate Motion (Figure 6) subsystem calculates Equations ( 72)-(75).The algorithm was divided into the following sections: • Plan Trajectory (Figure 6) -Equations ( 1)-( 49); • Generate profile (Figure 6) -Equations ( 50)-(71); while • Generate Motion (Figure 6) subsystem calculates Equations ( 72)-(75).The function blocks code for the 4th order trajectory generator is presented in Figure 8, with the Simulink model presented in Figure 6 serving as a base for this conversion.Structured text code generated with a Simulink PLC Coder is about 900 lines long, and the total memory allocated for this function block in PLC is 3 kB.This means that the devised solution is implementable in the majority of PLC/PAC controllers which are IEC-61131-3 [11,12] standard compliant.The function blocks code for the 4 th order trajectory generator is presented in Figure 8, with the Simulink model presented in Figure 6 serving as a base for this conversion.Structured text code generated with a Simulink PLC Coder is about 900 lines long, and the total memory allocated for this function block in PLC is 3 kB.This means that the devised solution is implementable in the majority of PLC/PAC controllers which are IEC-61131-3 [11,12] standard compliant.

Summary
The article presents, in a step by step manner, the procedure for implementation of a 4 th order trajectory generator that mirrors the MC_MoveAbsolute() function.Due to the straightforward nature of Equations ( 1)-(75), it is easily implementable in any of the available textual or graphical programming languages for real-time control systems.
The implementation of the presented algorithm is possible in control systems adhering to the IEC 61131-3 standard (e.g., PLC), and its source code was generated as a result of applying the fast prototyping paradigms, i.e., it was generated from the Simulink model.As an example, generated profiles are given with different parameters (i.e., boundaries).It is also possible to generate motion profiles of lower orders than fourth-the algorithm itself is scalable and following the procedure presented here will lead to obtaining simpler solutions constrained on third or second derivatives.
The most important contribution of this work is the unique nature of the presented approach, as it allows for practical implementation of this algorithm in commonly used real-time control systems.
The algorithm presented here is fully parametrized.It can be fully integrated by way of interfaces and its functionality, with other motion control features (i.e., collision avoidance or trajectory generation profile correction).As proposed in this paper, the implementation of MC_MoveAbsolute() is parametrized through its inputs (as it can be found in all functions and function blocks in graphical IEC 61131-3 compliant programming languages).Modularity of the approach enables the user to combine a proposed solution with an extension boom elasticity estimation feature

Summary
The article presents, in a step by step manner, the procedure for implementation of a 4th order trajectory generator that mirrors the MC_MoveAbsolute() function.Due to the straightforward nature of Equations ( 1)-(75), it is easily implementable in any of the available textual or graphical programming languages for real-time control systems.
The implementation of the presented algorithm is possible in control systems adhering to the IEC 61131-3 standard (e.g., PLC), and its source code was generated as a result of applying the fast prototyping paradigms, i.e., it was generated from the Simulink model.As an example, generated profiles are given with different parameters (i.e., boundaries).It is also possible to generate motion profiles of lower orders than fourth-the algorithm itself is scalable and following the procedure presented here will lead to obtaining simpler solutions constrained on third or second derivatives.
The most important contribution of this work is the unique nature of the presented approach, as it allows for practical implementation of this algorithm in commonly used real-time control systems.
The algorithm presented here is fully parametrized.It can be fully integrated by way of interfaces and its functionality, with other motion control features (i.e., collision avoidance or trajectory generation profile correction).As proposed in this paper, the implementation of MC_MoveAbsolute() is parametrized through its inputs (as it can be found in all functions and function blocks in graphical IEC 61131-3 compliant programming languages).Modularity of the approach enables the user to combine a proposed solution with an extension boom elasticity estimation feature (loader or forestry cranes applications) or dynamic collision avoidance (multi-axis serial robotic manipulators).

Figure 1 .
Figure 1.PLCopen Motion Control standard state execution state diagram.

Figure 1 .
Figure 1.PLCopen Motion Control standard state execution state diagram.
Appl.Sci.2018, 8, x FOR PEER REVIEW 3 of 18 status of the motion system, being the type of movement executed (if any), present errors, stopping, or awaiting commands from the control system.Of particular interest are the two states tied to the motion execution: a) Discrete Motion -movement to a defined position; b) Continuous Motion -movement without a setpoint (e.g., shaft rotating with set velocity).
Time values from Equation (53) are shown in the following Figure 5. Appl.Sci.2018, 8, x FOR PEER REVIEW 12 of 18

Figure 5 .
Figure 5. Fourth order trajectory profiles in point-to-point movement with profile times shown.

Figure 5 .
Figure 5. Fourth order trajectory profiles in point-to-point movement with profile times shown.