smgr = ISensorManager::getService(); if(smgr == nullptr) { LOGE("zcf_g:Fail to get sensor m_eis gyro">
当前位置:   article > 正文

[Camera专题]Qcom-获取sensor数据_eis gyro

eis gyro

前言

在集成eis算法时,我们需要获取sensor的数据:如gyro,ois,accelerate等传感器的数据。
本文以gyro数据为例子。

1.初始化

1.1 获取服务

void GyroReader::init()
{
	LOGE("zcf_g:E");
    //步骤1:获取sensor服务
	this->smgr = ISensorManager::getService();
	if(smgr == nullptr) {
		LOGE("zcf_g:Fail to get sensor manager!");
		this->deinit();
	}
	//打印所有支持的sensor信息
	this->printSensors();

	Result latestResult;
    //步骤2:获取相应的sensor:这里以gyro为例子
	this->smgr->getDefaultSensor(SensorType::GYROSCOPE,
		[&](const SensorInfo& sensorinfo,const Result & result){
		this->info = sensorinfo;
		latestResult = result;
	});

    //如果想获取accelerate的:
/*
	this->smgr->getDefaultSensor(:SensorType::ACCELEROMETER,
		[&](const SensorInfo& sensorinfo,const Result & result){
		this->info = sensorinfo;
		latestResult = result;
	});
*/
	if(latestResult != Result::OK) {
		LOGE("zcf_g:getDefaultSensor gyro failed !");
		return;
	}
    LOGE("[%s]: name: %s, version: 0x%x",
      this->iinfo.typeAsString.c_str(), this->iinfo.name.c_str(), this->iinfo.version);
    LOGE("[%s]: minDelay: %d, maxDelay: %d",
      this->iinfo.typeAsString.c_str(), this->iinfo.minDelay, this->iinfo.maxDelay);
    LOGE("[%s]: handle = 0x%x, flags: 0x%x",
      this->iinfo.typeAsString.c_str(), this->iinfo.sensorHandle, this->iinfo.flags);

	LOGE("sample_rate = ",USEC_PER_SEC/info.minDelay):

    /*callback object(sensorCb) is freed by IEventqueue implemter, don't delete in deinit*/
	this->sensorCb = new SensorEventCallback();
	this->sensorCb->gyroReader_ptr = (void*)this;

    //步骤3:创建EventQueue
	this->smgr->createEventQueue(this->sensorCb,
		[&](const auto& queue,const auto& result)->void{
			this->eventQueue = queue;
			latestResult = result;
		});

	if(latestResult != Result::OK){
		LOGE("zcf_g:createEventQueue failed!!!");
	}
	//步骤4:enable gyro sensor
	this->eventQueue->enableSensor(info.sensorHandle,info.minDelay,0);
	LOGE("zcf_g:X");
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59

1.2 创建sensorCallback

class SensorEventCallback : public IEventQueueCallback
{
public:
    SensorEventCallback()
    {
        LOGE("zcf_g:SensorEventCallback 构造函数");
    }

    ~SensorEventCallback()
    {
        LOGE("zcf_g:SensorEventCallback 析构函数");
    }

    Return<void> onEvent(const Event &e)
    {
        GyroReader * gr_obj = (GyroReader *)this->gyroReader_ptr;
        if(gr_obj != nullptr) {
            gr_obj->processEvent(e);//这里调用processEvent去处理事件
        }else {
            LOGE("zcf_g:gr_obj is null!")
        }
        return Void();
    }
public:
    void* gyroReader_ptr;//GyroReader类的指针
};
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26

1.3 processEvent

void GyroReader::processEvent(const Event& e)
{
    if(!e.sensorHandle){
        LOGE("zcf_g:invalid sensor handle, ignore");
        return;
    }
    switch(e.sensorType)
    {
    //这里只打印数据,有必要的话,可以创建数组存储起来,后续使用
        case SensorType::GYROSCOPE:
            LOGE("zcf_gd:Event type - gyro!");
            LOGE("zcf_gd:Gyro: evt_ts:%lld us x:%f y:%f z:%f",
                e.timestamp,
                e.u.uncal.x,
                e.u.uncal.y,
                e.u.uncal.z);
            break;
        case SensorType::ACCELEROMETER:
            LOGE("zcf_gd:Event type - Accel!");
            LOGE("zcf_gd:Accel: evt_timestamp:%lld us x:%f y:%f z:%f",
                e.timestamp,
                e.u.uncal.x,
                e.u.uncal.y,
                e.u.uncal.z);
            break;
        default:
            LOGE("zcf_gd:Event for sensor(type: %d) not interested", e.sensorType);
        break;
    }

}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31

1.4 编译

hardware/qcom/camera/QCamera2/Android.mk

--- a/hardware/qcom/camera/QCamera2/Android.mk
+++ b/hardware/qcom/camera/QCamera2/Android.mk
@@ -40,7 +40,7 @@ LOCAL_SRC_FILES += \
         HAL3/QCamera3CropRegionMapper.cpp \
         HAL3/QCamera3StreamMem.cpp
 
-LOCAL_CFLAGS := -Wall -Wextra -Werror
+LOCAL_CFLAGS := -Wall -Wextra
 LOCAL_CFLAGS += -DFDLEAK_FLAG
 LOCAL_CFLAGS += -DMEMLEAK_FLAG
 #HAL 1.0 source
@@ -68,7 +68,8 @@ LOCAL_SRC_FILES += \
         util/QCameraExtZoomTranslator.cpp \
         util/QCameraPprocManager.cpp \
         util/QCameraBokeh.cpp \
-        util/QCameraClearSight.cpp
+        util/QCameraClearSight.cpp \
+               HAL/GyroReader.cpp
 endif

 # System header file path prefix
 LOCAL_SHARED_LIBRARIES += libmmcamera_interface libmmjpeg_interface libui libcamera_metadata
 LOCAL_SHARED_LIBRARIES += libqdMetaData libqservice libbinder
 LOCAL_SHARED_LIBRARIES += libcutils libdl libhal_dbg
+LOCAL_SHARED_LIBRARIES += libhidlbase libhidltransport android.frameworks.sensorservice@1.0
 ifeq ($(IS_QC_BOKEH_SUPPORTED),true)
 LOCAL_SHARED_LIBRARIES += libdualcameraddm
 LOCAL_CFLAGS += -DENABLE_QC_BOKEH
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28

1.5 调用

hardware/qcom/camera/QCamera2/HAL/QCamera2HWI.h

--- a/hardware/qcom/camera/QCamera2/HAL/QCamera2HWI.h
+++ b/hardware/qcom/camera/QCamera2/HAL/QCamera2HWI.h
@@ -62,6 +62,8 @@ extern "C" {
 
 #include "QCameraTrace.h"
 
+#include "GyroReader.h"
+
 namespace qcamera {
 
 #ifndef TRUE
@@ -863,6 +865,7 @@ private:
     bool bDepthAFCallbacks;
     bool m_bOptimizeCacheOps;
     bool m_bNeedHalPP;
+       ISensorAPI::GyroReader gy;
 };
 
 }; // namespace qcamera
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19

2 完整源码

hardware/qcom/camera/QCamera2/HAL/GyroReader.h

#ifndef GYRO_READ_H_
#define GYRO_READ_H_

extern "C" {
#include "mm_camera_dbg.h"
}


#include <android/frameworks/sensorservice/1.0/ISensorManager.h>
#include <android/frameworks/sensorservice/1.0/types.h>
//#include <hardware/sensors.h>  // for sensor type strings

using ::android::frameworks::sensorservice::V1_0::ISensorManager;
using ::android::frameworks::sensorservice::V1_0::Result;
using ::android::frameworks::sensorservice::V1_0::IEventQueue;
using ::android::frameworks::sensorservice::V1_0::IEventQueueCallback;

using ::android::hardware::sensors::V1_0::Event;
using ::android::hardware::sensors::V1_0::SensorType;
using ::android::hardware::sensors::V1_0::SensorInfo;

using ::android::sp;
using ::android::hardware::Void;
using ::android::hardware::Return;

class SensorEventCallback;
namespace ISensorAPI {

class GyroReader
{
public:
	GyroReader();
	~GyroReader();
	void init();
	void deinit();
	void processEvent(const Event& e);
	void printSensors();
    static constexpr int32_t USEC_PER_SEC = 1e6;
    static constexpr int32_t NSEC_PER_SEC = 1e9;

private:
	SensorInfo info;
	sp<ISensorManager> smgr;
    /*callback object(sensorCb) is freed by IEventqueue implemter, don't delete in deinit*/
    //因此可以不用定义成智能指针:sp<SensorEventCallback > sensorCb;
	SensorEventCallback * sensorCb;	
	sp<IEventQueue> eventQueue;
};
}
#endif /* GYRO_READ_H_ */
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50

hardware/qcom/camera/QCamera2/HAL/GyroReader.cpp

#include "GyroReader.h"

using namespace ISensorAPI;

class SensorEventCallback : public IEventQueueCallback
{
public:
	SensorEventCallback()
	{
		LOGE("zcf_g:SensorEventCallback 构造函数");
	}
	
	~SensorEventCallback()
	{
		LOGE("zcf_g:SensorEventCallback 析构函数");
	}
		
	Return<void> onEvent(const Event &e)
	{
		GyroReader * gr_obj = (GyroReader *)this->gyroReader_ptr;
		if(gr_obj != nullptr) {
			gr_obj->processEvent(e);
		}else {
			LOGE("zcf_g:gr_obj is null!")
		}
		return Void();
	}
public:
	void* gyroReader_ptr;
};

GyroReader::GyroReader()
{
	this->init();
}

GyroReader::~GyroReader()
{
	this->deinit();
}

void GyroReader::init()
{
	LOGE("zcf_g:E");
    //步骤1:获取sensor服务
	this->smgr = ISensorManager::getService();
	if(smgr == nullptr) {
		LOGE("zcf_g:Fail to get sensor manager!");
		this->deinit();
	}
	//打印所有支持的sensor信息
	this->printSensors();

	Result latestResult;
    //步骤2:获取相应的sensor:这里以gyro为例子
	this->smgr->getDefaultSensor(SensorType::GYROSCOPE,
		[&](const SensorInfo& sensorinfo,const Result & result){
		this->info = sensorinfo;
		latestResult = result;
	});

    //如果想获取accelerate的:
/*
	this->smgr->getDefaultSensor(:SensorType::ACCELEROMETER,
		[&](const SensorInfo& sensorinfo,const Result & result){
		this->info = sensorinfo;
		latestResult = result;
	});
*/
	if(latestResult != Result::OK) {
		LOGE("zcf_g:getDefaultSensor gyro failed !");
		return;
	}
    LOGE("[%s]: name: %s, version: 0x%x",
      this->iinfo.typeAsString.c_str(), this->iinfo.name.c_str(), this->iinfo.version);
    LOGE("[%s]: minDelay: %d, maxDelay: %d",
      this->iinfo.typeAsString.c_str(), this->iinfo.minDelay, this->iinfo.maxDelay);
    LOGE("[%s]: handle = 0x%x, flags: 0x%x",
      this->iinfo.typeAsString.c_str(), this->iinfo.sensorHandle, this->iinfo.flags);

	LOGE("sample_rate = ",USEC_PER_SEC/info.minDelay):
    /*callback object(sensorCb) is freed by IEventqueue implemter, don't delete in deinit*/
	this->sensorCb = new SensorEventCallback();
	this->sensorCb->gyroReader_ptr = (void*)this;

    //步骤3:创建EventQueue
	this->smgr->createEventQueue(this->sensorCb,
		[&](const auto& queue,const auto& result)->void{
			this->eventQueue = queue;
			latestResult = result;
		});

	if(latestResult != Result::OK){
		LOGE("zcf_g:createEventQueue failed!!!");
	}
	//步骤4:enable gyro sensor
	this->eventQueue->enableSensor(info.sensorHandle,info.minDelay,0);
	LOGE("zcf_g:X");
}

void GyroReader::deinit()
{
	this->eventQueue->disableSensor(info.sensorHandle);
}

void GyroReader::processEvent(const Event& e)
{
	if(!e.sensorHandle){
		LOGE("zcf_g:invalid sensor handle, ignore");
		return;
	}
	switch(e.sensorType)
	{
		case SensorType::GYROSCOPE:
			LOGE("zcf_gd:Event type - gyro!");
			LOGE("zcf_gd:Gyro: evt_ts:%lld us x:%f y:%f z:%f",
				e.timestamp,
		        e.u.uncal.x,
		        e.u.uncal.y,
		        e.u.uncal.z);
			break;
		case SensorType::ACCELEROMETER:
			LOGE("zcf_gd:Event type - Accel!");
			LOGE("zcf_gd:Accel: evt_timestamp:%lld us x:%f y:%f z:%f",
				e.timestamp,
				e.u.uncal.x,
				e.u.uncal.y,
				e.u.uncal.z);
			break;
		default:
		    LOGE("zcf_gd:Event for sensor(type: %d) not interested", e.sensorType);
		break;
	}

}

void GyroReader::printSensors()
{
	
	if(this->smgr.get() != nullptr){
		this->smgr.get()->getSensorList([&](const auto &list,auto result){
			if(result == Result::OK){
				for(unsigned int i =0;i<list.size();i++){
					LOGE("zcf_g:Sensor:%s",list[i].name.c_str());
				}
			}else{
				LOGE("zcf_g:failed to get sensor list!");
			}
		});
	}
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/秋刀鱼在做梦/article/detail/778964
推荐阅读
相关标签
  

闽ICP备14008679号