简体   繁体   中英

Mapping a magnetometer data in python

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);

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM