0

我正在通过 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);
4

1 回答 1

2

将一系列值映射到另一个可用的解决方案:

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)
于 2013-08-20T10:21:29.267 回答