I am trying to use a Beaglebone Wireless (debian image 2017-07-01) to read values from an HMC5883L Magnetometer.
The device is connected through I2C2 (pins P9_19 and P9_20). The address for the device is 0x68.
I know the device is connected since I can see it by using i2cdetect as shown below:
However, whenever I run my python code to read the values, I get messages that the bone is having errors accessing the adresses.
As with most of my problems with the BBBW, the code works fine on an older bone (debian image 2015-11-12).
My faith in Beagleboard dwindles by the minute.
Here is the python code, first is my main file:
`
from time import sleep
import os
import hmcCl
sensor = hmcCl.hmc()
sensor.setConfig()
count = 0
while True:
count += 1
x = 0
y = 0
z = 0
he = 0
os.system(“clear”)
x, y, z = sensor.getAvgRead(5)
print(“X: {} Y: {} Z: {} Count: {}”.format(x,y,z, count))
print(sensor.getDirection())
sleep(.2)
`
Next is the object file (hmcCl.py):
`
from Adafruit_I2C import Adafruit_I2C
import math
class hmc:
def init(self):
self.hmc = Adafruit_I2C(0x1E)
self.debug = False
self.address = 0x1E
self.readingList = [0, 0, 0]
self.maxList = [0, 0, 0]
self.minList = [0, 0, 0]
self.mpu = Adafruit_I2C(0x68)
def setConfig(self, mode=0x00):
self.mpu.write8(0x6B, 0) #MPU6050 Power Management_1 Register
self.mpu.write8(0x6A, 0) #MPU6050 USER_CTRL Register: Disables FIFO Buffer
self.mpu.write8(0x37, 2) #MPU6050 INT_PIN_CFG Register: Enables I2C Bypass
self.hmc.write8(0x00, 0x70) #Config Reg A: Frequency of measurement = 15Hz
self.hmc.write8(0x01, 0xA0) #Config Reg B: Gain = 5
self.hmc.write8(0x02, mode) #Mode Register
return True
def readMag(self):
retList = []
regList = [0x03, 0x04, 0x07, 0x08, 0x05, 0x06]
for i in range(0, len(regList), 2):
msb = self.hmc.readS8(regList[i])
lsb = self.hmc.readS8(regList[i+1])
retVal = (msb<<8) | lsb
retList.append(retVal)
for j in range(0, len(retList)):
self.readingList[j] = retList[j]
if retList[j] > self.maxList[j]:
self.maxList[j] = retList[j]
if retList[j] < self.minList[j]:
self.minList[j] = retList[j]
return True
def getAvgRead(self, samples):
xRead = 0
yRead = 0
zRead = 0
for i in range(0, samples):
if self.readMag():
xRead += self.readingList[0]
yRead += self.readingList[1]
zRead += self.readingList[2]
else:
print(“Error”)
xRead = xRead / samples
yRead = yRead / samples
zRead = zRead / samples
return xRead, yRead, zRead
def relativeRead(self, axis, reading):
#Takes the max and min of a given axis and finds 1/8 of that distance than
#checks the given number to see if it is within that range of the max or min
range = (self.maxList[axis] - self.minList[axis]) / 4
if (reading + range) > self.maxList[axis]:
return “High”
elif(reading - range) < self.minList[axis]:
return “Low”
else:
return False
def getDirection(self):
xMax = self.maxList[0]
yMax = self.maxList[1]
self.readMag()
if self.readingList[0] > self.readingList[1]:
if self.relativeRead(0, self.readingList[0]) == “High”:
return “North”
elif self.relativeRead(1, self.readingList[1]) == “Low”:
return “East”
elif self.readingList[1] > self.readingList[0]:
if self.relativeRead(1, self.readingList[1]) == “High”:
return “West”
elif self.relativeRead(0, self.readingList[0]) == “Low”:
return “South”
else:
return “N/A”
`
Any help into this matter is greatly appreciated.
Thanks,
Tomas