Minimu 9 v3 magnetometer values

I have a small problem.
The magnetometer values change stepwise.
Here is a picture with the values and the python code.
Sorry my english is not so good.

Thank you.

import smbus
import time
bus = smbus.SMBus(1)
address = 0x1d 
address2 = 0x6b


bus.write_byte_data(0x1d,0x20,0b10100111)
bus.write_byte_data(0x1d,0x21,0x00)
bus.write_byte_data(0x1d,0x24,0b01100100)
bus.write_byte_data(0x1d,0x25,0b00100000)

bus.write_byte_data(0x6b,0x20,0x0f)

def readSensorData():
        MX1 = bus.read_byte_data(address, 0x08)
        MX2 = bus.read_byte_data(address, 0x09)
        MX = 256 * MX2 + MX1
        if MX >= 32768:
                MX= MX - 65536
        else:
                MX= MX     


        MY1= bus.read_byte_data(address, 0x0A)
        MY2 = bus.read_byte_data(address, 0x0B)
        MY = 256 * MY2 + MY1
        if MY >= 32768:
                MY= MY - 65536
        else:
                MY= MY
                
        MZ1 = bus.read_byte_data(address, 0x0C)
        MZ2 = bus.read_byte_data(address, 0x0D)
        MZ = 256 * MZ2 + MZ1
        if MZ >= 32768:
                MZ= MZ - 65536
        else:
                MZ= MZ


        AX1 = bus.read_byte_data(address, 0x28)
        AX2 = bus.read_byte_data(address, 0x29)
        AX = 256 * AX2 + AX1
        if AX >= 32768:
                AX= AX - 65536
        else:
                AX= AX
                
        AY1= bus.read_byte_data(address, 0x2A)
        AY2 = bus.read_byte_data(address, 0x2B)
        AY = 256 * AY2 + AY1
        if AY >= 32768:
                AY= AY - 65536
        else:
                AY= AY
                
        AZ1 = bus.read_byte_data(address, 0x2C)
        AZ2 = bus.read_byte_data(address, 0x2D)
        AZ = 256 * AZ2 + AZ1      
        if AZ >= 32768:
                AZ= AZ - 65536
        else:
                AZ= AZ


        GX1 = bus.read_byte_data(address2, 0x28)
        GX2 = bus.read_byte_data(address2, 0x29)
        GX = 256 * GX2 + GX1      
        if GX >= 32768:
                GX= GX- 65536
        else:
                GX= GX


        GY1 = bus.read_byte_data(address2, 0x2A)
        GY2 = bus.read_byte_data(address2, 0x2B)
        GY = 256 * GY2 + GY1      
        if GY >= 32768:
                GY= GY- 65536
        else:
                GY= GY


        GZ1 = bus.read_byte_data(address2, 0x2C)
        GZ2 = bus.read_byte_data(address2, 0x2D)
        GZ = 256 * GZ2 + GZ1      
        if GZ >= 32768:
                GZ= GZ - 65536
        else:
                GZ= GZ


                      
        return MX, MY, MZ,AX, AY, AZ, GX, GY, GZ


while True:
        SensorData = readSensorData()     
        print SensorData
        time.sleep(0)

[quote=“olli, post:1, topic:9874”]
MX = 256 * MX2 + MX1
[/quote]Try ordering the bytes the other way around.

Hi Jim Remington.
Thanks for the answer.

MX = 256 * MX2 + MX1

I test it, This is not the Problem.

I test also the code from David Grayson site.

I start. minimu9-ahrs --mode raw
The result (problem) is the same.

On David Grayson site I can see the problem too.

-138 129 -416 112 -8 228 -50 14 9
-138 129 -419 120 -4 232 -49 20 18
-138 129 -419 116 -12 228 -51 15 8
-138 129 -419 116 -12 228 -50 21 17
-137 130 -421 116 -8 232 -51 22 11
-137 130 -421 120 -12 220 -56 20 14

I don’t see any problem with the data from David Grayson’s site.
Are you concerned that the same values are repeated?

What concerns me about your data are the very large jumps, especially with change of sign, between sets of repeated values. This can result from wrong byte ordering.

Hi Jim Remington.
Yes I am concerned that the magnetometer values repeated.
I think it is not 50 Hz.
I start both scripts. My python script and the minimu9-ahrs --mode raw from David Grayson.
I think it is not a byte ordering problem. The values looks like really.

.

Hi, olli.

Both your code and David’s select a 6.25 Hz ODR for the magnetometer, so the results you are getting are what I would expect. You can see this in the comment in David’s code here and read more about the setting in section 8.21 of the LSM303D datasheet (CTRL5 register).

To get a 50 Hz ODR from the magnetometer, you should change M_ODR from 001 to 100. In other words, change the line
bus.write_byte_data(0x1d,0x24,0b01100100) in your code to bus.write_byte_data(0x1d,0x24,0b01110000).

Kevin

Hi Kevin,
thank you.
I will test it.

Olli