0

I'm hacking my way through Python;

Right now I'm trying to get the heading of a robot using a magnetometer using Python. The problem is I want to be able to set "North" by mapping the degree based values onto my own set. If I were programming the Arduino I'd use the map() function. Is there anything similar in 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);
C. Thomas Brittain
  • 376
  • 1
  • 5
  • 12
  • possible duplicate of [Mapping a range of values to another](http://stackoverflow.com/questions/1969240/mapping-a-range-of-values-to-another) – planestepper Aug 20 '13 at 10:20

1 Answers1

2

Solution available on 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)
Community
  • 1
  • 1
planestepper
  • 3,277
  • 26
  • 38