Slope Comparison in CX-Programmer

jholm90

Member
Join Date
Mar 2012
Location
Ontario
Posts
155
I am looking for some input on how to proceed with programming some logic in CX-Programmer.

The set-up that I am faced with is a calibration machine that has a flow meter that will give me a 0-10v signal input. I need to turn an adjustment screw with a small DC motor until a proper flow rate is reached. If it were just a set-point value, we would be away to the races - but the customer has requested that I compare also a few scenarios to wield out bad parts. An ideal calibration would be a straight 45 degree angled line on a chart of flow over time, the more I adjust the screw the more flow, in an even manner until my set-point is reached. I also need to catch a flow spike and flag it as a blocked unit, a relativity unchanged horizontal slope as a leaking unit, and a parabolic-styled slope that indicates a stripped unit.

As for collecting my data, the current code I have is: on the start of the test, flag a 0.2 second pulsing timer that will move the scaled voltage input through a series of 30 data registers at a time - so I will have 6 seconds of data and 30 data points to work with. I was hoping to use a limit function to ensure that it is within a margin of error range from the ideal setting. The ideal setting I would like to calculate as a y=mx+b slope function - but that would entail making 30 calculations and making 60 total new data registers of a min and max value for each of my 30 data input points. To catch a leaking unit, I will add a plus/minus offset to the current data point and compare it to the previous 5 points, if it is within range than we know it is a leaking unit. The issue that I have with this code is it does not catch the blocked and stripped unit parameters

I was hoping that someone has had some experience with using slope-related math and could provide some insight on to the best way to make a simple code for this application. I have attached what the four possible slope graphs that we are looking to catch.

Pressure curves.jpg
 
Not sure if i understood you properly, but on Allen Bradley Control Compactlogix there is a instruction called Fxgen.
Other possibility is to use an Index and an Array, in such a way that every step of your index is a different value of your array, Use a timer and a counter so that every time the timer ticks the counter changes its value, counter accumulated value is the index for your array. In this case you can use four different arrays one for each of the curves above.
 
I think you are looking for a simple linear regression. Basically, it's taking a set of data points and calculates a straight line that best fits the data.

Although it's called simple, I think it's only because there are much, much worse methods of calculating regressions...:ROFLMAO:


Regression Equation(y) = a + bx
Slope(b) = (NΣXY - (ΣX)(ΣY)) / (NΣX2 - (ΣX)2) --> This is our target
Intercept(a) = (ΣY - b(ΣX)) / N

where:
x and y are the variables.
b = The slope of the regression line
a = The intercept point of the regression line and the y axis.
N = Number of values or elements
X = First Score (for us, it's time)
Y = Second Score (for is, it's the analog value)
ΣXY = Sum of the product of first and Second Scores
ΣX = Sum of First Scores
ΣY = Sum of Second Scores
ΣX2 = Sum of square First Scores


Luckily, since the time intervals are constant and theres a fixed number of samples, we can fill in a good potion of it:
N = 30
ΣX = 93 (0.2 + 0.4 + 0.6 ... + 6.0)
ΣX2 = 378.2

Plugging in and simplifying gives us:

Slope(b) = ((30 * ΣXY) - 93 * ΣY) / 2697

From here, you will need to find:

ΣY = sum of all of the analog values
ΣXY = sum of the products (0.2 * {first analog value} + 0.4 * {second analog value} ... 6.0 * {last analog value})


 
Using the least squares fit doesn't tell you how well the line fits. it just says what line fits the data the best. The least squares line fit will fit a line through any of the examples shown above even thought it is not a good fit.

To test the quality of the fit you should compare the each of the data points with the estimate from the least squares fit. Do this by using the sum of square errors.

Code:
sse:=0; // sum of square errors
for i:= 1 to 30
   sse:=sse+(actual_value[i]-estimated_value[i])^2
The actual_value is the array of actual readings for each 0.2 second time period. The estimated_value is the value calculated by the y=m*time+b calculated from the least squares slope and offset. If the SSE is large the least squares did not find a good fit. You will need to find this threshold by trial and error.

Personally, I think this is a lot to do in a PLC. There must be an easier way. If you are interested let me know.

Edit, linear regression alone will not provide a way of providing a 'goodness of fit'.
http://en.wikipedia.org/wiki/Anscombe's_quartet

More edit, if you don't need the linear regression equation and just and estimate of how linear the data is then there is the difference method. The difference method is simpler. If can tell you if there is a curve and whether the curve goes up or down.
 
Last edited:
Using the least squares fit doesn't tell you how well the line fits. it just says what line fits the data the best. The least squares line fit will fit a line through any of the examples shown above even thought it is not a good fit.

To test the quality of the fit you should compare the each of the data points with the estimate from the least squares fit. Do this by using the sum of square errors.

Code:
sse:=0; // sum of square errors
for i:= 1 to 30
   sse:=sse+(actual_value[i]-estimated_value[i])^2
The actual_value is the array of actual readings for each 0.2 second time period. The estimated_value is the value calculated by the y=m*time+b calculated from the least squares slope and offset. If the SSE is large the least squares did not find a good fit. You will need to find this threshold by trial and error.

Personally, I think this is a lot to do in a PLC. There must be an easier way. If you are interested let me know.

Edit, linear regression alone will not provide a way of providing a 'goodness of fit'.
http://en.wikipedia.org/wiki/Anscombe's_quartet

More edit, if you don't need the linear regression equation and just and estimate of how linear the data is then there is the difference method. The difference method is simpler. If can tell you if there is a curve and whether the curve goes up or down.



I would be interested in hearing more about the mentioned difference method. 95% of the parts that we are calibrating will fall either into our "ideal scenario" of a nice linear graph to reach a setpoint or a relatively flat line leaking part. This should at least give the operator a notification to rework the part if it fails both of the two scenarios.

the snippet of provided code makes sense to me, but it would be a challenge to integrate it into the existing ladder logic that I have.
 
jholm90, do you need to identify the type of curve or do you just need to tell if the curve is good or not? Since you haven't said I will start with the assumption that you just want a straight line. This is relatively easy. You can detect the slope by a[i+1]-a but if you want to know if the slope is changing then you can do this

Code:
slope_change:=(a[i+2]-a[i+1])  // the slope from i+1 to i+2
                     -(a[i+1]-a[i]);     // the slop from i to i+1
This can be simplified to
Code:
slope_change:=A[i+2]-2*A[i+1]+A[i];  // this is the second derivative of the data at i+1
The slope_change or second derivative should be 0 if the data is a straight line. Since you data is not perfect there will be some non-zero slope_change values. If the slope_change values are negative then the data curves down. If the slope_change is positive then the data is curving up. If you just need to check if the slope is reasonably constant then do this

Code:
sscs:=0;  // sum of slope change squared
for i:=0 to 27;   // assume the 30 element array A goes from 0 to 29
     sscs:=sscs+( A[i+2]-2*A[i+1]+A[i])^2;  
end_for
if sscs < straight_threshold   and         // check for straight a reasonably straight line
   a[29]-a[0] >  slope threshold  then  // make sure the line is not flat.
      good:=true;
else
      good:=false;
end_if

This is MUCH simpler than the least squares method.
 

Similar Topics

Ok guys this isn't a standard how do I use slope for analog. My daughter hates math. She doesn't understand I've tried and tries to explain how...
Replies
31
Views
12,125
i have studio 5000 v 24 and the processor is the L75, what i am trying to do is omit the ph controllers we use for waste water treatment. The new...
Replies
0
Views
3,262
I am hoping someone can explain, well actually point me in the correct direction to be able to do the math on an inverse slope. This is was I...
Replies
19
Views
6,086
Good Morning Gentlemen, Does anyone know of a device that will accurately sense slope and give an analog output based on percent slope? This...
Replies
9
Views
2,758
G
I am sure at least one of this site’s brilliant minds can assist me with this… My wife requested I find a program, excel macro, or simply the...
Replies
6
Views
8,404
Back
Top Bottom