PID adjustment
(tuning the servo controller with the motor)

Theory:
The operation of servo controls totally differs from that of stepping motor controls. As the principles and the processes taking place are rather complicated, therefore only a brief description can be given here (at user level). 

Fitting the Encoder:

DC motors do not have a predetermined stepping position as the stepping motors, so this function is totally realized by the control, with the help of an encoder mounted to the motor. This means continual and dynamic holding in position (if it is moved out from there, it will stand back).
The resolution of the motor is basically determined by the encoder, though it can be modified upward and downward by the control in some extent.
DSP is able to use the Encoder in 2 modes:

It uses the base resolution of the encoder (the number of its lines) - in 2× (duobling) mode doubling it,
- in 4× (quadroupling) mode quadroupling it.

The 4× mode is preferred and recommended so that the more precise position following could be realized. In case of very high Encoder base resolution (>2000) the 2× mode is recommended.
Beside that DSP is able to multiply the Step signal through its („Step multiplier”) register, in an extent of 1× - 10×.
It is necessary because of the CNC softwares, as the softwares are able to output (usually through the LPT port) Step signals only with limited frequencies. It would result in too low maximum revolution number in case of high-resolution Encoders. 

The maximum revolution number available can be calculated by the following formula:

fmax.=((FKerner×Step multiplier)/(Eresolution×Emode ))×60    [rev./minute]

fmax.=the maximum revolution number available  [1/min],
FKerner= is the maximum stepping frequency of the CNC software programme [Hz],
Step multiplier= is the value of the internal Step multiplying register of DSP,
Eresolution= is the base resolution of the encoder [the number of its lines, PPR],
Emode= is the value of the DSP „Encoder mode” internal register [2 or 4],
×60 = changing to minute.

Important!
As the feedback of the whole system is realized through the Encoder, its resolution influences all the other dynamic parameters (overshot, liability to swing, etc.) too. It is recommended to use the possible highest resolution, so that the possible best controlling ability could be reached.

Position-true following; swings; PID:
In a Step/Dir system the control will follow the moving commands with some time-delay, as it does not know in advance where to go. This delay is extremely small and negligible in case of smooth and uniform movements. In case of sudden and big changes of speed (e.g. changing direction) this delay is bigger, might even be substantial. The source of this delay are inertia of the mechanical parts (torques) and the response time of the motor+elektronics.

 
(rough sketch of the moving track)

The figure above shows the rough sketch of the deviation of the moving track. The dashed red line represent the moving track to be realized (with direction changes without accelerations and retardation), the blue line represent the real mechanical track. It can be seen that the control can follow the commands only with some swings and deviations at the points of direction changing. The so-called PID control procedure has been developed to solve this problem. This procedure is realized in the DSP internal software.

PID:
The PID control tries to hold the mechanical part in the track to be realized. This is realized by the motor excitation calculated from the resultant of three main components. The PID shortening comes from the initials of these components.

P = Proportional (proportional component);
I = Integral (error-integrating component);
D = Derival (component reacting to the fast changes).

P = proportional component. It raises the motor excitation proportionally to the deviation between the current and the intented positions. Its extent influences the motor dynamics (with how much force it should react to the increasing errors).

I = error-summing component. It amplifies the small remaining position errors, by summing them up, and it will set the motor to the intended position. It is for eliminating of small errors. its response time is relatively long.

D = fast response component. Its task is the fast response to the fast, sudden changes, excitation with extra dynamics. It will increase or decrease the excitation proportional to the speed of chages, intensifying by this the reaction of the motor or its swing-dumping (negative reaction). It is reponsible first of all for the swing-dumping (stability) of the system. It operates only when the speed changes and its effect is proportional to the chnages.


(transient curve)

The curve above shows a mechanical response to a track demanding a fast, sudden (pulse-like) position-change (mechanical setting-up). It represents a motor approachiung 0 point at a great speed, the mechanical parts will stop with swings and will set to the 0 point in the end. The interventions of the PID controller can be well observed.

P gives the base excitation, which increases proportionally to the extent of the deviation (this is the base torque for the motor). D gives the maximum angle of incidence (dumping), which is reponsible for the dumping of swings. The greater the angle is (the smaller the effect of D component), the more and greater overshots (a+b) can be measured, and the more time is needed for the system to to return to normal. The component I is responsible to fix remaining position-error (amplifying them after a time so much that it will set the motor to the position intended). It has a great importance in realizing the position-true moving

 The effect of the PID components:

 - P component: by increasing it the track-true moving of the motor will be better, the torque of the motor will increase.
- Low P component : great position-deviation errors, slow response, weak motor.
- High P component: over-reacting, swinging system (oscillation), jerking motor, liability to swing after direction changes.

 - By raising component I the position following will be better, the system will keep the 0 error-level harder. In case of position-following error the system will faster and more rigorously stand to position.
- Low I component: the remaining error will not be eliminated (not track-true position following, developing remaining errors after direction changes).
- High I component: swinging, oscillating mechanics (overcompensation), strong jerking, swinging motor. Strong oscillation, which cannot be dumped.

 - By raising component D, the accelerations will be more dynamic, the retardations will be realized with higher dumping. The stability of the system increases (swing dumping), the response time will increase too.
- Low D component: oscillating (swinging) system, swings, which will be dumped only slowly or not at all will be dumped after direction changes.
- High D component: overdumped, rigid motor-drive (srong warming-up in the motor and slow response, grouchy motor-voices).

Whe the motor is adjusted, all the three component must be adjusted at the same time. The quality of the control is determined by all the three components, therefore there are several sets of the values of the three componets, which result in good control.

Tuning of PID control:

As the phenomena cannot be followed by eye at all, threfore the built-in serial monitor (together with Quantum Sentinel or Hyperterminal) must be used for the exact adjustment of the controller.
It is worth studying this by all means, therefore before tuning please read carefully their description.


(non-graphic error-level analysis by the means of Hyperterminal)


(graphic analysis by the means of Quantum Sentinel)

The PID controller must always be adjusted together with complete mechanic unit (together with its braking resistance and its mass), possibly using the CNC control programme intended to apply (e.g. Mach3) (with the speed and acceleration intended to use). Running the monitor and Mach3 at the same time, the adjustment must be performed on each axis proceeding axis by axis.
The basic knowlidge of Mach3 CNC control programme is necessary for the adjustment.

Before adjustment Mach3 must be configured, as well as the base resolution of the CNC machine and its speed and acceleration values must be set-up. As for as the acceleration values the data of the motor must also be taken into account, because greater acceleration than the motor is capable (taking into account the gear ratio) will result in untrue error-levels. It is worth testing the acceleration values proceeding from the lower values to the higher values.
During the adjustment the mechanic parts will perform jumbled movements, and measuring these movements with the help of the monitor, the adjustment can be performed. The aim is that the mechanical parts could follow the moving track intended by the PC with the possible lowest error.
The moving track will be realized by a simple G-code programme, which must be loaded to Mach3 and it must be made to run with different speed and acceleration values.

Movement generating small programme (G-code):

G90G80G49
F2000
G1 X0.0000 Y0.0000 Z0.0000 A0.0000
M98 P1234 L50
G1 X0.0000 Y0.0000 Z0.0000 A0.0000
M5M30
O1234
G1 X0.0000 Y0.0000 Z0.0000 A0.0000
G1 X700.0000 Y00.0000 Z0.0000 A0.0000
M99

In the F line of the programme (here F2000) the programme moves the given axis (here axis X) at a determined speed (here 2000 mm/minute) 50×, between 0 and 700 mm, up to one end and back (performing cyclic subroutine calls). The marked lines need to be edited in the course of the test from time to time. The line F (here F2000) needs to be edited if the speed is intended to increase (in mm/minute unit), the line G1 X700.0000 ... needs to be edited if the other axes are intended to move (e.g. for axis Z the line G1 is X00.0000 Y00.0000 Z700.0000 A0.0000 ). If the value of the move is not suitable (here 700mm), it can be overwritten with any value (in mm units).

The Mach3 provides a screen for editing the programme (by using the notebook of Windows).

The process of the adjustment:

Important!
Before beginning the test, make sure whether the encoder is properly connected. In case of reverse (channel A and B) connection the motor will run to one direction at full speed. The connection can be checked with disconnected motor switching it on and trying to move out its shaft from position, the motor must not accelerate its spinning. In case of reverse connection exchange the two ends of the motor wire.

1. Set up the internal registers (Encoder mode and Step multiplier registers). This will also determine the extent of the available resolution, which must be set-up in Mach3 (see the formula above), referring to the given axis. In case of changing the Encoder mode, the controller must be restarted.

2. The current limit must be adjusted to the maximum allowed peak-current of the motor. (Limit potentiometer trimmer).

3. Do not connect the protection output for the time of the adjustment (FAULT output).

4. The resolution of the axes, their speed values and the acceleration values must be set-up in Mach3 (the initial value of the speed should be minimum 15000 mm/minute, the initial value for the acceleration should be 100 mm/s2 ). The acceleration values and those of the maximum speed will be necessary changed in the course of the adjustment.

5. The G-code must be loaded into Mach3 and the move for the axis going to be tested must be set-up by editing this code (e.g. if axis Y is going to be tested, then the co-ordinate must point to the value of 700.0000 and the others must point to 00.0000)

6. The 3 PID potentiometer trimmers must be adjusted to minimum (the slides must be set to towards GND). Then raise the slides of the potentiometer trimmer of P and D up to about 1/3 of the total length.

7. All the shafts of the machine must be adjusted to the middle position and make sure there is a track-length of 700 mm to both directions (if the track available is not so long, then the extent of the move in the test G-code must be reduced).

8. Start the programmes and the test. Observe the move and should the system swing heavily, adjust component P.

9. With the help of the Monitor programme measure in continual mode the lags (delays) and by adjusting component P set-up a delay between 0 - 10 Step (measuring it in the smooth section).

10. If the mechanic parts swing heavily at direction changing, then raise component D until the process becomes controllable (too strong is not at all ideal).

11. By raising gently component I adjust the system so that the controller could reduce the error 0 - 3 steps during smooth movements (with slight swing at near 0). Be careful, because too high I component may result in heavy swings. Should this happen, switch off the motor, take back component I and then continue tuning the system.

12. At this Mach set-up (with low acceleration values) a well-adjusted controller will work at each point (even during direction changes) with 0 error (max. ±3 Steps). The tuning must be done (by P-I-D adjustments) until this state is reached. If the mechanical parts get stuck, run untrue, then a little bigger error is also acceptible.

13. The error must be checked at several speed values (e.g. at 10, 100, 500, 1000, 3000, 4000 mm/minute, etc.). When increasing the speed the error may increase a little temporarily at the time of direction change, but the controller must fix it quickly (it can be mended by raising component P or component I). In case of increasing swing, component D can also be increased (but the system must be checked at lower speed values too).

14. None of the 3 items must be overfed. The slide of the potentiometer trimmers must be stopped just at the right position, where the error is just eliminated. Othrwise the adjustment will result in an overreacting control.

15. If everything is all right, the value of the motor acceleration of Mach3 may be raised and the error peaks developing at changes must be checked.

16. The best set-up value for the acceleration is where the controller is able to produce an error of (max. ±3 Steps) at near 0 even during braking and acceleration too. If this value has been found, then our system will be able to work with this acceleration value. In fast operating mode a short (pulse-like) error-signal is allowed even with a Step value of 50. If component I can even compensate this error in the linear part, this acceleration value can also be used (only in fast operating mode). The error-level can usually be adjusted between ± 3 Steps.

17. Stress Test:
To perform this test, Mach3 must be stopped and Test button of the controller must be pressed. The test is OK, if the motor jumps and stand to the test without oscillations. In case of remaining oscillation either component I and or component P must be reduced, or component D must be increased. If it was necessary to adjust any of them, then it is necessary to check the system at the above-written speed values.


(test with the Test button)

For this test the graphic display of Quantum Sentinel in Triger on mode is ideal.

18. Holding in position test:
With Mach3 stopped, observing the Monitor the error-signal must be between 0 (+-1). Then hold on the shaft of the motor with your hand and try to move it out. The motor must force back the shaft to the 0 position up to the force of its torque. This can also be traced on the data of the Monitor. The strength of position-holding can be increased mainly by raising component I, in a little extent by raising component P. If any of the three component was adjusted, the system must be checked in moving state too.
In case of a motor vibrating and emitting grouchy voices at standing position, the value of Antitremb(l)e register must be raised so that this state could be eliminated. It is recommended to apply a low value (1 - 2) in this register, so that the best position-holding could be achieved.

19. If acceptable values have been measured at each point, the adjustment is successful.

Further tips:

So that more precise adjustment could be realized, the use of Quantum Sentinel software is recommended (in Triger off mode). Because of the resouce-demand of the programme the use of an external PC is necessary (otherwise the pulses of Mach3 will be incorrect).
If the speed set-up at G-code is higher than the axis speed set-up in Mach3, the programme will cut down the motor (in this case the maximum speed of the axis at the Mach3 motor tuning must be raised).
A motor being in swing cannot be stopped by the end-position-switch, because the switch operates through the PC.
By bulding-in Stop, the controller can be stopped in any condition. After being stopped, the controller will work only after it has been restarted.
The Step pulses of poorer quality CNC softwares (e.g. KCam4) are not smooth enough. These can generate swings in the course of servo movements. Their use is not recommended.
If the swings developing at direction changes can no longer be further reduced, then lower acceleration values must be applied (at motor tuning set-up of Mach3).