Shop OBEX P1 Docs P2 Docs Learn Events
Like function — Parallax Forums

Like function

AImanAIman Posts: 531
edited 2007-02-28 19:37 in Learn with BlocklyProp
I need to use a function that will take something and compare it to the closest match. In this case its a scanner picking up·numbers.

Numbers·will be similar but not the same and the closest matching number is what dictates where things get shipped.

Can the BS2 do this?
·

Comments

  • Mike GreenMike Green Posts: 23,101
    edited 2007-02-28 17:52
    In principal it can do this. I'm assuming you'll have the scanner provide the information as a string of characters. The issue is: What do you mean by closest match? You will need to define what you mean. Will you have a table of numbers to compare against? How big does the table have to be? How long (big) are the numbers? Do you have to account for missing digits? If there is a table of numbers, how will you define it? How will you install it in the Stamp? How will you update it? If the table is too big, the Stamp may not be able to hold it along with the program. If the rules are too complicated, the Stamp may not be fast enough or may not have enough working memory (variables) to process the rules. What kind of scanner is it? How much time and resources will the Stamp have to devote to running the scanner?
  • Beau SchwabeBeau Schwabe Posts: 6,557
    edited 2007-02-28 17:58
    I am pretty sure the stamp would work for you, but you need to give us more information. Your result will ultimately depend on what
    kind of matching algorithm you choose, and how you implement it. A common style algorithm is to simply look at how many numbers
    match between a known and unknown. That in and of itself could have several implementations.

    Example:

    10.01.30.40.234 <-- Assume this is the number you want

    10.33.36.40.234 <-- Number received


    You could say that you have a 60% match because 3 of the 5 numbers match. A more aggressive approach would compare how close
    the numbers that did not match were to one another. In this example assuming that 255 is the highest number, you could again look
    at the data this way....

    (010:010) = 0 ---> 255 - 0 = 255
    (001:033) = 32 --> 255 - 32 = 223
    (030:036) = 6 ---> 255 - 6 = 249
    (040:040) = 0 ---> 255 - 0 = 255
    (234:234) = 0 ---> 255 - 0 = 255

    255+223+249+255+255 = 1237 ----> 1237 / 1275 = 97%

    Note: 1275 = 255 * 5 ... (The maximum value times the number of samples)

    Here, the same numbers have a 97% match to one another. So it really depends on how you want to look at your data.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Beau Schwabe

    IC Layout Engineer
    Parallax, Inc.
  • edited 2007-02-28 19:22
    AIman said...
    ...a function that will take something and compare it to the closest match. In this case its a scanner picking up·numbers....
    As far as a function for finding the closest number, yes, the BASIC Stamp can do it.· Here is an example:

    ' FindClosestValue.bs2
     
    ' {$STAMP BS2}
    ' {$PBASIC 2.5}
     
    index        VAR   Word
    value        VAR   Word
    closest      VAR   Word
    listItem     VAR   Word
     
    DO
     
      closest = 65535
      DEBUG CR, "Enter a value: "
      DEBUGIN DEC value
     
      FOR index = 0 TO 6
        LOOKUP index, [noparse][[/noparse]1, 5, 9, 80, 111, 200], listItem
        IF ABS(value - listItem) <= ABS(closest - value) THEN
          closest = listItem
        ENDIF
      NEXT
     
      DEBUG "Closet entry is: ", DEC closest
     
    LOOP
    

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Andy Lindsay

    Education Department
    Parallax, Inc.
  • AImanAIman Posts: 531
    edited 2007-02-28 19:29
    Thanks - thats exactly what I needed to know.
  • edited 2007-02-28 19:37
    For posterity's sake:

    The loop in FindClosestValue.bs2 can also be used for sorting packages by size assuming the BASIC Stamp is measuring it with an ultrasonic peripheral such as the Ping))) sensor.·

    As Mike Green pointed out, the BASIC Stamp 2 might not have the processing speed to for some sorting situations where the list of values is very large and the speed of the conveyer belt is fast.· In that case, a BASIC Stamp with more speed and/or memory such as the BASIC Stamp 2p or 2px, or even the Propeller chip·might be in order.· External EEPROM memory may also be necessary for storing larger lists.

    On the other hand, a BASIC Stamp 2 system on a given conveyer belt node probably doesn't need to store humungous lists.··Each node in an automatic sorting system is typically concerned with a subset of the data on the barcode.· The text or value can be parsed before it is sorted, significantly reducing the decision time.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Andy Lindsay

    Education Department
    Parallax, Inc.

    Post Edited (Andy Lindsay (Parallax)) : 2/28/2007 7:53:17 PM GMT
Sign In or Register to comment.