Sign in to follow this  
Followers 0
nissinbrake

Subroutine

12 posts in this topic

Hi all, I'm a little new to AB and have only used RSLogix a handful of times before. I'm looking to create a subroutine/subprocedure/subfunction in a program without creating a new file. I'm not sure what I want to do is possible so let me explain it a little. Here's what I have: *your basic green start pushbuttons (RH & LH) and a N.C. reset button *N.C. light curtain *3 solenoids with pneumatic cylinders attached to each *6 cylinder sensor to detect whether cylinder is stroked or not What I want: *When both LH & RH pusbuttons are pushed and light curtain and reset remain closed, I want to call a subprocedure *The subprocedure will then energize 3 solenoids which will used to clamp the part as long as the light curtain remains closed or the cylinders are fully advanced *The part will remain clamped until an internal bit (representing part testing finished) is energized to return My Questions: 1. when my contacts for LH and RH are energized, can I have the coil be the SBR? 2. Can I create a SBR (or an equilvalent method) in my main ladder logic because I want to avoid creating multiple files? 3. When the SBR returns, will the outputs turn off? Thanks in advanced (btw I'm a student in a PLC class and this is for an open ended project I designed and the professor didn't cover subroutines very well)

Share this post


Link to post
Share on other sites
if I understand your question (not 100% sure about that) then the answer is “yes, it can be done” ... investigate and experiment with the JMP (Jump) and LBL (Label) instructions ... specifically, don’t try to use the JSR (Jump to Subroutine), SBR (Subroutine), or RET (Return) instructions ... those are for jumping from one ladder file to another ladder file ... on the other hand, the JMP is only for making a “local” jump (from place to place inside the same ladder file) ... now the question I have is this: why don’t you want to use the “separate ladder file” approach that most programmers use for their subroutines? ... if you can’t get this to work correctly, then I suggest that you go as far as you can get, and then post your .RSS program file ... we’ll try to help you over the rough spots ... specific question and answer department: good ... now we don’t have to worry so much about “giving” you too much detailed help ... we don’t like to do that here ... it sort of defeats the instructor’s intentions sometimes ... hope this helps ... Edited by Ron Beaufort

Share this post


Link to post
Share on other sites
Why does this have to be a subroutine? Why not just add this logic to your main ladder file, or another unconditional ladder file? The jumps and labels can be a nightmare to trouble shoot. Unless you document your logic to the nth degree, you will not understand your own logic a few months down the road.

Share this post


Link to post
Share on other sites
Thanks for pointing this out. Besides from being an Electrical Engineering student, I'm also a Computer Science student. It's the high level language programmer in me that wants to make several different "functions" or labels in this case to break the program into parts. Having lots of experience with assembly language programming, I think I can handle troubleshooting the jmp commands. Seems fairly similar from what I can tell. Another question I've came up with: After the label becomes energized and I go onto the next rung, I want to ensure that the label is energized as a contact. Can I do that?

Share this post


Link to post
Share on other sites
I think the PLC will get a little upset about multiple labels with the same reference value (2 in this case). I don't know if this is just a typo, but if it isn't you need to revisit this. Also keep in mind that a label is not a point of exclusion. It is just a location the program will go to and begin executing normally from there. If you have multiple labels in succession and you jump to the top label, the code following the other labels is also going to execute. The only time the code will execute only one section is if the code jumps to the last label in the list. The AB PLCs don't have a program method that exactly matches up with a 'function' from something like C. A jump to subroutine is probably as close as you could get but it isn't exactly the same. Also keep in mind that if you jump over any rungs they are not executed. That means outputs will stay in their last state unless the code specifically sets them to another state elsewhere. Be extremely careful with this as it can cause some serious heartburn. My general feeling is that this isn't a real good application for jumps and labels. Not that it can't be done. It just won't be real clean. A subroutine is a possibility but would take a little babysitting also. You are probably better off just doing the code inline and conditioning it as necessary. Keith

Share this post


Link to post
Share on other sites
You will need only one 'Lbl' on the second rung, but one rung at the end of you logic with a "RET" (return) instruction for the processor to return the scan to whatever location the jump was initiated.

Share this post


Link to post
Share on other sites
Using subroutines to break up your program into function-specific sections is absolutely good fundamentals of PLC programming. You can have 10 subroutines of 2 rungs each and as long as they have a distinct functional relationship, there's nothing wrong with it. Being "too simple" is not the issue. In fact, using subroutines simplifies the understanding of the logic, it doesn't implement more complexity. Ladder Logic is not like high level programming....its designed to be simple and modular, and should be programmed as such. Functions and labels are not ideal from breaking up your program like they would be in C++, etc. Use subroutines instead. No program is too simple for a good subroutine or two 90% of my programs have one main routine which simply calls 10-20 subroutines which have anywhere from 5 to 15 rungs each. I rarely write a routine with more than 15 rungs (unless i'm doing some mundane i/o buffering or something) Good Luck $ Edited by Money4Nothing

Share this post


Link to post
Share on other sites
I think you are misunderstanding the label concept. The JMP is the same as a GOTO instruction. The LBL is just a location to land for a GOTO. This is just like assembler or any high level language. There can only be one program location with the same label.

Share this post


Link to post
Share on other sites
here's the scoop ... you asked "could it be done?" ... the answers have all pretty much been along the lines of "yes, it COULD be done - but you do NOT want to do it" ... I've attached a simple "let's-learn-about-JMPs-and-LBLs" program below ... if you've got a spare processor to experiment/play with, it would be a good idea to enter this program (or something like it) and just piddle around with the way this thing works ... the "scan counter" rungs are a very educational way to explore something like this ... note: if you don't understand "math overflow" faults, then ASK before you try this on a "working-money-making" system ... hint: there are limits as to how long you can let those values keep incrementing ... even with just this "simple-sample" program, I think that you'll start to realize that the "jumping-to-labels" programming approach is NOT going to make your program easier to deal with than a traditional JSR approach which jumps to separate subroutine files ... instead the JMP/LBL approach is probably going to make things very difficult ... I hope this helps ... TRY_JMP.pdf

Share this post


Link to post
Share on other sites
It appears to me that what you are looking for is an MCR instruction. The MCR is a Master Control Rung. The MCR is always used in pairs. The first one in the pair is conditional. The last one is unconditional. When the first is true, then all of the rungs in between the pairs will execute. When the first rung is false, then all of the outputs between the MCR pairs are turned off. You can find more informationa about this instruction, as well as all other SLC instructions in the SLC 500 instruction set reference that you can dowload from the literature library on ABs website. http://literature.rockwellautomation.com/i...rm001_-en-p.pdf Edited by Alaric

Share this post


Link to post
Share on other sites
I don't know much about or what is considered higher level programming but in the computer code I have wrote the layout is very similar to the JSRs to separate routines and sometimes even jumping to code in separate files. I have seen JUMP to LABEL methods used in older programs (I thought because they didn't have true subroutines). I know in ladder it makes it very hard to follow and for me it would be very hard to follow when writing computer code using the JUMP LABEL method. But I'm by no means a higher level computer programmer. A couple of things I don't know if has been covered here. You do know you can reuse the subroutines and they will virtually function the same as a JUMP LABEL? You are aware that excessively using JUMP LABELS can cause Watchdogs?

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0