HMC5883L Electronic Compass magnetic field interference filtering calibration with Raspberry Pi

Source: Internet
Author: User

HMC5883L Electronic Compass magnetic field interference filtering calibration with Raspberry Pi


This article is suitable for all the electronic compass calibration, not limited to HMC5883L. Hardware connection and Raspberry Pi construction.

This article only applies to the filtering of fixed intensity and direction magnetic field interference, such as the magnetic field produced by other devices of the robot itself. However, magnetic field interference such as loudspeakers is ineffective.

First, calibrate the x y direction, and flat the chip to a horizontal plane. Then, the Earth's magnetic field is approximately parallel to the XY plane and rotates the XY plane slowly around the Z axis, the Electronic Compass HMC5883L will measure the intensity of the Earth's magnetic field in the circumference + the fixed interference magnetic field. The intensity of a fixed magnetic field is the average value of all measurements.

#!/usr/bin/pythonimport smbusimport timeimport mathbus = smbus.SMBus(0)address = 0x1edef read_byte(adr):    return bus.read_byte_data(address, adr)def read_word(adr):    high = bus.read_byte_data(address, adr)    low = bus.read_byte_data(address, adr+1)    val = (high << 8) + low    return valdef read_word_2c(adr):    val = read_word(adr)    if (val >= 0x8000):        return -((65535 - val) + 1)    else:        return valdef write_byte(adr, value):    bus.write_byte_data(address, adr, value)write_byte(0, 0b01110000) # Set to 8 samples @ 15Hzwrite_byte(1, 0b00100000) # 1.3 gain LSb / Gauss 1090 (default)write_byte(2, 0b00000000) # Continuous samplingscale = 0.92for i in range(0,500):    x_out = read_word_2c(3)    y_out = read_word_2c(7)    z_out = read_word_2c(5)        bearing  = math.atan2(y_out, x_out)     if (bearing < 0):        bearing += 2 * math.pi        print x_out, y_out, (x_out * scale), (y_out * scale)    time.sleep(0.1)


Run the above program and rotate the horizontal plane containing the chip continuously within 50 seconds. If the horizontal plane is rotated slowly at 360 degrees, the stability of the horizontal plane must be ensured. Otherwise, a lot of strange interference data will be obtained.

sudo ./compass-test.py > compass-plot.dat


Import the output to the compas-plot.dat file. Next, we will use GNUPlot to look at the data results, with each pair of x y as the coordinate of the graphic effect.

set terminal wxt persist size 800,800 background'#000000'      set style line99 linecolor rgb "#ffffff" linetype 0 linewidth 2    set key topright textcolor linestyle 99     set gridlinestyle 99    set borderlinestyle 99     plot filenameusing 1:2 title "Raw compass values" linecolor rgb "green" 


Save the preceding command text to the gnuplot-compass.plg and run the following command to see the graph:

gnuplot -e "filename='compass-plot.dat'"gnuplot-compass.plg



We can see that the center of the graph is not. <喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KPHAgYWxpZ249 "left"> Replace the for loop content of the above program with the following code, and manually rotate the horizontal plane continuously for 50 seconds,

minx = 0maxx = 0miny = 0maxy = 0for i in range(0,500):    x_out = read_word_2c(3)    y_out = read_word_2c(7)    z_out = read_word_2c(5)            if x_out < minx:        minx=x_out        if y_out < miny:        miny=y_out        if x_out > maxx:        maxx=x_out        if y_out > maxy:        maxy=y_out        #print x_out, y_out, (x_out * scale), (y_out * scale)    time.sleep(0.1)print "minx: ", minxprint "miny: ", minyprint "maxx: ", maxxprint "maxy: ", maxyprint "x offset: ", (maxx + minx) / 2print "y offset: ", (maxy + miny) / 2


This time, the following values are output:

  minx:  -216    miny:  -193    maxx:  197    maxy:  213    x offset:  -10    y offset:  10

Add the offset of the output to the program. Replace the code for reading the value of the first program with the following:

x_offset = -10y_offset = 10x_out = (read_word_2c(3) - x_offset) * scaley_out = (read_word_2c(7) - y_offset) * scalez_out = (read_word_2c(5)) * scale


The Z-direction calibration is similar to the above method, but the xz z plane must be fixed to the horizontal plane to do the same thing.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.