#include "rgooseCamera.h" #include "../components/base/basicCameraInfo.h" #include "../components/object/objectSystemAnchor.h" #include "../hardware/uart/serialPort.h" #include "../hardware/uart/serialSOL.h" #include "../data/protocol/protocolPTZ_VISCA.h" #include "../data/protocol/protocolPTZ_ONVIF.h" #include "../data/protocol/protocolPTZ_PELCO.h" #define LOG( X ) m_pLog->Log( "RCAM", this, X ) #ifdef DEBUG #define LOG_ERROR( X ) {\ char _tBfrErrMSG2w34[256];\ sprintf( _tBfrErrMSG2w34, "In file %s[%d] in func:%s %s",__FILE__, __LINE__, __func__, X );\ m_pLog->Log( "RCAM", this, _LOG_ERROR, _tBfrErrMSG2w34 );\ } #else #define LOG_ERROR( X ) m_pLog->Log( "RCAM", this, _LOG_ERROR, X ); #endif // all sizes in mm static float AC_imager_diag[12] = { 5.0f, 5.68f, 6.0f, 6.592f, 8.0f, 8.933f, 11.0f, 16.0f, 22.5f, 30.1f, 43.3f, 69.7f }; static float AC_imager_width[12] = { 4.0f, 4.536f, 4.8f, 5.27f, 6.4f, 7.176f, 8.8f, 12.8f, 18.0f, 25.1f, 36.0f, 56.0f }; static float AC_imager_height[12] = { 3.0f, 3.416f, 3.6f, 3.96f, 4.8f, 5.319f, 6.6f, 9.6f, 13.5f, 16.7f, 24.0f, 41.5f }; ARGUS_Camera::ARGUS_Camera(): basicThread(){ m_pLog = basicLogCollection::getInstance(); m_pEvtHeap = basicEventHeap::getInstance(); m_tTimeTSeconds = 0; m_pPosition = NULL; m_pProtocol = NULL; m_pTransport = NULL; m_bState = 0; m_bBusy = true; m_fMaxRange = m_fMinHFov = m_fMaxHFov = m_fMinVFov = m_fMaxVFov = .0f; m_pDBC = argusDBC::getInstance(); }; ARGUS_Camera::~ARGUS_Camera(){ if( m_pTransport!= NULL ) { DBC_UART_DATA *tData = NULL; tData = m_pDBC->getUartData( m_sCamData.uart_id ); switch( tData->type ) { case UART_TYPE_HARDWARE:{ delete((serialPort*)m_pTransport); break; }; case UART_TYPE_SOL:{ delete((serialSOL*)m_pTransport); break; }; }; }; if( m_pProtocol != NULL ) { switch( m_sCamData.protocol ) { case CAM_CTRL_PROTO_VISCA: delete( (protocolPTZ_VISCA *) m_pProtocol ); break; case CAM_CTRL_PROTO_PELCO: delete( (protocolPTZ_PELCO *) m_pProtocol ); break; case CAM_CTRL_PROTO_ONVIF: delete( (protocolPTZ_ONVIF *) m_pProtocol ); break; default: delete( m_pProtocol ); }; }; if( m_pPosition != NULL ) delete( m_pPosition ); }; bool ARGUS_Camera::create( uint32_t id ){ DBC_CAM_DATA * tData = m_pDBC->getCamData( id ); if( tData != NULL ) { memcpy( &m_sCamData, tData, sizeof(DBC_CAM_DATA) ); free( tData ); } else { LOG_ERROR("Cannot Fetch CAM Data from Database!"); return false; }; if( m_sCamData.max_range == .0f ){ m_fMaxRange = calcMaxDistance_mm( AC_imager_diag[m_sCamData.imager_type], m_sCamData.max_f, 1280 ); } else { m_fMaxRange = m_sCamData.max_range; }; m_fMinHFov = calcFOV_mm( AC_imager_width[m_sCamData.imager_type], m_sCamData.min_f ); m_fMaxHFov = calcFOV_mm( AC_imager_width[m_sCamData.imager_type], m_sCamData.max_f ); m_fMinVFov = calcFOV_mm( AC_imager_height[m_sCamData.imager_type], m_sCamData.min_f ); m_fMaxVFov = calcFOV_mm( AC_imager_height[m_sCamData.imager_type], m_sCamData.max_f ); #ifdef DEBUG char tB[128]; LOG("Camera Info:"); sprintf( tB, "Maximal Range: %0.2f m. ", m_fMaxRange); LOG(tB); sprintf( tB, "Maximal FOVs, Horizontal: %0.2f Vertical: %0.2f", m_fMinHFov, m_fMinVFov ); LOG(tB); sprintf( tB, "Minimal FOVs, Horizontal: %0.2f Vertical: %0.2f", m_fMaxHFov, m_fMaxVFov ); LOG(tB); #endif m_pPosition = new basicPosition( m_sCamData.lat, m_sCamData.lon, m_sCamData.alt, m_sCamData.roll, m_sCamData.pitch, m_sCamData.yaw ); if( m_pPosition == NULL ) { LOG_ERROR("Position Constructor Failure!"); return false; }; if( m_pProtocol != NULL ){ LOG_ERROR("Cannot Start: Service Already Running!"); return false; }; switch( m_sCamData.protocol ){ case CAM_CTRL_PROTO_VISCA: { #ifdef DEBUG LOG("VISCA"); #endif m_pProtocol = (protocolPTZ *) new protocolPTZ_VISCA(); if( m_pProtocol!= NULL ) { DBC_UART_DATA *tData = NULL; tData = m_pDBC->getUartData( m_sCamData.uart_id ); if( tData!= NULL ) { basicTransport *tTransport = NULL; switch( tData->type ) { case UART_TYPE_HARDWARE:{ // Hardware serialPort *tT = NULL; tT = new serialPort(); if( tT!= NULL ){ if( tT->create( tData->path, tData->speed ) ){ tTransport = (basicTransport *) tT; } else { delete( tT ); }; }; break; }; case UART_TYPE_SOL:{ // E2S serialSOL *tT = NULL; tT = new serialSOL( (SOL_TYPE) tData->mode ); if( tT!= NULL ){ if( tT->create( tData->path, tData->port, tData->speed ) ){ tTransport = (basicTransport *) tT; } else { delete( tT ); }; }; break; }; }; if( tTransport!= NULL ) { m_pProtocol->setTransport( tTransport ); }; free( tData ); }; }; break; }; case CAM_CTRL_PROTO_PELCO:{ #ifdef DEBUG LOG("PELCO"); #endif m_pProtocol = (protocolPTZ *) new protocolPTZ_PELCO(); if( m_pProtocol!= NULL ) { DBC_UART_DATA *tData = NULL; tData = m_pDBC->getUartData( m_sCamData.uart_id ); if( tData!= NULL ) { basicTransport *tTransport = NULL; switch( tData->type ) { case UART_TYPE_HARDWARE:{ #ifdef DEBUG LOG("HW Port"); #endif serialPort *tT = NULL; tT = new serialPort(); if( tT!= NULL ){ if( tT->create( tData->path, tData->speed ) ){ tTransport = (basicTransport *) tT; } else { delete( tT ); }; }; break; }; case UART_TYPE_SOL:{ #ifdef DEBUG LOG("SOL Port"); #endif serialSOL *tT = NULL; tT = new serialSOL( (SOL_TYPE)tData->mode ); if( tT!= NULL ){ if( tT->create( tData->path, tData->port, tData->speed ) ){ tTransport = (basicTransport *) tT; } else { delete( tT ); }; }; break; }; #ifdef DEBUG default: LOG_ERROR("Unknown Port Type!"); #endif }; if( tTransport!= NULL ) { LOG( "Bind Transport Port!" ); if( m_pProtocol->setTransport( tTransport )){ if( !m_pProtocol->create() ){ LOG_ERROR("Protocol <<< Create >>> Failure!"); }; } else { LOG_ERROR("Binding Port Failure!"); }; } else LOG_ERROR("Transport Creation Failure!"); free( tData ); } #ifdef DEBUG else LOG_ERROR("No Uart DATA loaddesd!"); #endif } #ifdef DEBUG else LOG_ERROR("Protocol NEW failure!"); #endif break; }; case CAM_CTRL_PROTO_ONVIF:{ #ifdef DEBUG LOG("ONVIF"); #endif m_pProtocol = (protocolPTZ *) new protocolPTZ_ONVIF( m_sCamData.ip, m_sCamData.username, m_sCamData.password ); if( m_pProtocol!=NULL ) { if( !m_pProtocol->create() ) { LOG_ERROR("Error Creating Proto!"); delete( (protocolPTZ_ONVIF*) m_pProtocol ); m_pProtocol = NULL; }; }; break; }; }; if( m_pProtocol == NULL ) { LOG_ERROR("No Proto Created!!"); m_sCamData.online = false; } else { #ifdef DEBUG LOG("Proto is OK"); #endif m_sCamData.online = init(); }; return m_sCamData.online; }; void ARGUS_Camera::setBusy( bool Status ){ m_bBusy = Status; }; bool ARGUS_Camera::relativeMove( float PanSPD, float TiltSPD, float ZoomSPD ){ if( m_sCamData.online ) { uint32_t MoveMatrix = 0; if( PanSPD != .0f ){ if( PanSPD > 0 ) { MoveMatrix |= PTZ_DRIVE_PAN_RIGHT; } else { MoveMatrix |= PTZ_DRIVE_PAN_LEFT; }; }; if( TiltSPD != .0f ){ if( TiltSPD > 0 ) { MoveMatrix |= PTZ_DRIVE_TILT_UP; } else { MoveMatrix |= PTZ_DRIVE_TILT_DOWN; }; }; if( ZoomSPD != .0f ){ if( ZoomSPD > 0 ) { MoveMatrix |= PTZ_DRIVE_ZOOM_WIDE; } else { MoveMatrix |= PTZ_DRIVE_ZOOM_TELE; }; }; if( MoveMatrix!= 0 ) { m_pProtocol->setPTZSpeed( PanSPD, TiltSPD, ZoomSPD ); }; return m_pProtocol->move( MoveMatrix ); }; return false; }; bool ARGUS_Camera::relativeStop(){ if( m_sCamData.online ) { return m_pProtocol->move( 0 ); }; return false; }; bool ARGUS_Camera::absoluteMove( float PanPOS, float TiltPOS, float ZoomPOS ){ if( m_sCamData.online ) { CAM_PTZ_CMD *tCMD = (CAM_PTZ_CMD*) malloc( sizeof(CAM_PTZ_CMD)); if( tCMD ) { memset( tCMD, 0 , sizeof(CAM_PTZ_CMD)); tCMD->CMD = PTZ_ABSOLUTE; tCMD->PAN = PanPOS; tCMD->TILT = TiltPOS; tCMD->ZOOM = ZoomPOS; m_mQPROTECTOR.lock(); m_qCAM_CMD.push( tCMD ); m_mQPROTECTOR.unlock(); return true; }; }; return false; }; void ARGUS_Camera::m_fThreadProc( void ){ m_bThreadStatus = TH_WORKING; while (m_bThreadStatus < TH_ENDED) { switch( m_bState ) { case CAM_WS_INIT:{ // Init Camera - PowerMode if( m_pProtocol->initCamera() ) m_bState = CAM_WS_WHITE_BALLANCE; break; }; case CAM_WS_WHITE_BALLANCE:{ // Set Speed if( m_pProtocol->setWhiteBalanceMode( WB_AUTO ) ) m_bState = CAM_WS_BACKLIGHT; break; }; case CAM_WS_BACKLIGHT:{ // Go 0 if( m_pProtocol->setBackLightMode( true ) ) m_bState = CAM_WS_AUTO_FOCUS; //if( m_pProtocol->goHomePosition() ) m_bState++; break; }; case CAM_WS_AUTO_FOCUS:{ if( m_pProtocol->setAutoFocus( true ) ) m_bState = CAM_WS_INIT_GO_HOME; break; }; case CAM_WS_INIT_GO_HOME:{ LOG("INIT GO -1 -1 -1"); if( m_pProtocol->goAbsolute( -1, -1, -1 ) ){ m_bState = CAM_WS_READY; setBusy( false ); }; break; }; case CAM_WS_READY:{ m_mQPROTECTOR.lock(); if( m_qCAM_CMD.size() > 0 ) { char tB[128]; sprintf( tB,"Queue: %d commands!", m_qCAM_CMD.size() ); LOG( tB ); CAM_PTZ_CMD *tCMD = m_qCAM_CMD.front(); if( tCMD ) { m_qCAM_CMD.pop(); m_mQPROTECTOR.unlock(); switch( tCMD->CMD ) { case PTZ_STOP:{ m_pProtocol->goStop(); setBusy( false ); break; }; case PTZ_MOVE:{ uint32_t MoveMatrix = 0; if( m_pProtocol->setPTZSpeed( tCMD->PAN, tCMD->TILT, tCMD->ZOOM ) ){ SLEEP( 5 ); m_pProtocol->move( MoveMatrix ); }; break; }; case PTZ_ABSOLUTE:{ setBusy( true ); if( !m_pProtocol->goAbsolute( tCMD->PAN, tCMD->TILT, tCMD->ZOOM ) ) { LOG_ERROR("Cannot Issue goAbsolute Command!"); //SLEEP( 1000 ); }; setBusy( false ); break; }; }; } else m_mQPROTECTOR.unlock(); } else { m_mQPROTECTOR.unlock(); SLEEP( 5 ); }; break; }; case CAM_WS_END:{ m_bThreadStatus = TH_ENDED; setBusy( true ); break; }; }; }; }; bool ARGUS_Camera::inTheFOV( float lat, float lon, float alt ){ bool RetVal = false; char tB[128]; if( m_pPosition == NULL ) return RetVal; position3D * tPOS; transform_3 * tTrans = NULL; tPOS = new position3D(); if( tPOS == NULL ) return RetVal; tPOS->setPositionWGS84( lat, lon, alt ); tTrans = m_pPosition->getTransform(); if(tTrans == NULL ){ delete( tPOS ); return RetVal; }; vector_3 A, B; C3D_SPH_DEGREES tSph; C3D_DECART tLoc; tLoc = tPOS->getXYZ(); A[0]= tLoc.x; A[1]= tLoc.y; A[2]= tLoc.z; tTrans->get_vector_local(A, B); tLoc.x = B[0]; tLoc.y = B[1]; tLoc.z = B[2]; decart2spherical( tLoc, tSph ); // sprintf( tB,"Object Dist: %0.2f ... cam range: %0.2f", tSph.r, m_fMaxRange ); // LOG( tB ); RetVal = ( tSph.r < m_fMaxRange ); if( RetVal ) { //Checking if FOV is OK bool pOK = false; bool tOK = false; float HS = AC_imager_width[m_sCamData.imager_type]; float VS = AC_imager_height[m_sCamData.imager_type]; float HS2 = HS * HS; float VS2 = VS * VS; float IR = HS / VS; float IR2 = IR * IR; if( m_sCamData.pan_angles == 0 ) { // Fixed Camera pOK = ( tSph.fi > ( m_fMaxHFov / -2.0f ) ) && ( tSph.fi < ( m_fMaxHFov / 2.0f ) ); } else { if( m_sCamData.pan_angles > 359 ) { // Endless Pan pOK = true; } else { // Partial Pan switch( m_sCamData.pan_model ) { case 1: { // Regular Pan float tFOV = 0.5f * ( m_fMinHFov + ( m_fMaxHFov - m_fMinHFov ) * ( tSph.r / m_fMaxRange ) ) ; float tRad = deg2rad( tSph.fi ); // sprintf( tB, "FOV: %0.3f CC: %0.3f", tFOV, tRad ); //LOG( tB ); pOK = ( tRad > ( -1.0 * tFOV ) ) && ( tRad < ( deg2rad( m_sCamData.pan_angles ) + tFOV ) ) ; break; }; case 2:{ break; }; case 3:{ break; }; }; }; }; if( m_sCamData.tilt_angles == 0 ) { //Fixed Camera tOK = ( tSph.tetta > ( m_fMaxVFov / -2.0f ) ) && ( tSph.tetta < ( m_fMaxVFov / 2.0f ) ); } else { if( m_sCamData.tilt_angles > 89 ) { // Endless Tilt tOK = true; } else { // Partial Tilt switch( m_sCamData.tilt_model ) { case 1: { // Regular Tilt float tFOV = 0.5f * ( m_fMinVFov + ( m_fMaxVFov - m_fMinVFov ) * ( tSph.r / m_fMaxRange ) ) ; float tRad = deg2rad( tSph.tetta ); tOK = ( tRad > ( -1.0 * tFOV ) ) && ( tRad < ( deg2rad( m_sCamData.tilt_angles ) + tFOV ) ) ; break; }; case 2:{ break; }; }; }; }; //sprintf( tB, "Pan: %d Tilt: %d", pOK, tOK ); //LOG( tB ); RetVal = ( pOK && tOK ); } else { //LOG("Object Too Far!"); } delete( tPOS ); return RetVal; }; bool ARGUS_Camera::lookAtWGS84( uint32_t TargetRefID, float lat, float lon, float alt ){ C3D_WGS84_LOC tWGS; C3D_SPH_DEGREES tSph; char tB[128]; //sprintf(tB,"LookAt: %0.5f %0.5f %0.2f", lat, lon, alt ); //LOG( tB ); if( m_bBusy ){ //LOG("CAM BUSY!"); return false; }; // Exit for fixed Cam if( ( m_sCamData.pan_model == 0 ) && ( m_sCamData.tilt_model == 0 ) ) return true; // Exit for unpositioned Cam if( m_pPosition == NULL ) return false; tWGS.lat = lat; tWGS.lon = lon; tWGS.alt = alt; systemAnchor* tSystem = systemAnchor::getInstance(); if( tSystem != NULL ) { tSystem->convertToLocal( m_pPosition, tWGS, tSph ); } else { LOG_ERROR("No SYSTEM ANCHOR!"); return false; }; //sprintf( tB, "local coords: %0.2fm. %0.2f° %0.2f°", tSph.r, tSph.tetta, tSph.fi ); //LOG( tB ); float fZ, fT, fP; if( m_fMaxRange > 0.0f ) { if( tSph.r > m_fMaxRange ) { fZ = 1; } else { fZ = tSph.r / m_fMaxRange; }; } else fZ = 0.f; switch( m_sCamData.pan_model ){ case 1:{ fP = ( 2.0 / m_sCamData.pan_angles ) * tSph.tetta - 1.0; break; }; case 2:{ fP = ( 2.0 / 360.0 ) * tSph.tetta - 1.0; }; }; switch( m_sCamData.tilt_model ) { case 1:{ fT = -2.0f * ( ( m_sCamData.tilt_angles - tSph.fi ) / m_sCamData.tilt_angles ) + 1.0; break; }; case 2:{ fT = ( 2.0 / m_sCamData.tilt_angles ) * ( m_sCamData.tilt_angles - tSph.fi ) - 1.0; break; }; }; //sprintf(tB,"EVT:%d AM: %0.2f %0.2f %0.2f", TargetRefID, fP, fT, fZ ); //LOG( tB ); if( absoluteMove( fP, fT, fZ ) ) { #ifdef DEBUG //LOG("CHECK CAMERA STREAMS"); #endif EVENT_CONTAINER *tEVT; DBC_INT_LIST *tStreams; tStreams = m_pDBC->listCamMediaStreams( m_sCamData.id ); if( tStreams!= NULL ) { for( uint32_t idx = 0; idx < tStreams->size(); idx++ ) { tEVT = createBasicTargetedEvent( m_sCamData.node_id, tStreams->at(idx), EVT_CLASS_VIDEO, EVT_CAPTURE_START ); if( tEVT != NULL ) { tEVT->m_ref_id = TargetRefID; m_pEvtHeap->broadcast( tEVT ); }; }; delete( tStreams ); }; return true; }; return false; }; #undef LOG #undef LOG_ERROR