Onvif協議客戶端開發(8)--球機雲臺的控制

球機的雲臺控制

一、介紹

在安防攝像頭中,不僅僅涉及到固定攝像頭的槍擊,同樣還包含可以360°轉動的球機。因此對球機的雲臺方向控制是Onvif協議開發過程中必不可少的過程

球機的雲臺控制主要包含:八個方向(上、下、左、右、左上、左下、右上、右下),聚焦、放大、縮小等,這在個過程中還包含對轉動速度的控制或者放大縮小的速度控制。對應的方向及正負值如下圖:
在這裏插入圖片描述

二、代碼實現

八個方向、放下及縮小控制
struct soap *stSoapNew = soap_new();
if (stSoapNew == nullptr)
{
	return ;
}
soap_set_namespaces(stSoapNew, namespaces);                                 // 設置soap的namespaces
stSoapNew->recv_timeout = 5;                                           					 // 設置超時5秒(超過指定時間沒有數據就退出)
stSoapNew->send_timeout = 5;
stSoapNew->connect_timeout = 5;
soap_set_mode(stSoapNew, SOAP_C_UTFSTRING);                          // 設置爲UTF-8編碼,否則疊加中文		OSD會亂碼
if (stSoapNew == nullptr)
{
	printf( "Onvif New Soap error!");			
	return;
}

// 如果服務要求鑑權,則以下接口就需要加上用戶名密碼進行鑑權
soap_wsse_add_UsernameTokenDigest(stSoapNew, NULL, pUserName, pPassWord);//對用戶名密碼進行加密

struct _tptz__ContinuousMove stPtzMoveReq;
struct _tptz__ContinuousMoveResponse stPtzMoveRes;
memset(&stPtzMoveReq, 0x00, sizeof(stPtzMoveReq));
memset(&stPtzMoveRes, 0x00, sizeof(stPtzMoveRes));

struct tt__PTZSpeed* stVelocity = soap_new_tt__PTZSpeed(stSoapInfo, -1);
switch (nControlType)
{
case 0:		// 八個方向的控制
{
	struct tt__Vector2D* stPanTilt = soap_new_tt__Vector2D(stSoapInfo, -1);
	stPanTilt->x = 0.2;
	stPanTilt->y = 0.2;
	stPanTilt->space = "http://www.onvif.org/ver10/tptz/PanTiltSpaces/VelocityGenericSpace";
	stVelocity->PanTilt = stPanTilt;
	break;
}
case 1:			// 放大(stZoom->x > 0)、縮小(stZoom->x < 0)
{
	struct tt__Vector1D* stZoom = soap_new_tt__Vector1D(stSoapInfo, -1);
	stZoom->x = 0.2;		
	stZoom->space = "http://www.onvif.org/ver10/tptz/ZoomSpaces/VelocityGenericSpace";
	stVelocity->Zoom = 0.2;
	break;
}
default:
	break;
}

stPtzMoveReq.Velocity = stVelocity;
stPtzMoveReq.ProfileToken = pMainStreamToken;		// 前面獲取到的媒體流的token
// pProfilesAddr 是soap_call___tds__GetCapabilities接口獲取到的PTZ地址,具體實現參見:[獲取設備能力](https://blog.csdn.net/u013566528/article/details/102070372)
nRet = soap_call___tptz__ContinuousMove(stSoapNew, pProfilesAddr, nullptr, &stPtzMoveReq, &stPtzMoveRes);
if (nRet != SOAP_OK || stSoapInfo->error != SOAP_OK)
{
	printf("Login handle is %d Gsoap return is %d labbuf is %s", (int)this, nRet, stSoapInfo->labbuf);
	return;
}
聚焦控制
struct soap *stSoapNew = soap_new();
if (stSoapNew == nullptr)
{
	return ;
}
soap_set_namespaces(stSoapNew, namespaces);                                 // 設置soap的namespaces
stSoapNew->recv_timeout = 5;                                           					 // 設置超時5秒(超過指定時間沒有數據就退出)
stSoapNew->send_timeout = 5;
stSoapNew->connect_timeout = 5;
soap_set_mode(stSoapNew, SOAP_C_UTFSTRING);                          // 設置爲UTF-8編碼,否則疊加中文		OSD會亂碼
if (stSoapNew == nullptr)
{
	printf( "Onvif New Soap error!");			
	return;
}

// 如果服務要求鑑權,則以下接口就需要加上用戶名密碼進行鑑權
soap_wsse_add_UsernameTokenDigest(stSoapNew, NULL, pUserName, pPassWord);//對用戶名密碼進行加密

struct _timg__Move stMoveReq;
struct _timg__MoveResponse stMoveRes;
memset(&stMoveReq, 0x00, sizeof(stMoveReq));
memset(&stMoveRes, 0x00, sizeof(stMoveRes));
tt__FocusMove *stFocusMove = soap_new_tt__FocusMove(stSoapNew, 1);
tt__ContinuousFocus *stContinuFocus = soap_new_tt__ContinuousFocus(stSoapNew, 1);
stContinuFocus->Speed = fSpeed;					// 聚焦的速度
stFocusMove->Continuous = stContinuFocus;
stFocusMove->Absolute = nullptr;
stFocusMove->Relative = nullptr;
stMoveReq.Focus = stFocusMove;
stMoveReq.VideoSourceToken = VideoSourceConfigurationToken;		// 配置token
// pProfilesAddr 是soap_call___tds__GetCapabilities接口獲取到的Ptz地址,具體實現參見:[獲取設備能力](https://blog.csdn.net/u013566528/article/details/102070372)
nRet = soap_call___timg__Move(stSoapNew, pProfilesAddr, nullptr, &stMoveReq, &stMoveRes);
if (nRet != ONVIFSDK_NOERROR || stSoapNew->error != SOAP_OK)
{
	printf("Login handle is %d Gsoap return is %d labbuf is %s", (int)this, nRet, stSoapInfo->labbuf);
	return;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章