首页 > 其他分享 >树莓派读取mpu6050

树莓派读取mpu6050

时间:2023-05-07 11:11:05浏览次数:33  
标签:树莓 读取 read bus mpu6050 raw Address GYRO data

 

开启i2c

sudo raspi-config

 

  • In Interfacing option, Select -> I2C

 

 

 

查看id

 ls /dev/*i2c*

 

查看地址

 sudo i2cdetect -y 1

 

Scan or Test I2C device on Raspberry Pi

Now, we can test/scan for any I2C device connected to our Raspberry Pi board by installing i2c tools. We can get i2c tools by using apt package manager. Use following command in Raspberry Pi terminal.

sudo apt-get install -y i2c-tools
sudo apt-get install python-smbus

 

 

采集代码

'''
    Read Gyro and Accelerometer by Interfacing Raspberry Pi with MPU6050 using Python
	http://www.electronicwings.com
'''
import smbus			#import SMBus module of I2C
from time import sleep          #import

#some MPU6050 Registers and their Address
PWR_MGMT_1   = 0x6B
SMPLRT_DIV   = 0x19
CONFIG       = 0x1A
GYRO_CONFIG  = 0x1B
INT_ENABLE   = 0x38
ACCEL_XOUT_H = 0x3B
ACCEL_YOUT_H = 0x3D
ACCEL_ZOUT_H = 0x3F
GYRO_XOUT_H  = 0x43
GYRO_YOUT_H  = 0x45
GYRO_ZOUT_H  = 0x47


def MPU_Init():
	#write to sample rate register
	bus.write_byte_data(Device_Address, SMPLRT_DIV, 7)
	
	#Write to power management register
	bus.write_byte_data(Device_Address, PWR_MGMT_1, 1)
	
	#Write to Configuration register
	bus.write_byte_data(Device_Address, CONFIG, 0)
	
	#Write to Gyro configuration register
	bus.write_byte_data(Device_Address, GYRO_CONFIG, 24)
	
	#Write to interrupt enable register
	bus.write_byte_data(Device_Address, INT_ENABLE, 1)

def read_raw_data(addr):
	#Accelero and Gyro value are 16-bit
        high = bus.read_byte_data(Device_Address, addr)
        low = bus.read_byte_data(Device_Address, addr+1)
    
        #concatenate higher and lower value
        value = ((high << 8) | low)
        
        #to get signed value from mpu6050
        if(value > 32768):
                value = value - 65536
        return value


bus = smbus.SMBus(1) 	# or bus = smbus.SMBus(0) for older version boards
Device_Address = 0x68   # MPU6050 device address

MPU_Init()

print (" Reading Data of Gyroscope and Accelerometer")

while True:
	
	#Read Accelerometer raw value
	acc_x = read_raw_data(ACCEL_XOUT_H)
	acc_y = read_raw_data(ACCEL_YOUT_H)
	acc_z = read_raw_data(ACCEL_ZOUT_H)
	
	#Read Gyroscope raw value
	gyro_x = read_raw_data(GYRO_XOUT_H)
	gyro_y = read_raw_data(GYRO_YOUT_H)
	gyro_z = read_raw_data(GYRO_ZOUT_H)
	
	#Full scale range +/- 250 degree/C as per sensitivity scale factor
	Ax = acc_x/16384.0
	Ay = acc_y/16384.0
	Az = acc_z/16384.0
	
	Gx = gyro_x/131.0
	Gy = gyro_y/131.0
	Gz = gyro_z/131.0
	

	print ("Gx=%.2f" %Gx, u'\u00b0'+ "/s", "\tGy=%.2f" %Gy, u'\u00b0'+ "/s", "\tGz=%.2f" %Gz, u'\u00b0'+ "/s", "\tAx=%.2f g" %Ax, "\tAy=%.2f g" %Ay, "\tAz=%.2f g" %Az) 	
	sleep(1)

  

 

标签:树莓,读取,read,bus,mpu6050,raw,Address,GYRO,data
From: https://www.cnblogs.com/gooutlook/p/17379040.html

相关文章

  • 贴个IIC的代码和MPU6050寄存器地址的文档(MOVE版)
    IIC代码:里面的UV_Delay()那几个要自己定义一下。还有那几个子函数也要自己定义在一个头文件里面。比如:#ifndef_IIC_H#define_IIC_H#defineUV_Delay()delay5us()#defineUV_SDA_SET()P2^0=1#defineUV_SDA_......
  • python 串口读取IMU
    #coding:UTF-8#Version:V1.0.1importserialACCData=[0.0]*8GYROData=[0.0]*8AngleData=[0.0]*8FrameState=0#WhatisthestateofthejudgmentBytenum=0#ReadthenumberofdigitsinthisparagraphCheckSum=0#Sumcheckbita=[0.0......
  • java filter过滤器 读取配置文件properties的值
    http://www.yayihouse.com/yayishuwu/chapter/29811.获取application.properties的值如userId=1 2.一般实体中采用@Value既可获取@Value("userIdl")privateStringuserId; 但是在filter中,需要用上下文对象来获取filter的生命周期如下:web应用程序启动时,web服务器将创......
  • webservie 客户端读取服务器端日志例子(以网页展现)
    importjava.io.BufferedInputStream;下面是一个完整的servlet,直接复制它既可以使用,只需要修改红色部分路径即可,本例使用方法:在浏览器直接键入URL:即可展现日志,如下:http://localhost:8888/BPMDemo/BPMClientLogService?point=p1代码:importjava.io.BufferedReader;imp......
  • web开发总结----xml的写入、读取---2
    publicclassDOM4JTest{/***//***//***//***//***//***//***//***DOM4J读写XML示例**@paramargs*@throwsException*/publicstaticvoidmain(String[]args){try{XMLWriter......
  • python 串口读取gps
    #coding:utf-8#lastmodified:20220824importtimeimportserialimportre utctime=''lat=''ulat=''lon=''ulon=''numSv=''msl=''cogt=''cogm='&#......
  • Python实现遍历读取文件或文件夹
    参考:https://www.jb51.net/article/258341.htmos.walk本身已经是遍历读取,包含所有的子文件(夹)path=u'.'#文件路径defnewWalkFile2(file):#main_dir当前路径,sub_dir_list当前路径下的子文件夹是个数组,sub_file_list当前路径下具体文件formain_dir,sub_dir_l......
  • C#通过Spire.OCR读取图片文字
    1、项目属性修改首先创建一个winform窗体程序,然后将其目标平台属性修改为【×64】,【Spire.OCR】只支持【×64】,所以这一步不能少  2、添加引用通过管理Nuget包实现添加引用步骤 3、包安装完成后将包中dll复制到项目文件夹>bin>Debug目录下 4、代码中实现选择图片并......
  • 兼容 windows 和 Linux 的fgets文件读取方式(判断文件尾)
    /*检测文件尾范例使用fgets读取文件*//*范例:检测文件尾,windows和linux,unix平台兼容版本*/#include<stdio.h>#include<stdlib.h>#defineBUFSIZE100intmain(void){FILE*f;charstr[BUFSIZE];char*result;//读方式打开文件hello.txt,如果文件不......
  • 【pandas基础】--数据读取
    数据读取是第一步,只有成功加载数据之后,后续的操作才有可能。pandas可以读取和导入各种数据格式的数据,如CSV,Excel,JSON,SQL,HTML等,不需要手动编写复杂的读取代码。1.各类数据源pandas提供了导入各类常用文件格式数据的接口,这里介绍3种最常用的加载数据的接口。1.1从CSV文件读......