Count down subtract continue

dahnuguy

Member
Join Date
Mar 2007
Location
SC
Posts
720
I am using Step7 with a 313C.

I am in testing phase.

I need to read an oil level and calculate the drop in oil level per hour and display a running average.

I believe I have a block written that will do this, but I would not mind ideas on this.

The next and biggest issue is the testing.

I am using the simulator and I have a real 313C.

I have tried to write a small block to simulate an oil level dropping .

What I did:

I use the analog in and move it to a memory location every X seconds/minutes. Then set a bit and use that to allow the next cycle to write the new lower level data to a different memory location.

I do this several times, each pass allows the next to write into a new location.

After 4 samples spaced X minutes apart, I subtract each from the next and do an average.

This gives me the average consumption of oil over X period of time.

I think I did this OK.


Now the part that seems so easy but is not working due to scan time and my inexperience with Step7.

I want MD2002 to decriment X every Y seconds to simulate the oil level dropping in a tank over time.

Simple right?

I think so.

Then I would monitor this changing MD2002 with my other code and manipulate it around to see how it responds.

I started by just moving the value with the simulator. BUt I can't move the slide or enter the numbers in a steady timed fashion.

Plus I just think should be easy.

Maybe I am having a slow brain day / week.............

Any Ideas?

The worst part is not being able to subtract a NumberX from NumberY and put the result back in NumberX. This is down in some languages. If I could get that to work every X seconds then it would work.

Example: Every 10 seconds subtract 25 from MD2002 and put the answer in MD2002. If MD2002 is less than 100 move 27648 into Md2002. Repeat.

So the result would be a steady incrimental step down from 27648 to 100. Then reset MD2002 to 27648 and repeat.
 
Here's the screen shot of an FB that does as requested:

timedec.JPG
 
I have no idea.

This looks very much 20 of the first 50 things I tried.

Did you run this on the simulator? or a PLC?

If I run something like this on the simulator, the subtraction happens many times while the pulse is true. Not just once.

If I use a P in front of the timer to get one pulse then nothing happens.

Maybe it is an issue with the simulator is why I ask where you ran it.

The other issue I had with this , is I can't use SFB4.

In the help file I find an explaination on what is expected at "in" , "pt","q" and "et" , but when I try to use SFB4 the block has ??? above it. Nothing I put there satisfies the block.

I tried the same way as yours with "sfbtimer" , it stays red. I tried "T114" red.

I tried"#sfbtimer" still red. "complier information:type conflict"

So I just used a regular timer..............

Now , why can't I use SFB4 the way you did?

And what could have possibly kept this from working for the last 3 hours????

I suspect I am having difficulty with the save and DB updates / calls from OB1.

This last time , I saved all and exited the editor and deleted the DB associated with the FB and then made a new DB. Then saved and wiped the sim memory clear and downloaded.

Once you figure out the correct sequence of events , it's a simple 7 or 8 steps to make an edit.

The attached image is of the working block, which is just like the block I tried 3 hours ago.
 
The code was run in the simulator, I'm using
PLCSIM V5.3 + SP1 + HF1

There is a problem with the Simulator as reported here, sounds like you need the hotfix:
http://www.plctalk.net/qanda/showthread.php?t=37477&highlight=hotfix

Here's the source code for the FB. Did you enter the timer declaration in the STAT interface area of the FB as shown in my screen shot ?

If you entered SFB4 directly, the three questions marks are prompting you for an instance DB (e.g. DB444 if it is not used elsewhere)

Code:
FUNCTION_BLOCK FB 1
TITLE =
VERSION : 0.1

VAR
  sfbtimer : SFB 4; 
  bTimerQ : BOOL ; 
END_VAR
BEGIN
NETWORK
TITLE =
	  AN	#bTimerQ; 
	  =	 L	  0.0; 
	  BLD   103; 
	  CALL #sfbtimer (
		   IN					   := L	  0.0,
		   PT					   := T#10S,
		   Q						:= #bTimerQ);
	  NOP   0; 
NETWORK
TITLE =
	  A	 #sfbtimer.Q; 
	  =	 L	  0.0; 
	  A	 L	  0.0; 
	  JNB   _001; 
	  L	 MD  2002; 
	  L	 L#10; 
	  -D	; 
	  T	 MD  2002; 
_001: NOP   0; 
	  A	 L	  0.0; 
	  A(	; 
	  L	 MD  2002; 
	  L	 L#100; 
	  <=D   ; 
	  )	 ; 
	  JNB   _002; 
	  L	 L#27648; 
	  T	 MD  2002; 
_002: NOP   0; 
END_FUNCTION_BLOCK
 
Last edited:
dahnuguy said:
This looks very much 20 of the first 50 things I tried.

Did you run this on the simulator? or a PLC?

If I run something like this on the simulator, the subtraction happens many times while the pulse is true. Not just once.

If I use a P in front of the timer to get one pulse then nothing happens.

Maybe it is an issue with the simulator is why I ask where you ran it.

The other issue I had with this , is I can't use SFB4.

In the help file I find an explaination on what is expected at "in" , "pt","q" and "et" , but when I try to use SFB4 the block has ??? above it. Nothing I put there satisfies the block.

I tried the same way as yours with "sfbtimer" , it stays red. I tried "T114" red.

I tried"#sfbtimer" still red. "complier information:type conflict"

So I just used a regular timer..............

Now , why can't I use SFB4 the way you did?

And what could have possibly kept this from working for the last 3 hours????

I suspect I am having difficulty with the save and DB updates / calls from OB1.

This last time , I saved all and exited the editor and deleted the DB associated with the FB and then made a new DB. Then saved and wiped the sim memory clear and downloaded.

Once you figure out the correct sequence of events , it's a simple 7 or 8 steps to make an edit.

The attached image is of the working block, which is just like the block I tried 3 hours ago.
As for the SFB4, if ??? appears on top of a block call, it means you must define an instance DB. For example, type in DB4 and the system will ask you if it shall generate this DB (if it does not exist.)
 
L D[AR2 said:
Here's the screen shot of an FB that does as requested:

timedec.JPG

notice in your screen shot , there is no DB over the SFB4?

This is very missleading.

It looks like something is missing. Not sure how it could work as you posted it.

and I have a VERY difficult time entering the SFB4 in the variable declaration table. I enter SFB<nr> in "type"

then change <nr> to "4"

The SFB4 turns to "TON" but stays red. So I delete it and rename it a couple of times and then it works!?????!!!!

This is the sorts of issues I just don't need. I have come close to breaking something several times today.

Oh and the hotfix doesn't work for mine, I am using Sim V5.4.1.0 so it's not a HF issue.

But this might explain why we see slightly different images and references.
 
Now that is exactly as simple as I think this SHOULD be!!!!!!!! thanks,

I tried several times something like this, but to no avail.

I will try it again.
 
clemens said:
There is a simple solution that works fine on PLCSIM V5.3 without SP1


WORKS, and is all nice and compact with no variable declaration issues or any issues at all.

I like simple.

And I learned something I was looking for all along, the timer "bits".

Verah Nizzz
 
Caution - the MD addresses in the code won't work in a real 313 - Doesn't a 313 only have 256 byte addesses?
 
This works, but it isn't very pretty. Any ideas?

Mike_RH said:
Caution - the MD addresses in the code won't work in a real 313 - Doesn't a 313 only have 256 byte addesses?

Not sure , but it is a good point.

I have been exploring in SIM land tryin to get anything to work.

After I figure out what will work reliably, I will be better able to put it together in the PLC real world.

The only way I can get the original issue to work is by using set bits.

We have been exploring the decrimenting step generator here for a while.

Lots of good information. But that was just to be able to test the real code.

The actual problem was to read the oil level at some interval ( I use 10sec just to get some data, the machine will use 1 hour)

Then calculate the drop and thus oil usage since the last reading.

The way I did it is with brute force. Not pretty or lite. But it is chuggin along and seems unbreakable.

Function FC1030 is just a little FC that gives the difference of 2 int. regardless of which is larger.

This gives a running useage. I still need to keep the first reading and do a compare with each new "difference" to the first reading to get an alarm if usage increases from where it was originaly.

I post FC1030 separate for those who might be interested. It is included in the FB3.

***********************************
FUNCTION FC 1030 : VOID
TITLE =GIVES THE DIFFERENCE OF 2 INT.
VERSION : 0.1


VAR_INPUT
IN_01 : INT ;
IN_02 : INT ;
END_VAR
VAR_OUTPUT
DIFF_01 : INT ;
END_VAR
BEGIN
NETWORK
TITLE =

A( ;
L #IN_01;
L #IN_02;
>=I ;
) ;
JNB _001;
L #IN_01;
L #IN_02;
-I ;
T #DIFF_01;
_001: NOP 0;
NETWORK
TITLE =

A( ;
L #IN_02;
L #IN_01;
>=I ;
) ;
JNB _002;
L #IN_02;
L #IN_01;
-I ;
T #DIFF_01;
_002: NOP 0;
END_FUNCTION



******************************************************
FUNCTION FC 1030 : VOID
TITLE =GIVES THE DIFFERENCE OF 2 INT.
VERSION : 0.1


VAR_INPUT
IN_01 : INT ;
IN_02 : INT ;
END_VAR
VAR_OUTPUT
DIFF_01 : INT ;
END_VAR
BEGIN
NETWORK
TITLE =

A( ;
L #IN_01;
L #IN_02;
>=I ;
) ;
JNB _001;
L #IN_01;
L #IN_02;
-I ;
T #DIFF_01;
_001: NOP 0;
NETWORK
TITLE =

A( ;
L #IN_02;
L #IN_01;
>=I ;
) ;
JNB _002;
L #IN_02;
L #IN_01;
-I ;
T #DIFF_01;
_002: NOP 0;
END_FUNCTION

FUNCTION_BLOCK FB 3
TITLE =OIL CONSUMPTION
VERSION : 0.1

BEGIN
NETWORK
TITLE =

AN M 902.1;
AN M 110.0;
CU C 118;
BLD 101;
NOP 0;
NOP 0;
A( ;
L MW 9018;
L 60;
>=I ;
) ;
R C 118;
L C 118;
T MW 9018;
NOP 0;
A C 118;
= M 400.0;
NETWORK
TITLE =

AN M 400.0;
AN M 110.0;
JNB _001;
L MW 7002;
T MW 7100;
SET ;
SAVE ;
CLR ;
_001: A BR;
S M 110.0;
R M 110.1;
NETWORK
TITLE =

AN M 902.1;
A M 110.0;
CU C 120;
BLD 101;
NOP 0;
NOP 0;
A( ;
L MW 9020;
L 60;
>=I ;
) ;
R C 120;
L C 120;
T MW 9020;
NOP 0;
A C 120;
= M 400.1;
NETWORK
TITLE =

AN M 400.1;
A M 110.0;
AN M 110.1;
JNB _002;
L MW 7002;
T MW 7102;
SET ;
SAVE ;
CLR ;
_002: A BR;
S M 110.1;
R M 110.0;
NETWORK
TITLE =

CALL FC 1030 (
IN_01 := MW 7100,
IN_02 := MW 7102,
DIFF_01 := MW 7104);
NOP 0;
END_FUNCTION_BLOCK
 
L D[AR2 said:
Why have you used a function block (FB) as opposed to a function (FC) ?


As I understand it, FBs will allow data in memory locations to be accessed in other parts of the program.

I like functions for most things as they are portable and do not require defined memory locations for everything. This bothered me at first, but now I see the advantage. I am still curious where the CPU puts things but I guess it doesn't matter. PLCs have become more like the PC in this regard. No one is ever concerned about exactly where Windows puts a variable in memory. I am still anchored in the past on that one. It is easier for me to relate to "M300.5" than to "Eastend Stripper Pump Run". I often misspell words, but rarely miss a number.

Anyway, I felt the FB fit the issue.

I am using OB1 as the "Main" and then using FBs from there. Each FB is dedicated to a machine function etc. I use Functions inside the FBs and in OB1.

I have other OBs for start up and errors etc.

I could have easily wrote this all in OB1 in "regular" ladder with no special tricks. My primary goal is to get the machine going, but my secondary goal is to learn as much about Step7 as possible.

So short answer: Used FBs because I need to reference some of the data in other locations and I do not see much difference between FBs and FCs except for the memory issue and some variable declaration differences.

The structure I use is the best solution for the problem at hand based on my current skill set.


Of course I am still learning and will probably re-write the whole program several times before I am done as I have already done with every FB so far.
 

Similar Topics

I would like to have my Micro820 send a modbus message every first of the month just to show the system is up, alive and running properly. I don't...
Replies
6
Views
1,996
Hi All, Can this be done ? I looking at writing code for "stand alone" Micrologix500 (No other control purposes intended) - Trying to track...
Replies
3
Views
2,464
I am trying to set up a timer to count down from an input time in minutes to zero. Looking through the CCW software there is a function block to...
Replies
5
Views
2,603
hi all, i have 16 lines here and almost all are identical. each has a steel seam belt to transport product on. each belt stretches and must be...
Replies
8
Views
2,354
I am more used to RSLogix 500 programming, and while I have been doing it a little while I know I still have a lot to learn. The test engineers...
Replies
4
Views
2,198
Back
Top Bottom