YAMFPP (Yet Another My First PLC Program)

Final version

Uniform- and Gaussian-distribution Random Number Generators (RNGs) for AB PLCs are working and available here.

Both CompactLogix/ControlLogix and MicroLogix 1100 versions are provided: the former has transferrable AOIs for the RNGs; the latter is a monolithic proof of concept with no practical application but could be dissasembled for parts if needed.

Details

  • The MicroLogix 1100 Gaussian RNG uses ladder to implement instructions that are unavailable on MicroLogix 1100 hardware:
    • LN (natural logarithm);
    • SIN (sine);
    • COS (cosine).
  • There were bits lost when using 16-bit INTEGERs to implement a 15-bit Linear Congruential Generator (LCG) with a period of 32k samples, so it now uses 32-bit LONGs.
    • The LCG still uses the 15-bit parameters, but they could be replaced to make it a 31-bit MicroLogix LCG with a period of 2G samples
  • The ControlLogix/CompactLogix RNG use 31-bit LCGs
    • The Gaussian RNG will be around 27 or 28 bits, with a period of 125-250M samples, due to the design of 32-bit floating point representation.
  • The Anderson-Darling test (Python/numpy) has verified that the null hypothesis of normality for the first 1024 random numbers cannot be rejected (I hope I phrased that correctly).
  • To recap, this was an exercise for me to get comfortable programming PLCs.
    • Many thanks to plctalk members for their help; most of it was indirect, but mining the wealth of information on this site was invaluable.
 
drbitboy, any chance you can put limits to the number range, then it would passable be easier to see if it is totally random. say limit from 0 -100, but primarily then l would have a use for it to do random numbers between 0-9 and 0-1000 (then l can use it like the TV show here "Letters and Numbers). As you would have found, most random generators aren't random, if you stop them at exactly the same time you will get the same answer.
I think people haven't responded much because your project is well outside a first PLC program, hence why you got the answer about it being really your first attempt, also way past me with all the names of algorithms, obviously as you have stated you are into more higher level programming methods. Your brother better watch out, you could easily take his job.
I currently don't have a SLC CPU and my 5000 v15 doesn't do AOI so will have to pull it to bits at some time and put it into a routine, so l can your program in action.
Thanks
 
Adding limits

drbitboy, any chance you can put limits to the number range [...] Thanks

(Hey, it's on Github: fork the repo, fix it th'self, and issue a PR;-)

For the AOI I don't have anything that can edit it; maybe my brother can fix it, or I can fiddle with the XML in the .L5X file. I thought we included Out_lo and Out_hi input parameters on the AOI for scaling ... yes, I just checked the XML and we did that for the uniform LCG AOI, but nothing for the Gaussian AOI.

In the MicroLogix 1100 example the limits are (0:1] for the uniform LCG and the gaussian data have a mean of ~zero with a variance (and standard deviation) of ~unity; neither is a self-contained object like an AOI that would lend itself to adding limits or scaling. One should be able to use the SCL or SCP instructions to scale the outputs. Hmm, it might be useful to turn this into a subroutine to make it AOI-like.

That said, I would not add limit parameters for the Gaussian RNG; I think it makes more sense to specify the mean and the standard deviation as a one-sigma value (or three-sigma e.g. for modelling noise that should stay mostly within a deadband in a control application); the SCL instruction does that in one step. There is no theoretical limit to a gaussian value, but in practice for a 31-bit LCG I think the algorithm is limited to around six or seven sigma.


P.S. Don't be intimidated by the algorithms: I'd be glad to lead anyone through them; and nothing in there goes beyond high-school math plus The Google plus a feel for how numbers behave*; 90% of engineering is multiplying by one in some form or another.


P.P.S. The MicroLogix example doesn't need any hardware; it can be run PC-standalone with the RSLogix Emulator500.


P.P.P.S. Thank you for your kind words. It really is my first PLC program**, and I am still interested in anybody's critique; maybe no one is interested in yet another RNG, but style is something I probably need to learn. E.g. my rungs tend to be very long vertically (and I had my reasons***), but I find that rungs taller than the one-screen window make reading/editing/understanding awkward at best, so maybe someone more experienced has a better approach.

* Sure, I spent my life doing this kind of thing and it comes naturally, but anyone who was taught and understood long division in elementary should have the chops to understand the LCG algorithm.

** Although my brother has been regaling me with tales of his exploits for years, which often require some pedagogy to follow - you wouldn't believe the number of times he's drawn a self-sealing on switch for me.

*** Out of habit I group things by function and I didn't want to have to put my ODDEVEN bit on every rung
 
Last edited:
Yes I understand the need to be cautious, however. you have used a N/C B3:0/0 to effectively reset B3:0/0 but if N/C (or in AB money XIC) then it is already reset or 0.
I have simulated your code in another plc as I do not have the licenses installed for RSL and in my case did not see the need, I'm not criticising your code just showing a way of tidying it up. I like the way you are thinking though I too was a programmer in C, Pascal, basic & assembler some years ago.
I used a simple RNG in a simulation of PID loops, however, it does not suit that well as PID is not random so I added it to the output then with a little maths added it to the process variable this gave a bit of randomness and proved the PID process well enough for simulation. Anyway, If like me I got the PLC programming bug and found it quite stimulating so much that I continued and spent most of the rest of my career in PLC's, still enjoy the other side though and thankfully my job allowed me to do both. Good luck :site:
 
Turn your monitor sideways physically, then also in your display settings.
Get a bigger monitor.
Zoom out.
Use Structured Text.



I haven't looked at your code.

Tried one of those already (Zoom out) but the eyes don't have it (;-)

Hey look, I have one of those rotating monitors!

Structured text? hmm, very interesting.

Thanks!
 
Yes I understand the need to be cautious, however. you have used a N/C B3:0/0 to effectively reset B3:0/0 but if N/C (or in AB money XIC) then it is already reset or 0.[...] :site:

Ah, right, I forgot I had that in an earlier version. I dunno what I was thinking in those early days, but now I can clearly see that was a faulty thought process. Thanks!

When my brother saw it he told me about the [First Pass S:1/15] bit, that did what I was naively trying to do there. Also, that bit has a different designation in ControlLogix, so there is a lot of in-house, hardware-specific knowledge to learn. I found the overflow bits on my own, but for a while I thought the [overflow trap/minor fault] getting promoted to a major fault at the end of the scan was the end of the project!
 
Last edited:

Similar Topics

Any news on Studio 5000 Designer V34 yet? I assume it is going to be released late summer early fall. Has anyone heard anything about new...
Replies
107
Views
56,880
So, after adding a second PC, purchasing another server license and setting up to function as a redundant server for FTV SE, I think I've...
Replies
2
Views
2,176
Starting Oct. 1, 2020 UL508A panels shops have to have at least one person on staff that has passed this exam. I plan on doing it within a few...
Replies
70
Views
75,789
Opto is hyping these like crazy but I'm wondering what the experiences are of someone whose actually installed and commissioned one in the real...
Replies
3
Views
1,836
What software are you using or would recommend for drawing interlock, control and sequence diagrams? A full blown CAD feels over the top to me...
Replies
10
Views
3,232
Back
Top Bottom