I wnat to use the Hitechnic Compass sensor with the BirckPi3. I have written a class to use it. But I get only Numbers between 206 and 160 from the Compass. What is wrong?
#!/usr/bin/python
########################################
# class hitechnic compass #
# file: HitechnicCompassSensor.py #
# Author: Th. Kaffka, Cologne, Germany #
# Date: 10.11.2020 #
########################################
import brickpi3
import time
BP = brickpi3.BrickPi3()
class HitechnicCompassSensor:
I2C_ADR = 0b00000010
GET_DIR = 0x42
COMMAND = 0x41
BEGIN_CALIBRATION = 0x43
END_CALIBRATION = 0x00
def __init__(self, port):
self.port = port
BP.set_sensor_type(port, BP.SENSOR_TYPE.I2C, [0, 20])
def getDirection(self):
# compass intervall 160, 206
BP.transact_i2c(self.port, self.I2C_ADR, [self.GET_DIR], 2)
time.sleep(0.01)
value = BP.get_sensor(self.port)
return ((value[0] & 0xFF) << 1) + value[1]
def startCalibration (self):
BP.transact_i2c(self.port, self.I2C_ADR, [self.COMMAND, self.BEGIN_CALIBRATION], 0)
time.sleep(0.01)
def endCalibration (self):
BP.transact_i2c(self.port, self.I2C_ADR, [self.COMMAND, self.END_CALIBRATION], 0)
time.sleep(0.01)