2013-08-20 32 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)