Quantcast
Channel: Raspberry Pi Forums
Viewing all articles
Browse latest Browse all 7160

Beginners • Acceleration and Gyroscope read 0 on the ICM 20468

$
0
0
I'm trying to read from the ICM 20948 using a Pico W, however, whenever I read from the acceleration and gyroscope addresses I get 0. What could be the problem?
imu.c

Code:

void init_imu(IMU* imu) {    gpio_set_function(imu->miso, GPIO_FUNC_SPI);    gpio_set_function(imu->mosi, GPIO_FUNC_SPI);    gpio_set_function(imu->clk, GPIO_FUNC_SPI);    gpio_init(imu->cs);    gpio_set_dir(imu->cs, GPIO_OUT);    gpio_put(imu->cs, 1);    uint8_t temp_data;    enable_imu(imu);        // Reset IMU    write_reg(imu, PWR_MGMT_1, 0xc1, b0);    sleep_ms(100);    // Wake up from sleep    write_reg(imu, PWR_MGMT_1, 0x01, b0);    // output data rate start time alignment    write_reg(imu, ODR_ALIGN_EN, 0x01, b2);    // gyroscope config sample rate divisor    write_reg(imu, GYRO_SMPLRT_DIV, 0x00, b2);    // gyroscope range and disable filtering    write_reg(imu, GYRO_CONFIG_1, GYRO_RANGE << 1, b2);    // acclerometer sample rate diivider    write_reg(imu, ACCEL_SMPLRT_DIV_1, 0x00, b2);    write_reg(imu, ACCEL_SMPLRT_DIV_2, 0x00, b2);    // Accelerometer configuration, sample rate divider = 0 and disabled filtering    write_reg(imu, ACCEL_CONFIG, ACCEL_RANGE << 1, b2);    // Set to SPI mode only    read_reg(imu, USER_CTRL, b0, &temp_data, 1);    temp_data |= 0x10;    write_reg(imu, USER_CTRL, temp_data, b2);    select_bank(imu, b0);    disable_imu(imu);}void get_data(IMU* imu, Sample* smp) {    uint8_t data_rx[12];    uint8_t temp_data = READ_BIT | ACCEL_XOUT_H;    enable_imu(imu);    read_reg(imu, temp_data, b0, data_rx, 12);    smp->accel_x = (((int16_t) data_rx[0] << 8)) + data_rx[1];    smp->accel_y = (((int16_t) data_rx[2] << 8)) + data_rx[3];    smp->accel_z = (((int16_t) data_rx[4] << 8)) + data_rx[5];    smp->gyro_x = (((int16_t) data_rx[6] << 8)) + data_rx[7];    smp->gyro_y = (((int16_t) data_rx[8] << 8)) + data_rx[9];    smp->gyro_z = (((int16_t) data_rx[10] << 8)) + data_rx[11];    disable_imu(imu);}void wait() {    sleep_us(500);}void enable_imu(IMU* imu) {    gpio_put(imu->cs, 0);    wait();}void disable_imu(IMU* imu) {    gpio_put(imu->cs, 1);    wait();}void write_reg(IMU* imu, uint8_t reg, uint8_t value, user_bank usr_bnk) {    select_bank(imu, usr_bnk);    uint8_t data[2];    data[0] = reg;    data[1] = value;    spi_write_blocking(SPI_PORT, data, 2);    wait();}void read_reg(IMU* imu, uint8_t reg, user_bank usr_bnk, uint8_t* buffer, uint buffer_size) {    select_bank(imu, usr_bnk);    uint8_t send_read_bit = reg | READ_BIT;    spi_write_blocking(SPI_PORT, &send_read_bit, 1);    wait();    spi_read_blocking(SPI_PORT, 0, buffer, buffer_size);}void select_bank(IMU* imu, user_bank usr_bnk) {    uint8_t data[2];    data[0] = BANK_SEL_REGISTER;    data[1] = (uint8_t) usr_bnk;    spi_write_blocking(SPI_PORT, data, 2);    wait();}
main.c

Code:

int main(){    stdio_init_all();    spi_init(SPI_PORT, FREQ);    IMU yellow_imu = {        .miso = YELLOW_MISO,        .mosi = YELLOW_MOSI,        .clk  = YELLOW_SCLK,        .cs   = YELLOW_CS    };    IMU green_imu = {        .miso = GREEN_MISO,        .mosi = GREEN_MOSI,        .clk  = GREEN_SCLK,        .cs   = GREEN_CS    };    IMU red_imu = {        .miso = RED_MISO,        .mosi = RED_MOSI,        .clk  = RED_SCLK,        .cs   = RED_CS    };    Sample curr_smp;    init_imu(&yellow_imu);    init_imu(&green_imu);    init_imu(&red_imu);    while(1){        get_data(&yellow_imu, &curr_smp);        printf("\nAcceleration x: %.4f", curr_smp.accel_x);        sleep_ms(100);    }}

Statistics: Posted by jayson12000 — Mon Aug 19, 2024 9:02 am — Replies 0 — Views 9



Viewing all articles
Browse latest Browse all 7160

Trending Articles