Old fashion design for PC-104
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

492 lines
15 KiB

//----------------------------------------------------------------------------
// 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 3
#define MAX_NO_REPLY_COUNT 20
#define MAX_NOT_IN_POSITION_COUNT 10
#define DELAY_BEFORE_GET_POSITION 500
CLgDevicePTZHebron::CLgDevicePTZHebron()
{
m_nMode = CMD_NONE;
m_nStage = STAGE_GET_POSITION;
m_nNoReplyCount = 0;
m_nNotInPositionCount = 0;
m_bModeDopler = FALSE;
m_nMaskIndex = 0;
}
CLgDevicePTZHebron::~CLgDevicePTZHebron()
{
}
BOOL CLgDevicePTZHebron::CmdInit()
{
// 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 ];
INT nSendCount = 0;
// cast to protocol
CProtocolLGS *pProtocolLGS = (CProtocolLGS*)pProtocol;
// check input command
if( m_nMode == CMD_NONE )
{
switch( GetCmd() )
{
case CMD_INIT:
m_nMode = CMD_INIT;
m_bModeDopler = FALSE;
break;
case CMD_GET_PTZ:
m_nMode = CMD_GET_PTZ;
break;
case CMD_SET_PTZ:
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;
m_bModeDopler = FALSE;
break;
case CMD_MOVE_TILT:
m_nMode = CMD_MOVE_TILT;
m_bModeDopler = FALSE;
break;
case CMD_MOVE_PAN:
m_nMode = CMD_MOVE_PAN;
m_bModeDopler = FALSE;
break;
case CMD_MOVE_ZOOM:
m_nMode = CMD_MOVE_ZOOM;
m_bModeDopler = FALSE;
break;
case CMD_SET_DOPLER:
LGLOG( LOG_LEVEL_LOW, LOG_TYPE_INFO, this, _T("CLgDevicePTZHebron::WriteProc"), _T("CMD_SET_DOPLER %d,%d,%d"), m_Dopler.bEnable, m_Dopler.dPanStep, m_Dopler.dwTimeout );
m_nMode = CMD_SET_DOPLER;
m_bModeDopler = m_Dopler.bEnable;
break;
case CMD_GET_DOPLER:
m_nMode = CMD_GET_DOPLER;
break;
case CMD_SET_IRIS:
m_nMode = CMD_SET_IRIS;
break;
case CMD_SET_DOPLER_MASK:
m_nMaskIndex = -1;
m_nMode = CMD_SET_DOPLER_MASK;
break;
case CMD_GET_DOPLER_MASK:
m_nMaskIndex = -1;
m_nMode = CMD_GET_DOPLER_MASK;
break;
case CMD_NONE:
// no new commands
break;
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_SET_DOPLER:
// ask for pan position
nSendCount += PTZ_HEBRON::BuildSetDopler( pSendBufferPtr, GetAddress(),
m_Dopler.bEnable,
PTZ_HEBRON::DeltaDegree2Pan( m_Dopler.dPanStep ),
m_Dopler.dwTimeout );
break;
case CMD_GET_DOPLER:
// ask for pan position
nSendCount += PTZ_HEBRON::BuildGetDopler( pSendBufferPtr, GetAddress() );
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:
LGLOG( LOG_LEVEL_LOW, LOG_TYPE_INFO, this, _T("SET HEBRON MOVE PAN"), _T("%d %d"), m_MovePan.bRight, m_MovePan.dSpeed );
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_SET_IRIS:
nSendCount += PTZ_HEBRON::BuildSetIris( pSendBufferPtr, GetAddress(), PTZ_HEBRON::Percent2Iris(m_Iris) );
break;
case CMD_SET_DOPLER_MASK:
if( m_nMaskIndex == -1 ) nSendCount += PTZ_HEBRON::BuildSetDoplerMask( pSendBufferPtr, GetAddress(), m_Mask.bEnable, m_Mask.nCount );
else if( m_nMaskIndex == m_Mask.nCount ) nSendCount += PTZ_HEBRON::BuildSetDoplerMaskSave( pSendBufferPtr, GetAddress() );
else nSendCount += PTZ_HEBRON::BuildSetDoplerMaskValue( pSendBufferPtr, GetAddress(), m_nMaskIndex, PTZ_HEBRON::Degree2Pan( m_Mask.pMask[m_nMaskIndex].nStartAngle ), PTZ_HEBRON::Degree2Pan( m_Mask.pMask[m_nMaskIndex].nStopAngle ) );
break;
case CMD_GET_DOPLER_MASK:
if( m_nMaskIndex == -1 ) nSendCount += PTZ_HEBRON::BuildGetDoplerMask( pSendBufferPtr, GetAddress() );
else nSendCount += PTZ_HEBRON::BuildGetDoplerMaskValue( pSendBufferPtr, GetAddress(), m_nMaskIndex );
break;
case CMD_NONE:
default:
if( m_bModeDopler )
{
nSendCount += PTZ_HEBRON::BuildGetDoplerAlarm( pSendBufferPtr, GetAddress() );
}
else
{
/* static int nTargetTime = 0;
static int nTargetAngle = 0;
if( nTargetTime++ > 100 )
{
nTargetAngle = (nTargetAngle + 10) % 360;
OnEvtDoplerAlarm( TRUE, nTargetAngle );
nTargetTime = 0;
}
*/
*pTimeout = 10L;
return FALSE;
}
break;
}
// write packet
if( !pProtocolLGS->Write( pSendBuffer, nSendCount, 1000UL, TRUE ) )
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;
// have to be checked!!!!!!!!
// ready to process new message
OnPerformCmd();
//notify
OnEvtInit();
}
return FALSE;
}
DOPLER_ALARM alarm;
// reset retry counter
m_nNoReplyCount = 0;
// 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_LOW, 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] );
// ready to process new message
OnPerformCmd();
//notify
OnEvtGetPTZ( m_Position.dPan, m_Position.dTilt, m_Position.dZoom );
break;
case CMD_INIT:
// 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_LOW, 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] );
// 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] ) );
// 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 )
{
LGLOG( LOG_LEVEL_LOW, LOG_TYPE_INFO, this, _T("HEBRON SET PTZ"), _T("%d(%d,%d),%d(%d,%d),%d(%d,%d)"),
m_TempPosition.dPan, (INT)pReadBuffer[2], (INT)pReadBuffer[3],
m_TempPosition.dTilt, (INT)pReadBuffer[4], (INT)pReadBuffer[5],
m_TempPosition.dZoom, (INT)pReadBuffer[6], (INT)pReadBuffer[7] );
// 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_LOW, LOG_TYPE_INFO, this, _T("GET HEBRON MOVE PAN"), _T("%d %d(%d)"), m_MovePan.bRight, m_MovePan.dSpeed, (INT)pReadBuffer[3] );
// ready to process new message
OnPerformCmd();
OnEvtMovePan( m_MovePan.bRight, m_MovePan.dSpeed );
break;
case CMD_MOVE_TILT:
LGLOG( LOG_LEVEL_LOW, LOG_TYPE_INFO, this, _T("GET HEBRON MOVE TILT"), _T("%d %d"), m_MoveTilt.bUp, m_MoveTilt.dSpeed );
// ready to process new message
OnPerformCmd();
OnEvtMoveTilt( m_MoveTilt.bUp, m_MoveTilt.dSpeed );
break;
case CMD_MOVE_ZOOM:
LGLOG( LOG_LEVEL_LOW, LOG_TYPE_INFO, this, _T("GET HEBRON MOVE ZOOM"), _T("%d %d"), m_MoveZoom.bTele, m_MoveZoom.dSpeed );
// ready to process new message
OnPerformCmd();
OnEvtMoveZoom( m_MoveZoom.bTele, m_MoveZoom.dSpeed );
break;
case CMD_SET_DOPLER:
m_Dopler.bEnable = pReadBuffer[2];
m_Dopler.dPanStep = PTZ_HEBRON::DeltaPan2Degree( PTZ_HEBRON::Pan2Value( pReadBuffer[3], pReadBuffer[4] ) );
m_Dopler.dwTimeout = *(LPDWORD)(pReadBuffer + 5);
LGLOG( LOG_LEVEL_LOW, LOG_TYPE_INFO, this, _T("GET HEBRON SET DOPLER"), _T("%d,%d,%d"), m_Dopler.bEnable, m_Dopler.dPanStep, m_Dopler.dwTimeout );
// ready to process new message
OnPerformCmd();
OnEvtSetDopler( m_Dopler.bEnable, m_Dopler.dPanStep, m_Dopler.dwTimeout );
break;
case CMD_GET_DOPLER:
m_Dopler.bEnable = pReadBuffer[2];
m_Dopler.dPanStep = PTZ_HEBRON::DeltaPan2Degree( PTZ_HEBRON::Pan2Value( pReadBuffer[3], pReadBuffer[4] ) );
m_Dopler.dwTimeout = *(LPDWORD)(pReadBuffer + 5);
LGLOG( LOG_LEVEL_LOW, LOG_TYPE_INFO, this, _T("GET HEBRON GET DOPLER"), _T("%d,%d,%d"), m_Dopler.bEnable, m_Dopler.dPanStep, m_Dopler.dwTimeout );
// ready to process new message
OnPerformCmd();
OnEvtGetDopler( m_Dopler.bEnable, m_Dopler.dPanStep, m_Dopler.dwTimeout );
break;
case CMD_SET_IRIS:
m_Iris = PTZ_HEBRON::Iris2Percent( pReadBuffer[2] );
LGLOG( LOG_LEVEL_LOW, LOG_TYPE_INFO, this, _T("GET HEBRON SET IRIS"), _T("%d"), m_Iris );
// ready to process new message
OnPerformCmd();
OnEvtSetIris( m_Iris );
break;
case CMD_SET_DOPLER_MASK:
if( m_nMaskIndex == -1 )
{
if( m_Mask.bEnable != pReadBuffer[2] || m_Mask.nCount != pReadBuffer[3] )
break;
}
else
{
// if last command (store eeprom)
if( m_nMaskIndex == m_Mask.nCount )
;
else
if( m_nMaskIndex != pReadBuffer[2] )
break;
else
{
WORD nStartAngle = PTZ_HEBRON::Pan2Degree( PTZ_HEBRON::Pan2Value( pReadBuffer[3], pReadBuffer[4] ) );
WORD nStopAngle = PTZ_HEBRON::Pan2Degree( PTZ_HEBRON::Pan2Value( pReadBuffer[5], pReadBuffer[6] ) );
LGLOG( LOG_LEVEL_LOW, LOG_TYPE_INFO, this, _T("CMD_SET_DOPLER_MASK"), _T("%d %d %d %d"), m_Mask.nCount, m_nMaskIndex, nStartAngle, nStopAngle );
}
}
m_nMaskIndex++;
if( m_nMaskIndex <= m_Mask.nCount )
break;
LGLOG( LOG_LEVEL_LOW, LOG_TYPE_INFO, this, _T("CMD_SET_DOPLER_MASK"), _T("DONE") );
// ready to process new message
OnPerformCmd();
OnEvtSetDoplerMask( &m_Mask );
break;
case CMD_GET_DOPLER_MASK:
if( m_nMaskIndex == -1 )
{
m_Mask.bEnable = pReadBuffer[2];
m_Mask.nCount = pReadBuffer[3];
}
else
{
if( m_nMaskIndex != pReadBuffer[2] )
break;
INT nStartAngle = PTZ_HEBRON::Pan2Degree( PTZ_HEBRON::Pan2Value( pReadBuffer[3], pReadBuffer[4] ) );
INT nStopAngle = PTZ_HEBRON::Pan2Degree( PTZ_HEBRON::Pan2Value( pReadBuffer[5], pReadBuffer[6] ) );
LGLOG( LOG_LEVEL_LOW, LOG_TYPE_INFO, this, _T("CMD_GET_DOPLER_MASK"), _T("%d %d %d %d"), m_Mask.nCount, m_nMaskIndex, nStartAngle, nStopAngle );
m_Mask.pMask[m_nMaskIndex].nStartAngle = nStartAngle;
m_Mask.pMask[m_nMaskIndex].nStopAngle = nStopAngle;
}
m_nMaskIndex++;
if( m_nMaskIndex < m_Mask.nCount )
break;
LGLOG( LOG_LEVEL_LOW, LOG_TYPE_INFO, this, _T("CMD_GET_DOPLER_MASK DONE"), _T("") );
// ready to process new message
OnPerformCmd();
OnEvtGetDoplerMask( &m_Mask );
break;
case CMD_NONE:
if( !m_bModeDopler )
break;
alarm.bAlarm = pReadBuffer[2];
alarm.dPan = PTZ_HEBRON::Pan2Degree( PTZ_HEBRON::Pan2Value( pReadBuffer[3], pReadBuffer[4] ) );
if( !alarm.bAlarm )
break;
LGLOG( LOG_LEVEL_LOW, LOG_TYPE_INFO, this, _T("GET HEBRON DOPLER ALARM"), _T("%d,%d"), alarm.bAlarm, alarm.dPan );
// ready to process new message
OnPerformCmd();
OnEvtDoplerAlarm( alarm.bAlarm, alarm.dPan );
break;
}
return TRUE;
}