E
@livius
I test this code with my Gpy and its working fine. My suggestion is not connect the pytrack or pysense board and MPU9250 together, use the Gpy board stand alone because when you connect SDA and SCL the I2C bus fail.
import machine, time, math, pycom, os
from machine import I2C
from mpu9250 import MPU9250
from ak8963 import AK8963
i2c = I2C(0, mode=I2C.MASTER, pins=('P22','P21'))
pycom.heartbeat(False)
sensor = MPU9250(i2c)
sensor2= AK8963(i2c)
#Declination Mex City
#https://www.ngdc.noaa.gov/geomag-web/calculators/calculateDeclination?lat1=%2719.48698%27&lon1=%27-99.18594%27
decli=4.56
print("MPU9250 id: " + hex(sensor.whoami))
print("AK8963 id: " + hex(sensor2.whoami))
print("To Calibrate Magnetometer please draw an 8 on the air 1 minute")
cali=sensor2.calibrate(60)
offset=cali[0]
scale=cali[1]
sensor2= AK8963(i2c,offset=offset, scale=scale)
print("Calibration finished (offset), (scale): ",cali)
time.sleep_ms(5000)
n=0
while True:
print('acc :',sensor.acceleration)
print('gyro:',sensor.gyro)
print('mag :',sensor.magnetic)
#Compass Direction
x=sensor.magnetic[0]
y=sensor.magnetic[1]
z=sensor.magnetic[2]
if (y > 0):
heading=math.atan(x/y)*(180/(math.pi))
if (y < 0):
heading=180-math.atan(x/y)*(180/(math.pi))
if (y==0) & (x < 0):
heading=180.00
if (y==0) & (x > 0):
heading=0.0
#print('mx=',x,'uT, my=',y,'uT, mz=',z,'uT')
print('Compass Direction:',round(heading+decli),'degrees')
# Turn light on Green when 0 degrees
if (round(heading+decli)==0):
pycom.rgbled(0x000800)
if (round(heading+decli)!=0):
pycom.rgbled(0x000000)
time.sleep_ms(50)
n=n+1