nRF52832-Bluefruit52 Learning External Development--MPU6050 Six Axis Module

Keywords: angular

nRF52832 Technical Exchange Group: 680723714

       nRF52832-Bluefruit52 Core Board Details:

https://blog.csdn.net/solar_Lan/article/details/88688451

* nRF52832 drives MPU6050

nRF52832-Bluefruit52 Bluetooth core board MPU6050 chip, connected to I2C bus, SCL--P 0.26, SDA--P 0.25.

MPU-6050 has a full-range angular velocity sensation of +250, +500, +1000 and +2000 degrees/sec (dps). Fast and slow motion can be tracked accurately. User-programmable full-range sensation of accelerators is +2g, +4g +8g and +16g.The product can be transmitted through I2C up to 400kHz.MPU-6050 can operate at different voltages, VDD power supply voltage ranges from 2.5V +5%, 3.0V +5% or 3.3V +5%, VDDIO power supply for logical interface is 1.8V +5% (MPU6000 only uses VDD).The packaging size of MPU-6050 is 4x4x0.9mm(QFN), which is revolutionary in the industry.Other features include a built-in temperature sensor and an oscillator that varies by only (+) 1% in the operating environment.

There are applications in many areas, such as smartphones, tablets, handheld game products, game consoles, 3D somatosensory remote controls, portable navigation devices, and so on.Bluetooth is used in a wider range of scenarios, such as wireless mouse, wireless game handle, wireless 3D remote control, pedometer, wearable devices, and so on.

On the I2C driver of the chip, simply speaking, MPU6050 is an I2C slave device, just like a microcontroller, to use it, you need to know registers, initialization, reading, writing, etc.This time, using nRF52832 as the master chip, using hardware I2C to drive MPU6050, and combining with anonymous four-axis upper machine, the data of posture resolution can be monitored in real time through serial port protocol, which has great reference significance for the subsequent development of the product.

There are three main driver files of MPU6050, which are ported in combination with the serial routine of nRF52832 chip.

Main main function source:

/**
 * @brief Function for main application entry.
 */
int main(void)
{
    int16_t AccValue[3],GyroValue[3];
	uint8_t id;
	float pitch,roll,yaw; 			//Angle of Euler
	short aacx,aacy,aacz;		  	//Acceleration sensor raw data
	short gyrox,gyroy,gyroz;		//Raw gyroscope data
	
	nrf_gpio_cfg_output(LED_1);		//Configure pin P 0.17 as output, drive indicator D1
	nrf_gpio_pin_set(LED_1);   		//Set indicator D1 initial state to off
	
    uart_init();  //Configure serial port, no flow control, baud rate: 115200	
		
	twi_master_init();
	
	nrf_delay_ms(2000);
	if(mpu6050_init(0x68) == false)
	{
		while (true)
		{
			printf("mpu6050 init fail\r\n");
			nrf_delay_ms(500);
		}	
	}
	else
	{	
		nrf_delay_ms(1000);
		printf("mpu6050 init ok\r\n");
	}
		
    mpu6050_register_read(0x75U, &id, 1);
	printf("mpu6050 id is %d \r\n",id);
		
	while(mpu_dmp_init())
	{
		nrf_delay_ms(1000);
		printf("mpu6050 init Error\r\n");
	}

    while (true)
    {
		if(mpu_dmp_get_data(&pitch,&roll,&yaw)==0)
		{ 
			MPU6050_ReadAcc(&aacx,&aacy,&aacz);	    //Reading Acceleration Sensor Data
			MPU6050_ReadGyro(&gyrox,&gyroy,&gyroz);	//Reading gyroscope data
			mpu6050_send_dat(aacx,aacy,aacz,gyrox,gyroy,gyroz);//Send acceleration and gyro raw data with custom frames
			Uart_ReportIMU(aacx,aacy,aacz,gyrox,gyroy,gyroz,(int)(roll*100),(int)(pitch*100),(int)(yaw*10));
            nrf_delay_ms(50);
		}
    }
#else

    // This part of the example is just for testing the loopback .
    while (true)
    {
        uart_loopback_test();
    }
#endif
}

To support anonymous quad-axis upper machine, we need data protocol specification for serial data:

/***********************************************************************************
 * Description: Serial port sends data in anonymous 4-axis upper computer software (V2.6) data format
 * Entry: fun: function code
 *        : dat:Data cache address, up to 28 bytes
 *        : len:Data length, maximum 28 bytes
 * Return value: None
 **********************************************************************************/ 
void Uart_SendDat_ToPC(uint8_t fun,uint8_t *dat,uint8_t len)
{
	uint8_t send_buf[32];
	uint8_t i;
	if(len>28)return;	  //Up to 28 bytes of data 
	send_buf[len+3]=0;	//Zero check number
	send_buf[0]=0x88;	  //Frame Header
	send_buf[1]=fun;	  //Function Code
	send_buf[2]=len;	  //Data Length
	for(i=0;i<len;i++)send_buf[3+i]=dat[i];			      //Copy data
	for(i=0;i<len+3;i++)send_buf[len+3]+=send_buf[i];	//Check Summing	
	for(i=0;i<len+4;i++)app_uart_put(send_buf[i]);	  //Serial Output Data
}
/***********************************************************************************
 * Description: Send acceleration sensor data and gyroscope data
 * Involvement: Acceleration values in the three directions aacx,aacy,aacz:x,y,z
 *          gyrox,gyroy,gyroz:x,y,z Gyroscope values in three directions
 * Return value: None
 **********************************************************************************/
void mpu6050_send_dat(short aacx,short aacy,short aacz,short gyrox,short gyroy,short gyroz)
{
	uint8_t tx_buf[12]; 
	tx_buf[0]=(aacx>>8)&0xFF;
	tx_buf[1]=aacx&0xFF;
	tx_buf[2]=(aacy>>8)&0xFF;
	tx_buf[3]=aacy&0xFF;
	tx_buf[4]=(aacz>>8)&0xFF;
	tx_buf[5]=aacz&0xFF; 
	tx_buf[6]=(gyrox>>8)&0xFF;
	tx_buf[7]=gyrox&0xFF;
	tx_buf[8]=(gyroy>>8)&0xFF;
	tx_buf[9]=gyroy&0xFF;
	tx_buf[10]=(gyroz>>8)&0xFF;
	tx_buf[11]=gyroz&0xFF;
	Uart_SendDat_ToPC(0xA1,tx_buf,12);//Custom Frame, 0XA1
}	
/***********************************************************************************
 * Description: Serial upload of MPU6050 posture data
 * Involvement: Acceleration values in the three directions aacx,aacy,aacz:x,y,z
 *        : gyrox,gyroy,gyroz:x,y,z Gyroscope values in three directions
 *        : roll:Roll angle. Unit 0.01 degrees.-18000 -> 18000 corresponds to-180.00 -> 180.00 degrees
 *        : pitch:Pitch. Unit 0.01 degrees.-9000 - 9000 corresponds to -90.00 -> 90.00 degrees
 *        : yaw:Direction angle. Unit 0.1 degrees 0 -> 3600 corresponds to 0 -> 360.0 degrees
 * Return value: None
 **********************************************************************************/ 
void Uart_ReportIMU(short aacx,short aacy,short aacz,short gyrox,short gyroy,short gyroz,short roll,short pitch,short yaw)
{
	uint8_t i,tx_buf[28]; 

	for(i=0;i<28;i++)tx_buf[i]=0;//Clear 0
	tx_buf[0]=(aacx>>8)&0xFF;
	tx_buf[1]=aacx&0xFF;
	tx_buf[2]=(aacy>>8)&0xFF;
	tx_buf[3]=aacy&0xFF;
	tx_buf[4]=(aacz>>8)&0xFF;
	tx_buf[5]=aacz&0xFF; 
	tx_buf[6]=(gyrox>>8)&0xFF;
	tx_buf[7]=gyrox&0xFF;
	tx_buf[8]=(gyroy>>8)&0xFF;
	tx_buf[9]=gyroy&0xFF;
	tx_buf[10]=(gyroz>>8)&0xFF;
	tx_buf[11]=gyroz&0xFF;	
	tx_buf[18]=(roll>>8)&0xFF;
	tx_buf[19]=roll&0xFF;
	tx_buf[20]=(pitch>>8)&0xFF;
	tx_buf[21]=pitch&0xFF;
	tx_buf[22]=(yaw>>8)&0xFF;
	tx_buf[23]=yaw&0xFF;
	Uart_SendDat_ToPC(0xAF,tx_buf,28);//Anonymous four-axis flight control display frame, 0xAF
} 

After the porting of MPU6050 and the writing of serial protocol, we can use keil to compile and download.Open the V2.6 version of the anonymous quad-axis upper machine and open the serial port to see the interactive interface:

You can see that MPU6050's DMP function has been transplanted in our transplant file. It is very convenient to calculate the Euler angle of pitch, roll and yaw directly.

 

There are many interesting cases to follow with the Bluetooth feature: pedometer, air mouse, 3D game handle, 3D remote control, gesture remote control and so on.

Posted by stretchy on Fri, 02 Aug 2019 18:51:28 -0700