关闭

Android—GSensor驱动与HAL/Framework

1707人阅读 评论(1) 收藏 举报
分类:

1、驱动文件RANGE:

(1)

/*
  Range: unit(ug 1g=1 000 000 ug)
  option(2g,4g,8g)
  G would be defined on android HAL
  Precision: bit wide of valid data
  Boundary: Max positive count
  Gravity_step: gravity value indicated by per count
 */
#define FREAD_MASK				0 /* enabled(1<<1) only if reading MSB 8bits*/
#define MMA845X_RANGE			2000000
/* mma8452 */
#define MMA8452_PRECISION       12
#define MMA8452_BOUNDARY        (0x1 << (MMA8452_PRECISION - 1))
#define MMA8452_GRAVITY_STEP    MMA845X_RANGE / MMA8452_BOUNDARY


(2)

result = ((int)high_byte << (MMA8452_PRECISION-8)) | ((int)low_byte >> (16-MMA8452_PRECISION)); //合并数据
if (result < MMA8452_BOUNDARY) //判断符号
    result = result* MMA8452_GRAVITY_STEP;
else
    result = ~( ((~result & (0x7fff>>(16-MMA8452_PRECISION)) ) + 1) * MMA8452_GRAVITY_STEP) + 1;
		


2、HAL层

(1)MMA IOCTL (mma8452.h )
#define MMAIO 0xA1
/* IOCTLs for MMA8452 library */
#define ECS_IOCTL_INIT                  _IO(MMAIO, 0x01)
// #define ECS_IOCTL_RESET                _IO(MMAIO, 0x04)
#define ECS_IOCTL_CLOSE           _IO(MMAIO, 0x02)
#define ECS_IOCTL_START             _IO(MMAIO, 0x03)
// #define ECS_IOCTL_GETDATA               _IOR(MMAIO, 0x08, char[RBUFF_SIZE+1])
/* IOCTLs for APPs */
#define ECS_IOCTL_APP_SET_RATE _IOW(MMAIO, 0x10, char)
(2)(nusensors.h)
#define ACCELERATION_RATIO_ANDROID_TO_HW        (9.80665f / 1000000)
(3)(MmaSensor.cpp)
void MmaSensor::processEvent(int code, int value)
{    
    switch (code) {
        case EVENT_TYPE_ACCEL_X:
            mPendingMask |= 1<<Accelerometer;
            mPendingEvents[Accelerometer].acceleration.x = value * ACCELERATION_RATIO_ANDROID_TO_HW;
            break;
        case EVENT_TYPE_ACCEL_Y:
            mPendingMask |= 1<<Accelerometer;
            mPendingEvents[Accelerometer].acceleration.y = value * ACCELERATION_RATIO_ANDROID_TO_HW;
            break;
        case EVENT_TYPE_ACCEL_Z:
            mPendingMask |= 1<<Accelerometer;
            mPendingEvents[Accelerometer].acceleration.z = value * ACCELERATION_RATIO_ANDROID_TO_HW;
            break;
    }
}
(4)Sensor_params.h
//ACCEL_ID_LIS331
#define ACCEL_LIS331_RANGE      (2.480f*GRAVITY_EARTH)
#define ACCEL_LIS331_RESOLUTION     (.001f*GRAVITY_EARTH)
#define ACCEL_LIS331_POWER      (1.0f)
//ACCEL_ID_LSM303DLX
#define ACCEL_LSM303DLX_RANGE       (2.480f*GRAVITY_EARTH)
#define ACCEL_LSM303DLX_RESOLUTION  (0.001f*GRAVITY_EARTH)
#define ACCEL_LSM303DLX_POWER       (1.0f)
//ACCEL_ID_LIS3DH
#define ACCEL_LIS3DH_RANGE      (2.480f*GRAVITY_EARTH)
#define ACCEL_LIS3DH_RESOLUTION     (0.001f*GRAVITY_EARTH)
#define ACCEL_LIS3DH_POWER      (1.0f)
//ACCEL_ID_KXSD9
#define ACCEL_KXSD9_RANGE       (2.5006f*GRAVITY_EARTH)
#define ACCEL_KXSD9_RESOLUTION      (0.001f*GRAVITY_EARTH)
#define ACCEL_KXSD9_POWER       (1.0f)
//ACCEL_ID_KXTF9
#define ACCEL_KXTF9_RANGE       (1.0f*GRAVITY_EARTH)
#define ACCEL_KXTF9_RESOLUTION      (0.033f*GRAVITY_EARTH)
#define ACCEL_KXTF9_POWER       (0.35f)
//ACCEL_ID_BMA150
#define ACCEL_BMA150_RANGE      (2.0f*GRAVITY_EARTH)
#define ACCEL_BMA150_RESOLUTION     (0.004f*GRAVITY_EARTH)
#define ACCEL_BMA150_POWER      (0.2f)
//ACCEL_ID_BMA222
#define ACCEL_BMA222_RANGE      (2.0f*GRAVITY_EARTH)
#define ACCEL_BMA222_RESOLUTION     (0.001f*GRAVITY_EARTH)
#define ACCEL_BMA222_POWER      (0.1f)
//ACCEL_ID_BMA250
#define ACCEL_BMA250_RANGE      (2.0f*GRAVITY_EARTH)
#define ACCEL_BMA250_RESOLUTION     (0.00391f*GRAVITY_EARTH)
#define ACCEL_BMA250_POWER      (0.139f)
//ACCEL_ID_ADXL34X
#define ACCEL_ADXL34X_RANGE     (2.0f*GRAVITY_EARTH)
#define ACCEL_ADXL34X_RESOLUTION    (0.001f*GRAVITY_EARTH)
#define ACCEL_ADXL34X_POWER     (1.0f)
//ACCEL_ID_MMA8450
#define ACCEL_MMA8450_RANGE     (2.0f*GRAVITY_EARTH)
#define ACCEL_MMA8450_RESOLUTION    (0.001f*GRAVITY_EARTH)
#define ACCEL_MMA8450_POWER     (1.0f)
//ACCEL_ID_MMA845X
#define ACCEL_MMA845X_RANGE     (2.0f*GRAVITY_EARTH)
#define ACCEL_MMA845X_RESOLUTION    (0.001f*GRAVITY_EARTH)
#define ACCEL_MMA845X_POWER     (1.0f)
//ACCEL_ID_MPU6050
#define ACCEL_MPU6050_RANGE     (2.0f*GRAVITY_EARTH)
#define ACCEL_MPU6050_RESOLUTION    (0.004f*GRAVITY_EARTH)
#define ACCEL_MPU6050_POWER     (0.0f)
/******************************************/
//GYRO MPU3050
#define RAD_P_DEG (3.14159f/180.0f)
#define GYRO_MPU3050_RANGE      (2000.0f*RAD_P_DEG)
#define GYRO_MPU3050_RESOLUTION     (32.8f*RAD_P_DEG)
#define GYRO_MPU3050_POWER      (6.1f)
//GYRO MPU6050
#define GYRO_MPU6050_RANGE      (2000.0f*RAD_P_DEG)
#define GYRO_MPU6050_RESOLUTION     (16.4f*RAD_P_DEG)
#define GYRO_MPU6050_POWER      (5.5f)

(6)MPLSensor.cpp

void MPLSensor::fillAccel(unsigned char accel, struct sensor_t *list)
{
    switch (accel) {
    case ACCEL_ID_LIS331:
        list[Accelerometer].maxRange = ACCEL_LIS331_RANGE;
        list[Accelerometer].resolution = ACCEL_LIS331_RESOLUTION;
        list[Accelerometer].power = ACCEL_LIS331_POWER;
        break;


    case ACCEL_ID_LIS3DH:
        list[Accelerometer].maxRange = ACCEL_LIS3DH_RANGE;
        list[Accelerometer].resolution = ACCEL_LIS3DH_RESOLUTION;
        list[Accelerometer].power = ACCEL_LIS3DH_POWER;
        break;


    case ACCEL_ID_KXSD9:
        list[Accelerometer].maxRange = ACCEL_KXSD9_RANGE;
        list[Accelerometer].resolution = ACCEL_KXSD9_RESOLUTION;
        list[Accelerometer].power = ACCEL_KXSD9_POWER;
        break;


    case ACCEL_ID_KXTF9:
        list[Accelerometer].maxRange = ACCEL_KXTF9_RANGE;
        list[Accelerometer].resolution = ACCEL_KXTF9_RESOLUTION;
        list[Accelerometer].power = ACCEL_KXTF9_POWER;
        break;


    case ACCEL_ID_BMA150:
        list[Accelerometer].maxRange = ACCEL_BMA150_RANGE;
        list[Accelerometer].resolution = ACCEL_BMA150_RESOLUTION;
        list[Accelerometer].power = ACCEL_BMA150_POWER;
        break;


    case ACCEL_ID_BMA222:
        list[Accelerometer].maxRange = ACCEL_BMA222_RANGE;
        list[Accelerometer].resolution = ACCEL_BMA222_RESOLUTION;
        list[Accelerometer].power = ACCEL_BMA222_POWER;
        break;


    case ACCEL_ID_BMA250:
        list[Accelerometer].maxRange = ACCEL_BMA250_RANGE;
        list[Accelerometer].resolution = ACCEL_BMA250_RESOLUTION;
        list[Accelerometer].power = ACCEL_BMA250_POWER;
        break;


    case ACCEL_ID_ADXL34X:
        list[Accelerometer].maxRange = ACCEL_ADXL34X_RANGE;
        list[Accelerometer].resolution = ACCEL_ADXL34X_RESOLUTION;
        list[Accelerometer].power = ACCEL_ADXL34X_POWER;
        break;


    case ACCEL_ID_MMA8450:
        list[Accelerometer].maxRange = ACCEL_MMA8450_RANGE;    
        list[Accelerometer].maxRange = ACCEL_MMA8450_RANGE;
        list[Accelerometer].maxRange = ACCEL_MMA8450_RANGE;
        break;


    case ACCEL_ID_MMA845X:
        list[Accelerometer].maxRange = ACCEL_MMA845X_RANGE;
        list[Accelerometer].resolution = ACCEL_MMA845X_RESOLUTION;
        list[Accelerometer].power = ACCEL_MMA845X_POWER;
        break;


    case ACCEL_ID_MPU6050:
        list[Accelerometer].maxRange = ACCEL_MPU6050_RANGE;
        list[Accelerometer].resolution = ACCEL_MPU6050_RESOLUTION;
        list[Accelerometer].power = ACCEL_MPU6050_POWER;
        break;
    default:
        ALOGE("unknown accel id -- accel params will be wrong.");
        break;
    }
}

3、Framework-Java-->SensorEvent.java

     *     public void onSensorChanged(SensorEvent event)
     *     {
     *          // alpha is calculated as t / (t + dT)
     *          // with t, the low-pass filter's time-constant
     *          // and dT, the event delivery rate
     *
     *          final float alpha = 0.8;
     *
     *          gravity[0] = alpha * gravity[0] + (1 - alpha) * event.values[0];
     *          gravity[1] = alpha * gravity[1] + (1 - alpha) * event.values[1];
     *          gravity[2] = alpha * gravity[2] + (1 - alpha) * event.values[2];
     *
     *          linear_acceleration[0] = event.values[0] - gravity[0];
     *          linear_acceleration[1] = event.values[1] - gravity[1];
     *          linear_acceleration[2] = event.values[2] - gravity[2];
     *     }



0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:399030次
    • 积分:5156
    • 等级:
    • 排名:第5358名
    • 原创:106篇
    • 转载:116篇
    • 译文:0篇
    • 评论:38条
    最新评论