Good programming practice

geniusintraining said:
agreed but with reservation, buy documentation and letting the other person see what you are 'thinking' you can limit the confusion...

that was a bad example, that was just bad programming practices, hard to understand for me?, would be

This is well documented but the 'AND' bitwise etc, ladder 31 rung??31ish thats where it gets hard for me...Ron it may be easy for you but to me...its anything but simple and I'm not stupid.


Data manipulation.

31 = mask - only keep left hand 4 bits (xxxxxxxxxxxx1111).

32 = mask - only keep next 4 bits (xxxxxxxx1111xxxx). By the divide by 16, I presume AB does not have the ability to shift right or left.

I must get another AB job, its been at least 4 years now and that was a small job. Any jobs going???
 
PeterW said:
Data manipulation.

I presume AB does not have the ability to shift right or left.

I must get another AB job, its been at least 4 years now and that was a small job. Any jobs going???

AB has direct instruction to shift right or left that is BSL & BSR
to do Bit shifting right or Left
 
Peter you are correct, one of the issues that we had with that program was that with a BSL and BSR you can only shift 1 to the right or 1 to the left not 4 right or 4 left...

also we needed 64bit float or unsigned integer (I think)this was a 504

What this program did it took a value from a touch screen (panel view) input and sent it as a output using a ASCII output to a SEW motion controller changing/incrementing a variable (distance traveled)

I help with it but alot of it is hard for me to grasp, this was my friends first AB project, he's a Siemens boy, but I was impressed with the out come and the way he got around the barriers.

Could he made it simple? I don't know, but I still think that you need to put forth the effort
 
Sometimes things are just difficult because of the application or lack of tools.

Sometimes things can't be as simple as you wish because the PLCs have a CISC instruction set. Crippled Instructions Set Computer. PLCs lack instructions such as ADD with carry or ADDC instructions that would allow one to use two N registers as a 32 bit register. A SUBC instruction would be good too. Comparing 32 bit values is more difficult yet.

Most PLC do not have a bitwise toggle or exclusive or that would make the 1 button toggle a breeze.

What about ENCODE and DECODE instructions? I find these handy. Some how they have disappeared from many PLCs.

What about circular indexing. DSP have it. This is where you can add 1 to and index variable and if the index variable is incremented past the end of the array it is set back to 0.

Rockwell should be EMBARRASSED about the COP command. The idea that you can't easily copy from 10 register starting from N7:0 to N7:1 is a crime.

PLC lack many string commands that make HMI programming easier, but I don't think PLC should be formatting data. That is the job of the HMI.
PLCs often don't have character or 8 bit instructions. I know the DSP we use doesn't know the different between a DINT, INT or CHAR. They are all 32 bits.

I can go on and on. Sometimes the tools you use are not as good as they could be. Sometime the applications are difficult applications and there is no easy way to get the job done because the PLC doesn't provide the magic instruction you need.

GIT, why would you need 64 bit floating point? That provides more resolution than what one could physically measure. Incrementing a position does not seem that hard.

Did you know you can shift a value left by adding it to itself? Adding a value to itself 4 times will shift it left 4 bits.

So what is a programmer to do when confronted with a difficult task and there is no 'easy button' to get the job done? You get the job done the best way you know how and document it. You can be sure that if the next guy can not understand the application he will be lost but is it your fault for getting the job done under difficult circumstances or the fault of the new guy would not have a clue about how to overcome the same set of difficult circumstances?

Sometime PLC programming, or any programming, gets hard and there is no way to make it easy.

Here is an extreme example. What if I needed to know the speed of a conveyor VERY accurately and a simple low pass filter wouldn't do. What if I used a Kalman filter and that satisfied the requirements. Would I be at fault for not making the code easy for the maintenance guy to follow what I have done? Now think of this? PLCs don't have matrix array math. What looks like 4 lines of code in C or a text book would take pages in a PLC.
 
Peter Nachtwey said:
GIT, why would you need 64 bit floating point? That provides more resolution than what one could physically measure. Incrementing a position does not seem that hard...

I think it went something like this, we needed a 9 digit real number in the controller, so to generate a 9 digit hex-decimal we would need 36 bits, so all of the math functions could of/would of been done in a 64bit format

Peter Nachtwey said:
Did you know you can shift a value left by adding it to itself? Adding a value to itself 4 times will shift it left 4 bits....

Yes but that would of even made the program that much longer, because this would of had to be done numerous times.

Peter Nachtwey said:
Sometime PLC programming, or any programming, gets hard and there is no way to make it easy.....

Those are hard real facts, this I agree with all of you, but I still think that you need to 'try' and make it user friendly, in the beginning I felt that Terry was not stating that and the KISS concept is a "good programming practice" that is what this thread was about.

This was a good heated conversation
 
First off, I never considered it heated

This has been very educational for me.

Those are hard real facts, this I agree with all of you, but I still think that you need to 'try' and make it user friendly, in the beginning I felt that Terry was not stating that and the KISS concept is a "good programming practice" that is what this thread was about.

This was a good heated conversation

The fact is there is no such thing as simple; therefore KISS can not be considered a form of programming, let alone good programming practice. Why would anyone limit there programming capabilities based on those not qualified to program?

Again I will state that in the last 4 yrs I have debated this many times, now I will offer my original thoughts
1. Maintenance personnel work hard to be good at what they do, they should not be expected to learn code.
2. Basically only ladder should be used because it was similar to relay logic so it may be easier to understand.

I was WRONG. IF someone needs to work with the code then they should be familiar with all instructions etc. If you do something YOU KNOW BUBBA can not understand then you have to provide a means for understanding.

In another thread rdrast stated he hated someone equating PLC programming to hardwired relay logic, though similar, it is not the same thing. I AGREE.

If you are programming to make it be like relay logic or for Bubba then you are not keeping it simple; as Terry states you are keeping it Stupid.

I reiterate, simple is not a term that is relevant because automation nor programming is not simple.

Took me awhile to understand, technically still borderline but I progress a little here and there.

There was one more thing:
Originally Posted by Peter Nachtwey
Did you know you can shift a value left by adding it to itself? Adding a value to itself 4 times will shift it left 4 bits....

Yes but that would of even made the program that much longer, because this would of had to be done numerous times.

You stated a 5/04, if so then why couldn't you have just moved whatever the value was to another register say N:10/0 then used CPT to add it 4 times then MVM for the value? Appears to me that would only be 1 or 2 lines of code and it could calculate values from different registers on each scan. This was just a thought from the comments made etc.

I did not look at the details of the ASCII because ASCII is not my strong suit.

BTW:
but I still think that you need to 'try' and make it user friendly

That statement also applies to what Terry has been saying, if operator friendly then maintenance will have less concerns or needs for working with the code i.e. if maintenance goes to machine and operator can state PExxx is not working all the better.
 
Last edited:
geniusintraining said:
I think it went something like this, we needed a 9 digit real number in the controller.
That would mean the controller is a 64 bit controller. A thirty bit bit controller has at most 7 significant numbers. The mantissa of a 32 bit float can count to 16,000,000 with out losing precision. That isn't enough for 9 digits.

Mulitiply by 16 is the same as shifting left by 4. When I did BCD output I used to add 4 times back in the days of 8085. 4 shifts were still faster when the 8086 came along. A shift left 4 times instruction was faster yet on the 80186. Now I have a DSP with a barrel shifter and it can shift or mulitply in 1 clock cycle. The multiply and add may allow me to shift left the BCD number by 4 and add the next 4 bit digit in one instruciton. That simplifies things a lot. The problem is the C compiler will not generate that code so I would need to use assembly language if I want to multiply and add in one instruction.

What you do and how you do it depends on the tools at hand. Sometime you must work around the limitations.
 
Terry Woods said:
Ron...

All I can say to you Ron is... Damn it! One of these days... I'm gonna have to sit down with you and burn a couple of cases of MGD! (You can partake in the MGD, or not, as you wish... I'll buy in either case!)

By the way... I'll be in Chicago at the end of November.

I missed the last part, I may be able to make it to Chicago then. Let me know some details and I will see what happens. I will take you up on the MGD.
 
I have the responsibilty to make the process as easy as possible for production folks to use... without sacrificing management's requirements!

I have the responsibility to make it as easy as possible for the night-guy to understand the code... without sacrificing either of my two previous responsibilities!

If I faithfully execute those responsibilities... then I do not become the "go to guy" at 2 o'clock in the morning!

This means, ultimately, I, the PROGRAMMER, have to put in the effort to make it so! DUH??? Are you the programmer?

Yes, yes...of course. But this is not what you said in your post. The original poster that brought up the KISS principle was obviously saying to keep it simple WHERE POSSIBLE. In other words, don't try to do something cute and fancy in 2 lines of code where 12 would have worked and been much easier to understand. Especially if you are not pushing the limits of the PLC you are using.
This is a sound principle for MANY (Most?) environments you are programming in. Some processes are obviously not easy and programming will be complex.
If you are the 'owner' of the process and are primarily responsible for it's up keep, the rules would be different for you.
 

Similar Topics

I'm looking for some starter kits but there aren't so many on the market, so I'm thinking about buying individual components for my needs. I just...
Replies
15
Views
2,461
Good Day Friends! I really want to advance in PLC Programming like our guys here in this forum, and I'm Pleading to all friends here to help by...
Replies
5
Views
2,760
hi al.. i am a bigginer to plc programming..ladder logic can anybody pls suggest the best programming practices using in industries..
Replies
21
Views
5,197
Morning folks--I have been programming in RS 5 and 500 for quite a few years. I need to get proficient in navigating in 5000 and I would like to...
Replies
3
Views
1,797
Hi, Following a large project our company recently finished, I would like to ask what is the best method for object oriented programming. We...
Replies
10
Views
12,435
Back
Top Bottom