You are not registered yet. Please click here to register!


 
 
plc storereviewsdownloads
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.

---------->>>>>Get FREE PLC Programming Tips

New Here? Please read this important info!!!


Go Back   PLCS.net - Interactive Q & A > PLCS.net - Interactive Q & A > LIVE PLC Questions And Answers

PLC training tools sale

Reply
 
Thread Tools Display Modes
Old April 21st, 2017, 02:54 AM   #1
somlioy
Member
Norway

somlioy is offline
 
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 (0-99) 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...
  Reply With Quote
Old April 21st, 2017, 04:55 AM   #2
Manglemender
Member
United Kingdom

Manglemender is offline
 
Join Date: Jul 2007
Location: Lancashire
Posts: 1,082
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
  Reply With Quote
Old April 21st, 2017, 06:33 AM   #3
somlioy
Member
Norway

somlioy is offline
 
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 z-transformation and discrete mathematics.

It's an Mitsubishi FX1N PLC, so 32bit.

Here's the D-part of the PD-regulator:


Where Kpff, Nff, Tdff and Ts all are regulator parameters. v(k) and v(k-1) is the measured disturbance (now and previous) and u(k) and u(k-1) 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.
  Reply With Quote
Old April 21st, 2017, 07:09 AM   #4
JesperMP
Lifetime Supporting Member + Moderator
Denmark

JesperMP is offline
 
JesperMP's Avatar
 
Join Date: Feb 2003
Location: Copenhagen.
Posts: 13,054
With 32-bit 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 what-if 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.
  Reply With Quote
Old April 21st, 2017, 07:44 AM   #5
AustralIan
Member
United Kingdom

AustralIan is offline
 
Join Date: Jan 2013
Location: UK
Posts: 485
Use fixed-point 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
a-b                     a-b
a/b                     a*100/b
a*b                     a*b/100
  Reply With Quote
Old April 21st, 2017, 09:07 AM   #6
boneless
Lifetime Supporting Member + Moderator
United States

boneless is offline
 
Join Date: Feb 2008
Location: OKC
Posts: 1,275
I am pretty certain the FX1N is capable of doing the arithmetics you are wanting?

Are you certain you are doing conversion correctly?
  Reply With Quote
Old April 21st, 2017, 09:45 AM   #7
tragically1969
Member
United Kingdom

tragically1969 is offline
 
Join Date: Jun 2007
Location: uk
Posts: 1,180
Quote:
Originally Posted by somlioy View Post
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 (0-99) 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...
From memory non floating division in the Fx series results in the whole number in register n and the remainder in register n+1 so you need to make sure your destination is 2 registers so for instance 99/12 = 8.25 so reg n would be 8 and register n+1 25

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 5-25:

http://www.autocontcontrol.cz/Files/...nual_Basic.pdf

Last edited by tragically1969; April 21st, 2017 at 09:52 AM.
  Reply With Quote
Old April 21st, 2017, 10:57 AM   #8
doomsword
Member
Croatia

doomsword is offline
 
Join Date: Aug 2016
Location: Zagreb
Posts: 81
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.
  Reply With Quote
Old April 21st, 2017, 11:44 AM   #9
Peter Nachtwey
Member
United States

Peter Nachtwey is offline
 
Peter Nachtwey's Avatar
 
Join Date: Apr 2002
Location: Vancouver, WA, US
Posts: 6,540
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
  Reply With Quote
Old April 21st, 2017, 11:53 AM   #10
NetNathan
Lifetime Supporting Member
United States

NetNathan is offline
 
Join Date: Nov 2011
Location: Corona, CA
Posts: 1,213
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 0-2500.0 degrees real. Temp x 10 and then convert to real it is now 0-25000. 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.
  Reply With Quote
Old April 21st, 2017, 01:54 PM   #11
somlioy
Member
Norway

somlioy is offline
 
Join Date: Jan 2011
Location: Localhost
Posts: 22
Thanks for answers everyone.

As I said, formula provided was only the D-part of the feedforward regulator.
Heres complete formulas:

Upi is the PI-regulator. Sum of Upff + udff makes the PD-regulator.
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 PI-regulator without handling the remainder you'll end up with stationary error.
  Reply With Quote
Old April 21st, 2017, 04:23 PM   #12
Peter Nachtwey
Member
United States

Peter Nachtwey is offline
 
Peter Nachtwey's Avatar
 
Join Date: Apr 2002
Location: Vancouver, WA, US
Posts: 6,540
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
  Reply With Quote
Old April 22nd, 2017, 11:43 AM   #13
thingstodo
Member
Canada

thingstodo is offline
 
Join Date: Feb 2012
Location: Saskatoon
Posts: 60
Quote:
Originally Posted by somlioy View Post
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 (0-99) 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...
Old school solution for dealing with 'rounding' in integer math:

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.
  Reply With Quote
Old April 22nd, 2017, 05:18 PM   #14
Sergei Troizky
Member
Canada

Sergei Troizky is offline
 
Join Date: Oct 2004
Location: Montreal
Posts: 679
Quote:
Originally Posted by thingstodo View Post
Old school solution for dealing with 'rounding' in integer math:
Add 1/2 of your divisor (50) to your integer before doing your divide.
This will work correctly only for the dividend and divisor of the same sign.
The complete rule is: increase the dividend absolute value by half of the divisor absolute value, before doing divide.
  Reply With Quote
Old April 22nd, 2017, 07:06 PM   #15
thingstodo
Member
Canada

thingstodo is offline
 
Join Date: Feb 2012
Location: Saskatoon
Posts: 60
Quote:
Originally Posted by Sergei Troizky View Post
This will work correctly only for the dividend and divisor of the same sign.
The complete rule is: increase the dividend absolute value by half of the divisor absolute value, before doing divide.
Yes, of course!

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!
  Reply With Quote
Reply
Jump to Live PLC Question and Answer Forum

Bookmarks


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

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

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


All times are GMT -5. The time now is 02:57 AM.


.