コード例 #1
0
ファイル: mpu9150.c プロジェクト: 4dahalibut/RIOT
int mpu9150_init(mpu9150_t *dev, i2c_t i2c, mpu9150_hw_addr_t hw_addr,
        mpu9150_comp_addr_t comp_addr)
{
    char temp;

    dev->i2c_dev = i2c;
    dev->hw_addr = hw_addr;
    dev->comp_addr = comp_addr;
    dev->conf = DEFAULT_STATUS;

    /* Initialize I2C interface */
    if (i2c_init_master(dev->i2c_dev, I2C_SPEED_FAST)) {
        DEBUG("[Error] I2C device not enabled\n");
        return -1;
    }

    /* Acquire exclusive access */
    i2c_acquire(dev->i2c_dev);

    /* Reset MPU9150 registers and afterwards wake up the chip */
    i2c_write_reg(dev->i2c_dev, dev->hw_addr, MPU9150_PWR_MGMT_1_REG, MPU9150_PWR_RESET);
    hwtimer_wait(HWTIMER_TICKS(MPU9150_RESET_SLEEP_US));
    i2c_write_reg(dev->i2c_dev, dev->hw_addr, MPU9150_PWR_MGMT_1_REG, MPU9150_PWR_WAKEUP);

    /* Release the bus, it is acquired again inside each function */
    i2c_release(dev->i2c_dev);

    /* Set default full scale ranges and sample rate */
    mpu9150_set_gyro_fsr(dev, MPU9150_GYRO_FSR_2000DPS);
    mpu9150_set_accel_fsr(dev, MPU9150_ACCEL_FSR_2G);
    mpu9150_set_sample_rate(dev, MPU9150_DEFAULT_SAMPLE_RATE);

    /* Disable interrupt generation */
    i2c_acquire(dev->i2c_dev);
    i2c_write_reg(dev->i2c_dev, dev->hw_addr, MPU9150_INT_ENABLE_REG, REG_RESET);

    /* Initialize magnetometer */
    if (compass_init(dev)) {
        i2c_release(dev->i2c_dev);
        return -2;
    }
    /* Release the bus, it is acquired again inside each function */
    i2c_release(dev->i2c_dev);
    mpu9150_set_compass_sample_rate(dev, 10);
    /* Enable all sensors */
    i2c_acquire(dev->i2c_dev);
    i2c_write_reg(dev->i2c_dev, dev->hw_addr, MPU9150_PWR_MGMT_1_REG, MPU9150_PWR_PLL);
    i2c_read_reg(dev->i2c_dev, dev->hw_addr, MPU9150_PWR_MGMT_2_REG, &temp);
    temp &= ~(MPU9150_PWR_ACCEL | MPU9150_PWR_GYRO);
    i2c_write_reg(dev->i2c_dev, dev->hw_addr, MPU9150_PWR_MGMT_2_REG, temp);
    i2c_release(dev->i2c_dev);
    hwtimer_wait(HWTIMER_TICKS(MPU9150_PWR_CHANGE_SLEEP_US));

    return 0;
}
コード例 #2
0
ファイル: main.c プロジェクト: AdamRLukaitis/RIOT
int main(void)
{
    mpu9150_t dev;
    mpu9150_results_t measurement;
    int32_t temperature;
    int result;

    puts("MPU-9150 test application\n");

    printf("+------------Initializing------------+\n");
    result = mpu9150_init(&dev, TEST_I2C, TEST_HW_ADDR, TEST_COMP_ADDR);

    if (result == -1) {
        puts("[Error] The given i2c is not enabled");
        return 1;
    }
    else if (result == -2) {
        puts("[Error] The compass did not answer correctly on the given address");
        return 1;
    }

    mpu9150_set_sample_rate(&dev, 200);
    if (dev.conf.sample_rate != 200) {
        puts("[Error] The sample rate was not set correctly");
        return 1;
    }
    mpu9150_set_compass_sample_rate(&dev, 100);
    if (dev.conf.compass_sample_rate != 100) {
        puts("[Error] The compass sample rate was not set correctly");
        return 1;
    }

    printf("Initialization successful\n\n");
    printf("+------------Configuration------------+\n");
    printf("Sample rate: %"PRIu16" Hz\n", dev.conf.sample_rate);
    printf("Compass sample rate: %"PRIu8" Hz\n", dev.conf.compass_sample_rate);
    printf("Gyro full-scale range: 2000 DPS\n");
    printf("Accel full-scale range: 2 G\n");
    printf("Compass X axis factory adjustment: %"PRIu8"\n", dev.conf.compass_x_adj);
    printf("Compass Y axis factory adjustment: %"PRIu8"\n", dev.conf.compass_y_adj);
    printf("Compass Z axis factory adjustment: %"PRIu8"\n", dev.conf.compass_z_adj);

    printf("\n+--------Starting Measurements--------+\n");
    while (1) {
        /* Get accel data in milli g */
        mpu9150_read_accel(&dev, &measurement);
        printf("Accel data [milli g] - X: %"PRId16"   Y: %"PRId16"   Z: %"PRId16"\n",
                measurement.x_axis, measurement.y_axis, measurement.z_axis);
        /* Get gyro data in dps */
        mpu9150_read_gyro(&dev, &measurement);
        printf("Gyro data [dps] - X: %"PRId16"   Y: %"PRId16"   Z: %"PRId16"\n",
                measurement.x_axis, measurement.y_axis, measurement.z_axis);
        /* Get compass data in mikro Tesla */
        mpu9150_read_compass(&dev, &measurement);
        printf("Compass data [mikro T] - X: %"PRId16"   Y: %"PRId16"   Z: %"PRId16"\n",
                measurement.x_axis, measurement.y_axis, measurement.z_axis);
        /* Get temperature in milli degrees celsius */
        mpu9150_read_temperature(&dev, &temperature);
        printf("Temperature [milli deg] : %"PRId32"\n", temperature);
        printf("\n+-------------------------------------+\n");

        xtimer_usleep(SLEEP);
    }

    return 0;
}