Backstuffed Sortron

Aeronica

New Member
Jul 29, 2019
27
0
0
In my experiments a painted item with a full destination and no other place to go will return to the sortron and will be re-emited when space becomes available.
 

PhilHibbs

Forum Addict
Trusted User
Jan 15, 2013
3,174
1,128
183
Birmingham, United Kingdom
So what happens in the meantime? Can I continue to issue commands to the Sortron? Will further commands to dispatch items fail? If so, can I still call SORTSLOT to query the contents of an inventory or will everything fail until the backstuffing is cleared?
 

Aeronica

New Member
Jul 29, 2019
27
0
0
In my setup further commands will not process until the stuffed item is able to be sent somewhere successfully. Now I'm not sure if the commands will queue up or are simply ignored. But my sort program continues to run either way. So once the blockage is cleared everything starts up again.

The system I'm working on can and does get stuffed up, but that can be alleviated by having extra chests or barrels at various points in the pipe network.

My setup is a learned sort system. Items are associated with colors. Those associations are stored in an array. When the sort is running each slot of the input chest is checked and the item id is compared with the array. Items with an association get painted that color and sent on their way. All other item will be sent out unpainted.

2lsTs

Input chest (far back): Sortron ->pipe network: T1 (Restrictor) T2 (Restrictor) barrel overflow storage (All manual
T1 (right) = A branch with ALL color pipes in sequence attached to a chest or on the end for UNSORTED items - Colored items won't get past this. Unpainted items will.
T2 (left) = a branch that splits off to each destination. At the end another restrictor where painted items can flow to. For example a set of barrels.

2ltdY
 

PhilHibbs

Forum Addict
Trusted User
Jan 15, 2013
3,174
1,128
183
Birmingham, United Kingdom
I need to work out how to make an array of values in Forth. Do you save your array to disk? I'd like to see how that's done if you have a snippet that you could share. I need to do basically the same, but I probably won't have a "default" route, anything that is not specifically handled stays in the input chest. That way I can throw my used Tin Cans into my Enderpouch, they get sent to my Canning Machine, and the filled cans end up back in the same pouch where I can pull them out. Likewise my empty buckets get re-filled with water and dropped back in the pouch. I guess I could use a different Enderpouch, as I have a second one that is the input to my remote control processing system, but having the unidentified items stay in the pouch is a nice reminder of what I need to program into the system. Most of the Sorting Machine slots are full now, but I have space for one more Sorting Machine in the heart of my system. The Filter and Wireless Receiver are gone now so there's a space underneath the Enderchest.
2013-02-26_21.33.31.jpg
 

Aeronica

New Member
Jul 29, 2019
27
0
0
Here's the code I've been playing with. I'm new to Forth and this has not been cleaned up or factored to to be more efficient. There are 4 8K RAM modules attached. It only supports 1 Sortron at this time. It's a work in progress.

I don't explicitly save the array to disk. But when you do a SAVE" YOUR DISK NAME" to a non system disk the whole Forth system, your program plus any data in the array is saved to disk. When you boot from that disk everything is restored just the way you left it.

There two basic WORDS you use to interact with the sorting system.

1) LEARN - sets the item to color assiciation. If the item already exists it simply updates the color association.
1 LEARN . \ associates the ITEM in INVENTORY SLOT 0 with 1 (white)

2) RUNSORT - Pull items from the attached inventory and routes them to the colored pipes. Hit the SPACEBAR to stop it.

The data is stored in an array of a simple structure of three words. Two are for the item id and one for the color. The code uses a structure gleened from to OLPC project. But I've seen it discussed in other Forth tutorials.

Data structure, variables and helper words to manage the array.
Code:
\ http://wiki.laptop.org/go/Forth_Lesson_18
: STRUCT 0 ;
: FIELD
  CREATE OVER , + DOES> @ +
  ;
 
\ ITEMS ARRAY DATA STRUCTURE
STRUCT ( SORT ID AND DESTINATION PIPE COLOR )
1 CELL * FIELD >LOADDRID
1 CELL * FIELD >HIADDRID
1 CELL * FIELD >COLOR
CONSTANT /ITEMSIZE
 
1024 CONSTANT MAXITEMS
/ITEMSIZE MAXITEMS 1 - * CONSTANT LASTITEM
CREATE ITEMS /ITEMSIZE MAXITEMS * ALLOT \ ARRAY OF ITEMS - COLOR ASSOCIATIONS
VARIABLE XITEM \ TEMP ITEM INDEX
VARIABLE NITEM \ NEXT ITEM INDEX
0 NITEM ! \  NEXT ITEM INDEX INITIALZED TO FIRST POSITION
VARIABLE CITEM \ CURRENT SCRATCH INDEX FOR SEARCHING
 
: INITITEM ( -- ) \ CLEAR THE STRUCTURE FOR 1 ITEM
  0 ITEMS NITEM @ + >LOADDRID !
  0 ITEMS NITEM @ + >HIADDRID !
  0 ITEMS NITEM @ + >COLOR !
  ;
 
: NITEM+ ( -- F )
  NITEM @ LASTITEM < IF
    NITEM @ /ITEMSIZE + NITEM !  \ ( -- ) INCREMENT THE NITEM INDEX
    INITITEM
    FALSE \ NOT PAST END OF ITEMS
  ELSE
    TRUE  \ NO MORE SPACE IN ITEMS ARRAY
    ." ITEMS ARRAY IS FULL" CR
  THEN ;
 
INITITEM  \ CLEAR 0TH 0 0 ITEM ID HOLDER.  THIS IS SLOT TO SIMPLIFIY THE CODE.
NITEM+    \ PREP FOR A NEW ID.
 
: CITEM- CITEM @ /ITEMSIZE - CITEM ! ; \ ( -- ) DECREMENT THE CITEM INDEX


Whole Program
Code:
\ Aeronica's ALPHA Sorting System in Forth
DECIMAL
 
\ http://wiki.laptop.org/go/Forth_Lesson_18
: STRUCT 0 ;
: FIELD
  CREATE OVER , + DOES> @ +
  ;
 
\ STUFF FROM THE INTEGRATED REDSTONE LIBRARY OF FUNCTIONS FOR MINEOS
: HEXDIGIT \ N -- C
\ CONVERT LOWEST DIGIT OF N TO CHARACTER C IN BASE 16
  15 AND DUP 9 > IF 55 ELSE 48 THEN + ;
: .B  \ B --
\ WRITE OUT BYTE B IN HEX - 2 DIGITS
  DUP HEXDIGIT SWAP 4 U>> HEXDIGIT EMIT EMIT ;
: .W  \ W --
\ WRITE OUT 2 BYTES W IN HEX - 4 DIGITS
  DUP 8 U>> .B .B ;
 
\ SIMPLE DOUBLE WORD (32 BIT) EQUALITY - USEFUL FOR SORTRON ITEM IDENTIFIERS.
\ D D -- F
: M= ROT = -ROT = AND ;
 
\ D D -- F
: M<> M= 0= ;
 
\ ITEMS ARRAY DATA STRUCTURE
STRUCT ( SORT ID AND DESTINATION PIPE COLOR )
1 CELL * FIELD >LOADDRID
1 CELL * FIELD >HIADDRID
1 CELL * FIELD >COLOR
CONSTANT /ITEMSIZE
 
1024 CONSTANT MAXITEMS
/ITEMSIZE MAXITEMS 1 - * CONSTANT LASTITEM
CREATE ITEMS /ITEMSIZE MAXITEMS * ALLOT \ ARRAY OF ITEMS - COLOR ASSOCIATIONS
VARIABLE XITEM \ TEMP ITEM INDEX
VARIABLE NITEM \ NEXT ITEM INDEX
0 NITEM ! \  NEXT ITEM INDEX INITIALZED TO FIRST POSITION
VARIABLE CITEM \ CURRENT SCRATCH INDEX FOR SEARCHING
 
: INITITEM ( -- ) \ CLEAR THE STRUCTURE FOR 1 ITEM
  0 ITEMS NITEM @ + >LOADDRID !
  0 ITEMS NITEM @ + >HIADDRID !
  0 ITEMS NITEM @ + >COLOR !
  ;
 
: NITEM+ ( -- F )
  NITEM @ LASTITEM < IF
    NITEM @ /ITEMSIZE + NITEM !  \ ( -- ) INCREMENT THE NITEM INDEX
    INITITEM
    FALSE \ NOT PAST END OF ITEMS
  ELSE
    TRUE  \ NO MORE SPACE IN ITEMS ARRAY
    ." ITEMS ARRAY IS FULL" CR
  THEN ;
 
INITITEM  \ CLEAR 0TH 0 0 ITEM ID HOLDER.  THIS IS SLOT TO SIMPLIFIY THE CODE.
NITEM+    \ PREP FOR A NEW ID.
 
: CITEM- CITEM @ /ITEMSIZE - CITEM ! ; \ ( -- ) DECREMENT THE CITEM INDEX
 
: (PUSHID) ( D -- ) \
  ITEMS NITEM @ + XITEM !
  XITEM @ >LOADDRID !
  XITEM @ >HIADDRID !
  ;
 
: (PUSHCOLOR) ( B -- ) \
  ITEMS NITEM @ + XITEM !
  XITEM @ >COLOR !
  ;
 
: DUMPIT \ DUMP THE ITEMS ARRAY
  CR
  NITEM @ 0 ?DO
  I ITEMS + XITEM !
  XITEM @ >HIADDRID @ .W ."  "
  XITEM @ >LOADDRID @ .W ."  "
  XITEM @ >COLOR @ .B CR
  /ITEMSIZE +LOOP ;
 
: (FINDITEM) ( D -- F INDEX )
  NITEM @ CITEM !          \ SEARCH BACKWARDS DOWN THE ITEMS BUFFER
  CITEM-
  BEGIN
  CITEM @ 0 >=
  WHILE
    2DUP
    ITEMS CITEM @ + XITEM ! \
    XITEM @ >HIADDRID @    \ POP OPPSITE OF PUSH
    XITEM @ >LOADDRID @
    M= IF
        2DROP
        CITEM @
        TRUE
        EXIT
      ELSE
        CITEM-
      THEN
  REPEAT
    2DROP
    -2
    FALSE
;
 
 
\ LEARN ( COLOR -- F) \ ASSOCIATES A COLORED PIPE WITH AN ITEM IN INVENTORY SLOT 0
\ EXAMPLE:
\  WHITE LEARN
\  -OR-
\  1 LEARN
: LEARN
  SORTCOLOR!        \ SET THE SORT COLOR OR THE SORTRON RIGHT AWAY, WE CAN USE THIS LATER.
  0 SORTSLOT@ -ROT  \ GET THE ITEM ID AND STASH THE QUANTITY AWAY FOR NOW
  2DUP              \ COPY THE ITEM ID FOR LATER
  (FINDITEM)        \ LETS SEE IF THE ITEM EXISTS OR NOT IF SO WE WILL JUST UPDATE THE COLOR
  IF
    \ TRUE - WE FOUND AN EXISTING ITEM!
    ITEMS + XITEM !  \ POINT TO THE ITEM'S LOCATION IN THE ARRAY ( CONSUMES THE (FINDITEM) INDEX )
    SORTCOLOR@      \ GET THE OUTPUT SORT COLOR OF THE SORTRON
    XITEM @ >COLOR ! \ UPDATE THE COLOR/SORT DESTINATION
    2DROP            \ DON'T NEED THE ITEMS ID ANYMORE. ALL WE HAVE IS A QUANTITY NOW
    0                \ WE DEFAULT TO SLOT 0
    SORTPULL        \ SEND THE ITEM ON IT'S WAY
    DROP            \ DISCARD THE NUMBER OF ITEMS SENT
    TRUE ." UPDATE " CR
    EXIT
  ELSE
    \ FALSE - THIS IS A NEW ITEM
    DROP            \ DROP THE INDEX
    ITEMS NITEM @ + XITEM !  \ INDEX + ITEMS
    XITEM @ >LOADDRID !
    XITEM @ >HIADDRID !
    SORTCOLOR@      \ GET THE OUTPUT SORT COLOR OF THE SORTRON
    XITEM @ >COLOR ! \ UPDATE THE COLOR/SORT DESTINATION
    0                \ WE DEFAULT TO SLOT 0
    SORTPULL        \ SEND THE: ITEM ON IT'S WAY
    DROP            \ DISCARD THE NUMBER OF ITEMS SENT
    NITEM+          \ INCREMENT THE NEXT ITEM POINTER
    INVERT ." ADD " CR
  THEN
  ;
 
\ DEV TESTING
: DUPCHK
  200 0 DO
    I 16 MOD 1 + LEARN DROP
  LOOP ;
 
\ UNUSED
: 2IOXRST 2 IOXSET 5 TICKS 2 IOXRST ; \ PULSE BIT 2 OF THE IO EXPANDER
: 1IOXTST IOX@ 1 AND 1 = IF TRUE ELSE FALSE THEN ; \ TEST IF BIT 1 SET
 
\ UNUSED
: TSTB
  PAGE
  BEGIN
  5 5 AT-XY ." BUTTON TEST"
  1IOXTST
  IF
    5 6 AT-XY ." PRESSED"
    2IOXRST
    5 6 AT-XY ." RESET  "
  THEN
  KEY?
  IF KEY 32 = IF CR EXIT
  THEN THEN
  AGAIN ;
 
\ SORT ITEMS.  SPACEBAR TO STOP
: RUNSORT
  BEGIN
  SORTSLOTS 0 ?DO
    I SORTSLOT@ -ROT
    (FINDITEM)
    IF
      \ TRUE - LET'S GET THE COLOR
      ITEMS + XITEM !
      XITEM @ >COLOR @
    ELSE
      DROP \ INDEX NOT NEEDED
      0    \ UNPAINTED ITEM
    THEN
      SORTCOLOR!
        \ QUANTITY IS ON THE STACK
      I \ INVENTORY SLOT
      SORTPULL DROP
      5 TICKS
      KEY?
      IF KEY 32 = IF CR UNLOOP EXIT
      THEN THEN
      LOOP
  AGAIN
  ;
 
  • Like
Reactions: PhilHibbs

PhilHibbs

Forum Addict
Trusted User
Jan 15, 2013
3,174
1,128
183
Birmingham, United Kingdom
Great, thanks for that. My plan involves an array of item IDs and colours plus the stack size to pull. So if 16 Coal gets painted white, the Sortron initially only pulls stacks of 16 or more. A separate array of 27 entries records the item ID and stack size detected on the previous pass, and if there's been a stack smaller than 16 that hasn't changed since the last pass, that gets pulled and painted white. That should provide an ideal stacking mechanism while preventing clutter.

My learning function will be remote controlled. I'll put a dye in Slot 26 and some items in Slot 27 (or 25 and 26 for 0-based addressing), activate the "Learn" frequency on my Wireless Remote, and the Sortron adds the item type and stack size to the array, overwriting an existing entry with a different stack size. I can't see any value in having multiple entries with different stack sizes. Unless... maybe having "Gravel, 1, Red" and "Gravel, 63, Blue" could send a small portion of my gravel to a Macerator and the rest into storage, and the largest stack size would be used as the default route on a second pass. That's tricky, and definitely a "version 2" feature.
 

Aeronica

New Member
Jul 29, 2019
27
0
0
Glad I could help. Getting back to the Back-stuffed Sortron issue, based on the available documentation the last command sent will be held until the condition clears. So if Item X has no place to go, the command that initiated that action will be held by the Sortron until the operation is complete. So if there is a loop running the Sortron the program will hang until the back-stuff is cleared. I tested this and indeed, the RUNSORT loop I use hangs until the the back-stuff is cleared.
 

PhilHibbs

Forum Addict
Trusted User
Jan 15, 2013
3,174
1,128
183
Birmingham, United Kingdom
Ah, right, a bit like where an in-line Sortron will make a CPU wait until it has received the appropriate number of items, a backstuffed Sortron will simply hang the CPU unti it's resolved. OK. So if I send a Wireless Remote signal, it'll simply miss it because it isn't checking the IO Expander input while the Sortron is hanging it. Fair enough I guess, makes me glad that I have separate CPUs for my two Sortrons (which I actually did because of the screwdriver Bus ID bug). So I need a third CPU & IO Expander for the Sortron that will be part of my sorting system, which is on a different tube network to the existing Sortrons.
 
  • Like
Reactions: Aeronica

TheWolfJack

New Member
Jul 29, 2019
4
0
0
Aeronica, I was trying to upload your code into a new sortron (our server's sorting system is getting to be unmanageable) but I received an error fairly early on:

"/ITEMSIZE MAXITEMS 1 - * CONSTANT LASTITEM Unknown Token: /ITEMSIZE"

I'll freely admit I know zero Fourth, best I have is some bash, basic, and VB in college, so I am really just wanting to grab something and go. I do wish there was an easier way to get the code into the Sortron, but I'll do it this way as long as it works in the end!

-Wolfie
 

Aeronica

New Member
Jul 29, 2019
27
0
0
Hi Wolfie,

/ITEMSIZE is defined earlier in the code. It's the size of the structure. The associated code is:
Post your code and I'll take a look.

Code:
\ http://wiki.laptop.org/go/Forth_Lesson_18
: STRUCT 0 ;
: FIELD
  CREATE OVER , + DOES> @ +
  ;
 
\ ITEMS ARRAY DATA STRUCTURE
STRUCT ( SORT ID AND DESTINATION PIPE COLOR )
1 CELL * FIELD >LOADDRID
1 CELL * FIELD >HIADDRID
1 CELL * FIELD >COLOR
CONSTANT /ITEMSIZE
 
1024 CONSTANT MAXITEMS
 
/ITEMSIZE MAXITEMS 1 - * CONSTANT LASTITEM
 . . .
 

TheWolfJack

New Member
Jul 29, 2019
4
0
0
Aeronica, it was an issue on my part with how I was inputing the code with AutoIT. It's all working perfectly now!

I do have another question though, I've run a couple tests and I see that an unknown item is uncolored when it enters the system. I am guessing that this code block controls that behavior:
ELSE
DROP \ INDEX NOT NEEDED
0 \ UNPAINTED ITEM
THEN

If I were to change that to something like:
ELSE
DROP \ INDEX NOT NEEDED
16 \ UNPAINTED ITEM
THEN

Would I be correct that any unknown/new item would leave the sortron colored black? If I am correct in that, is there a way that I can update the program as it is on disk or do I need to start from scratch again and reload/save everything with the change?

Thanks for all the help, finding this program has been absolutely wonderful!

-Wolfie
 

Aeronica

New Member
Jul 29, 2019
27
0
0
Hello again Wolfie,

Yes, changing 0 to 16 would paint any unknown items black.

One way to update is to simply redefine the word that those statements are used in. In my code as posted you would re-enter RUNSORT again and make your changes. Doing this simply hides the previous definition. A bit wasteful perhaps, but it maybe easier that reloading the whole thing from scratch. That code has been updated a bit. I added a bulk learn word and also started work on a menu.

Regards,
-Aeronica
 

TheWolfJack

New Member
Jul 29, 2019
4
0
0
That code has been updated a bit. I added a bulk learn word and also started work on a menu.

Regards,
-Aeronica

Funny enough, that was going to be my next question for you. :)

Aeronica, this has really been hugely helpful for me (the fact that you posted your code so I didn't have to learn Fourth AND that you've been so helpful in explaining your code to me) and I want to say that you, very much, for doing all of this.

If you are feeling even more amazing, I would LOVE to see (and by see I mean copy and slowly paste onto my server) this updated code. :D

-Wolfie