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)
## C 语言
使用WiringPi的C语言库来从MPU6050模块读取数据
mpu6050.c:
/* MPU6050 Interfacing with Raspberry Pi www.electronicwings.com */
#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);
delay(500);
}
return 0;
}
### 编译运行
gcc -lwiringPi mpu6050.c ./a.out
### 输出
命令行中将显示以下数据
Gx = Gyro X-axis data in degree/seconds Gy = Gyro Y-axis data in degree/seconds
既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上物联网嵌入式知识点,真正体系化!
由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、电子书籍、讲解视频,并且后续会持续更新