# Thread: Servo tuning with SnapAmp

1. ## Servo tuning with SnapAmp

Hello Tom.

I have been playing with the Kflop and Snap Amp combination Trying to get a handle on servo tuning. I have been following along in the Optimizing Servo Tuning thread started by Judleroy. But I did not want to clutter his thread up with my pictures and questions as well.

This tuning is done with very little load on the shaft of the motor.

With Tom's Help calculaing.
Start of the caculation to figure velocity, acceleration and jerk
I wanted to start out at 100ipm
my encoders are 1250CPR or 5000pulses/revolution
1inch/.125" per revolution= 8 revolutions per inch
8 x 5000 = 40,000 pulses per inch

Settings to enter into Step Response
Velocity = (desired feedrate/60) x steps per unit(number of encoder in a inch)
( 100ipm/60 ) x 40,000
(1.666666667) x 40,000 = 66666.66 rounded up to 670000
Acceleration = 2 x velocity = 133333.3333 rounded to 133000
Jerk = 10 x Acceleration = 1333333.333 rounded to 1330000 or 1.33e+006

This is how I understand the math for these settings.

Now for max limits
I think it was mentioned
OUTPUT = MAX Current/1024 or 35/1024 = 0.034179688amps/per bits
so 200 x 0.034179668 = 6.8359375 amps max output. Is this right?
Is this where you would set constant current limit?

INTERGRATOR = limits the amount of output to this number. so
anything that is put into P,I,or D. the output
(green line) is limited to a max swing of this
Setting. Is this correct?

ERROR = Position error Is this correct when ever the encoder
and comanded are not with in this setting it will fault
Is this correct?

Feed Forward
Velocity = ?
Acceleration = ?
Not sure when to use these or how to calculate what is needed.

I have attached a few of pictures of my tuning attempts. step responce and bode plot. I don't understand how to read the bode or what it is telling me.
And how would I fugure up filters for improved responce. I tried the setting for a lowpass filter on the other thread and it sent the servo into violent oscillations. so took that out.

Thank you
Rob

2. started over with the motor tuning. Firs one showes a high P-gain with no
D-gain to slow it down, the second picture the P-gain is reduced to 1 and started to add D-gain to smooth out the wave form

started adding more P-gain till it started to become unstable again then increaed the D-gain to smooth it out some more.

Zoomed in on the plot to see how it looked. looks good to me but
I am not for sure.

Now when I apply a little pressure to the motor shaft it does not track the comanded position verry well, and I notice a little bump when the move starts every so often. Zoomed in on the beginging of the plot and noticed a little oscillation. I hav no Idea where that is coming from.

Thank you
Rob

3. I tried to tune servos on an earlier system on the table and not connected to the loads they were driving. When I hooked them up to the machine it didn't work at all like it did unloaded. I don't know if there is any rules involved but I would connect everything to the loads they are driving before tuning. Tom is great and he can fix you on what direction to move in. He can't do it all because there are so many variables that some trial and error is involved. (sucks but what can you do). If I can help in any way I will. I'm just starting to really understand the plots myself.(thanks Tom).
Judleroy

4. Hi Rob,

Your response actually looks very good. It looks like you are following within around 1 count or 25 microinches! Of course this is under no load and at the encoder not the table. But it looks like your tuning is very aggressive with the gains pushed right up to the max before it will go unstable. The Bode Plot indicates this also with a high bandwidth of 87Hz but only a few degrees of phase margin. This indicates a very fast but nearly unstable system. You should back off on your gains a bit.

Bode plots are pretty difficult to understand if you haven't studied them for a while. They are a very power tool to help understand how a system behaves dynamically. The idea behind a Bode plot is you take the complex system you are trying to understand and excite it at a frequency and see how it responds at that frequency. Then repeat the experiment at a different frequency and study how it responds at that frequency. Then keep performing experiments until you have checked many different frequencies. Then if you plot all of your results you will end up with a Bode Plot. The Bode plot Screen does all this for you in a minute or so.

It turns out that no matter how complex a system is with springs and masses and dampers and inductors and resistors etc... if the system is "Linear" if you excite it with a continuous wave like sinusoidal motion its response will always settle also into a wavelike sinusoidal motion of the same frequency as the stimulus. The only difference between the input stimulus and the output response will be a change in amplitude and a change in phase.

So the X axis of a bode plot is frequency and there are two y axis values plotted Gain and Phase. A bode plot is plotted on a log-log scale because we are interested in a wide range of frequencies and gains. This is why the gain is plotted in db where 0 represents a gain of 1.

It is important to understand that as I said earlier for a Bode plot to be valid and useful it must be a "Linear" system. The term "Linear" means that if you do any weird thing to the system and it responds in some particular way in response. Then if at some other time you do that same weird thing to the system but say 2X as hard. Then you will get the exact same response as before except 2X bigger. This is a very important characteristic to be able to predict the behavior of a system. Unfortunately in the real world systems are never perfectly consistent and linear. For example if we try to drive a motor harder and harder eventually we will hit some limit of voltage or current which will change the expected response. Similarly if we drive a motor with smaller and smaller signals at some point the forces might become less than static friction and we suddenly get zero motion. Or the motion might be less than one encoder count. So for our purposes in order to make a good meaningful Bode Plot measurement it is important to check and adjust the stimulus amplitude (and frequency emphasis) such that we get response in the linear region. Not too big to have something in a saturation limit and not too small that friction dominates or we don't see enough encoder changes to get an accurate measurement of motion. This is what the Time Domain plot on the Bode Plot Screen is for. It allows you to see if the Output (green) is clipping and how much encoder (red) motion is being observed.
I see in your examples you did 3 measurements with 3 different levels of stimulus: 100, 200, and 300. Note with 100 the encoder motion was only around 5 counts with output peaks of only 30, with 200 encoder motion increased to ~20 with output peaks of 100, and finally at 300 encoder motion was ~40 with output peaks of 175. These are all fairly in the linear region and the Frequency domain Bode Plots all came out very similar in terms of bandwidth and phase margin.

Many things can be observed in a Bode plot but two of the most important things are the Bandwidth and Phase margin.

Bandwidth is where the Gain (blue) first crosses below the zero db line. The frequency where this occurs is called the Bandwidth of the system. Basically at low frequencies where the Gain is very high - errors will be greatly eliminated (Gain of 100 (40db) means all but 1% of the error will be corrected). At high frequencies where the gain is low - errors will mostly remain (gain of 0.01 (-40db) means that 99% of the error will remain). Where the gain is 1 (0db) is where the error is being reduced by about half (or actually about 70%). So the Bandwidth (or zero crossover frequency) is a good indication of the speed of correction of the servo. Higher is better indicating errors will be quickly eliminated. Your plots show a Bandwidth of around 87 Hz which is quite high.

Phase Margin is a measure of how stable the system is. The further away from -180 degrees of phase the system is when the Gain is 1 the more stable the system is. This is because -180 degrees of phase shift is just like inverting a signal or like having a gain of -1. A system gain of -1 in a negative feedback arrangement results in an overall gain of +1 which results in a sustained oscillation or an unstable system. A good stable system should usually have a phase margin of 30-45 degrees. Notice in your Bode Plots the phase at 87Hz is approx -170 degrees or only ~ 10 degrees of phase margin.

I suppose that is more than you wanted to know about Bode Plots. There is more info here

Regarding Max Limits: Yes that calculation looks correct. But this is the Max current setting not the “constant” current as you describe it. Any higher output (green line) will be clipped at this value.

The Integrator Limit applies to the Integrator only. This limits “Integrator Wind Up”

No the Max Error does not generate a fault (there is a Max Following Error on the Configuration Screen for that). This setting just limits the value passed into the PID calculations. It is used to avoid having the servo react too violently to abnormally large errors.

This diagram may help.

Velocity and Feedfoward values are described here.

You should put in a Low Pass filter as described in the other tuning thread. If you reduce your gains first to not have such a marginally stable system the system should then not go unstable. A low pass filter has negative phase and will always decrease your phase margin depending on how close it is to your 0db crossover frequency.

Your follow up plots look reasonable. Your performance is very good. You can’t expect a servo to have zero error. It needs some to make corrections. Some I gain would probably help with your loaded test.

You currents are fairly low so a bigger power supply probably won’t make any difference. SnapAmp allows you to plot supply voltage in real time so you can see if there is any power supply issue.

The small “bump” at the beginning is not important. This is where the servo is disabled, the parameters are updated, filters and integrator cleared, and the servo re-started. This would not happen in normal operation.

There is probably not much point in tuning further as everything will change when you add all your loads.

HTH

5. Thank's for the reply guys.
All right getting better at this now. No where close expert yet. hooked it up to a motor on the mill. It is now loaded.

I have reduced the gains back quite a bit from where they were. And it seams to respond well. On the step responce it seames that the encoder does not follow the comand very closely on the down swing. Not shure where to adjust to get that a little better.

I did another blode plot as well had to increase the amplitude to at least 800 to get any readings.

let me know what you see.

When I run it through mach and watch the axis screen in Kmotion.
on g-code moves it over shoots by about 4-10 encoder counts and then comes back really slow. It depends on how far the axis is comanded to move.
1" over shoot is very little at 100IPM. 3" over shoot is on the larger side. I would like to get that down a littl lower.

Thank you
Rob

6. First I would try to tune the motors at a much smaller move. Maybe 1000 counts instead of 10000. Did you add the low pass filter? If not I would. Take out your I gain until everything else is good. Increase your d gain until the system goes unstable. If like me you can't get it to go unstable with d gain stop around 1-200. Then try to increase your p gain until it goes unstable again and then back off by 30-50 percent of where that happens. Then you can try your I gain. From what I understand p gain tries to force things to stay on track while accelerating. D gain is like putting on the brakes or adds an element of deceleration to prevent overshoots. I gain works hard to force things to zero quickly but destabilizes the system quickly so add it last when everything else looks good. Tom understands this much better then me so I hope I got it right. Good luck.
Judleroy

7. Hi Rob,

Those plots actually look quite good. Please realize that you are zooming in about 300X so that the entire range of the scale is about 1 mil. So those errors are tiny and only when doing a balls out acceleration to rapid speed.

And no, increasing the acceleration or jerk will make things worse not make errors smaller. It is more difficult to have high accuracy when moving fast or accelerating hard. Those setting have nothing to do with how the servo responds. They define the path that we are testing.

Again the Bode Plots show a fairly high bandwidth of 67Hz but a fairly low phase margin. This means you are being quite aggressive at trying to push performance right up to the point of being marginally stable. The risk is that if something on the system changes - friction, load, table position, etc... you risk going unstable.

The overshoot of a few counts and slow correction is probably dictated by the Integrator gain being somewhat low. Again I think this is very small and probably not significant. But because you are so close to instability if you increase the I gain you will probably go unstable. So you might decrease the PD gains then try a bit more I gain.

HTH

8. I feel like I am getting closer.
I applied the filter you reccomended, and it looks like it helped alot, I also applied a second order lowpass filter. I would like to know how you come up with the calculations for the filters.
I have some photos comanding 10000 and 1000. the position error is lower than it has ever been.
There is still some noise and overshoot I would like to get rid of if it can be done.

Thank you for all the help
Rob

9. Hi Rob,

Those look really good. What are your expectations for this machine accuracy wise? I think you are already well down below the mechanics. It would have been nice to see a final Bode Plot.

There aren't really any calculations for the filters. It is mostly just common sense and trial and error. With the Lead/Lag compensator as I described previously you need to estimate your target bandwidth so as to set maximum phase lead at that point for maximaum phase margin. Then set the pole/zero frequencies on both sides of that by some factor. The wider the frequencies are apart the more of an effect there will be - more phase lead but also more pushing the gains higher on the right. If the higher gains get pushed up close to the 0db line the system can go unstable. So for example if my target is 40Hz and I want to use a factor of 2 I would set the frequecies to 20 and 80 Hz.

You might now try some feed forward. Feed forward looks at what the trajectory is doing and anticipates a nominal need for motor drive and applies it automatically. Otherwise an error must develop before there will be changes in the motor drive. It is like a person driving a car around a curve. He knows the corner is coming so he starts to turn the expected amount beforehand without waiting until he starts to run off the road to make a correction. The feedback then only has to make small corrections based on unexpected variations rather than the full amount to make the turn.

With a servo there are two common effects: mass and drag. Mass requires motor drive to accelerate and drag requires motor drive to maintain a constant speed. So Acceleration Feeforward adds in a motor drive that is proportional to the current acceleration of the trajectory and Velocity Feedforward adds in a motor drive that is proportional to the current velocity of the trajectory.

So using trial and error adjust the FF values to reduce the following error. They are usually small numerical values like 1e-5. Sometimes it is useful to turn off the Integrator and reduce the servo gains to make it easier to see the effect of the FF values. If everything was perfectly consistant the FF could command the motion with no servo feedback at all.

Regards

10. I will get the bode plot tonight for that tuning setup. Then show you another set where I droped the P down a little and added more I. that tightened up the position error but it also added some noise.

What I am looking for accuracy wise is .0001, right now if my calculations are correct I have a resolution of .000025. With the ball screw being .125lead and a 1250cpr encoder. That may be hard to get it right at .0001 but If i could keep it under .0005" that would be great. I think it is really close but will keep tweaking untill the servo sounds nice and smooth.

Question. I am using single ended encoders with about 4' of cable all well sheilded to a single point and sheilded motor cable.

Would It be any better if I was using differential encoders?

This I what I can confirm so far. When I command the machine .0001 it moves
.0001 on the indicator. I like that alot. Before it with the gecko servo drives and step and direction control, each step would be plus or minus a little, if the drive did not make it the whole way on the first step it would make it up on the next step.

And correct me if I am wrong. I was using gecko G340 servo drives before with step and direction with a smooth stepper. The geckos are nice easy drives to work with but they have a larger error to them, than what I am seeing now with the snap amp.

I will put up the Bode plots tonight.

Thank you
Rob

11. Here are the bode plots from the last tuning

12. Wow. 120Hz Bandwidth with 49 degrees of phase margin. Very nice.

No, differential encoders will have no effect on servo performance. They only help with noise problems resulting in loss or gain of encoder counts. If you run for a while and then move back to the same encoder count the physical tool position should be the same (within the mechanical tolerances). If it isn't that means counts were lost or gained.

Your resolution calcs look correct to me. I'm not sure why you didn't have similar accuracy with the Geckos. It isn't really that hard to make a small step. Any servo with any Integrator should eventually drive the error to zero. I think the main benefit to our system is we have the tools to view and tune the system to more of an optimum.

We do have a slight advantage in that we maintain the target position as a floating point number that is never truncated to "counts" or "steps". So for example if the commanded position works out to be 1000.4 encoder counts KFLOP+SnapAmp will try to servo to that exact position. Since the encoder can only tell us it is at count 1000 or 1001 the result will be a dither between those counts where 40% of the time will be at 1001 and 60% at 1000. Ironically, combined with vibration and mechanical noise this can improve resolution below what one would normally think possible. I think I recall this first being discovered in the '50s with regard to radar. An analogy would be that imagine you have a distance that you are trying to measure but you only have a ruler that reads whole inches. If you make one (or many) very careful measurements the best you can expect the answer to be is within +/- 0.5inches. But then if you make 100 really sloppy measurements and average the result might be accurate to 0.1 inches.

Happy Holidays

1. ###### Yahoo! Groups
10-02-2013, 12:32 PM