Is anyone interested in how to compute the velocity and acceleration from an encoder?

TurpoUrpo is right about the precedence

I just got to work. I will make a quick video.

Code:
// Step 0 
VAR
	Omega: REAL;
	T:  REAL;
END_VAR

T:=_LoopTime ;
Omega:=PI2*Hz;
IF FilterSelect=0 THEN
	K0:=3.0*Omega*T;			// CRITICALLY DAMPED
	K1:=3.0*Omega**2.0*T;
	K2:=Omega**3.0*T;
ELSEIF FilterSelect=1 THEN		
	K0:=2.0*Omega*T;			// BUTTERWORTH
	K1:=2.0*Omega**2.0*T;
	K2:=Omega**3.0*T;
ELSE
	K0:=2.089582*Omega*T;	// MINIMUM IAE
	K1:=1.479222*Omega**2.0*T;
	K2:=Omega**3.0*T;
END_IF
x0:=_Axis[1].TarPos;			// start at the reference position
x1:=0.0;						// assume initial velocity is 0
x2:=0.0;						// assume iniitial acceleration is 0

// STEP 1
VAR
	T: REAL;	// controller update interval
	err: REAL;	// error between actual and estimated position
	x0p: REAL;	// estimated or predicted position
END_VAR

T:=_LoopTime ;						// cache the loop time to avoid multipe GSVs
x0p:=((( 0.5*x2*T)*T+x1)*T+x0);	        // calculate estimated or predicted position
err:=_Axis[1].TarPos-x0p;			// calculate error beween actual and estimated position
x0:=x0p+K0*err;					// update position
x1:=x1+(x2*T+K1*err);			// update velocity
x2:=x2+K2*err;					// update acceleration
This is the code. I will use. Step 0 executes only once when the user program or SFC sequence is started. Step 1 executes forever. The variables declared in the step are local to the step only. The variables that are not declared here are global variables declared else where.
You will need to modify how you calculate the loop time, T, and how you get the position from the encoder to make this work on a S7. On a S7 the update time, T, will be around 10 milliseconds. At these slower rates you may be able to get the filter frequency up to 20 Hz without the filter frequency over reacting and oscillating. 20 Hz should be more than good enough for conveyors and things that don't accelerate too quickly.
 
Hi Peter!

I am planning to use the filter, but it's lack of time yet.

I will try it on a loss in weight system with some big outhouse tanks (0.5 ktons) that has a noisy weight signal caused by unsteady wind forces. I guess the 'noise' period is fluctuating much, but it's more than 1 sec. Perhaps is this a stupid model to test it on?

Today I use the AIFB's built-in lowpass.

Kalle
 
Kalle, you really care about phase delay on such a system? You aren't going to use the derivative are you? That is what the ABG filter provides. If you don't need the derivative or close to 0 phase delay you can simply cascade a bunch of low pass filters together to filter the signal.
 
Interested and Watching closely

Hi just seen this thread - Was looking for this type of code 3 weeks ago.

Implementation on the control logix platform would be interesting due to the variability of the scan time (Motion group update time) and a delay
eg 5 ms desired update with 0.7 ms variability - the variability is due to the motion task taking different execution times each scan and the delay is because the code would be in an event task that executes after the motion group update time has finished so the change in commanded speed and accel occurs 4.3 to 5 ms later
 
Kalle, you really care about phase delay on such a system? You aren't going to use the derivative are you? That is what the ABG filter provides. If you don't need the derivative or close to 0 phase delay you can simply cascade a bunch of low pass filters together to filter the signal.

The lowpass works OK. Just curious to see if I can get the filter code playing. Compare graphs.

Kalle
 
Awesome, Peter. I have to DL your attachment and take a look at it; I've very interested in this stuff, and in fact I was looking up ABG filters a few months ago and I came across you in several sites I visited - you get around!

I have an air cylinder with an electropneumatic regulator and a linear position sensor, and I want to modify the air pressure to create a velocity. Well, so the first question becomes, "What is the cylinder velocity?" This is exactly what a Kalman or AB filter was originally intended to do... given a series of position measurements, estimate velocity. And, in fact, an AB or ABG filter does an awesome job of providing a useable velocity output.

I played around a lot in Excel - man I wish there had been computers when I was learning this stuff in school 30 years ago - and I then actually implemented an AB filter in an Allen-Bradley uLogix PLC. It runs, I have to admit I'm not sure how well yet. I played with it some last October and then got diverted; I am hoping to get another shot at the machine between Xmas and New Year. I would like to get it up to an ABG filter, but I'm not sure I implemented that one correctly. I'm looking forward to reading your attachment.

I also ran across (and tried) using an AB filter, then using a second AB filter again on the output of the first filter. That seemed to help, too, but again I'm not entirely sure of the algorithm I created. Certainly an AB filter takes a hell of a lot less memory and math ops than a full ABG, which matters probably more on my little PLC than it would to you on a motion control platform.

Anyway, GREAT topic, thanks! I will be back after I read your attachment.

Paul T
 
Awesome, Peter. I have to DL your attachment and take a look at it; I've very interested in this stuff, and in fact I was looking up ABG filters a few months ago and I came across you in several sites I visited - you get around!
You must be referring to Pavel Holoborodko's Smooth Noise Robust Differentiator website. That is good stuff but I can't see where I can use it because the ABG filter works much better for motion control.

I have an air cylinder with an electropneumatic regulator and a linear position sensor, and I want to modify the air pressure to create a velocity. Well, so the first question becomes, "What is the cylinder velocity?"
We control air cylinders too. A four pole Butterworth filter works just fine in these cases because a little phase delay is not a big deal with such a slow system.


This is exactly what a Kalman or AB filter was originally intended to do... given a series of position measurements, estimate velocity. And, in fact, an AB or ABG filter does an awesome job of providing a useable velocity output.
Yes but you just made things a lot more complicated when you are trying to control a system using a Kalman or ABG filter. Now you need to have a model of the system. In my simple example I was not controlling the wheel. I was simply trying to estimate the velocity and acceleration accurately. There is a big difference because now you need a model.

I can make a video of how we use a model. When we use a model we use the feedback to correct the model but we also use the control output to the model and the plant to make them move. If you have a good model the feedback doesn't need to do much correcting. I will see if I can make one up next week.

I played around a lot in Excel - man I wish there had been computers when I was learning this stuff in school 30 years ago - and I then actually implemented an AB filter in an Allen-Bradley uLogix PLC.
They were not teaching this stuff 30 years ago. I learned most on my own in the last 15 years when DSPs because available. Until then why bother if it cost too much to implement?
 
For Paul T. Warning, there is a lot of geek speak here.

I have an air cylinder with an electropneumatic regulator and a linear position sensor, and I want to modify the air pressure to create a velocity. Well, so the first question becomes, "What is the cylinder velocity?" This is exactly what a Kalman or AB filter was originally intended to do... given a series of position measurements, estimate velocity. And, in fact, an AB or ABG filter does an awesome job of providing a useable velocity output.
As I pointed out above the ABG filter is simple because one is simply estimating velocity and acceleration. The my example of the ABG filter assumes that the position, velocity and acceleration will continue to do what it did last time. That is not true in most systems. If the control signal is turned off the system will decelerate to a stop. When controlling an actuator a better model should be developed that takes into account the control signal and how the actuator or plant will respond as the control signal changes. Then a good model can be developed. Unfortunately this requires a LOT of math and is not practical to do inside a PLC. It must be done in the programming software.

When controlling an actuator we use an observer and not a ABG filter
http://en.wikipedia.org/wiki/State_observer

I know the code looks like greek but implementing the observer itself is only a little more difficult that implementing the ABG filter. The real problem is doing the system identification to develop a model so you have good numbers to use in the matrices.

Here is a link to another video about how and why we use an observer.
http://deltamotion.com/peter/Videos/AutoTuningModelingObserver.mp4

The encoder resolution on my system is very poor. Only 8000 counts per revolution. The green line or control output jumps in steps at first because there is no filtering and the angular velocity resolution is only 0.5 revolutions per second. It is the derivative gain term that is causing these spikes because the derivative gain is multiplied by the error between the target and actual velocity. If the actual velocity just changes in large steps the control output will change in large steps too. Obviously you don't want these kinds of pulses going to a big drive. This is a major reason why people don't like to use the derivative gain. However, I must use the derivative gain because my amplifier does not have a velocity loop. It simply converts the control voltage to a control current.

The auto tuning process computes a system model which is used by the observer. The observer returns more accurate and less noisy estimates of the angular velocity. This allows me to not only use the derivative gains but to crank them up to provide stability and electronic damping.

Paul, on pneumatic system we have found that the second derivative gain can help a lot. Te second derivative gain is multiplied by the error between the target and actual acceleration therefore an accurate estimate of the actual acceleration is necessary.

So why not use Kalman filters? For the same reason I provided for the ABG filter. The average guy is not going to know what the measurement covariance or the system noise is so the numbers are fudged and the results are less than optimal anyway. We provide a filter frequency that one adjusts to get the desired results although our defaults work well most of the time.
 
How we do pneumatic control

I have an air cylinder with an electropneumatic regulator and a linear position sensor, and I want to modify the air pressure to create a velocity. Well, so the first question becomes, "What is the cylinder velocity?" This is exactly what a Kalman or AB filter was originally intended to do... given a series of position measurements, estimate velocity. And, in fact, an AB or ABG filter does an awesome job of providing a useable velocity output.
Paul T
Paul, as stated above, you need to have a model and use an observer. This takes a little extra effort but it pays off in excellent control. The controller can generate accurate velocities and accelerations. The feed back is a MDT rod and the resolution is about 0.0005 inches.

Hydraulic and pneumatic systems can be modeled as a mass between two springs. This requires more gains for accurate control. The servo motor only requires a PID and velocity and acceleration feed forward because it can be modeled as a single pole system. The pneumatic system is a two pole system. The controller should use a second derivative gain and a jerk feed forward. Also, pneumatic systems are non-linear. The auto tuning get close but some manual tweaking of the gains is required. The observer tends to linearize the non-linear system and make it easier to control.

The pneumatic system is down in the shop and it is very noisy noisy. So I am accessing it using Ethernet so I could make the video in the quiet of my office except for the intercom.
http://deltamotion.com/peter/Videos/PneuMove.mp4
 
I was hoping to see more questions. Is it clear or confusing?
Obviously it is mathemagic in action.

Peter, I think the math is clear but I'm wondering where I can actually use this? I guess I have never tried to create my own motion loops or Control Loops in a PLC. I would much prefer to use something from Delta or my German fiends :)
 

Similar Topics

I keep moving this thing around my garage thinking I will do somehting with it. I did power it up a few weeks ago, and it seems to work fine...
Replies
7
Views
2,190
I have been looking at this program for awhile now: http://www.cie-wc.edu/Electronic-Engineering-Technology.asp It works for me and my time...
Replies
3
Views
1,853
I received an email from a student with the following code attached. This is supposed to control a floodgate system, and supposed to be written...
Replies
23
Views
777
does anyone have an install or know if/where i can download it for the following "ABB PS501 Control Builder Plus V2.3 " the software was a free...
Replies
2
Views
80
They are installed in a control panel that was made in France and are intended for the termination of analog inputs. Each of the red capped...
Replies
4
Views
397
Back
Top Bottom