//---------------------------------------------------------------------------- // Laser Guard Ltd. // Copyright © 2000-2002. All Rights Reserved. // Version 1.0 01/01/2000 // Last modification: 08/10/2003 // PROJECT: // SUBSYSTEM: // FILE: DPG3AD.cpp // AUTHOR: Sergey Chizhevskiy // OVERVIEW // ~~~~~~~~ // Source file of DPG3AD.cpp //---------------------------------------------------------------------------- #include "stdafx.h" #ifdef __BORLANDC__ #include "..\LgLib\Device\DHebron.h" #include "..\LgLib\Comm\ProtLGSm.h" #include "..\LgLib\Comm\Hebron.h" #else #include "DHebron.h" #endif #define POSITION_QUALYTY 2 #define MAX_NO_REPLY_COUNT 20 #define MAX_NOT_IN_POSITION_COUNT 1 #define DELAY_BEFORE_GET_POSITION 500 CLgDevicePTZHebron::CLgDevicePTZHebron() { m_nMode = CMD_NONE; m_nStage = STAGE_GET_POSITION; m_nNoReplyCount = 0; m_nNotInPositionCount = 0; } CLgDevicePTZHebron::~CLgDevicePTZHebron() { } BOOL CLgDevicePTZHebron::CmdInit() { printf( _T("INIT ") ); // check availibility return DoCmd( CMD_INIT ); } void CLgDevicePTZHebron::OnPerformCmd() { // ready for new message ReleaseCmd(); // ready to process new message m_nMode = CMD_NONE; } BOOL CLgDevicePTZHebron::WriteProc( void *pProtocol, PLONG pTimeout ) { BYTE pSendBuffer[ CProtocolLGS::MAX_COMMAND_SIZE ]; // PBYTE pSendBufferPtr = pSendBuffer; INT nSendCount = 0; // // prepare address // *pSendBufferPtr++ = GetAddress(); // nSendCount++; // cast to protocol CProtocolLGS *pProtocolLGS = (CProtocolLGS*)pProtocol; // set working address // pProtocolLGS->SetAddress( GetAddress() ); // check input command if( m_nMode == CMD_NONE ) { switch( GetCmd() ) { case CMD_INIT: printf( _T("CI ") ); m_nMode = CMD_INIT; break; case CMD_GET_PTZ: printf( _T("CG ") ); m_nMode = CMD_GET_PTZ; break; case CMD_SET_PTZ: printf( _T("CS ") ); // LGLOG( LOG_LEVEL_LOW, LOG_TYPE_INFO, this, _T("CLgDevicePTZHebron::WriteProc"), _T("CMD_SET_PTZ %d,%d,%d"), m_Position.dPan, m_Position.dTilt, m_Position.dZoom ); // validate positions PTZ_HEBRON::ValidatePan( m_Position.dPan ); PTZ_HEBRON::ValidateTilt( m_Position.dTilt ); PTZ_HEBRON::ValidateZoom( m_Position.dZoom ); m_nMode = CMD_SET_PTZ; m_nStage = STAGE_GOTO_POSITION; m_nNotInPositionCount = 0; break; case CMD_MOVE_TILT: printf( _T("CT ") ); m_nMode = CMD_MOVE_TILT; break; case CMD_MOVE_PAN: printf( _T("CP ") ); m_nMode = CMD_MOVE_PAN; break; case CMD_MOVE_ZOOM: printf( _T("CZ ")) ; m_nMode = CMD_MOVE_ZOOM; break; /* case CMD_SET_IRIS: m_nStage = STAGE_GET_POSITION; m_nMode = CMD_SET_IRIS; break; */ case CMD_NONE: // no new commands break; case CMD_SET_FOCUS: LGLOG( LOG_LEVEL_LOW, LOG_TYPE_WARNING, this, _T("CLgDevicePTZHebron::WriteProc"), _T("CMD_FOCUS is not supported yet") ); case CMD_WIPER: default: //command unsupported OnPerformCmd(); OnEvtCommandError( m_nMode, _T("UNSUPPORTED") ); break; } } LPBYTE pSendBufferPtr = pSendBuffer; // fill packet depend on command switch( m_nMode ) { case CMD_SET_PTZ: switch( m_nStage ) { case STAGE_GOTO_POSITION: // ask for pan position nSendCount += PTZ_HEBRON::BuildSetPosition( pSendBufferPtr, GetAddress(), PTZ_HEBRON::Degree2Pan( m_Position.dPan ), PTZ_HEBRON::Degree2Tilt( m_Position.dTilt ), PTZ_HEBRON::Percent2Zoom( m_Position.dZoom ) ); break; case STAGE_GET_POSITION: // ask for pan position nSendCount += PTZ_HEBRON::BuildGetPosition( pSendBufferPtr, GetAddress() ); break; } break; case CMD_GET_PTZ: // ask for pan position nSendCount += PTZ_HEBRON::BuildGetPosition( pSendBufferPtr, GetAddress() ); break; case CMD_INIT: // ask for pan position nSendCount += PTZ_HEBRON::BuildGetPosition( pSendBufferPtr, GetAddress() ); break; case CMD_MOVE_PAN: nSendCount += PTZ_HEBRON::BuildMove( pSendBufferPtr, GetAddress(), PTZ_HEBRON::MOVE_USE_PAN | ( m_MovePan.bRight ? PTZ_HEBRON::MOVE_PAN_RIGHT : 0 ), PTZ_HEBRON::Percent2PanSpeed(m_MovePan.dSpeed), 0, 0 ); break; case CMD_MOVE_TILT: nSendCount += PTZ_HEBRON::BuildMove( pSendBufferPtr, GetAddress(), PTZ_HEBRON::MOVE_USE_TILT | ( m_MoveTilt.bUp ? PTZ_HEBRON::MOVE_TILT_UP : 0 ), 0, PTZ_HEBRON::Percent2TiltSpeed(m_MoveTilt.dSpeed), 0 ); break; case CMD_MOVE_ZOOM: nSendCount += PTZ_HEBRON::BuildMove( pSendBufferPtr, GetAddress(), PTZ_HEBRON::MOVE_USE_ZOOM | ( m_MoveZoom.bTele ? PTZ_HEBRON::MOVE_ZOOM_TELE : 0 ), 0, 0, PTZ_HEBRON::Percent2ZoomSpeed(m_MoveZoom.dSpeed) ); break; case CMD_NONE: default: *pTimeout = 10L; return FALSE; } printf( _T("CW %p %d"), pProtocolLGS, nSendCount ); // write packet if( !pProtocolLGS->Write( pSendBuffer, nSendCount, 1000UL, TRUE ) ) return FALSE; /* // if command without reply if( !bReadReply ) { INT nMode; switch( m_nMode ) { case CMD_SET_PTZ: m_nStage = STAGE_GET_POSITION; *pTimeout = DELAY_BEFORE_GET_POSITION; return FALSE; case CMD_MOVE_PAN: case CMD_MOVE_TILT: case CMD_MOVE_ZOOM: case CMD_SET_IRIS: nMode = m_nMode; OnPerformCmd(); switch( nMode ) { case CMD_MOVE_PAN: OnEvtMovePan( m_MovePan.bRight, m_MovePan.dSpeed ); break; case CMD_MOVE_TILT: OnEvtMoveTilt( m_MoveTilt.bUp, m_MoveTilt.dSpeed ); break; case CMD_MOVE_ZOOM: OnEvtMoveZoom( m_MoveZoom.bTele, m_MoveZoom.dSpeed ); break; case CMD_SET_IRIS: OnEvtSetIris( m_Iris ); break; } return FALSE; } return FALSE; } */ *pTimeout = 250L; // TX + RX return TRUE; } BOOL CLgDevicePTZHebron::ReadProc( void *pProtocol, LONG lTimeout, BOOL bLastChance ) { CProtocolLGS *pProtocolLGS = (CProtocolLGS*)pProtocol; // read reply PBYTE pReadBuffer = NULL; INT nReadCount = 0; if( !pProtocolLGS->Read( &pReadBuffer, &nReadCount, lTimeout, TRUE ) || !pReadBuffer || !nReadCount ) { if( !bLastChance ) return FALSE; m_nNoReplyCount++; if( m_nNoReplyCount > MAX_NO_REPLY_COUNT ) { // perform goto idle state m_nMode = CMD_NONE; } return FALSE; } // reset retry counter m_nNoReplyCount = 0; WORD wPan; WORD wTilt; WORD wZoom; CLgDevicePTZ::PTZ_DEGREE vPan; CLgDevicePTZ::PTZ_DEGREE vTilt; CLgDevicePTZ::PTZ_PERCENT pZoom; // switch mode switch( m_nMode ) { case CMD_GET_PTZ: // store position // m_Position.dPan = PTZ_HEBRON::Pan2Degree( PTZ_HEBRON::Pan2Value( pReadBuffer[2], pReadBuffer[3] ) ); // m_Position.dTilt = PTZ_HEBRON::Tilt2Degree( PTZ_HEBRON::Tilt2Value( pReadBuffer[4], pReadBuffer[5] ) ); // m_Position.dZoom = PTZ_HEBRON::Zoom2Percent( PTZ_HEBRON::Zoom2Value( pReadBuffer[6], pReadBuffer[7] ) ); // LGLOG( LOG_LEVEL_REALTIME, LOG_TYPE_INFO, this, _T("HEBRON GET PTZ"), _T("%d(%d,%d),%d(%d,%d),%d(%d,%d)"), m_Position.dPan, (INT)pReadBuffer[2], (INT)pReadBuffer[3], m_Position.dTilt, (INT)pReadBuffer[4], (INT)pReadBuffer[5], m_Position.dZoom, (INT)pReadBuffer[6], (INT)pReadBuffer[7] ); printf( _T("RG ") ); // ready to process new message OnPerformCmd(); //notify OnEvtGetPTZ( m_Position.dPan, m_Position.dTilt, m_Position.dZoom ); break; case CMD_INIT: printf( _T("RI ") ); wPan = PTZ_HEBRON::Pan2Value( pReadBuffer[2], pReadBuffer[3] ); wTilt = PTZ_HEBRON::Tilt2Value( pReadBuffer[4], pReadBuffer[5] ); wZoom = PTZ_HEBRON::Zoom2Value( pReadBuffer[6], pReadBuffer[7] ); printf( _T("RI1 ") ); vPan = PTZ_HEBRON::Pan2Degree( wPan ); vTilt = PTZ_HEBRON::Tilt2Degree( wTilt ); pZoom = PTZ_HEBRON::Zoom2Percent( wZoom ); printf( _T("RI2 ") ); // store position m_Position.dPan = vPan; m_Position.dTilt = vTilt; m_Position.dZoom = pZoom; // LGLOG( LOG_LEVEL_REALTIME, LOG_TYPE_INFO, this, _T("HEBRON INIT"), _T("%d(%d,%d),%d(%d,%d),%d(%d,%d)"), m_Position.dPan, (INT)pReadBuffer[2], (INT)pReadBuffer[3], m_Position.dTilt, (INT)pReadBuffer[4], (INT)pReadBuffer[5], m_Position.dZoom, (INT)pReadBuffer[6], (INT)pReadBuffer[7] ); printf( _T("RI3 ") ); // ready to process new message OnPerformCmd(); //notify OnEvtInit(); break; case CMD_SET_PTZ: // m_TempPosition.dPan = PTZ_HEBRON::Pan2Degree( PTZ_HEBRON::Pan2Value( pReadBuffer[2], pReadBuffer[3] ) ); // m_TempPosition.dTilt = PTZ_HEBRON::Tilt2Degree( PTZ_HEBRON::Tilt2Value( pReadBuffer[4], pReadBuffer[5] ) ); // m_TempPosition.dZoom = PTZ_HEBRON::Zoom2Percent( PTZ_HEBRON::Zoom2Value( pReadBuffer[6], pReadBuffer[7] ) ); // LGLOG( LOG_LEVEL_REALTIME, LOG_TYPE_INFO, this, _T("HEBRON SET PTZ"), _T("%d(%d,%d),%d(%d,%d),%d(%d,%d)"), m_Position.dPan, (INT)pReadBuffer[2], (INT)pReadBuffer[3], m_Position.dTilt, (INT)pReadBuffer[4], (INT)pReadBuffer[5], m_Position.dZoom, (INT)pReadBuffer[6], (INT)pReadBuffer[7] ); printf( _T("RS ") ); /* // calculate difference between desired position and current position if( QualifyPosition( m_TempPosition.dPan - m_Position.dPan, m_TempPosition.dTilt - m_Position.dTilt, m_TempPosition.dZoom - m_Position.dZoom, POSITION_QUALYTY ) || m_nNotInPositionCount >= MAX_NOT_IN_POSITION_COUNT ) { // store position m_Position.dPan = m_TempPosition.dPan; m_Position.dTilt = m_TempPosition.dTilt; m_Position.dZoom = m_TempPosition.dZoom; */ // ready to process new message OnPerformCmd(); //notify OnEvtSetPTZ( m_Position.dPan, m_Position.dTilt, m_Position.dZoom ); /* } else { // does not in posiiton m_nStage = STAGE_GOTO_POSITION; // increate fault counter m_nNotInPositionCount++; return TRUE; } */ break; case CMD_MOVE_PAN: // LGLOG( LOG_LEVEL_REALTIME, LOG_TYPE_INFO, this, _T("HEBRON MOVE PAN"), _T("") ); printf( _T("RP ") ); // ready to process new message OnPerformCmd(); OnEvtMovePan( m_MovePan.bRight, m_MovePan.dSpeed ); break; case CMD_MOVE_TILT: // LGLOG( LOG_LEVEL_REALTIME, LOG_TYPE_INFO, this, _T("HEBRON MOVE TILT"), _T("") ); printf( _T("RT ") ); // ready to process new message OnPerformCmd(); OnEvtMoveTilt( m_MoveTilt.bUp, m_MoveTilt.dSpeed ); break; case CMD_MOVE_ZOOM: // LGLOG( LOG_LEVEL_REALTIME, LOG_TYPE_INFO, this, _T("HEBRON MOVE ZOOM"), _T("") ); printf( _T("RZ ") ); // ready to process new message OnPerformCmd(); OnEvtMoveZoom( m_MoveZoom.bTele, m_MoveZoom.dSpeed ); break; } return TRUE; }