Scaling

Lassera
2015-09-16
2015-09-16
  • Lassera - 2015-09-16

    Hello!

    i am new with programming so i'm asking maybe stupid questions, so if i have analog input what gives value from 400 to 1000. Value 400 is for example 80 cm and 1000 is 1100cm. How i can make a program which tells automatically how many cm is value 500?Hope somebody understand this...

    thanks

     
  • Anonymous - 2015-09-16

    Originally created by: scott_cunningham

    This is a linear equation of style y = ax + b. I will leave you to fill in the blanks on the equation details.

    So create a FUNCTION - but you first need to decide your data type. INT or REAL... You are using nice rounded numbers, so I will pick INT (using ST language):

    FUNCTION GetDistance : INT
    VAR_INPUT
    Β  Β Raw : INT;
    END_VAR
    VAR CONSTANT
    Β  Β SCALE: INT := 123;
    Β  Β OFFSET: INT := 123;
    END_VAR
    GetDistance := Raw * SCALE + OFFSET;
    END_FUNCTION
    

    Then in your regular program you can simply write:

    Cm := GetDistance(500);
    

    HINT: Always make sure you don't overflow the data type! INT only goes to +32767.

     

Log in to post a comment.