我一直在与grovepi +一起使用grove 6轴加速度计/陀螺仪。在下面的代码中,没有一个功能读取加速度计或陀螺仪,并且输出为“ None”。这对于该类中的所有函数都是相同的。
import sys
import time
import math
import smbus2 as smbus
import grovepi
#This will setup the correct i2c according to the pi version
rev = GPIO.RPI_REVISION
if rev == 2 or rev == 3:
bus = smbus.SMBus(1)
else:
bus = smbus.SMBus(0)
class LSM6DS3:
#This is the physical address of the sensor
address = 0x6a
#Sensor supply voltage(5v)
ADC_ref = 5.0
#Voltage calibration at 0g
zero_x = 1.569
zero_y = 1.569
zero_z = 1.569
#Standard Sensitivity: -300 mV/g
sensitivity_x = 0.3
sensitivity_y = 0.3
sensitivity_z = 0.3
#Starting variables
i2c = None
tempvar = 0
global accel_center_x
accel_center_x = 0
global accel_center_y
accel_center_y = 0
global accel_center_z
accel_center_z = 0
#Sensor setup
def __init__(self, address=0x6a):
self.address = address
self.i2c = bus
self.write_reg(0x4A, 0x10) #Writing to register 0x10, CTRL1_XL, 0x4a, or 01001010, setting
#the accel ODR to 104Hz, +- 4g, 100hz antialiasing
self.write_reg(0x46, 0x11) #Writing to register 0x11, CTRL2_G, 0x46, or 01000110, setting
#the gyro ODR to 104hz, 500dps, enabled !!Check last bit!!
self.write_reg(0x80, 0x13) #Writing to register 0x13, CTRL4_C, 0x80, or 10000000, setting
#the bandwidth to be determined by CTRL1, previously set
accel_center_x = self.read_reg(0x28)
accel_center_y = self.read_reg(0x2A)
accel_center_y = self.read_reg(0x2C)
time.sleep(.005)
# THIS SECTION IS CONVERTING RPi.GPIO TO
# ADAFRUIT_GPIO.I2C, USING THE EXAMPLE
#This is to write to a register
def write_reg(self, data, reg):
bus.write_byte_data(self.address, reg, data)
#This is to read a register
def read_reg(self, reg):
bus.read_byte_data(self.address, reg)
#this is to read an unassigned 16-bit value from the register
def readU16(self, reg, little_endian=True):
result = self.bus.read_word_data(self.address, reg) & 0xFFFF
if not little_endian:
result = ((result << 8) & 0xFF00) + (result >> 8)
return result
#This is to read a signed 16-bit value from the register
def readS16(self, reg, little_endian=True):
result = self.readU16(reg, litte_endian)
if result > 32767:
result -= 65536
return result
# END OF SECTION
def readRawAccelX(self):
output = self.read_reg(0x28)
def readRawAccelY(self):
output = self.read_reg(0x2A)
def readRawAccelZ(self):
output = self.read_reg(0x2C)
def getXRotation(self):
value_y = self.readRawAccelY()
value_z = self.readRawAccelZ()
yv=(value_y / 1024.0*ADC_ref - zero_y) / self.sensitivity_y
zv=(value_z / 1024.0*ADC_ref - zero_z) / self.sensitivity_z
#Calculate the angle between vectors Y and Z. *57.2957795 is for conversion from radians to degrees.
#+180 is the offset.
angle_x =math.atan2(-yv,-zv)*57.2957795+180
return angle_x;
def readRawGyroX(self):
output = self.read_reg(0x22)
return output
gyro = LSM6DS3()
while True:
print(gyro.readRawAccelX())
任何对grovepi +和芯片有帮助的东西都会受到赞赏。