Quick PLC Math question.

Fiddeflygare

Member
Join Date
Mar 2009
Location
Värnamo, Sweden
Posts
10
Hi everybody i am currently working with a project at my company and i have spent mostly of the evening yesterday and the morning today trying to get an answer to my question with no luck by searching the help texts, manuals and internet (Google) with no luck :confused:. I am programming a machine where the customer must be able to program a couple of different variables where two of them are the lenght of the used raw material and how long one part made of the material will be.

Now the problem i have are that they want the machine to calculate how many meters of raw material the machine will use for his order of say 100parts that are 320mm long. The rawmaterial they most commonly use comes in 4m lenghts and this give me a problem. The calculation of this gives me 12.5 parts per 4m raw material and that it will need 8.3 4m lenghts for the order to be completed.o_O Now to my question: Is there any way to make the PLC round down the 12.5 parts per raw material to 12 and round up the 8.3 raw material lenghts to 9? o_O

I am currently programming a Rexroth indradrive C MLD and i am using REAL variables for the calculation, i am programming most calculations in ST but if the solution lies within programming in FBD, Ladder or any other language i am all ears. Anyone knows how to solve this problem?

Thanks in advance for your help.

Best Regards
/Fidde
 
Is there any way to make the PLC round down the 12.5 parts per raw material to 12 and round up the 8.3 raw material lenghts to 9? o_O

Bubblegumfix:
(pseudocode, implement in st yourself)
for parts per rawmaterial:
Code:
int_to_real(real_to_int(partsPer))
for raw material usage:

Code:
if(rawUsage>(int_to_real(real_to_in(rawUsage)))
then
rawUsage:=rawUsage;
else
rawUsage:=rawUsage+1;
end_if;

something like this.

Btw does rexroth use CoDeSys or something their own? Just for curiosity.
 
Last edited:
Maybe just leave that partsPer as int?

Code:
real_to_int(partsPer);

use dint if you need that big numbers.
 
Last edited:
Hi turpo.
Thanks for the help though i cant get it to work properly. it still stays at 12.5 or it just goes down a little bit to for example 12.4 instead of going to a rounded even number.

Rexroth uses CoDeSys as far as i know.
 
This is siemens version of ST, SCL code:


Code:
FUNCTION Test : VOID

VAR_INPUT
    rawUsage : REAL;
    partsPer : REAL;
END_VAR

VAR_OUTPUT
    iRawUsage : REAL;
    iPartsPer : REAL;
END_VAR

//program
    
iPartsPer := INT_TO_REAL(REAL_TO_INT(partsPer));
    
IF rawUsage>(INT_TO_REAL(REAL_TO_INT(rawUsage)))THEN
    iRawUsage := INT_TO_REAL(REAL_TO_INT(rawUsage))+1;
ELSE
    iRawUsage := INT_TO_REAL(REAL_TO_INT(rawUsage));
END_IF;

END_FUNCTION
i have no access to CoDeSys right now so cant test what tricks have to be done to get it working, if i remember right, CoDeSys ST does not support nested functions like "INT_TO_REAL(REAL_TO_INT(rawUsage));" or they behave differently. In that case you might need to use some temp variables.
 
Last edited:
This is how i made it work. It is not optimal and not exactly like i wanted it to be but it works as far as i can see now at least.


Code:
I_MaterialBehov:=REAL_TO_INT(R_MaterialBehov);
I_ListPerMaterial:=REAL_TO_INT(R_ListPerMaterial);
 
 
 
 
 
IF R_ListPerMaterial>=REAL_TO_INT(I_ListPerMaterial) THEN
R_ListPerMaterial:=INT_TO_REAL(I_ListPerMaterial);
ELSE
R_ListPerMaterial:=INT_TO_REAL(I_ListPerMaterial-1);
END_IF;
 
 
 
 
 
IF R_MaterialBehov<=REAL_TO_INT(I_MaterialBehov) THEN
R_MaterialBehov:=INT_TO_REAL(I_MaterialBehov);
ELSE
R_MaterialBehov:=INT_TO_REAL(I_MaterialBehov+1);
END_IF;

ListPerMaterial is the parts per rawmaterial variable and MaterialBehov is the rawmaterial variable.

The variables with an R first are REALS ande the ones with I first are INT:s

Thanks for the help anyway because i probably would not have figured out the solution without your code and contribution.
I am not sure if i really need the long code in the IF statement but i let it be there just in case.. ;)

(y)
 
I don't know this model of PLC, but if it follows the rounding rules (>=.5 then round up, <.5 round down) simply take the number you calculated for parts per raw material and SUBRACT 0.499 from it. For the sheets needed do the opposite, ADD 0.499 to the number before you round. This should make it always round the way you want.
 
Yes, as i said, bubblegumfix :)

btw IEC 61131 says this about TRUNC

TRUNC(1.6) is equivalent to 1
TRUNC(-1.6) is equivalent to -1
TRUNC(1.4) is equivalent to 1
TRUNC(-1.4) is equivalent to -1
 
Last edited:
As I read the thread I was going to suggest that TRUNC (truncate) be used - I see that TurpoUrpo already mentioned the TRUNC instruction.

calculate parts per piece of stock
Code:
Parts_per_piece :=  TRUNC(Stock_Length/Part_Length);

Calculate amount of stock needed. Here we need to allow for the times when the number of parts ordered is evenly divisible by the parts per piece.
Code:
Number_of_pieces_of_stock_needed :=  TRUNC(Parts_Ordered/Parts_per_Piece);
IF Number_of_pieces_of_Stock_needed < (Parts_Ordered/Parts_per_Piece) Then
    Number_of_pieces_of_stock_needed := Number_of_pieces_of_stock_needed +1;
End_if
 

Similar Topics

Hi, I have tried uploading the program from an old Quick Panel 2 using Proficy ME 9.0 and Quick Designer 3.7. I can get the program to upload...
Replies
1
Views
1,457
Q(1) Design a controlling system using DVP-40ES Delta PLC for a threestory Elevator Prototype as shown in the figure below. Show in details the...
Replies
4
Views
2,171
I apologize for not having the model of the plc. I am trying to get it. I have a print out of the program and it seems straight fwd. One...
Replies
12
Views
3,437
What is the current cable to use to connect a laptop PC to a PLC-5 channel 0 (25 pin connector). I have a homemade cable that is just like...
Replies
3
Views
1,538
hello I use GE_FANUC plc and quick panel HMI and I want to access to GE_FANUC variables such as inputs and output and memory varibles through...
Replies
5
Views
3,805
Back
Top Bottom