Android 传感器

 

Android的定义了一个抽象接口,传感器硬件用户空间C。 该接口定义在头 硬件/ libhardware /包括/硬件/ sensors.h机器人。为了与集成传感器的机器人,你需要建立一个共享库,实现了该传感器包括目前支持的接口。通过的种类:

  • 加速度计
  • 磁场
  • 取向
  • 陀螺仪
  • 压力
  • 温度
  • 接近

 

传感器图书馆建设

要实现一个传感器驱动程序,创建一个共享库中定义的接口实现的 sensors.h 您必须命名你的共享库 libsensors.so ,以便它会从加载 /系统/库 在运行时。

下面的stub文件, Android.mk,确保 libsensors 编译和链接到相应的库:


 

传感器

在这份文件中

接口

Android的定义了一个抽象接口,传感器硬件用户空间C。 该接口定义在头 硬件/ libhardware /包括/硬件/ sensors.h机器人。为了与集成传感器的机器人,你需要建立一个共享库,实现了该传感器包括目前支持的接口。通过的种类:

  • 加速度计
  • 磁场
  • 取向
  • 陀螺仪
  • 压力
  • 温度
  • 接近

 

传感器图书馆建设

要实现一个传感器驱动程序,创建一个共享库中定义的接口实现的 sensors.h 您必须命名你的共享库 libsensors.so ,以便它会从加载 /系统/库 在运行时。

下面的stub文件, Android.mk,确保 libsensors 编译和链接到相应的库:

 
     
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)

LOCAL_MODULE := sensors

LOCAL_PRELINK_MODULE := false

LOCAL_MODULE_PATH := $(TARGET_OUT_SHARED_LIBRARIES)/hw

LOCAL_SHARED_LIBRARIES := liblog
# include any shared library dependencies

LOCAL_SRC_FILES := sensors.c

include $(BUILD_SHARED_LIBRARY)

接口

:本文件依赖于一些Doxygen的生成的内容将出现在下面的iFrame一。 要返回到该页面Doxygen的默认内容, 请点击这里


sensors.h File Reference

 

Data Structures

struct   sensor_t
struct   sensors_control_device_t
  Every device data structure must begin with hw_device_t followed by module specific public methods and attributes. More...
struct   sensors_data_device_t
struct   sensors_data_t
  Union of the various types of sensor data that can be returned. More...
struct   sensors_module_t
  Every hardware module must have a data structure named HAL_MODULE_INFO_SYM and the fields of this data structure must begin with hw_module_t followed by module specific information. More...
struct   sensors_vec_t
  Definition of the axis ----------------------. More...


sensors.h

00001 /*
00002  * Copyright (C) 2008 The Android Open Source Project
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License");
00005  * you may not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  *      http://www.apache.org/licenses/LICENSE-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  */
00016 
00017 #ifndef ANDROID_SENSORS_INTERFACE_H
00018 #define ANDROID_SENSORS_INTERFACE_H
00019 
00020 #include 
00021 #include 
00022 #include 
00023 
00024 #include 
00025 #include 
00026 
00027 __BEGIN_DECLS
00028 
00029 /**
00030  * The id of this module
00031  */
00032 #define SENSORS_HARDWARE_MODULE_ID "sensors"
00033 
00034 /**
00035  * Name of the sensors device to open
00036  */
00037 #define SENSORS_HARDWARE_CONTROL    "control"
00038 #define SENSORS_HARDWARE_DATA       "data"
00039 
00040 /**
00041  * Handles must be higher than SENSORS_HANDLE_BASE and must be unique.
00042  * A Handle identifies a given sensors. The handle is used to activate
00043  * and/or deactivate sensors.
00044  * In this version of the API there can only be 256 handles.
00045  */
00046 #define SENSORS_HANDLE_BASE             0
00047 #define SENSORS_HANDLE_BITS             8
00048 #define SENSORS_HANDLE_COUNT            (1<
00049 
00050 
00051 /**
00052  * Sensor types
00053  */
00054 #define SENSOR_TYPE_ACCELEROMETER       1
00055 #define SENSOR_TYPE_MAGNETIC_FIELD      2
00056 #define SENSOR_TYPE_ORIENTATION         3
00057 #define SENSOR_TYPE_GYROSCOPE           4
00058 #define SENSOR_TYPE_LIGHT               5
00059 #define SENSOR_TYPE_PRESSURE            6
00060 #define SENSOR_TYPE_TEMPERATURE         7
00061 #define SENSOR_TYPE_PROXIMITY           8
00062 
00063 /**
00064  * Values returned by the accelerometer in various locations in the universe.
00065  * all values are in SI units (m/s^2)
00066  */
00067 
00068 #define GRAVITY_SUN             (275.0f)
00069 #define GRAVITY_MERCURY         (3.70f)
00070 #define GRAVITY_VENUS           (8.87f)
00071 #define GRAVITY_EARTH           (9.80665f)
00072 #define GRAVITY_MOON            (1.6f)
00073 #define GRAVITY_MARS            (3.71f)
00074 #define GRAVITY_JUPITER         (23.12f)
00075 #define GRAVITY_SATURN          (8.96f)
00076 #define GRAVITY_URANUS          (8.69f)
00077 #define GRAVITY_NEPTUNE         (11.0f)
00078 #define GRAVITY_PLUTO           (0.6f)
00079 #define GRAVITY_DEATH_STAR_I    (0.000000353036145f)
00080 #define GRAVITY_THE_ISLAND      (4.815162342f)
00081 
00082 /** Maximum magnetic field on Earth's surface */
00083 #define MAGNETIC_FIELD_EARTH_MAX    (60.0f)
00084 
00085 /** Minimum magnetic field on Earth's surface */
00086 #define MAGNETIC_FIELD_EARTH_MIN    (30.0f)
00087 
00088 
00089 /**
00090  * status of each sensor
00091  */
00092 
00093 #define SENSOR_STATUS_UNRELIABLE        0
00094 #define SENSOR_STATUS_ACCURACY_LOW      1
00095 #define SENSOR_STATUS_ACCURACY_MEDIUM   2
00096 #define SENSOR_STATUS_ACCURACY_HIGH     3
00097 
00098 /**
00099  * Definition of the axis
00100  * ----------------------
00101  *
00102  * This API is relative to the screen of the device in its default orientation,
00103  * that is, if the device can be used in portrait or landscape, this API
00104  * is only relative to the NATURAL orientation of the screen. In other words,
00105  * the axis are not swapped when the device's screen orientation changes.
00106  * Higher level services /may/ perform this transformation.
00107  *
00108  *   x<0         x>0
00109  *                ^
00110  *                |
00111  *    +-----------+-->  y>0
00112  *    |           |
00113  *    |           |
00114  *    |           |
00115  *    |           |   / z<0
00116  *    |           |  /
00117  *    |           | /
00118  *    O-----------+/
00119  *    |[]  [ ]  []/
00120  *    +----------/+     y<0
00121  *              /
00122  *             /
00123  *           |/ z>0 (toward the sky)
00124  *
00125  *    O: Origin (x=0,y=0,z=0)
00126  *
00127  *
00128  * Orientation
00129  * ----------- 
00130  * 
00131  * All values are angles in degrees.
00132  * 
00133  * azimuth: angle between the magnetic north direction and the Y axis, around 
00134  *  the Z axis (0<=azimuth<360).
00135  *      0=North, 90=East, 180=South, 270=West
00136  * 
00137  * pitch: Rotation around X axis (-180<=pitch<=180), with positive values when
00138  *  the z-axis moves toward the y-axis.
00139  *
00140  * roll: Rotation around Y axis (-90<=roll<=90), with positive values when
00141  *  the x-axis moves towards the z-axis.
00142  *
00143  * Note: For historical reasons the roll angle is positive in the clockwise
00144  *  direction (mathematically speaking, it should be positive in the
00145  *  counter-clockwise direction):
00146  *
00147  *                Z
00148  *                ^
00149  *  (+roll)  .--> |
00150  *          /     |
00151  *         |      |  roll: rotation around Y axis
00152  *     X <-------(.)
00153  *                 Y
00154  *       note that +Y == -roll
00155  *
00156  *
00157  *
00158  * Note: This definition is different from yaw, pitch and roll used in aviation
00159  *  where the X axis is along the long side of the plane (tail to nose).
00160  *  
00161  *  
00162  * Acceleration
00163  * ------------
00164  *
00165  *  All values are in SI units (m/s^2) and measure the acceleration of the
00166  *  device minus the force of gravity.
00167  *  
00168  *  x: Acceleration minus Gx on the x-axis 
00169  *  y: Acceleration minus Gy on the y-axis 
00170  *  z: Acceleration minus Gz on the z-axis
00171  *  
00172  *  Examples:
00173  *    When the device lies flat on a table and is pushed on its left side
00174  *    toward the right, the x acceleration value is positive.
00175  *    
00176  *    When the device lies flat on a table, the acceleration value is +9.81,
00177  *    which correspond to the acceleration of the device (0 m/s^2) minus the
00178  *    force of gravity (-9.81 m/s^2).
00179  *    
00180  *    When the device lies flat on a table and is pushed toward the sky, the
00181  *    acceleration value is greater than +9.81, which correspond to the
00182  *    acceleration of the device (+A m/s^2) minus the force of 
00183  *    gravity (-9.81 m/s^2).
00184  *    
00185  *    
00186  * Magnetic Field
00187  * --------------
00188  * 
00189  *  All values are in micro-Tesla (uT) and measure the ambient magnetic
00190  *  field in the X, Y and Z axis.
00191  *
00192  * Proximity
00193  * ---------
00194  *
00195  * The distance value is measured in centimeters.  Note that some proximity
00196  * sensors only support a binary "close" or "far" measurement.  In this case,
00197  * the sensor should report its maxRange value in the "far" state and a value
00198  * less than maxRange in the "near" state.
00199  *
00200  * Light
00201  * -----
00202  *
00203  * The light sensor value is returned in SI lux units.
00204  *
00205  */
00206 typedef struct {
00207     union {
00208         float v[3];
00209         struct {
00210             float x;
00211             float y;
00212             float z;
00213         };
00214         struct {
00215             float azimuth;
00216             float pitch;
00217             float roll;
00218         };
00219     };
00220     int8_t status;
00221     uint8_t reserved[3];
00222 } sensors_vec_t;
00223 
00224 /**
00225  * Union of the various types of sensor data
00226  * that can be returned.
00227  */
00228 typedef struct {
00229     /* sensor identifier */
00230     int             sensor;
00231 
00232     union {
00233         /* x,y,z values of the given sensor */
00234         sensors_vec_t   vector;
00235 
00236         /* orientation values are in degrees */
00237         sensors_vec_t   orientation;
00238 
00239         /* acceleration values are in meter per second per second (m/s^2) */
00240         sensors_vec_t   acceleration;
00241 
00242         /* magnetic vector values are in micro-Tesla (uT) */
00243         sensors_vec_t   magnetic;
00244 
00245         /* temperature is in degrees centigrade (Celsius) */
00246         float           temperature;
00247 
00248         /* distance in centimeters */
00249         float           distance;
00250 
00251         /* light in SI lux units */
00252         float           light;
00253     };
00254 
00255     /* time is in nanosecond */
00256     int64_t         time;
00257 
00258     uint32_t        reserved;
00259 } sensors_data_t;
00260 
00261 
00262 struct sensor_t;
00263 
00264 /**
00265  * Every hardware module must have a data structure named HAL_MODULE_INFO_SYM
00266  * and the fields of this data structure must begin with hw_module_t
00267  * followed by module specific information.
00268  */
00269 struct sensors_module_t {
00270     struct hw_module_t common;
00271 
00272     /**
00273      * Enumerate all available sensors. The list is returned in "list".
00274      * @return number of sensors in the list
00275      */
00276     int (*get_sensors_list)(struct sensors_module_t* module,
00277             struct sensor_t const** list);
00278 };
00279 
00280 struct sensor_t {
00281     /* name of this sensors */
00282     const char*     name;
00283     /* vendor of the hardware part */
00284     const char*     vendor;
00285     /* version of the hardware part + driver. The value of this field is
00286      * left to the implementation and doesn't have to be monotonicaly
00287      * increasing.
00288      */    
00289     int             version;
00290     /* handle that identifies this sensors. This handle is used to activate
00291      * and deactivate this sensor. The value of the handle must be 8 bits
00292      * in this version of the API. 
00293      */
00294     int             handle;
00295     /* this sensor's type. */
00296     int             type;
00297     /* maximaum range of this sensor's value in SI units */
00298     float           maxRange;
00299     /* smallest difference between two values reported by this sensor */
00300     float           resolution;
00301     /* rough estimate of this sensor's power consumption in mA */
00302     float           power;
00303     /* reserved fields, must be zero */
00304     void*           reserved[9];
00305 };
00306 
00307 
00308 /**
00309  * Every device data structure must begin with hw_device_t
00310  * followed by module specific public methods and attributes.
00311  */
00312 struct sensors_control_device_t {
00313     struct hw_device_t common;
00314     
00315     /**
00316      * Returns a native_handle_t, which will be the parameter to
00317      * sensors_data_device_t::open_data(). 
00318      * The caller takes ownership of this handle. This is intended to be
00319      * passed cross processes.
00320      *
00321      * @return a native_handle_t if successful, NULL on error
00322      */
00323     native_handle_t* (*open_data_source)(struct sensors_control_device_t *dev);
00324 
00325     /**
00326      * Releases any resources that were created by open_data_source.
00327      * This call is optional and can be NULL if not implemented
00328      * by the sensor HAL.
00329      *
00330      * @return 0 if successful, < 0 on error
00331      */
00332     int (*close_data_source)(struct sensors_control_device_t *dev);
00333 
00334     /** Activate/deactivate one sensor.
00335      *
00336      * @param handle is the handle of the sensor to change.
00337      * @param enabled set to 1 to enable, or 0 to disable the sensor.
00338      *
00339      * @return 0 on success, negative errno code otherwise
00340      */
00341     int (*activate)(struct sensors_control_device_t *dev, 
00342             int handle, int enabled);
00343     
00344     /**
00345      * Set the delay between sensor events in ms
00346      *
00347      * @return 0 if successful, < 0 on error
00348      */
00349     int (*set_delay)(struct sensors_control_device_t *dev, int32_t ms);
00350 
00351     /**
00352      * Causes sensors_data_device_t.poll() to return -EWOULDBLOCK immediately.
00353      */
00354     int (*wake)(struct sensors_control_device_t *dev);
00355 };
00356 
00357 struct sensors_data_device_t {
00358     struct hw_device_t common;
00359 
00360     /**
00361      * Prepare to read sensor data.
00362      *
00363      * This routine does NOT take ownership of the handle
00364      * and must not close it. Typically this routine would
00365      * use a duplicate of the nh parameter.
00366      *
00367      * @param nh from sensors_control_open.
00368      *
00369      * @return 0 if successful, < 0 on error
00370      */
00371     int (*data_open)(struct sensors_data_device_t *dev, native_handle_t* nh);
00372     
00373     /**
00374      * Caller has completed using the sensor data.
00375      * The caller will not be blocked in sensors_data_poll
00376      * when this routine is called.
00377      *
00378      * @return 0 if successful, < 0 on error
00379      */
00380     int (*data_close)(struct sensors_data_device_t *dev);
00381     
00382     /**
00383      * Return sensor data for one of the enabled sensors.
00384      *
00385      * @return sensor handle for the returned data, 0x7FFFFFFF when 
00386      * sensors_control_device_t.wake() is called and -errno on error
00387      *  
00388      */
00389     int (*poll)(struct sensors_data_device_t *dev, 
00390             sensors_data_t* data);
00391 };
00392 
00393 
00394 /** convenience API for opening and closing a device */
00395 
00396 static inline int sensors_control_open(const struct hw_module_t* module, 
00397         struct sensors_control_device_t** device) {
00398     return module->methods->open(module, 
00399             SENSORS_HARDWARE_CONTROL, (struct hw_device_t**)device);
00400 }
00401 
00402 static inline int sensors_control_close(struct sensors_control_device_t* device) {
00403     return device->common.close(&device->common);
00404 }
00405 
00406 static inline int sensors_data_open(const struct hw_module_t* module, 
00407         struct sensors_data_device_t** device) {
00408     return module->methods->open(module, 
00409             SENSORS_HARDWARE_DATA, (struct hw_device_t**)device);
00410 }
00411 
00412 static inline int sensors_data_close(struct sensors_data_device_t* device) {
00413     return device->common.close(&device->common);
00414 }
00415 
00416 
00417 __END_DECLS
00418 
00419 #endif  // ANDROID_SENSORS_INTERFACE_H

原文:http://source.android.com/porting/sensors.html

 

 

 

你可能感兴趣的:(android,struct,module,structure,float,distance,Android,传感器)