Read IMU data

1. Experimental purpose

Use the IMU attitude sensor chip of the microROS control board to learn the function of ESP32 to read IMU device data through the I2C interface.

 

2. Hardware connection

As shown in the figure below, the microROS control board integrates the IMU attitude sensor chip. There is no need to connect additional external devices. You only need to connect the type-C data cable to the computer and the microROS control board for the firmware burning function.

image-20240112142258274

 

3. Core code analysis

The virtual machine path corresponding to the program source code is as follows:

The IMU sensor chip uses the ICM42670P chip. The ICM42670P chip uses I2C communication to transmit data, so ESP32 needs to be initialized as an I2C host first. I2C_MASTER_GPIO_SDA corresponds to hardware GPIO40, I2C_MASTER_GPIO_SCL corresponds to hardware GPIO39, and the I2C setting communication frequency is 400KHz.

I2C reads a string of data.

I2C reads a byte.

I2C writes a string of data

I2C writes a byte.

The interrupt pin of ICM42670P corresponds to hardware GPIO41, the mode is set to input mode, and an interrupt callback is added.

The interrupt callback is triggered when the data of ICM42670P is ready.

ICM42670P needs to first set the bound I2C read data callback and determine whether the WHO_AM_I flag of the chip is correct.

Next, configure the relevant parameters of ICM42670P. The accelerometer range is selected as ±4g, the gyroscope range is ±2000dps, the output frequency is 400hz, and low noise mode is allowed.

Create a new task for ICM42670P to update data. After ICM42670P prepares the data, it will read the data in time.

Get the raw data of the ICM42670P accelerometer.

Get the raw data of the gyroscope of ICM42670P.

Get the scaled data of the accelerometer of ICM42670P

Obtain the scaled data of the gyroscope of ICM42670P

Call the Icm42670p_Init function in app_main to initialize the IMU chip, and then read the IMU scaled accelerometer and gyroscope data every 200 milliseconds in the loop. Since the frequency of IMU data output is 400Hz, in order to read data accurately and effectively, the interval between reading data needs to be greater than 2.5 milliseconds.

 

4. Compile, download and flash firmware

Use a Type-C data cable to connect the virtual machine/computer and the microROS control board. If the system pops up, choose to connect to the virtual machine.

Activate the ESP-IDF development environment. Note that every time you open a new terminal, you need to activate the ESP-IDF development environment before compiling the firmware.

Enter the project directory

Compile, flash, and open the serial port simulator

If you need to exit the serial port simulator, press Ctrl+].

 

5. Experimental results

The serial port simulator prints the "hello yahboom" greeting. And print the accelerometer and gyroscope scaled data every 200 milliseconds. At this time, shake the microROS control board and you can see that the data will change.

image-20240112161838285