2013-08-20 35 views
0

我正在通过Python进行黑客攻击;在Python中映射磁力计数据

现在我正试图让使用Python的磁力仪使用机器人的标题。问题是我想通过将基于度的值映射到我自己的集上来设置“北”。如果我编程Arduino,我会使用map()函数。 Python中有类似的东西吗?

// how many degrees are we off 
int diff = compassValue-direc; 

// modify degress 
if(diff > 180) 
    diff = -360+diff; 
else if(diff < -180) 
    diff = 360+diff; 

// Make the robot turn to its proper orientation 
diff = map(diff, -180, 180, -255, 255); 
+0

可能重复(http://stackoverflow.com/questions/1969240/mapping-a-range-of - 值到另一个) – leon

回答

2

Mapping a range of values to another解决方案可用的:[映射的值的范围,以另一]的

def translate(value, leftMin, leftMax, rightMin, rightMax): 
    # Figure out how 'wide' each range is 
    leftSpan = leftMax - leftMin 
    rightSpan = rightMax - rightMin 

    # Convert the left range into a 0-1 range (float) 
    valueScaled = float(value - leftMin)/float(leftSpan) 

    # Convert the 0-1 range into a value in the right range. 
    return rightMin + (valueScaled * rightSpan)