ESP32/MPU-9250 losing accuracy

derasd
Posts: 4
Joined: Tue Oct 23, 2018 7:43 am

ESP32/MPU-9250 losing accuracy

Postby derasd » Tue Oct 23, 2018 8:15 am

Hi, I'm using an ESP32 in connection with an MPU-9250 to measure acceleration over SPI. Interrupts are used to measure 10'000 samples. Those are sent over WIFI and the process is started again. After around 100+ measurements the sensor suddenly loses accuracy (and also gets a small offset compared to the normal measurement).

This is how it normally looks
meas1.png
meas1.png (7.54 KiB) Viewed 6365 times
This is how it looks after
meas2.png
meas2.png (6.98 KiB) Viewed 6365 times
The hex values, depending on the sensors position, look something like this:

Code: Select all

00DF 00DF 00DF 00DF 00E0 00DF 00DF 00DF 00DF 00E0 00E0 00DF 00E0 00DF 00DF 00DF 00E0 00E0 00E0 00DF 00DF 00E0 00DF 00DF 
But when I inspect the data coming from the MPU-9250 with an oscilloscope it looks like valid data.

This only happens around 50% of the time, the other 50% the ESP core dumps first. I'm not sure if this is related but I need to fix it as well. This core dump happend after 386 measurements.

Code: Select all

Guru Meditation Error: Core  0 panic'ed (LoadProhibited). Exception was unhandled.
Core 0 register dump:
PC      : 0x40083f0c  PS      : 0x00060031  A0      : 0x4008206d  A1      : 0x3ffb05d0
0x40083f0c: spi_intr at C:/msys32/esp-idf/components/driver/spi_master.c:433

0x4008206d: _xt_lowint1 at C:/msys32/esp-idf/components/freertos/xtensa_vectors.S:1105

A2      : 0x3ffc43a4  A3      : 0x3ffc43b8  A4      : 0x3ffb0570  A5      : 0x3ffc441c
A6      : 0x3ffb8570  A7      : 0x3ffb3684  A8      : 0x80083f05  A9      : 0x9fffcffc
A10     : 0x4001f140  A11     : 0x00000000  A12     : 0xc8746060  A13     : 0x00000001
A14     : 0x00060021  A15     : 0x00060021  SAR     : 0x0000001a  EXCCAUSE: 0x0000001c
EXCVADDR: 0x9fffd104  LBEG    : 0x4000c2e0  LEND    : 0x4000c2f6  LCOUNT  : 0xffffffff
Core 0 was running in ISR context:
EPC1    : 0x40083f0c  EPC2    : 0x00000000  EPC3    : 0x00000000  EPC4    : 0x00000000
0x40083f0c: spi_intr at C:/msys32/esp-idf/components/driver/spi_master.c:433


Backtrace: 0x40083f0c:0x3ffb05d0 0x4008206a:0x3ffb0600 0x4008cf52:0x00000000
0x40083f0c: spi_intr at C:/msys32/esp-idf/components/driver/spi_master.c:433

0x4008206a: _xt_lowint1 at C:/msys32/esp-idf/components/freertos/xtensa_vectors.S:1105

0x4008cf52: xQueueGenericReceive at C:/msys32/esp-idf/components/freertos/queue.c:2037


CPU halted.

Has anyone seen this before?
Please let me know what more information I can provide.

*Edit*: It was different yesterday but today the core dump is much more of an issue than losing the accuracy.

User avatar
luisonoff
Posts: 40
Joined: Fri Feb 09, 2018 12:20 pm

Re: ESP32/MPU-9250 losing accuracy

Postby luisonoff » Tue Oct 23, 2018 11:55 am

Post code of your SPI driver (if any apart from IDF) and the functions using this SPI driver.

ESP_Sprite
Posts: 9772
Joined: Thu Nov 26, 2015 4:08 am

Re: ESP32/MPU-9250 losing accuracy

Postby ESP_Sprite » Wed Oct 24, 2018 2:28 am

Agreed. Smells like a SPI transaction struct somehow got corrupted.

derasd
Posts: 4
Joined: Tue Oct 23, 2018 7:43 am

Re: ESP32/MPU-9250 losing accuracy

Postby derasd » Wed Oct 24, 2018 7:05 am

Hi, thanks for your help. Here is the SPI code.

*Edit* Made some changes to the code, performance and readability should be better now. Problem still persists, especially the one from the post below.

Code: Select all


//This function is called with every interrupt
static void IRAM_ATTR mpu_isr(void *discard)
{
  spi_transaction_t t = {};
  uint8_t tx_buffer[2] = {0x00, 0x00};
  t.length = 2 * 8;
  t.flags = SPI_TRANS_USE_RXDATA; 
  t.tx_buffer = tx_buffer;
  t.cmd = MPUREG_ACCEL_XOUT_H | READ_FLAG;                                      
  spi_device_queue_trans(mpu9250_spi_handle, &t, 0);
}

void task_mpu9250(void *pvParameters)
{
  // Create a queue capable of containing 20 int16 values.
  data_queue = xQueueCreate(20, sizeof(int16_t));

  mpu9250_init_spi();
  // Check if connection to Sensor is ok
  if (mpu9250_whoami())
  {
    mpu9250_init();
    gpio_set_intr_type(PIN_MPU_INT, GPIO_INTR_NEGEDGE); 
    gpio_install_isr_service(0);
    gpio_isr_handler_add(PIN_MPU_INT, mpu_isr, (void *)PIN_MPU_INT);
  }
  vTaskDelete(NULL);
}

// This is called as a post SPI transaction 
static void IRAM_ATTR mpu9250_post_spi_trans(spi_transaction_t *t)
{
  BaseType_t xHigherPriorityTaskWoken;

  if (t->cmd == (MPUREG_ACCEL_XOUT_H | READ_FLAG))
  {
    //Writes x-values into data_buffer and sends them to the queue
    data_buffer = ((int16_t)t->rx_data[0] << 8) | t->rx_data[1];
    xQueueSendToBackFromISR(data_queue, &data_buffer, &xHigherPriorityTaskWoken);
  }
}

void mpu9250_init_spi()
{
  esp_err_t ret;
  spi_bus_config_t buscfg = {.miso_io_num = PIN_SPI_SDI,
                             .mosi_io_num = PIN_SPI_SDO,
                             .sclk_io_num = PIN_SPI_SCK,
                             .quadwp_io_num = -1,
                             .quadhd_io_num = -1};
  spi_device_interface_config_t devcfg = {
      .command_bits = 8,                
      .dummy_bits = 0,                  
      .mode = 3,                        
      .clock_speed_hz = 1000000,       
      .spics_io_num = PIN_MPU_CS,     
      .queue_size = 7,                 
      .post_cb = mpu9250_post_spi_trans 
                                       
                                        
  };
  ret = spi_bus_initialize(HSPI_HOST, &buscfg, 1); 
  assert(ret == ESP_OK);
  ESP_LOGI(TAG, "... Initializing bus.");
  ret = spi_bus_add_device(HSPI_HOST, &devcfg, &mpu9250_spi_handle);
  assert(ret == ESP_OK);
  ESP_LOGI(TAG, "... Adding device bus.");
}
Last edited by derasd on Fri Nov 02, 2018 5:51 pm, edited 1 time in total.

derasd
Posts: 4
Joined: Tue Oct 23, 2018 7:43 am

Re: ESP32/MPU-9250 losing accuracy

Postby derasd » Thu Nov 01, 2018 3:28 pm

Anyone sees a problem with the code?

Something else I've got is that from time to time the sensor returns a value of exactly 0

Code: Select all

t->rx_data[0] = 0
which is not correct. When I analyze the data on the SPI bus with an oscilloscope there is always a value returned.


Edit: Problem has been solved by using spi_device_polling_transmit() instead of spi_device_queue_trans()

Who is online

Users browsing this forum: No registered users and 65 guests