树莓派4B读取MPU6050数据(iic)

要安装 wiringPi ,安装方法百度

#include <wiringPiI2C.h>
#include <stdlib.h>
#include <stdio.h>
#include <wiringPi.h>

#define Device_Address 0x68	/*Device Address/Identifier for MPU6050*/

#define PWR_MGMT_1   0x6B
#define SMPLRT_DIV   0x19
#define CONFIG       0x1A
#define GYRO_CONFIG  0x1B
#define INT_ENABLE   0x38
#define ACCEL_XOUT_H 0x3B
#define ACCEL_YOUT_H 0x3D
#define ACCEL_ZOUT_H 0x3F
#define GYRO_XOUT_H  0x43
#define GYRO_YOUT_H  0x45
#define GYRO_ZOUT_H  0x47

int fd;

void MPU6050_Init()
{
	
	wiringPiI2CWriteReg8 (fd, SMPLRT_DIV, 0x07);	/* Write to sample rate register */
	wiringPiI2CWriteReg8 (fd, PWR_MGMT_1, 0x01);	/* Write to power management register */
	wiringPiI2CWriteReg8 (fd, CONFIG, 0);		    /* Write to Configuration register */
	wiringPiI2CWriteReg8 (fd, GYRO_CONFIG, 24);	    /* Write to Gyro Configuration register */
	wiringPiI2CWriteReg8 (fd, INT_ENABLE, 0x01);	/*Write to interrupt enable register */
} 

short read_raw_data(int addr)
{
	short high_byte,low_byte,value;
	high_byte = wiringPiI2CReadReg8(fd, addr);
	low_byte = wiringPiI2CReadReg8(fd, addr+1);
	value = (high_byte << 8) | low_byte;
	return value;
}

void ms_delay(int val)
{
	int i,j;
	for(i=0;i<=val;i++)
		for(j=0;j<1200;j++);
}

int main()
{
	
	float Acc_x,Acc_y,Acc_z;
	float Gyro_x,Gyro_y,Gyro_z;
	float Ax=0, Ay=0, Az=0;
	float Gx=0, Gy=0, Gz=0;
	fd = wiringPiI2CSetup(Device_Address);   /*Initializes I2C with device Address*/
	MPU6050_Init();		                 /* Initializes MPU6050 */
	
	while(1)
	{
		/*Read raw value of Accelerometer and gyroscope from MPU6050*/
		Acc_x = read_raw_data(ACCEL_XOUT_H);
		Acc_y = read_raw_data(ACCEL_YOUT_H);
		Acc_z = read_raw_data(ACCEL_ZOUT_H);
		
		Gyro_x = read_raw_data(GYRO_XOUT_H);
		Gyro_y = read_raw_data(GYRO_YOUT_H);
		Gyro_z = read_raw_data(GYRO_ZOUT_H);
		
		/* Divide raw value by sensitivity scale factor */
		Ax = Acc_x/16384.0;
		Ay = Acc_y/16384.0;
		Az = Acc_z/16384.0;
		
		Gx = Gyro_x/131;
		Gy = Gyro_y/131;
		Gz = Gyro_z/131;
		
		printf("\n Gx=%.3f °/s\tGy=%.3f °/s\tGz=%.3f °/s\tAx=%.3f g\tAy=%.3f g\tAz=%.3f g\n",Gx,Gy,Gz,Ax,Ay,Az);
		ms_delay(500);
	}
	return 0;
}

project(wdd_robot)
cmake_minimum_required(VERSION 3.18)
#指定头文件目录,PROJECT_SOURCE_DIR为工程的根目录  
include_directories(${PROJECT_SOURCE_DIR}/include)

#可执行文件输出路径 
set(EXECUTABLE_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/bin)

#库文件输出路径  
set(LIBRARY_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/lib)

#指定库所在路径
link_directories("/home/wdd/wdd_robot/lib")

#生成动态库    
add_library(share_mpu6050 SHARED src/mpu6050.c)

#生成静态库  
add_library(static_mpu6050 STATIC src/mpu6050.c)

#生成可执行文件
add_executable(mpu6050 src/mpu6050.c)
#链接库,将*.so链接到可执行文件
target_link_libraries(mpu6050 share_mpu6050  -lpthread -lwiringPi)
target_link_libraries(mpu6050)

### 使用树莓派4B控制MPU6050传感器 #### 安装依赖包 为了使树莓派能够与 MPU6050 进行通信并读取数据,需要先安装 I2C 工具以及 Python 的 smbus 库。可以利用以下命令完成这些工具的安装: ```bash sudo apt-get update sudo apt-get install python-smbus i2c-tools ``` 着要启用I2C口,在Raspberry Pi Configuration中选择Interfaces选项卡,并开启I2C功能。 #### 配置硬件连 确保 MPU6050 正确线至树莓派上的 GPIO 口。通常情况下 VCC 和 GND 分别到 3.3V 和地针脚上,而 SDA (数据线) 和 SCL (时钟线) 则对应到 GPIO 上标记为 I2C 的两个引脚[^1]。 #### 编写Python程序获取IMU数据 下面给出一段简单的 Python 脚本用来初始化 MPU6050 并持续打印其加速度计和陀螺仪测量值: ```python import smbus import time # 初始化i2c总线, 创建一个smbus实例 bus = smbus.SMBus(1) # MPU6050 地址 address = 0x68 # 默认地址 def read_word_2c(addr): high = bus.read_byte_data(address, addr) low = bus.read_byte_data(address, addr+1) val = (high << 8) + low if (val >= 0x8000): # 如果最高位是1,则表示负数 return -((65535 - val) + 1) else: return val if __name__ == "__main__": # 设置MPU6050工作模式 bus.write_byte_data(address, 0x6b, 0) try: while True: accel_xout = read_word_2c(0x3b) accel_yout = read_word_2c(0x3d) accel_zout = read_word_2c(0x3f) gyro_xout = read_word_2c(0x43) gyro_yout = read_word_2c(0x45) gyro_zout = read_word_2c(0x47) print(f'Accel X:{accel_xout}, Y:{accel_yout}, Z:{accel_zout}') print(f'Gyro X:{gyro_xout}, Y:{gyro_yout}, Z:{gyro_zout}\n') time.sleep(0.5) except KeyboardInterrupt: pass ``` 这段代码展示了如何设置 MPU6050 设备进入正常运行状态,并周期性地查询加速计和角速率传感器的数据输出寄存器以获得当前姿态信息。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值