我正在通过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 :(得分: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)