//---------------------------------------------------------------------------- // 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\DPG3AD.h" #else #include "DPG3AD.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 CLgDevicePTZPG3AD::CLgDevicePTZPG3AD() { m_nMode = CMD_NONE; m_nStage = STAGE_GET_POSITION; m_nNoReplyCount = 0; m_nNotInPositionCount = 0; memset( &m_PtzState, 0, sizeof(m_PtzState) ); m_pPan2Degree = PTZ_PHILIPS_G3_AUTODOME::Pan2DegreeNew; m_pDegree2Pan = PTZ_PHILIPS_G3_AUTODOME::Degree2PanNew; } CLgDevicePTZPG3AD::~CLgDevicePTZPG3AD() { } BOOL CLgDevicePTZPG3AD::SetOldType() { m_pPan2Degree = PTZ_PHILIPS_G3_AUTODOME::Pan2DegreeOld; m_pDegree2Pan = PTZ_PHILIPS_G3_AUTODOME::Degree2PanOld; return TRUE; } void CLgDevicePTZPG3AD::OnPerformCmd() { // ready for new message ReleaseCmd(); // ready to process new message m_nMode = CMD_NONE; } BOOL CLgDevicePTZPG3AD::WriteProc( void *pProtocol, PLONG pTimeout ) { BYTE pSendBuffer[ CProtocolPTZPG3AD::MAX_COMMAND_SIZE ]; BYTE nSendCount = 0; // cast to protocol CProtocolPTZPG3AD *pProtocolPTZ = (CProtocolPTZPG3AD*)pProtocol; // set working address pProtocolPTZ->SetAddress( GetAddress() ); // check input command if( m_nMode == CMD_NONE ) { switch( GetCmd() ) { case CMD_GET_PTZ: m_nMode = CMD_GET_PTZ; break; case CMD_SET_PTZ: // LGLOG( LOG_LEVEL_LOW, LOG_TYPE_INFO, this, _T("CLgDevicePTZPG3AD::WriteProc"), _T("CMD_SET_PTZ %d,%d,%d"), m_Position.dPan, m_Position.dTilt, m_Position.dZoom ); // validate positions PTZ_PHILIPS_G3_AUTODOME::ValidatePan( m_Position.dPan ); PTZ_PHILIPS_G3_AUTODOME::ValidateTilt( m_Position.dTilt ); PTZ_PHILIPS_G3_AUTODOME::ValidateZoom( m_Position.dZoom ); m_nMode = CMD_SET_PTZ; m_nStage = STAGE_GOTO_POSITION; m_nNotInPositionCount = 0; break; case CMD_MOVE_TILT: m_nMode = CMD_MOVE_TILT; break; case CMD_MOVE_PAN: m_nMode = CMD_MOVE_PAN; break; case CMD_MOVE_ZOOM: 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("CLgDevicePTZPG3AD::WriteProc"), _T("CMD_FOCUS is not supported yet") ); case CMD_WIPER: default: //command unsupported OnPerformCmd(); OnEvtCommandError( m_nMode, _T("UNSUPPORTED") ); break; } } BOOL bReadReply = FALSE; // 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_PHILIPS_G3_AUTODOME::BuildGotoPosition( pSendBuffer, GetAddress(), m_pDegree2Pan( m_Position.dPan ), PTZ_PHILIPS_G3_AUTODOME::Degree2Tilt( m_Position.dTilt ), PTZ_PHILIPS_G3_AUTODOME::Percent2Zoom( m_Position.dZoom ) ); break; case STAGE_GET_POSITION: // ask for pan position nSendCount += PTZ_PHILIPS_G3_AUTODOME::BuildGetPosition( pSendBuffer, GetAddress() ); // short response bReadReply = TRUE; break; } break; case CMD_GET_PTZ: // ask for pan position nSendCount += PTZ_PHILIPS_G3_AUTODOME::BuildGetPosition( pSendBuffer, GetAddress() ); // short response bReadReply = TRUE; break; case CMD_SET_IRIS: switch( m_nStage ) { case STAGE_GET_POSITION: // ask for zoom position nSendCount += PTZ_PHILIPS_G3_AUTODOME::BuildGetPosition( pSendBuffer, GetAddress() ); // short response bReadReply = TRUE; break; case STAGE_SET_IRIS: // set iris command nSendCount += PTZ_PHILIPS_G3_AUTODOME::BuildIris( pSendBuffer, GetAddress(), PTZ_PHILIPS_G3_AUTODOME::Percent2Iris( m_Iris ) ); break; } break; case CMD_MOVE_PAN: nSendCount += PTZ_PHILIPS_G3_AUTODOME::BuildPan( pSendBuffer, GetAddress(), m_MovePan.dSpeed > 0 ? ( m_MovePan.bRight ? PTZ_PHILIPS_G3_AUTODOME::PAN_RIGHT : PTZ_PHILIPS_G3_AUTODOME::PAN_LEFT ) : PTZ_PHILIPS_G3_AUTODOME::PAN_STOP, PTZ_PHILIPS_G3_AUTODOME::Percent2PanSpeed( m_MovePan.dSpeed ), m_PtzState ); break; case CMD_MOVE_TILT: nSendCount += PTZ_PHILIPS_G3_AUTODOME::BuildTilt( pSendBuffer, GetAddress(), m_MoveTilt.dSpeed > 0 ? ( m_MoveTilt.bUp ? PTZ_PHILIPS_G3_AUTODOME::TILT_UP : PTZ_PHILIPS_G3_AUTODOME::TILT_DOWN ) : PTZ_PHILIPS_G3_AUTODOME::TILT_STOP, PTZ_PHILIPS_G3_AUTODOME::Percent2TiltSpeed( m_MoveTilt.dSpeed ), m_PtzState ); break; case CMD_MOVE_ZOOM: nSendCount += PTZ_PHILIPS_G3_AUTODOME::BuildZoom( pSendBuffer, GetAddress(), m_MoveZoom.dSpeed > 0 ? ( m_MoveZoom.bTele ? PTZ_PHILIPS_G3_AUTODOME::ZOOM_TELE : PTZ_PHILIPS_G3_AUTODOME::ZOOM_WIDE ) : PTZ_PHILIPS_G3_AUTODOME::ZOOM_STOP, PTZ_PHILIPS_G3_AUTODOME::Percent2ZoomSpeed( m_MoveZoom.dSpeed ), m_PtzState ); break; case CMD_NONE: default: *pTimeout = 10L; return FALSE; } // write packet if( !pProtocolPTZ->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 = 2000;250L; // TX + RX return TRUE; } BOOL CLgDevicePTZPG3AD::ReadProc( void *pProtocol, LONG lTimeout, BOOL bLastChance ) { CProtocolPTZPG3AD *pProtocolPTZ = (CProtocolPTZPG3AD*)pProtocol; // read reply PBYTE pReadBuffer = NULL; INT nReadCount = 0; if( !pProtocolPTZ->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; // switch mode switch( m_nMode ) { case CMD_GET_PTZ: // store position m_Position.dPan = m_pPan2Degree( PTZ_PHILIPS_G3_AUTODOME::Pan2Value( pReadBuffer[6], pReadBuffer[7], pReadBuffer[8] ) ); m_Position.dTilt = PTZ_PHILIPS_G3_AUTODOME::Tilt2Degree( PTZ_PHILIPS_G3_AUTODOME::Tilt2Value( pReadBuffer[9], pReadBuffer[10], pReadBuffer[11] ) ); m_Position.dZoom = PTZ_PHILIPS_G3_AUTODOME::Zoom2Percent( PTZ_PHILIPS_G3_AUTODOME::Zoom2Value( pReadBuffer[12], pReadBuffer[13] ) ); // ready to process new message OnPerformCmd(); //notify OnEvtGetPTZ( m_Position.dPan, m_Position.dTilt, m_Position.dZoom ); break; case CMD_SET_PTZ: m_TempPosition.dPan = m_pPan2Degree( PTZ_PHILIPS_G3_AUTODOME::Pan2Value( pReadBuffer[6], pReadBuffer[7], pReadBuffer[8] ) ); m_TempPosition.dTilt = PTZ_PHILIPS_G3_AUTODOME::Tilt2Degree( PTZ_PHILIPS_G3_AUTODOME::Tilt2Value( pReadBuffer[9], pReadBuffer[10], pReadBuffer[11] ) ); m_TempPosition.dZoom = PTZ_PHILIPS_G3_AUTODOME::Zoom2Percent( PTZ_PHILIPS_G3_AUTODOME::Zoom2Value( pReadBuffer[12], pReadBuffer[13] ) ); // 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_SET_IRIS: // store position m_Position.dPan = m_pPan2Degree( PTZ_PHILIPS_G3_AUTODOME::Pan2Value( pReadBuffer[6], pReadBuffer[7], pReadBuffer[8] ) ); m_Position.dTilt = PTZ_PHILIPS_G3_AUTODOME::Tilt2Degree( PTZ_PHILIPS_G3_AUTODOME::Tilt2Value( pReadBuffer[9], pReadBuffer[10], pReadBuffer[11] ) ); m_Position.dZoom = PTZ_PHILIPS_G3_AUTODOME::Zoom2Percent( PTZ_PHILIPS_G3_AUTODOME::Zoom2Value( pReadBuffer[12], pReadBuffer[13] ) ); // position have got so can perform set iris m_nStage = STAGE_SET_IRIS; return TRUE; } return TRUE; }