This board is for PLC Related Q&A ONLY. Please DON'T use it for advertising, etc. 

Try our online
PLC Simulator FREE.
Click here now to try it.

New Here? Please read this important info!!!
April 21st, 2017, 02:54 AM  #1 
Member
Join Date: Jan 2011
Location: Localhost
Posts: 22

Integer arithmetic, no decimalpoint
Hello
I'm dealing with a couple of PLCs that arent able to handle floats. The task I have is to program a regulator and not use the integrated one. Say I'm multiplying every number comming from the HMI with 100, and then dividing on 100 in the PLC. How do I then take care of the reminder. If for instance the Kp is set to anything between 0 and 0.99 (099) this will end up as 0. I have access to regular math operations lik divison, multiplication and modulo. I tried doing some searching, but I'm not quite sure what I should search for... 
April 21st, 2017, 04:55 AM  #2 
Member
Join Date: Jul 2007
Location: Lancashire
Posts: 1,089

Some Questions:
Real project or learning assignment? What type of regulator? and why not use the integrated one? What processor? 8 bit? 16 bit? 32 bit? Some things to think about: Multiply before divide to preserve resolution Limits of a fixed number system  signed and unsigned Shift Left / shift right to multiply/divide by 2 Notes on efficiency of code: Multiplying is much faster than dividing  think how you would code each function in assembly code and you will begin to understand! To divide by 100 in many CPUs it is quicker to convert INT to BCD, Shift Right 8 and then convert BCD to INT. Nick 
April 21st, 2017, 06:33 AM  #3 
Member
Join Date: Jan 2011
Location: Localhost
Posts: 22

Its a learning task on lab equipment.
PI regulator with PD feed forward. Not using the integrated one because we're supposed to use what we've learnt about ztransformation and discrete mathematics. It's an Mitsubishi FX1N PLC, so 32bit. Here's the Dpart of the PDregulator: Where Kpff, Nff, Tdff and Ts all are regulator parameters. v(k) and v(k1) is the measured disturbance (now and previous) and u(k) and u(k1) is the output (now and previous). Ts is the sampling time. Kpff, Tdff and Ts is multiplied with 100 from HMI and can be decimals. Nff is integer only. 
April 21st, 2017, 07:09 AM  #4 
Lifetime Supporting Member + Moderator

With 32bit integers you have a value range between −2147483648 to 2147483647
If we assume that the decimal point is offset by 2 positions, then you can represent −21474836.48 to 21474836.47. If we assume that the decimal point is offset by 4 positions, then you can represent −214748.3648 to 214748.3647. The problem will be that the values used risk hitting the limits for every calculation in the formulas. So you have to perform a whatif analysis of every possible combination of data that is input into the formulas, and see if you hit the limits. If you see that you risk hitting a limit, you may have to rearrange the math in a way that the limit is avoided. It wont be simple ! Also, in your runtime code you should make checks for if there has been an overflow. Apart from that, a PLC without floating point support is c**p. Seriously. edit: And even if this is for training only, then it is IMO useless training. You wont ever use it again in real life.
__________________
Jesper 3 strikes and you're out
Last edited by JesperMP; April 21st, 2017 at 07:11 AM. 
April 21st, 2017, 07:44 AM  #5 
Member
Join Date: Jan 2013
Location: UK
Posts: 651

Use fixedpoint number representation.
the maths is faster if you use a decimal fraction as a power of 2, but you can do it with 100 if this makes more sense. your 4 operators: Code:
Fixed Point operation 32bit integer equivelent a+b a+b ab ab a/b a*100/b a*b a*b/100 
April 21st, 2017, 09:07 AM  #6 
Lifetime Supporting Member + Moderator
Join Date: Feb 2008
Location: OKC
Posts: 1,391

I am pretty certain the FX1N is capable of doing the arithmetics you are wanting?
Are you certain you are doing conversion correctly? 
April 21st, 2017, 09:45 AM  #7  
Member
Join Date: Jun 2007
Location: uk
Posts: 1,232

Quote:
But like i say this is from memory and its a while since i used Fx for maths ! In fact here is a manual, i was half right, sort of, its described on page 525: http://www.autocontcontrol.cz/Files/...nual_Basic.pdf Last edited by tragically1969; April 21st, 2017 at 09:52 AM. 

April 21st, 2017, 10:57 AM  #8 
Member
Join Date: Aug 2016
Location: Zagreb
Posts: 108

http://dl.mitsubishielectric.com/dl/...992d88101e.pdf
FX1N PLC can do floating point arithmetics, check this programming manual for further help. Float division mnemonic is EDIV. 
April 21st, 2017, 11:44 AM  #9 
Member

Back in the dark ages when floating point cost a lot I used lots of integer math. DSP programming was mostly integer math. The suggestions provided so far are not optimal.
DSP programmers use what they call Q15 format but that was for 16 bit DSPs. What this meant is that all numbers were scaled from 1 to 0.99999 with the lower 15 bits being used as a mantissa and bit 15 was the sign bit. Note, since FN1 has 32 bit math I would use Q16 format. In this case the lower 16 bits are fractions of one and the upper 16 bits are a sign + integer. Q formats reduce or eliminate the need to do division. The trick is to use the right Q??? for the application. Look into DSP Q15 math routines. Much of the scaling is done with quick shift lefts or rights. I agree with JesperMP that using integer math is a waste of time now that floating point is so cheap. I see no feed forwards, integrator or derivative term in Somlioy's formula. It would be nice to know what is being controlled and what the other parameters are. I can easily make a PID difference equation in fact I have done it so many times I have it memorized. I know what a PID should look like and many other filters too. Doing PID control on a PLC is a pain. PLCs are not suitable for fast closed loop control. It would have been better to use Matlab or Scilab and NOT use the built in functions except for plotting. Does the FN1 have a plotting function that is fast enough. This is nuts. Refer to my rant about college professors that know little about doing control in the real world.
__________________
"Living is easy with eyes closed, misunderstanding all you see...." Strawberry Fields Forever, John Lennon 
April 21st, 2017, 11:53 AM  #10 
Lifetime Supporting Member
Join Date: Nov 2011
Location: Corona, CA
Posts: 1,357

I have run into PID blocks that require integers only.
I usually multiply the real by 10 and then convert to integer and use that value for the PID. Control is better because the PID is now operating on the least significant digit which is a tenth of the value. Example...Temp1 is type K TC at 02500.0 degrees real. Temp x 10 and then convert to real it is now 025000. That value is used in the PID block. This makes the PID block a lot more sensitive to changes. If you are looking at 0.00 value, the I see why you are using x100 on the real.
__________________
****Control Freak**** Net is where I be and Nathan is me. Last edited by NetNathan; April 21st, 2017 at 12:00 PM. 
April 21st, 2017, 01:54 PM  #11 
Member
Join Date: Jan 2011
Location: Localhost
Posts: 22

Thanks for answers everyone.
As I said, formula provided was only the Dpart of the feedforward regulator. Heres complete formulas: Upi is the PIregulator. Sum of Upff + udff makes the PDregulator. Sum of PI and PD makes the whole regulator again. The system is a water tank with variable output flow. The series regulator controlls a proportional inlet valve. Feed forward regulator measures the output flow. If you implement the PIregulator without handling the remainder you'll end up with stationary error. 
April 21st, 2017, 04:23 PM  #12 
Member

That makes more sense. v is the out flow not the level error. If looks like there is a low pass filter for the derivative term.
You need to keep track of the remainders for the integrator and accumulate them.
__________________
"Living is easy with eyes closed, misunderstanding all you see...." Strawberry Fields Forever, John Lennon 
April 22nd, 2017, 11:43 AM  #13  
Member
Join Date: Feb 2012
Location: Saskatoon
Posts: 96

Quote:
Add 1/2 of your divisor (50) to your integer before doing your divide. 50 and higher becomes above 100, so divide by 100 gives 1 instead of 0. I think that was your original problem. Agreed with all of the rants about not using such a simple PLC for PID control of the real world. If you are also processor bound, you can do bit shifts to make the scan time lower. But that is *REALLY* an exercise. Something like shift left 2, (*4), store as temp1, shift left 3 (*8, *32 total), store as temp2, shift left (*2, * 64 total) gives you *64. Add temp2 for *96, add temp1 for *100. Icky. 

April 22nd, 2017, 05:18 PM  #14  
Member
Join Date: Oct 2004
Location: Montreal
Posts: 692

Quote:
The complete rule is: increase the dividend absolute value by half of the divisor absolute value, before doing divide. 

April 22nd, 2017, 07:06 PM  #15  
Member
Join Date: Feb 2012
Location: Saskatoon
Posts: 96

Quote:
You caught me in a short cut. I find it increasingly disturbing that I can't rely on my memory for details like this ... sigh! 

Bookmarks 
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)  
Thread Tools  
Display Modes  


Similar Topics  
Thread  Thread Starter  Forum  Replies  Last Post 
PLC with 64bit floating point and integer arithmetic?  AustralIan  LIVE PLC Questions And Answers  20  February 21st, 2017 08:34 PM 
AB PLC5 Integer Maximum Elements Allowed  GENERY  LIVE PLC Questions And Answers  10  March 7th, 2016 02:04 PM 
ML 1100 floatvalues to two integer values  srv  LIVE PLC Questions And Answers  3  December 3rd, 2012 04:52 PM 
Copying integer values  gbradley  LIVE PLC Questions And Answers  6  April 25th, 2008 08:49 PM 
Migrating VB6 IDEA app to VB.Net  Extrasoft  LIVE PLC Questions And Answers  5  November 4th, 2007 06:13 AM 