convert real to word in SCL, use round function

Sandor-CRX

Member
Join Date
Oct 2009
Location
Timboektoe
Posts
22
Hello guys,

I'm using SCL to create the control for my drive.
I have a value in the OS (0-100%) what stands for the speed of the drive= setpoint. The setpoint is tranferred to process image output-> QW. to the selected drive.
At first my program looked like this:

//Convert OS value to drive logic (100% = 16384)
act_sp := act_sp * 163.84;
//Set max. and min. range
IF act_sp > MAX_SPEED
THEN act_sp := MAX_SPEED;
END_IF;
IF act_sp < MIN_SPEED
THEN act_sp := MIN_SPEED;
END_IF;

qact_sp_i := REAL_TO_INT(act_sp);
qact_sp_w := INT_TO_WORD(qact_sp_i);
//Send speed to drive
QW [MRV] := qact_sp_w;

When I set the setpoint to 100, I see the process value which is converted from WORD to INT and then from INT to REAL and displayed at 99.99999. Also If I set the setpoint to 20, the PV will be 21.9, so there is a conversion problem.
I want to round this value...i'm not enable to use the ROUND function. Also, I then have to convert from DINT to WORD.

What is the best approach for this function?

Thank you,
 
Some values cannot be perfectly represented with 32 bit REAL (32-bit, IEEE 754 flating point format).
That is the reason you see the 99.999999 etc.
There is no easy way around it.
In all practical senses it does not matter. Just accept it.

However, using RND in STL or REAL_TO_INT in SCL will certainly convert "99.999999" to "100".
 
What about doing the scaling in the HMI itself. Most HMI's allow for this function already and you don't have to worry about it any more.
 
I have done something like this. Do you use Sinamics so you need to convert 0-100 to 0-4000h (0-16384)?

Instead of SCL, you can use STL and use RND.

Simply:

L act_sp
L 163.84
*R
RND
T QW[MRV]
 
When I set the setpoint to 100, I see the process value which is converted from WORD to INT and then from INT to REAL and displayed at 99.99999. Also If I set the setpoint to 20, the PV will be 21.9, so there is a conversion problem.

Where are you seeing these values ? Here's what I get when monitoring SCL

scl009.jpg
 
Some values cannot be perfectly represented with 32 bit REAL (32-bit, IEEE 754 flating point format).
That is the reason you see the 99.999999 etc.
There is no easy way around it.
In all practical senses it does not matter. Just accept it.

However, using RND in STL or REAL_TO_INT in SCL will certainly convert "99.999999" to "100".
Something is seriously wrong if integer numbers between -16777216 and +16777215 can't be represented accurately using reals. There must be some previous math that caused a round off error.

Why not add 0.5 and then do the REAL_TO_DINT.
 
Simple addition of fractions can cause rounding errors.

After a few times through this code:
Code:
      A     M    290.0
      JCN   END
      L     MD   300
      L     MD   304
      +R    
      T     MD   300
      R     M    290.0
END:  NOP   0
You will see this:

S7_round_err.GIF
 
Hmmm thank you guys for all the reactions...
I'm not seriously trained with conversions, but its looks a bit complicated.

Maybe if I post the reading and writing code, it would be easier to understand how I programmed this...

So it's a drive which is controlled from Scada, programmed with SCL

//Read out the Status Word and Main Actual Value
i_stw_w :=IW [STW];
i_mav_w :=IW [MAV];
//Convert Statusword from WORD to INT
i_stw :=WORD_TO_INT(i_stw_w);
i_mav :=WORD_TO_INT(i_mav_w);

//Check speed of the drive
PV := INT_TO_REAL (i_mav);
PV := (PV / 163.84);

The Drive actual frequency is read and displayed at Scada.
On the other hand, the drive can be controlled by extern setpoint (software logic) or intern setpoint (operator), looking like this:

--> act_sp = extern or intern setpoint

//Convert OS value to drive logic
act_sp := act_sp * 163.84;
//Convert
qact_sp_i := REAL_TO_INT(act_sp);
qact_sp_w := INT_TO_WORD(qact_sp_i);
//Send speed to drive
QW [MRV] := qact_sp_w;

I give the intern setpoint 20.0, but the drive will send 21.5.
That is the main problem. My program looks complicated and maybe could be made easier and better.

I'll try the answers you gave me and i'll post the updates...

Thank you
 
The PV is the process value and is called "PV" in the program. This will read the main actual value that the drive is sending.
So at 50%, the value HEX2000 is returned. I convert that number to a real value, so I can see that the drive is operating at 50% (Hmm need to change that, because I want RPM)

The setpoint is SP_OP (operator can set a percentage->for example, drive at 50%) and SP_EXT (is created in the software). When in automatic mode, the SP_EXT value is written to the drive, whil in manual mode, the operator can select the SP_OP to manual give a speed



//Check speed of the drive
PV := INT_TO_REAL (i_mav);
PV := (PV / 163.84);

//SELECT EXTERN SETPOINT IF DRIVE IS IN AUTOMODE
IF SPEXT_EN OR (NOT SPEXT_EN AND NOT SPINT_EN AND AUT_ON_OP)
THEN act_sp :=SP_EXT;
QSPEXTON :=TRUE;
//SELECT INTERN SETPOINT IF DRIVE IS IN MANUAL MODE
ELSIF SPINT_EN OR (NOT SPEXT_EN AND NOT SPINT_EN AND NOT AUT_ON_OP)
THEN act_sp :=SP_OP;
QSPEXTON :=FALSE;
END_IF;

//Convert OS value to drive logic
act_sp := act_sp * 163.84;
//Convert
qact_sp_i := REAL_TO_INT(act_sp);
qact_sp_w := INT_TO_WORD(qact_sp_i);
//Send speed to drive
QW [MRV] := qact_sp_w;


 
Last edited:
So to put it shortly, you want to scale the REAL value SP_OP from (0.0-100.0) to the INT qact_sp_i (0-16384).
The returned value from the drive is also so that i_mav goes from 0-2000h (0-16384) when the speed goes from 0-100%.

Your code looks OK, functionally I mean.

Check in a VAT that the variables qact_sp_i and qact_sp_w are 16384 when the SP_OP is 100.0.
If that is OK, then the problem is with some scaling or other function in the drive.
 

Similar Topics

Hi, is it possible to convert a WORD to REAL value using S7? Regards, henno2000
Replies
2
Views
12,594
Hi all. Me again still learning Rockwell. So I'll be polling an INT data array from a Modbus SE power meter with a L82 (with a Modbus ProSoft in...
Replies
56
Views
1,384
Hello, First, please excuse my question I am new to Modbus. I have a CX-5140 control unit from Beckhoff and an instrumentation device that...
Replies
3
Views
3,114
Greetings fellow PLC programmers, I know there is a simple way to get the system date time of the RSLogix system. But, is there then a way to...
Replies
12
Views
8,585
Hi Guys, i'm begginer and i'm trying to convert a real value (not fix value) to BCD. It works if i put fix real value but when i put variable...
Replies
2
Views
1,940
Back
Top Bottom