Sorting in Plc from smallest to largest

Lol, It's usually the other way round, Can't you sleep :sleep:
Fortunately I have done this on a brick storage system, But the other way round & far more complex, the brick stacks were put into pigeon holes in a rack matrix probably about 10 rows, each row had 6 racks split into 8 locations, there was a robot that took the bricks from the brick palletiser, and located them in the store, this contained a multidimentional array as it needed to know which ones were empty, how long they had been air dried & type. So effectively there were two one placing them in one taking them out,rick, it did give me a bit of a headache.
I have assumed that if there were two locations with the same value it takes thefirst one it comes to if it is enabled, but not added a check for that.
 
I agree with DR, not done ST or SCL in S7 but DW's in S7 is byte orientated not words, not sure how Siemens do it perhaps the compiler does it automtically i.e. for I:= 0 to xx or perhaps you need to do it by Step 2.
You could conceivably do it in bytes as long as it is not a large number.
Also, in FBD or Lad the way they use pointers is a bit different, i.e. p#0.0 so it appears it points to byte/bit. Not used S7 in earnest, virtually not at all in the last 6 years so it is a learning curve for me, I prefer the old S5 so simple
L KF 0 // load the pointer value
T FW100 //Store it in a variable
L DW 10 // put the vale in DW10 into acc 1
DO FW100 // Process the pointer
T DW0 // Store the value into DW pointed to in FW100
L FW100 // Get the pointer
L KF1 //
+F // increment it
T FW100 // Store it for next itteration

Here is an implementation of a Step 7 classic (Not TIA) block copy of recipes

S7 Indirect.png
 
Not really, the way I see it, does not want to move data in an array, just find the cell with the lowest number (well I think this is what is required).
This is far shorter than a bubble sort.
 
Are you ok with STL, I believe you are using Step7 Classic, the code I posted was to move recipe data in block form but for looping it is the same principle, just remember for words it's increment it by 2 not 1 i.e. 2.0 (because the pointer is byte/bit). Also remember if using an array of 2 the words are consecutive (I believe)
i.e.
DBW[0,0] is say DBW4,6
DBW[1,0] is DBW8,10
so for every column of the array you need to add 2 like
4, 8, 12, 16
 
Are you ok with STL, I believe you are using Step7 Classic, the code I posted was to move recipe data in block form but for looping it is the same principle, just remember for words it's increment it by 2 not 1 i.e. 2.0 (because the pointer is byte/bit). Also remember if using an array of 2 the words are consecutive (I believe)
i.e.
DBW[0,0] is say DBW4,6
DBW[1,0] is DBW8,10
so for every column of the array you need to add 2 like
4, 8, 12, 16


And for indirect adressing you need also SLD 3 before indirect addressing (which is ame than multiple by 8).

This is because if you don't make SLD 3 command then indirect addressing is pointing to dbw.x bits not to words.
 
Yes forgot to mention that, but it is in the code I posted earlier 🔨
Ah.. But if you increment it by P#x.0 you do not have to.
 
Last edited:
Not once you understand it, The same with S5, there were even some opcodes that were not documented in general, S5 had (compiled if you like to call it but was actually opcodes for an interpreter called MC5) I suspect S7 has the same thing, never looked into it though, It was possible to use Datawords as the opcodes, very dangerous though, it meant by manipulating the hex value in a DW you could have code that changed on the fly. for example (not real MC5 instruction cannot remember them) but for example H4564 could be AND I 0.1
so a DW with a hex value could be processed as an instruction (done if I remember correctly by DO RS instruction) , changing the value on the fly could change it to say AND I0.2, have seen it, bad programming but I did try it & it worked.
I suppose in theory, you could put a block of code in a datablock & by changing the call to a different DB run completly different code.
I also expect that SCL or ST does not produce smaller code as the "Compiled code" needs to follow the same as FDB or Ladder, converts what is essentially Pascal to some form of op codes to be compiled or used, it is just more convenient at reduced typing & let the compiler do the work.
Actually, just googled it & in S7 it does use the binaries called MC7.
 
Here is a quick bit of code that does what I think you need.
When Memory bit M10.0 is set to true, it runs through FC4 & finds the lowest number in the Container words, providing it is above 0 & it is enabled per the DW associated with them i.e. DW12-30 are the containers DW32-50 are the status words, currently the status words re either 0 or 1 (you could add other values for some purpose).
When M10.0 is set, it first places the first container value in a temp word, it then cycles through each container & if the value is > 0 and < than the stored one and it's associated enable is 1 then it writes that over the previous one.
Loops through to the end to check the rest, then resets M10.0 so only processes the code on a oneshot. So providing the container is above 0, the container is enabled and it is the lowest value it is stored in the robot word DW0, have left a few spares, you may need to shift the data about if you have already got some sort of formant, I did not effectively use a 2 dimentional array (although it is sort of), thought it was cleaner & it was quick to code but I'm sure someone will post if they have a better way of doing it, my last experience on S7 was probably about 12 years ago & with that & using my experience of S5 some 22 years ago I cobbled it up, well if you do it like this.....
There is a caveat, if no valid container is availlable then the robot word will stay as is so you will need to deal with this.
Anyway, it should give you some ideas.
 
Is this what you try to code?


DB2 is original "vaults" data values.



DB3 is copy of DB2 data and used for sorting from smallest to biggest values.


DB4 id sorted data values of DB2.
(DBW0 has allways smallest value and DBW18 has biggest if data
is 10 words long).



DB5 has dbw-addresses offset values. (DB5.dbw0 shows smallest value's dbw address)



All is handled on one PLC scan with two loops.

VAT_table.jpg
 
Last edited:
No, The way the OP tried to explain it is that he has a robot (no idea if it looks at another PLC or is controlled by the PLC), however, It appears that the robot is somehow reading DB1.DBW0 (one variable), so the way I see it work is that there are 10 variables (containers) perhaps a poor choice of words but for example 10 variables that hold values or number of parts, at some point the robot requests a new set of parts from the array, the PLC then loops though these & picks the one with the lowest number but not 0, & stores it in DB1.DBW0 for the robot to use, he also mentioned some form of enable i.e. the container in the 10 variables.
So this is how I see it working.
A request for the next lowest set of parts.
a bit is set, runs the code by looping through it checks if the container is not 0, and is enabled, the value in the first container is checked for > 0 & enabled then put into a temp, next loop the temp is compared with the container +1 again if > 0 AND enabled and less than the previous it overwites the temp
Does this for all 10 containers then transfers the temp into DB1.DBW0
So effectively there is a 2 dimensional array Array(Value,Enabled)
I have chosen as an idea for it to be in one DB DB1.DBW0 is the one the robot uses, then some spares (you never know), DB1.DBW12 to 30 are the 10 parts value containers & DB1.DBW32 - 50 are the enables
Not done it as arrays (this always uses consecutive words not sure if OP will decipher it so used seperate areas & STL is easy).
The OP seemed to indicate that these cells would be on an HMI & can be manipulated by an operator. i.e. perhaps the values are entered manually, can be enabled etc. so we have no idea how the data is entered into the array, perhaps only manually, perhaps by some other process, may have got it wrong & does need sorting, in other words takes the 10 containers & stores them in order lowest to highest then shifts them into the robot cell, although the OP has indicated what I have posted seems to be something like. Also many other questions, is there a need to zero the containers once transferred, remove the enable, does the robot decrement the parts in DB1.DBW0 How do we tell the robot we have moved new data into the robot container ? so many questions.
The code I produced was done on the fly in about 20 mins, tested partly for about 10, could re-jig it in so many ways but why change something that works.
This is always the problem in answering posts, lack of relevant info, interpretation of the problem language barriers.
For example, I often send emails to freinds in Thailand who cannot speak English, my Thai is a few words, cannot translate into their characters, so use Google to translate, many other languages reverse the words & have the same words that mean different things depending on the preceeding or following word. fish sauce is nam pla, nam is water or a liquid or sauce, so for example literally translated its water fish, sauce fish, liquid fish. Google is pretty good but not perfect, once sent an email, google translated it but the translation put some words that were well rather ****ographic, fortunately they understood that it was not me being rude.
 
Wait a minute, now I have confused myself.
Thinking about it if you have a robot & 10 containers regardless if they are actual or just a list in a PLC, the way I see a robot working in real hardware is there are 10 physicl locations, the robot moves to a physical location and works on that, then moves onto the next, lets assume this is the case, then I have actually done this.
The operator or other process loads parts into a line (or circle) of containers.
The system then produces a list for the robot to follow based on lowest value first, however, this poses a problem, so for example we use a shift register, some process places parts into each container, say in order of lowest to highest quantity, containers 3 (1 part), 4 (3 parts) 2 (4 parts) & so on, fine but this only works if containers have to be populated before robot run, if it is a continuous process if a lower value is entered into a container not being used while the robot is running on one that has a higher value does it wait untill it's finished on that container, are the register contents shifted in order again before the robot re-starts, if not then this is not a true lowest to highest.
The system I did had a multidimensional array so it's location, was it availlable or occupied, time spent in location & a few others, so it relied on the following, loading robot, how long a space was empty, type of product was it occupied, for the take out robot was basically the same but drying time up (based on a set time & hirarchy & type) etc.
Seemed like a nice project, but the spec was lacking, we spent many hours with the customer & plant designer/builder thrashing out the what if scenarios, how to make it optimised etc.
 

Similar Topics

I have worked on small projects using AB Micrologix but now we want to take a photo, process it online, and sort based on returned variables...
Replies
5
Views
315
First off, I am new to the whole PLC world and was tasked with a project at work. I was given a Click C0-11dre-D, Honeywell 310G barcode scanner...
Replies
4
Views
2,306
I am trying to make an object sorting system using PLCs for university. The system uses the shape of incoming objects to figure out if they need...
Replies
9
Views
3,743
Hello all, I am currently reseaching the best way to create a simple sorting system. My challange is that all sorting decisions are made by...
Replies
2
Views
6,056
I am a user of Siemens PLCS. I am not a regular visitor of this site, but whenever I am here, I click mostly * Postings that are related to...
Replies
14
Views
3,242
Back
Top Bottom