Mapping a range of values to another

前端 未结 6 2221
谎友^
谎友^ 2020-11-28 02:39

I am looking for ideas on how to translate one range values to another in Python. I am working on hardware project and am reading data from a sensor that can return a range

6条回答
  •  半阙折子戏
    2020-11-28 03:02

    I was looking for the same thing in python to map angles 0-300deg to raw dynamixel values 0-1023, or 1023-0 depending on the actuator orientations.

    I ended up going very simple.

    Variables:

    x:input value; 
    a,b:input range
    c,d:output range
    y:return value
    

    Function:

    def mapFromTo(x,a,b,c,d):
       y=(x-a)/(b-a)*(d-c)+c
       return y
    

    Usage:

    dyn111.goal_position=mapFromTo(pos111,0,300,0,1024)
    

提交回复
热议问题