Onvif协议学习:14、球机云台控制PTZ
原文链接:https://blog.csdn.net/u013566528/article/details/102079845
一、介绍
在安防摄像头中,不仅仅涉及到固定摄像头的枪击,同样还包含可以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;
}