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.

2803 lines
66 KiB

​
​
​
​
​
​
​
3 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
2 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
2 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
2 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
2 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
2 years ago
​
​
​
​
​
​
​
​
​
2 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
3 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
2 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
2 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
2 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. #include "deviceNodeCamera.h"
  2. #include "../../components/base/basicCameraInfo.h"
  3. #include "../../components/object/objectSystemAnchor.h"
  4. #include "../../data/protocol/protocolPTZ_ONVIF.h"
  5. #include "../../data/protocol/protocolPTZ_PELCO.h"
  6. #include "../../data/protocol/protocolPTZ_VISCA.h"
  7. #include "../../hardware/uart/serialPort.h"
  8. #include "../../hardware/uart/serialSOL.h"
  9. #include "../mojo/mojoKernel.h"
  10. #include "hardwareManager.h"
  11. #include "hardwareResourceSerialProvider.h"
  12. #include "kernelModuleEmbedVideo.h"
  13. #define LOG(X) deviceNode::m_pLog->Log("RCAM", this, X)
  14. // #define DEBUG_EXTRA
  15. #ifdef DEBUG
  16. #define LOG_ERROR(X) \
  17. { \
  18. char _tBfrErrMSG2w34[256]; \
  19. sprintf(_tBfrErrMSG2w34, "In file %s[%d] in func:%s %s", __FILE__, __LINE__, __func__, X); \
  20. deviceNode::m_pLog->Log("RCAM", this, _LOG_ERROR, _tBfrErrMSG2w34); \
  21. }
  22. #else
  23. #define LOG_ERROR(X) deviceNode::m_pLog->Log("RCAM", this, _LOG_ERROR, X);
  24. #endif
  25. // all sizes in mm
  26. static const 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};
  27. static const 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};
  28. static const 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};
  29. #define CAM_FOCAL_LENGTH_CNT 49
  30. static const float cam_predef_focal_length[CAM_FOCAL_LENGTH_CNT] = {2.7f, 3.0f, 3.2f, 3.5f, 3.7f, 3.9f, 4.0f, 4.3f, 4.8f, 5.0f, 5.1f, 5.5f, 5.8f, 6.0f, 6.2f, 6.6f, 7.0f, 7.1f, 7.5f, 7.6f, 8.0f, 8.6f, 8.9f, 9.0f, 9.2f, 9.3f, 10.0f, 10.5f, 10.6f,
  31. 10.7f, 11.0f, 11.3f, 12.0f, 12.9f, 13.0f, 13.5f, 13.6f, 14.0f, 14.6f, 15.0f, 15.1f, 15.5f, 16.0f, 17.0f, 17.5f, 17.7f, 18.0f, 18.2f, 19.0f};
  32. deviceNodeCamera::deviceNodeCamera(uint32_t NodeID) :
  33. deviceNode(NodeID),
  34. basicThread()
  35. {
  36. // m_pLog = basicLogCollection::getInstance();
  37. m_pBEH = basicEventHeap::getInstance();
  38. m_bSkeepPositioningWait = true;
  39. m_bNoAbsPositionning = false;
  40. m_bCameraOnHomePosition = false;
  41. m_tTimeTSeconds = 0;
  42. m_pProtocol = NULL;
  43. // m_pTransport = NULL;
  44. m_pCurrentROI = NULL;
  45. m_nRealFrameWidth = 640;
  46. // m_bState = 0;
  47. // m_bBusy = true;
  48. m_fMaxRange = m_fMinHFov = m_fMaxHFov = m_fMinVFov = m_fMaxVFov = .0f;
  49. m_nRelativeMoveCounter = 0;
  50. m_bRelativeMoveMatrix = 0;
  51. m_bCurrentPresetCol = 0;
  52. m_bCurrentPresetRow = 0;
  53. m_bShineOnTarget = false;
  54. m_bCameraIsStatic = false;
  55. m_bIRIsOn = false;
  56. m_bIRTO = 0;
  57. m_fSpeedRise = 0.03f;
  58. m_fZoomCorrection = 1.0f;
  59. m_fRM_Pan = m_fRM_Tilt = m_fRM_Zoom = .0f;
  60. // m_bSourceConnected = false;
  61. // m_pSource = NULL;
  62. }
  63. deviceNodeCamera::deviceNodeCamera(DBC_NODE_DATA * Data) :
  64. deviceNode(Data),
  65. basicThread()
  66. {
  67. // m_pLog = basicLogCollection::getInstance();
  68. m_pBEH = basicEventHeap::getInstance();
  69. m_bSkeepPositioningWait = true;
  70. m_bNoAbsPositionning = false;
  71. m_bCameraOnHomePosition = false;
  72. m_tTimeTSeconds = 0;
  73. m_pProtocol = NULL;
  74. // m_pTransport = NULL;
  75. m_pCurrentROI = NULL;
  76. m_nRealFrameWidth = 640;
  77. // m_bState = 0;
  78. // m_bBusy = true;
  79. m_fMaxRange = m_fMinHFov = m_fMaxHFov = m_fMinVFov = m_fMaxVFov = .0f;
  80. m_nRelativeMoveCounter = 0;
  81. m_bRelativeMoveMatrix = 0;
  82. m_bCurrentPresetCol = 0;
  83. m_bCurrentPresetRow = 0;
  84. m_bShineOnTarget = false;
  85. m_bIRIsOn = false;
  86. m_bIRTO = 0;
  87. m_fSpeedRise = 0.03f;
  88. m_fZoomCorrection = 1.0f;
  89. m_fRM_Pan = m_fRM_Tilt = m_fRM_Zoom = .0f;
  90. // m_bSourceConnected = false;
  91. // m_pSource = NULL;
  92. }
  93. deviceNodeCamera::~deviceNodeCamera()
  94. {
  95. LOG("Destruct");
  96. // m_mQPROTECTOR.unlock();
  97. waitThreadEnd(true);
  98. LOG("Thread Del OK");
  99. /*if( m_pTransport!= NULL ) {
  100. if( m_sCamData.uart_id ) {
  101. DBC_UART_DATA *tData = NULL;
  102. tData = m_pDBC->getUartData( m_sCamData.uart_id );
  103. switch( tData->type ) {
  104. case UART_TYPE_HARDWARE:{
  105. delete((serialPort*)m_pTransport);
  106. break;
  107. };
  108. case UART_TYPE_SOL:{
  109. delete((serialSOL*)m_pTransport);
  110. break;
  111. };
  112. };
  113. };
  114. };
  115. LOG("Transport Del OK");
  116. */
  117. if (m_pProtocol != NULL)
  118. {
  119. switch (m_sCamData.protocol)
  120. {
  121. case CAM_CTRL_PROTO_VISCA: delete((protocolPTZ_VISCA *) m_pProtocol); break;
  122. case CAM_CTRL_PROTO_PELCO: delete((protocolPTZ_PELCO *) m_pProtocol); break;
  123. case CAM_CTRL_PROTO_ONVIF: delete((protocolPTZ_ONVIF *) m_pProtocol); break;
  124. default : delete(m_pProtocol);
  125. };
  126. };
  127. LOG("Proto Del OK");
  128. // if( m_pPosition != NULL ) delete( m_pPosition );
  129. // LOG("Pos Del OK");
  130. // if( m_bSourceConnected ){
  131. // if( m_pSource != NULL ){
  132. // m_pSource->_close();
  133. // delete( m_pSource );
  134. // };
  135. // };
  136. // LOG("Stream Del OK");
  137. }
  138. void deviceNodeCamera::updateOpticalParameters(uint16_t frame_width, uint16_t frame_heigth)
  139. {
  140. #ifdef DEBUG
  141. char tB[128];
  142. LOG("Set Optical Parameters");
  143. sprintf(tB, "\tCapturing Frame: %dx%d ", frame_width, frame_heigth);
  144. LOG(tB);
  145. #endif
  146. if (frame_width > 0 && frame_heigth > 0)
  147. {
  148. m_nRealFrameWidth = frame_width;
  149. m_nRealFrameHeight = frame_heigth;
  150. float tFrameDiag = sqrtf(m_nRealFrameHeight * m_nRealFrameHeight + m_nRealFrameWidth * m_nRealFrameWidth);
  151. switch (m_sCamData.zoom_model)
  152. {
  153. case 0:
  154. {
  155. #ifdef DEBUG
  156. LOG("\tFixed Zoom");
  157. #endif
  158. m_fMaxFL = m_fMinFL = cam_predef_focal_length[m_sCamData.lens_f];
  159. m_fMinRange = calcMinDistance_mm(AC_imager_diag[m_sCamData.imager_type], m_fMinFL, tFrameDiag);
  160. m_fMaxRange = calcMaxDistance_mm(AC_imager_diag[m_sCamData.imager_type], m_fMaxFL, tFrameDiag);
  161. m_fR2FL = .0f;
  162. m_fZoomCorrection = .0f;
  163. m_pDBC->updateCamRange(m_sCamData.id, m_fMaxRange);
  164. break;
  165. };
  166. case 1:
  167. {
  168. #ifdef DEBUG
  169. LOG("\tAutomated Zoom");
  170. #endif
  171. m_fMinFL = cam_predef_focal_length[m_sCamData.lens_f];
  172. m_fMinRange = calcMinDistance_mm(AC_imager_diag[m_sCamData.imager_type], m_fMinFL, tFrameDiag);
  173. m_fMaxFL = cam_predef_focal_length[m_sCamData.lens_f] * m_sCamData.zoom;
  174. m_fMaxRange = calcMaxDistance_mm(AC_imager_diag[m_sCamData.imager_type], m_fMaxFL, tFrameDiag);
  175. if (m_fMaxRange > m_fMinRange)
  176. {
  177. m_fR2FL = ((m_fMaxFL - m_fMinFL) / (m_fMaxRange - m_fMinRange));
  178. }
  179. else
  180. {
  181. m_fR2FL = .0f;
  182. };
  183. m_fZoomCorrection = 1.0f;
  184. m_pDBC->updateCamRange(m_sCamData.id, m_fMaxRange);
  185. break;
  186. };
  187. case 2:
  188. {
  189. #ifdef DEBUG
  190. LOG("\tManual Zoom");
  191. #endif
  192. m_fMinRange = 5.0f; // Minimal 5 meters
  193. if (m_sCamData.max_range < m_fMinRange)
  194. {
  195. m_fMaxRange = m_fMinRange;
  196. }
  197. else
  198. {
  199. m_fMaxRange = m_sCamData.max_range;
  200. };
  201. m_fZoomCorrection = m_sCamData.zoom * 0.01f;
  202. m_fMinFL = calcCFL(AC_imager_width[m_sCamData.imager_type], m_fMinRange, tFrameDiag);
  203. m_fMaxFL = calcCFL(AC_imager_width[m_sCamData.imager_type], m_fMaxRange, tFrameDiag);
  204. break;
  205. };
  206. };
  207. m_fMinHFov = rad2deg(calcFOV_mm(AC_imager_width[m_sCamData.imager_type], m_fMaxFL));
  208. m_fMaxHFov = rad2deg(calcFOV_mm(AC_imager_width[m_sCamData.imager_type], m_fMinFL));
  209. m_fMinVFov = rad2deg(calcFOV_mm(AC_imager_height[m_sCamData.imager_type], m_fMaxFL));
  210. m_fMaxVFov = rad2deg(calcFOV_mm(AC_imager_height[m_sCamData.imager_type], m_fMinFL));
  211. #ifdef DEBUG
  212. sprintf(tB, "\tMinimal FL: %0.3fmm. Maximal FL: %0.3fmm. ", m_fMinFL, m_fMaxFL);
  213. LOG(tB);
  214. sprintf(tB, "\tMinimal Range: %0.2fm. Maximal Range: %0.2fm. ", m_fMinRange, m_fMaxRange);
  215. LOG(tB);
  216. sprintf(tB, "\tMinimal FOVs, Horizontal: %0.2f Vertical: %0.2f", m_fMinHFov, m_fMinVFov);
  217. LOG(tB);
  218. sprintf(tB, "\tMaximal FOVs, Horizontal: %0.2f Vertical: %0.2f", m_fMaxHFov, m_fMaxVFov);
  219. LOG(tB);
  220. #endif
  221. };
  222. }
  223. bool deviceNodeCamera::create()
  224. {
  225. if (m_bThreadStatus == TH_WORKING)
  226. {
  227. return true;
  228. }
  229. setWM(CAM_WS_INIT);
  230. if (m_sNodeData.aux_id == 0)
  231. {
  232. return false;
  233. }
  234. DBC_CAM_DATA * tData = m_pDBC->getCamData(m_sNodeData.aux_id);
  235. if (tData != NULL)
  236. {
  237. memcpy(&m_sCamData, tData, sizeof(DBC_CAM_DATA));
  238. free(tData);
  239. }
  240. else
  241. {
  242. LOG_ERROR("Cannot Fetch CAM Data from Database!");
  243. return false;
  244. };
  245. if (! (m_sCamData.lens_f < CAM_FOCAL_LENGTH_CNT))
  246. {
  247. m_sCamData.lens_f = CAM_FOCAL_LENGTH_CNT - 1;
  248. }
  249. updateOpticalParameters(1280, 720); // HD Ready
  250. if (m_sCamData.ptz_speed_step > .0f)
  251. {
  252. m_fSpeedRise = m_sCamData.ptz_speed_step;
  253. };
  254. // m_pPosition = new basicPosition();// m_sCamData.lat, m_sCamData.lon, m_sCamData.alt, m_sCamData.roll, m_sCamData.pitch, m_sCamData.yaw );
  255. // if( m_pPosition == NULL ) {
  256. // LOG_ERROR("Position Constructor Failure!");
  257. // return false;
  258. // };
  259. // m_oPosition.setPositionXYZ( m_sCamData.x, m_sCamData.y, m_sCamData.z );
  260. // m_oPosition.setOrientationDeg( m_sCamData.x_rot, m_sCamData.y_rot, m_sCamData.z_rot );
  261. systemAnchor * SA = systemAnchor::getInstance();
  262. if (SA)
  263. {
  264. LOG("SysAnchor OK");
  265. if (SA->beLocal())
  266. {
  267. LOG("SysAnchor beLocal!");
  268. m_oPosition.setPositionXYZ(m_sCamData.x, m_sCamData.y, m_sCamData.z, 0); // SA->getUTMZone() );
  269. m_oPosition.setOrientationDeg(m_sCamData.x_rot, m_sCamData.y_rot, m_sCamData.z_rot);
  270. }
  271. else
  272. {
  273. LOG("SysAnchor is Global Position System!");
  274. C3D_DECART LD = SA->getXYZ();
  275. m_oPosition.setPositionXYZ(LD.x + m_sCamData.x, LD.y + m_sCamData.y, LD.z + m_sCamData.z, SA->getUTMZone());
  276. m_oPosition.setOrientationDeg(m_sCamData.x_rot, m_sCamData.y_rot, m_sCamData.z_rot);
  277. };
  278. }
  279. else
  280. {
  281. LOG("No SysAnchor!");
  282. m_oPosition.setPositionXYZ(m_sCamData.x, m_sCamData.y, m_sCamData.z);
  283. m_oPosition.setOrientationDeg(m_sCamData.x_rot, m_sCamData.y_rot, m_sCamData.z_rot);
  284. };
  285. m_oPosition.updateMatrix();
  286. #ifdef DEBUG
  287. char tB[128];
  288. sprintf(tB, "Finalized Camera Location: Lat:%.3f Lon:%.3f Alt:%.3f", m_oPosition.getLat(), m_oPosition.getLon(), m_oPosition.getAlt());
  289. LOG(tB);
  290. sprintf(tB, "Finalized Camera Location: x:%.3f y:%.3f z:%.3f", m_oPosition.getX(), m_oPosition.getY(), m_oPosition.getZ());
  291. LOG(tB);
  292. sprintf(tB, "Orientation: x:%.3f y:%.3f z:%.3f", m_oPosition.getRotX_deg(), m_oPosition.getRotY_deg(), m_oPosition.getRotZ_deg());
  293. LOG(tB);
  294. #endif
  295. if (m_pProtocol != NULL)
  296. {
  297. LOG_ERROR("Cannot Start: Service Already Running!");
  298. return false;
  299. };
  300. #ifdef CAM_POST_INIT
  301. if ((m_sCamData.type == ARGUS_IP_PRESET_CAMERA) || (m_sCamData.type == ARGUS_ANALOG_PRESET_CAMERA))
  302. {
  303. m_bNoAbsPositionning = true;
  304. }
  305. if (m_sCamData.protocol == 0)
  306. {
  307. m_bCameraIsStatic = true;
  308. m_bSkeepPositioningWait = true;
  309. m_sCamData.online = true;
  310. m_pDBC->updateCamOnlineStatus(m_sCamData.id, m_sCamData.online);
  311. return m_sCamData.online;
  312. };
  313. m_pDBC->updateCamOnlineStatus(m_sCamData.id, m_sCamData.online);
  314. return init();
  315. #else //! CAM_POST_INIT
  316. switch (m_sCamData.protocol)
  317. {
  318. case CAM_CTRL_PROTO_VISCA:
  319. {
  320. #ifdef DEBUG
  321. LOG("VISCA");
  322. #endif
  323. m_pProtocol = (protocolPTZ *) new protocolPTZ_VISCA(m_sCamData.protocol_aux);
  324. if (m_pProtocol != NULL)
  325. {
  326. basicTransport * tTransport = NULL;
  327. hardwareManager * tHM = hardwareManager::getInstance();
  328. if (tHM)
  329. {
  330. hardwareResource * tRES = tHM->requestResource(HARDWARE_PROVIDER_SERIAL, m_sCamData.uart_id);
  331. if (tRES != NULL)
  332. {
  333. if (tRES->isValid())
  334. {
  335. tTransport = static_cast<basicTransport *>(static_cast<hardwareResourceSerialProvider *>(tRES));
  336. if (m_pProtocol->setTransport(tTransport))
  337. {
  338. if (! m_pProtocol->create())
  339. {
  340. LOG_ERROR("Protocol <<< Create >>> Failure!");
  341. };
  342. }
  343. else
  344. {
  345. LOG_ERROR("Binding Port Failure!");
  346. };
  347. };
  348. #ifdef DEBUG
  349. }
  350. else
  351. {
  352. LOG("HWM Has No UART PORT!");
  353. #endif
  354. };
  355. #ifdef DEBUG
  356. }
  357. else
  358. {
  359. LOG_ERROR("Can not get Hardware Manager!");
  360. #endif
  361. };
  362. if (tTransport != NULL)
  363. {
  364. LOG("Bind Transport Port!");
  365. }
  366. else
  367. {
  368. LOG_ERROR("Transport Creation Failure!");
  369. delete((protocolPTZ_VISCA *) m_pProtocol);
  370. m_pProtocol = NULL;
  371. };
  372. #ifdef DEBUG
  373. }
  374. else
  375. {
  376. LOG_ERROR("Protocol Creation failure!");
  377. #endif
  378. };
  379. break;
  380. };
  381. case CAM_CTRL_PROTO_PELCO:
  382. {
  383. #ifdef DEBUG
  384. LOG("PELCO");
  385. #endif
  386. m_pProtocol = (protocolPTZ *) new protocolPTZ_PELCO(m_sCamData.protocol_aux);
  387. if (m_pProtocol != NULL)
  388. {
  389. basicTransport * tTransport = NULL;
  390. hardwareManager * tHM = hardwareManager::getInstance();
  391. if (tHM)
  392. {
  393. hardwareResource * tRES = tHM->requestResource(HARDWARE_PROVIDER_SERIAL, m_sCamData.uart_id);
  394. if (tRES != NULL)
  395. {
  396. if (tRES->isValid())
  397. {
  398. tTransport = static_cast<basicTransport *>(static_cast<hardwareResourceSerialProvider *>(tRES));
  399. if (m_pProtocol->setTransport(tTransport))
  400. {
  401. if (! m_pProtocol->create())
  402. {
  403. LOG_ERROR("Protocol <<< Create >>> Failure!");
  404. };
  405. }
  406. else
  407. {
  408. LOG_ERROR("Binding Port Failure!");
  409. };
  410. };
  411. #ifdef DEBUG
  412. }
  413. else
  414. {
  415. LOG("HWM Has No UART PORT!");
  416. #endif
  417. };
  418. #ifdef DEBUG
  419. }
  420. else
  421. {
  422. LOG_ERROR("Can not get Hardware Manager!");
  423. #endif
  424. };
  425. if (tTransport != NULL)
  426. {
  427. LOG("Bind Transport Port!");
  428. }
  429. else
  430. {
  431. LOG_ERROR("Transport Creation Failure!");
  432. delete((protocolPTZ_PELCO *) m_pProtocol);
  433. m_pProtocol = NULL;
  434. };
  435. #ifdef DEBUG
  436. }
  437. else
  438. {
  439. LOG_ERROR("Protocol Creation failure!");
  440. #endif
  441. };
  442. break;
  443. };
  444. case CAM_CTRL_PROTO_ONVIF:
  445. {
  446. #ifdef DEBUG_EXTRA
  447. LOG("ONVIF");
  448. #endif
  449. // m_pProtocol = (protocolPTZ *) new protocolPTZ_ONVIF( m_sCamData.ip, m_sCamData.port, m_sCamData.username, m_sCamData.password, m_sCamData.auth_mode );
  450. if (strstr(m_sCamData.onvif_ctrl, "http") == NULL)
  451. {
  452. char tURI[256];
  453. if (m_sCamData.port)
  454. {
  455. sprintf(tURI, "http://%s:%d%s", m_sCamData.ip, m_sCamData.port, m_sCamData.onvif_ctrl);
  456. }
  457. else
  458. {
  459. sprintf(tURI, "http://%s%s", m_sCamData.ip, m_sCamData.onvif_ctrl);
  460. };
  461. m_pProtocol = (protocolPTZ *) new protocolPTZ_ONVIF(tURI, m_sCamData.username, m_sCamData.password, m_sCamData.auth_mode);
  462. }
  463. else
  464. {
  465. m_pProtocol = (protocolPTZ *) new protocolPTZ_ONVIF(m_sCamData.onvif_ctrl, m_sCamData.username, m_sCamData.password, m_sCamData.auth_mode);
  466. };
  467. #ifdef DEBUG_EXTRA
  468. LOG("ONVIF-END");
  469. #endif
  470. if (m_pProtocol != NULL)
  471. {
  472. if (! m_pProtocol->create())
  473. {
  474. LOG_ERROR("Error Creating Proto!");
  475. delete((protocolPTZ_ONVIF *) m_pProtocol);
  476. m_pProtocol = NULL;
  477. };
  478. #ifdef DEBUG_EXTRA
  479. LOG("PROTO CREATE DONE");
  480. #endif
  481. };
  482. break;
  483. };
  484. default:
  485. {
  486. #ifdef DEBUG
  487. LOG("Camera Has no defined control protocol");
  488. #endif
  489. };
  490. };
  491. // Check - No ABS Movement!
  492. if ((m_sCamData.type == ARGUS_IP_PRESET_CAMERA) || (m_sCamData.type == ARGUS_ANALOG_PRESET_CAMERA))
  493. {
  494. m_bNoAbsPositionning = true;
  495. }
  496. if (m_pProtocol == NULL)
  497. {
  498. if (m_sCamData.protocol == 0)
  499. {
  500. m_bCameraIsStatic = true;
  501. m_bSkeepPositioningWait = true;
  502. m_sCamData.online = true;
  503. }
  504. else
  505. {
  506. LOG_ERROR("No Proto Created!");
  507. }
  508. }
  509. else
  510. {
  511. #ifdef DEBUG
  512. LOG("Proto is OK");
  513. #endif
  514. // Load Presets only if we have protocol
  515. load_presets();
  516. mojoKernel * tKRNL = mojoKernel::getInstance();
  517. if (tKRNL)
  518. {
  519. m_bSkeepPositioningWait = ((tKRNL->getMediaMode() & EVE_OUTPUT_VIDEO) == EVE_OUTPUT_VIDEO);
  520. #ifdef DEBUG
  521. char ttTT[128];
  522. sprintf(ttTT, "Kernel Media Mode:%d", tKRNL->getMediaMode());
  523. LOG(ttTT);
  524. if (m_bSkeepPositioningWait)
  525. {
  526. LOG("Camera No Wait On Pos!");
  527. }
  528. else
  529. {
  530. LOG("Camera Will Wait On Pos!");
  531. };
  532. #endif
  533. }
  534. else
  535. {
  536. LOG_ERROR("No Kernel Instance!");
  537. };
  538. m_sCamData.online = init();
  539. };
  540. m_pDBC->updateCamOnlineStatus(m_sCamData.id, m_sCamData.online);
  541. return m_sCamData.online;
  542. #endif //! CAM_POST_INIT
  543. }
  544. // void deviceNodeCamera::setBusy( bool Status ){
  545. // m_bBusy = Status;
  546. //};
  547. bool deviceNodeCamera::relativeMove(float PanSPD, float TiltSPD, float ZoomSPD)
  548. {
  549. if (m_sCamData.online)
  550. {
  551. uint32_t MoveMatrix = 0;
  552. if (PanSPD != .0f)
  553. {
  554. if (PanSPD > 0)
  555. {
  556. if (m_sCamData.flip_mode == 0 || m_sCamData.flip_mode == 2)
  557. {
  558. MoveMatrix |= PTZ_DRIVE_PAN_RIGHT;
  559. }
  560. else
  561. {
  562. MoveMatrix |= PTZ_DRIVE_PAN_LEFT;
  563. };
  564. }
  565. else
  566. {
  567. if (m_sCamData.flip_mode == 0 || m_sCamData.flip_mode == 2)
  568. {
  569. MoveMatrix |= PTZ_DRIVE_PAN_LEFT;
  570. }
  571. else
  572. {
  573. MoveMatrix |= PTZ_DRIVE_PAN_RIGHT;
  574. };
  575. };
  576. };
  577. if (TiltSPD != .0f)
  578. {
  579. if (TiltSPD > 0)
  580. {
  581. if (m_sCamData.flip_mode == 0 || m_sCamData.flip_mode == 1)
  582. {
  583. MoveMatrix |= PTZ_DRIVE_TILT_UP;
  584. }
  585. else
  586. {
  587. MoveMatrix |= PTZ_DRIVE_TILT_DOWN;
  588. };
  589. }
  590. else
  591. {
  592. if (m_sCamData.flip_mode == 0 || m_sCamData.flip_mode == 1)
  593. {
  594. MoveMatrix |= PTZ_DRIVE_TILT_DOWN;
  595. }
  596. else
  597. {
  598. MoveMatrix |= PTZ_DRIVE_TILT_UP;
  599. };
  600. };
  601. };
  602. if (ZoomSPD != .0f)
  603. {
  604. if (ZoomSPD > 0)
  605. {
  606. MoveMatrix |= PTZ_DRIVE_ZOOM_WIDE;
  607. }
  608. else
  609. {
  610. MoveMatrix |= PTZ_DRIVE_ZOOM_TELE;
  611. };
  612. };
  613. if (MoveMatrix != 0)
  614. {
  615. m_pProtocol->setPTZSpeed(fabs(PanSPD), fabs(TiltSPD), fabs(ZoomSPD));
  616. };
  617. return m_pProtocol->move(MoveMatrix);
  618. };
  619. return false;
  620. }
  621. bool deviceNodeCamera::relativeStop()
  622. {
  623. if (m_sCamData.online)
  624. {
  625. return m_pProtocol->move(0);
  626. };
  627. return false;
  628. }
  629. CAM_TASK * deviceNodeCamera::fetchCamTask(void)
  630. {
  631. CAM_TASK * rVal = NULL;
  632. m_mQPROTECTOR.lock();
  633. if (m_qCAM_TASK.size())
  634. {
  635. rVal = m_qCAM_TASK.front();
  636. m_qCAM_TASK.pop();
  637. };
  638. m_mQPROTECTOR.unlock();
  639. return rVal;
  640. }
  641. bool deviceNodeCamera::pushCamTask(CAM_TASK * ptr)
  642. {
  643. if (ptr == NULL)
  644. {
  645. return false;
  646. }
  647. m_mQPROTECTOR.lock();
  648. m_qCAM_TASK.push(ptr);
  649. m_mQPROTECTOR.unlock();
  650. return true;
  651. }
  652. bool deviceNodeCamera::fusion_compare(CAM_ROI * ptr, C3D_SPH_DEGREES tp)
  653. {
  654. if (ptr == NULL)
  655. {
  656. return false;
  657. }
  658. float tFI = fabsf(ptr->cam_position.fi - tp.fi);
  659. float tTETTA = fabsf(ptr->cam_position.tetta - tp.tetta);
  660. #ifdef DEBUG_ALL
  661. char tB[128];
  662. sprintf(tB, "FC: FOV:[%.5f:%.5f] dTG:[%.5f:%.5f] %.2f<%.2f>%.2f", ptr->h_fov, ptr->v_fov, tFI, tTETTA, ptr->min_range, tp.r, ptr->max_range);
  663. LOG(tB);
  664. #endif
  665. if (tFI > ptr->h_fov)
  666. {
  667. return false;
  668. }
  669. if (tTETTA > ptr->v_fov)
  670. {
  671. return false;
  672. }
  673. if (tp.r < ptr->min_range)
  674. {
  675. return false;
  676. }
  677. if (tp.r > ptr->max_range)
  678. {
  679. return false;
  680. }
  681. return true;
  682. }
  683. bool deviceNodeCamera::fusion_consumeData(uint32_t db_id, C3D_SPH_DEGREES tOR)
  684. {
  685. if (db_id == 0)
  686. {
  687. return false;
  688. }
  689. CAM_TRACK_RECORD * tTrackRecord = (CAM_TRACK_RECORD *) malloc(sizeof(CAM_TRACK_RECORD));
  690. if (tTrackRecord == NULL)
  691. {
  692. return false;
  693. }
  694. #ifdef DEBUG_EXTRA
  695. LOG("Fusion: Consuming new Data!");
  696. #endif
  697. memset(tTrackRecord, 0, sizeof(CAM_TRACK_RECORD));
  698. tTrackRecord->target_direction.fi = tOR.fi;
  699. tTrackRecord->target_direction.r = tOR.r;
  700. tTrackRecord->target_direction.tetta = tOR.tetta;
  701. tTrackRecord->evt_db_ID = db_id;
  702. tTrackRecord->ts_ms = getMilliseconds();
  703. m_mROIsProtector.lock();
  704. if (m_vROIS.size())
  705. {
  706. vCAM_ROIS::iterator idx = m_vROIS.begin();
  707. for (; idx < m_vROIS.end(); idx++)
  708. {
  709. if (fusion_compare(*idx, tOR))
  710. {
  711. break;
  712. }
  713. }
  714. if (idx != m_vROIS.end())
  715. {
  716. // have Match!
  717. #ifdef DEBUG_EXTRA
  718. LOG("ROI MATCHED!");
  719. #endif
  720. (*idx)->targets.push_back(tTrackRecord);
  721. fusion_validateROI(tTrackRecord->ts_ms, *idx);
  722. }
  723. else
  724. {
  725. #ifdef DEBUG_EXTRA
  726. LOG("NO MATCH FOUND!");
  727. #endif
  728. // No mathced ROI -> create new one
  729. CAM_ROI * tROI = (CAM_ROI *) malloc(sizeof(CAM_ROI));
  730. if (tROI)
  731. {
  732. memset(tROI, 0, sizeof(CAM_ROI));
  733. tROI->ts_ms = tTrackRecord->ts_ms;
  734. spherical2onvif(tOR, tROI->cam_orientation, m_fMaxRange, m_sCamData.pan_model, m_sCamData.pan_angles, m_sCamData.tilt_model, m_sCamData.tilt_angles);
  735. tROI->cam_position.fi = tOR.fi;
  736. tROI->cam_position.tetta = tOR.tetta;
  737. tROI->cam_position.r = tOR.r;
  738. float cFL = m_fMinFL + m_fR2FL * (tROI->cam_position.r - m_fMinRange);
  739. tROI->max_range = calcMaxDistance_mm(AC_imager_diag[m_sCamData.imager_type], cFL, m_nRealFrameWidth);
  740. tROI->min_range = calcMinDistance_mm(AC_imager_diag[m_sCamData.imager_type], cFL, m_nRealFrameWidth);
  741. tROI->h_fov = rad2deg(calcFOV_mm(AC_imager_width[m_sCamData.imager_type], cFL)) / 2.0f; // div to no to div in comparation function
  742. tROI->v_fov = rad2deg(calcFOV_mm(AC_imager_height[m_sCamData.imager_type], cFL)) / 2.0f; // that will be called more ofthen
  743. tROI->targets.push_back(tTrackRecord);
  744. m_vROIS.push_back(tROI);
  745. };
  746. };
  747. }
  748. else
  749. {
  750. #ifdef DEBUG_EXTRA
  751. LOG("Fusion: NO ROI -> CREATE!");
  752. #endif
  753. //
  754. CAM_ROI * tROI = (CAM_ROI *) malloc(sizeof(CAM_ROI));
  755. if (tROI)
  756. {
  757. memset(tROI, 0, sizeof(CAM_ROI));
  758. tROI->ts_ms = tTrackRecord->ts_ms;
  759. // Set vector Cam Coordinates
  760. tROI->cam_position.fi = tOR.fi;
  761. tROI->cam_position.tetta = tOR.tetta;
  762. tROI->cam_position.r = tOR.r;
  763. // Set vector OnVIF coordinates
  764. spherical2onvif(tOR, tROI->cam_orientation, m_fMaxRange, m_sCamData.pan_model, m_sCamData.pan_angles, m_sCamData.tilt_model, m_sCamData.tilt_angles);
  765. // make fusion data BOX
  766. float cFL = m_fMinFL + m_fR2FL * (tROI->cam_position.r); // - m_fMinRange );
  767. tROI->min_range = calcMinDistance_mm(AC_imager_diag[m_sCamData.imager_type], cFL, m_nRealFrameWidth);
  768. tROI->max_range = calcMaxDistance_mm(AC_imager_diag[m_sCamData.imager_type], cFL, m_nRealFrameWidth);
  769. tROI->h_fov = rad2deg(calcFOV_mm(AC_imager_width[m_sCamData.imager_type], cFL)) / 2.0f; // div to prevent division in comparation
  770. tROI->v_fov = rad2deg(calcFOV_mm(AC_imager_height[m_sCamData.imager_type], cFL)) / 2.0f; // function that will be called more ofthen
  771. // Add ROI to vector
  772. tROI->targets.push_back(tTrackRecord);
  773. m_vROIS.push_back(tROI);
  774. }
  775. else
  776. {
  777. LOG_ERROR("No MEM4ROI");
  778. }
  779. };
  780. m_mROIsProtector.unlock();
  781. return true;
  782. }
  783. bool deviceNodeCamera::fusion_validateROI(uint64_t cts, CAM_ROI * ptr)
  784. {
  785. if (ptr == NULL)
  786. {
  787. return false;
  788. }
  789. if ((cts - ptr->ts_ms) > LOSE_INTERES_TIME_MS)
  790. {
  791. return false;
  792. }
  793. #ifdef DEBUG_EXTRA
  794. char tB[128];
  795. sprintf(tB, "\tROI [%d]", ptr->targets.size());
  796. LOG(tB);
  797. #endif
  798. if (ptr->targets.size())
  799. {
  800. double tA = 0.0;
  801. double tB = 0.0;
  802. double tC = 0.0;
  803. uint32_t tCNT = 0;
  804. for (vCAM_TRACK_RECORDS::iterator idx = ptr->targets.begin(); idx < ptr->targets.end();)
  805. {
  806. if ((cts - (*idx)->ts_ms) > LOSE_INTERES_TIME_MS)
  807. {
  808. free(*idx);
  809. ptr->targets.erase(idx);
  810. }
  811. else
  812. {
  813. tA = tA + (*idx)->target_direction.fi;
  814. tB = tB + (*idx)->target_direction.tetta;
  815. tC = tC + (*idx)->target_direction.r;
  816. tCNT++;
  817. idx++;
  818. };
  819. };
  820. if (tCNT)
  821. {
  822. C3D_SPH_DEGREES L = {0};
  823. L.fi = (float) (tA / (double) tCNT);
  824. L.tetta = (float) (tB / (double) tCNT);
  825. L.r = (float) (tC / (double) tCNT);
  826. spherical2onvif(L, ptr->cam_orientation, m_fMaxRange, m_sCamData.pan_model, m_sCamData.pan_angles, m_sCamData.tilt_model, m_sCamData.tilt_angles);
  827. ptr->ts_ms = cts;
  828. float cFL = m_fMinFL + m_fR2FL * (ptr->cam_position.r - m_fMinRange);
  829. ptr->max_range = calcMaxDistance_mm(AC_imager_diag[m_sCamData.imager_type], cFL, m_nRealFrameWidth);
  830. ptr->min_range = calcMinDistance_mm(AC_imager_diag[m_sCamData.imager_type], cFL, m_nRealFrameWidth);
  831. ptr->h_fov = rad2deg(calcFOV_mm(AC_imager_width[m_sCamData.imager_type], cFL)) / 2.0f; // div to prevent division in comparation
  832. ptr->v_fov = rad2deg(calcFOV_mm(AC_imager_height[m_sCamData.imager_type], cFL)) / 2.0f; // function that will be called more ofthen
  833. return true;
  834. };
  835. };
  836. return false;
  837. }
  838. CAM_ROI * deviceNodeCamera::fusion_fetchROI(void)
  839. {
  840. CAM_ROI * tVAL = NULL;
  841. m_mROIsProtector.lock();
  842. if (m_vROIS.size())
  843. {
  844. vCAM_ROIS::iterator sRec;
  845. uint64_t cts = 0;
  846. for (vCAM_ROIS::iterator idx = m_vROIS.begin(); idx != m_vROIS.end(); idx++)
  847. {
  848. if (tVAL == NULL)
  849. {
  850. sRec = idx;
  851. tVAL = (*idx);
  852. cts = tVAL->ts_ms;
  853. }
  854. else
  855. {
  856. #ifdef CAM_FETCH_ROI_NEWEST
  857. if ((*idx)->ts_ms > cts)
  858. #else
  859. if ((*idx)->ts_ms < cts)
  860. #endif
  861. {
  862. sRec = idx;
  863. tVAL = (*idx);
  864. cts = tVAL->ts_ms;
  865. };
  866. };
  867. };
  868. m_vROIS.erase(sRec);
  869. };
  870. m_mROIsProtector.unlock();
  871. if (m_bNoAbsPositionning && tVAL)
  872. {
  873. // Skip targets older 3 seconds
  874. if (getMilliseconds() - tVAL->ts_ms > 3000)
  875. #ifdef DEBUG_EXTRA
  876. {
  877. LOG("Targetd too old! ---> Skiping");
  878. utilizeROI(tVAL);
  879. };
  880. #else
  881. utilizeROI(tVAL);
  882. #endif
  883. };
  884. return tVAL;
  885. }
  886. DBC_SNAPSHOT_DATA * deviceNodeCamera::fusion_translate_ROI(CAM_ROI *(&ptr))
  887. {
  888. DBC_SNAPSHOT_DATA * rVal = NULL;
  889. if (ptr == NULL)
  890. {
  891. return NULL;
  892. }
  893. if (ptr->targets.size())
  894. {
  895. rVal = (DBC_SNAPSHOT_DATA *) malloc(sizeof(DBC_SNAPSHOT_DATA));
  896. if (rVal)
  897. {
  898. memset(rVal, 0, sizeof(DBC_SNAPSHOT_DATA));
  899. rVal->stream_id = m_sNodeData.aux_parameter;
  900. for (vCAM_TRACK_RECORDS::iterator idx = ptr->targets.begin(); idx < ptr->targets.end(); idx++)
  901. {
  902. rVal->assoc_event_ids.push_back((*idx)->evt_db_ID);
  903. }
  904. };
  905. ptr->targets.clear();
  906. }
  907. else
  908. {
  909. LOG_ERROR("No Events ID to ROI");
  910. };
  911. utilizeROI(ptr);
  912. // Not Setting MD5 and TS -> will set at capture time
  913. return rVal;
  914. }
  915. void deviceNodeCamera::fusion_validate(void)
  916. {
  917. if (m_bNoAbsPositionning)
  918. {
  919. return;
  920. }
  921. m_mROIsProtector.lock();
  922. if (m_vROIS.size())
  923. {
  924. #ifdef DEBUG_EXTRA
  925. char tB[128];
  926. sprintf(tB, "Fusion: Have [%d] Regions Of Interes", m_vROIS.size());
  927. LOG(tB);
  928. #endif
  929. uint64_t cts = getMilliseconds();
  930. for (vCAM_ROIS::iterator idx = m_vROIS.begin(); idx != m_vROIS.end();)
  931. {
  932. if (! fusion_validateROI(cts, *idx))
  933. {
  934. utilizeROI(*idx);
  935. m_vROIS.erase(idx);
  936. }
  937. else
  938. {
  939. idx++;
  940. }
  941. };
  942. };
  943. m_mROIsProtector.unlock();
  944. }
  945. bool deviceNodeCamera::absoluteMove(uint32_t TargetRefID, float PanPOS, float TiltPOS, float ZoomPOS)
  946. {
  947. if (m_sCamData.online)
  948. {
  949. CAM_TASK * tTask = (CAM_TASK *) malloc(sizeof(CAM_TASK));
  950. if (tTask)
  951. {
  952. memset(tTask, 0, sizeof(CAM_TASK));
  953. tTask->CMD = (CAM_PTZ_CMD *) malloc(sizeof(CAM_PTZ_CMD));
  954. if (tTask->CMD)
  955. {
  956. tTask->CMD->CMD = PTZ_ABSOLUTE;
  957. tTask->CMD->PAN = PanPOS;
  958. tTask->CMD->TILT = TiltPOS;
  959. tTask->CMD->ZOOM = ZoomPOS;
  960. tTask->movemet_needed = true;
  961. tTask->source_evt_id = TargetRefID;
  962. setWM(CAM_WS_BUSY);
  963. return pushCamTask(tTask);
  964. }
  965. else
  966. {
  967. LOG_ERROR("CMD malloc Error!");
  968. free(tTask);
  969. };
  970. }
  971. else
  972. {
  973. LOG_ERROR("Task malloc error!");
  974. }
  975. };
  976. return false;
  977. }
  978. bool deviceNodeCamera::absoluteMove(uint32_t TargetRefID, C3D_ORIENT_ONVIF O)
  979. {
  980. if (m_sCamData.online)
  981. {
  982. CAM_TASK * tTask = (CAM_TASK *) malloc(sizeof(CAM_TASK));
  983. if (tTask)
  984. {
  985. memset(tTask, 0, sizeof(CAM_TASK));
  986. tTask->CMD = (CAM_PTZ_CMD *) malloc(sizeof(CAM_PTZ_CMD));
  987. if (tTask->CMD)
  988. {
  989. tTask->CMD->CMD = PTZ_ABSOLUTE;
  990. tTask->CMD->PAN = O.pan;
  991. tTask->CMD->TILT = O.tilt;
  992. tTask->CMD->ZOOM = O.zoom;
  993. tTask->movemet_needed = true;
  994. tTask->source_evt_id = TargetRefID;
  995. setWM(CAM_WS_BUSY);
  996. return pushCamTask(tTask);
  997. }
  998. else
  999. {
  1000. LOG_ERROR("CMD malloc Error!");
  1001. free(tTask);
  1002. };
  1003. }
  1004. else
  1005. {
  1006. LOG_ERROR("Task malloc error!");
  1007. }
  1008. };
  1009. return false;
  1010. }
  1011. uint8_t deviceNodeCamera::getPtzStatus()
  1012. {
  1013. if (m_sCamData.protocol == 0)
  1014. {
  1015. return 1;
  1016. }
  1017. if (getWM() > CAM_WS_INIT)
  1018. {
  1019. return 1;
  1020. }
  1021. return 0;
  1022. }
  1023. uint8_t deviceNodeCamera::getStreamStatus()
  1024. {
  1025. return 1;
  1026. }
  1027. void deviceNodeCamera::m_fThreadProc(void)
  1028. {
  1029. DBC_PUSHED_CMD * PUSHED = NULL;
  1030. m_bThreadStatus = TH_WORKING;
  1031. bool bNewSecondLoop = false;
  1032. uint32_t cTimeSeconds = 0;
  1033. uint32_t oTimeSeconds = 0;
  1034. while (m_bThreadStatus < TH_ENDED)
  1035. {
  1036. cTimeSeconds = (uint32_t) time(NULL);
  1037. if (bNewSecondLoop = (cTimeSeconds != oTimeSeconds))
  1038. {
  1039. oTimeSeconds = cTimeSeconds;
  1040. }
  1041. uint8_t tState = getWM();
  1042. char tB[128];
  1043. fusion_validate();
  1044. switch (tState)
  1045. {
  1046. case CAM_WS_INIT:
  1047. {
  1048. if (m_pProtocol != NULL)
  1049. {
  1050. switch (m_sCamData.protocol)
  1051. {
  1052. case CAM_CTRL_PROTO_VISCA: delete((protocolPTZ_VISCA *) m_pProtocol); break;
  1053. case CAM_CTRL_PROTO_PELCO: delete((protocolPTZ_PELCO *) m_pProtocol); break;
  1054. case CAM_CTRL_PROTO_ONVIF: delete((protocolPTZ_ONVIF *) m_pProtocol); break;
  1055. default : delete(m_pProtocol);
  1056. };
  1057. m_pProtocol = NULL;
  1058. m_sCamData.online = false;
  1059. };
  1060. switch (m_sCamData.protocol)
  1061. {
  1062. case CAM_CTRL_PROTO_VISCA:
  1063. {
  1064. #ifdef DEBUG
  1065. LOG("VISCA");
  1066. #endif
  1067. m_pProtocol = (protocolPTZ *) new protocolPTZ_VISCA(m_sCamData.protocol_aux);
  1068. if (m_pProtocol != NULL)
  1069. {
  1070. basicTransport * tTransport = NULL;
  1071. hardwareManager * tHM = hardwareManager::getInstance();
  1072. if (tHM)
  1073. {
  1074. hardwareResource * tRES = tHM->requestResource(HARDWARE_PROVIDER_SERIAL, m_sCamData.uart_id);
  1075. if (tRES != NULL)
  1076. {
  1077. if (tRES->isValid())
  1078. {
  1079. tTransport = static_cast<basicTransport *>(static_cast<hardwareResourceSerialProvider *>(tRES));
  1080. if (m_pProtocol->setTransport(tTransport))
  1081. {
  1082. if (! m_pProtocol->create())
  1083. {
  1084. LOG_ERROR("Protocol <<< Create >>> Failure!");
  1085. };
  1086. }
  1087. else
  1088. {
  1089. LOG_ERROR("Binding Port Failure!");
  1090. };
  1091. };
  1092. #ifdef DEBUG
  1093. }
  1094. else
  1095. {
  1096. LOG("HWM Has No UART PORT!");
  1097. #endif
  1098. };
  1099. #ifdef DEBUG
  1100. }
  1101. else
  1102. {
  1103. LOG_ERROR("Can not get Hardware Manager!");
  1104. #endif
  1105. };
  1106. if (tTransport != NULL)
  1107. {
  1108. LOG("Bind Transport Port!");
  1109. }
  1110. else
  1111. {
  1112. LOG_ERROR("Transport Creation Failure!");
  1113. delete((protocolPTZ_VISCA *) m_pProtocol);
  1114. m_pProtocol = NULL;
  1115. };
  1116. #ifdef DEBUG
  1117. }
  1118. else
  1119. {
  1120. LOG_ERROR("Protocol Creation failure!");
  1121. #endif
  1122. };
  1123. break;
  1124. };
  1125. case CAM_CTRL_PROTO_PELCO:
  1126. {
  1127. #ifdef DEBUG
  1128. LOG("PELCO");
  1129. #endif
  1130. m_pProtocol = (protocolPTZ *) new protocolPTZ_PELCO(m_sCamData.protocol_aux);
  1131. if (m_pProtocol != NULL)
  1132. {
  1133. basicTransport * tTransport = NULL;
  1134. hardwareManager * tHM = hardwareManager::getInstance();
  1135. if (tHM)
  1136. {
  1137. hardwareResource * tRES = tHM->requestResource(HARDWARE_PROVIDER_SERIAL, m_sCamData.uart_id);
  1138. if (tRES != NULL)
  1139. {
  1140. if (tRES->isValid())
  1141. {
  1142. tTransport = static_cast<basicTransport *>(static_cast<hardwareResourceSerialProvider *>(tRES));
  1143. if (m_pProtocol->setTransport(tTransport))
  1144. {
  1145. if (! m_pProtocol->create())
  1146. {
  1147. LOG_ERROR("Protocol <<< Create >>> Failure!");
  1148. };
  1149. }
  1150. else
  1151. {
  1152. LOG_ERROR("Binding Port Failure!");
  1153. };
  1154. };
  1155. #ifdef DEBUG
  1156. }
  1157. else
  1158. {
  1159. LOG("HWM Has No UART PORT!");
  1160. #endif
  1161. };
  1162. #ifdef DEBUG
  1163. }
  1164. else
  1165. {
  1166. LOG_ERROR("Can not get Hardware Manager!");
  1167. #endif
  1168. };
  1169. if (tTransport != NULL)
  1170. {
  1171. LOG("Bind Transport Port!");
  1172. }
  1173. else
  1174. {
  1175. LOG_ERROR("Transport Creation Failure!");
  1176. delete((protocolPTZ_PELCO *) m_pProtocol);
  1177. m_pProtocol = NULL;
  1178. };
  1179. #ifdef DEBUG
  1180. }
  1181. else
  1182. {
  1183. LOG_ERROR("Protocol Creation failure!");
  1184. #endif
  1185. };
  1186. break;
  1187. };
  1188. case CAM_CTRL_PROTO_ONVIF:
  1189. {
  1190. #ifdef DEBUG_EXTRA
  1191. LOG("ONVIF");
  1192. #endif
  1193. // m_pProtocol = (protocolPTZ *) new protocolPTZ_ONVIF( m_sCamData.ip, m_sCamData.port, m_sCamData.username, m_sCamData.password, m_sCamData.auth_mode );
  1194. if (strstr(m_sCamData.onvif_ctrl, "http") == NULL)
  1195. {
  1196. char tURI[256];
  1197. if (m_sCamData.port)
  1198. {
  1199. sprintf(tURI, "http://%s:%d%s", m_sCamData.ip, m_sCamData.port, m_sCamData.onvif_ctrl);
  1200. }
  1201. else
  1202. {
  1203. sprintf(tURI, "http://%s%s", m_sCamData.ip, m_sCamData.onvif_ctrl);
  1204. };
  1205. m_pProtocol = (protocolPTZ *) new protocolPTZ_ONVIF(tURI, m_sCamData.username, m_sCamData.password, m_sCamData.auth_mode);
  1206. }
  1207. else
  1208. {
  1209. m_pProtocol = (protocolPTZ *) new protocolPTZ_ONVIF(m_sCamData.onvif_ctrl, m_sCamData.username, m_sCamData.password, m_sCamData.auth_mode);
  1210. };
  1211. #ifdef DEBUG_EXTRA
  1212. LOG("ONVIF-END");
  1213. #endif
  1214. if (m_pProtocol != NULL)
  1215. {
  1216. if (! m_pProtocol->create())
  1217. {
  1218. LOG_ERROR("Error Creating Proto!");
  1219. delete((protocolPTZ_ONVIF *) m_pProtocol);
  1220. m_pProtocol = NULL;
  1221. };
  1222. #ifdef DEBUG_EXTRA
  1223. LOG("PROTO CREATE DONE");
  1224. #endif
  1225. };
  1226. break;
  1227. };
  1228. default:
  1229. {
  1230. #ifdef DEBUG
  1231. LOG("Camera Has no defined control protocol");
  1232. #endif
  1233. };
  1234. };
  1235. if (m_pProtocol != NULL)
  1236. {
  1237. // Load Presets only if we have protocol
  1238. load_presets();
  1239. mojoKernel * tKRNL = mojoKernel::getInstance();
  1240. if (tKRNL)
  1241. {
  1242. m_bSkeepPositioningWait = ((tKRNL->getMediaMode() & EVE_OUTPUT_VIDEO) == EVE_OUTPUT_VIDEO);
  1243. #ifdef DEBUG_EXTRA
  1244. char ttTT[128];
  1245. sprintf(ttTT, "Kernel Media Mode:%d", tKRNL->getMediaMode());
  1246. LOG(ttTT);
  1247. if (m_bSkeepPositioningWait)
  1248. {
  1249. LOG("Camera No Wait On Pos!");
  1250. }
  1251. else
  1252. {
  1253. LOG("Camera Will Wait On Pos!");
  1254. };
  1255. #endif
  1256. }
  1257. else
  1258. {
  1259. LOG_ERROR("No Kernel Instance!");
  1260. };
  1261. m_sCamData.online = true;
  1262. setWM(CAM_WS_POWER_INIT);
  1263. };
  1264. m_pDBC->updateCamOnlineStatus(m_sCamData.id, m_sCamData.online);
  1265. break;
  1266. };
  1267. case CAM_WS_POWER_INIT:
  1268. {
  1269. #ifdef DEBUG
  1270. LOG("Init Camera - PowerMode");
  1271. #endif
  1272. // setBusy( true );
  1273. if (m_pProtocol->initCamera())
  1274. {
  1275. setWM(CAM_WS_WHITE_BALLANCE);
  1276. }
  1277. // setBusy( false );
  1278. break;
  1279. };
  1280. case CAM_WS_WHITE_BALLANCE:
  1281. {
  1282. #ifdef DEBUG
  1283. LOG("Set WB");
  1284. #endif
  1285. // setBusy( true );
  1286. if (m_pProtocol->setWhiteBalanceMode(WB_AUTO))
  1287. {
  1288. setWM(CAM_WS_BACKLIGHT);
  1289. }
  1290. // setBusy( false );
  1291. break;
  1292. };
  1293. case CAM_WS_BACKLIGHT:
  1294. {
  1295. #ifdef DEBUG
  1296. LOG("Set BLC");
  1297. #endif
  1298. // setBusy( true );
  1299. if (m_pProtocol->setBackLightMode(true))
  1300. {
  1301. setWM(CAM_WS_AUTO_FOCUS);
  1302. }
  1303. // setBusy( false );
  1304. break;
  1305. };
  1306. case CAM_WS_AUTO_FOCUS:
  1307. {
  1308. #ifdef DEBUG
  1309. LOG("Set AF");
  1310. #endif
  1311. // setBusy( true );
  1312. if (m_pProtocol->setAutoFocus(true))
  1313. {
  1314. setWM(CAM_WS_INIT_GO_HOME);
  1315. }
  1316. // setBusy( false );
  1317. break;
  1318. };
  1319. case CAM_WS_INIT_GO_HOME:
  1320. {
  1321. m_nHomingCounter = 0;
  1322. /* Skip For Restartin bug
  1323. if( !m_bNoAbsPositionning ) {
  1324. #ifdef DEBUG
  1325. LOG("INIT GO -1 0 -1");
  1326. #endif
  1327. if (m_pProtocol->goAbsolute(-1, 0, -1)) {
  1328. m_fCP_Pan = -1;
  1329. m_fCP_Tilt = 0;
  1330. m_fCP_Zoom = -1;
  1331. setWM( CAM_WS_READY );
  1332. };
  1333. } else {
  1334. #ifdef DEBUG
  1335. LOG("Go Home!");
  1336. #endif
  1337. CSTR TK = getPresetToken( ARGUS_PRESET_TYPE_HOME );
  1338. if( TK ) {
  1339. if ( !m_bCameraOnHomePosition ) {
  1340. if( !m_pProtocol->goPreset( TK )){
  1341. LOG_ERROR("Cannot Issue goPreset Command!");
  1342. };
  1343. m_bCameraOnHomePosition = true;
  1344. };
  1345. };
  1346. setWM( CAM_WS_READY );
  1347. };*/
  1348. setWM(CAM_WS_READY);
  1349. break;
  1350. };
  1351. case CAM_WS_AUTOMATED_MOVE:
  1352. {
  1353. m_bCameraOnHomePosition = false;
  1354. m_nRelativeMoveCounter++;
  1355. if ((m_bRelativeMoveMatrix & 0x03) != 0)
  1356. {
  1357. // Pan Move
  1358. if ((m_bRelativeMoveMatrix & 0x01) == 0)
  1359. {
  1360. m_fRM_Pan -= m_fSpeedRise;
  1361. }
  1362. else
  1363. {
  1364. m_fRM_Pan += m_fSpeedRise;
  1365. };
  1366. };
  1367. if ((m_bRelativeMoveMatrix & 0x0C) != 0)
  1368. {
  1369. // TILT Move
  1370. if ((m_bRelativeMoveMatrix & 0x04) == 0)
  1371. {
  1372. m_fRM_Tilt -= m_fSpeedRise;
  1373. }
  1374. else
  1375. {
  1376. m_fRM_Tilt += m_fSpeedRise;
  1377. }
  1378. };
  1379. if ((m_bRelativeMoveMatrix & 0x30) != 0)
  1380. {
  1381. // ZOOM Move
  1382. if ((m_bRelativeMoveMatrix & 0x10) == 0)
  1383. {
  1384. // Wide
  1385. m_fRM_Zoom -= m_fSpeedRise;
  1386. }
  1387. else
  1388. {
  1389. // Tele
  1390. m_fRM_Zoom += m_fSpeedRise;
  1391. };
  1392. };
  1393. if (m_fRM_Pan < -1.0f)
  1394. {
  1395. m_fRM_Pan = -1.0f;
  1396. }
  1397. if (m_fRM_Pan > 1.0f)
  1398. {
  1399. m_fRM_Pan = 1.0f;
  1400. }
  1401. if (m_fRM_Tilt < -1.0f)
  1402. {
  1403. m_fRM_Tilt = -1.0f;
  1404. }
  1405. if (m_fRM_Tilt > 1.0f)
  1406. {
  1407. m_fRM_Tilt = 1.0f;
  1408. }
  1409. if (m_fRM_Zoom < -1.0f)
  1410. {
  1411. m_fRM_Zoom = -1.0f;
  1412. }
  1413. if (m_fRM_Zoom > 1.0f)
  1414. {
  1415. m_fRM_Zoom = 1.0f;
  1416. }
  1417. m_fCP_Zoom += 0.5f * m_fRM_Zoom;
  1418. if (m_fCP_Zoom < -1.0f)
  1419. {
  1420. m_fCP_Zoom = -1.0f;
  1421. }
  1422. if (m_fCP_Zoom > 1.0f)
  1423. {
  1424. m_fCP_Zoom = 1.0f;
  1425. }
  1426. if (! relativeMove(m_fRM_Pan, m_fRM_Tilt, m_fRM_Zoom))
  1427. {
  1428. m_nRelativeMoveCounter = 0;
  1429. setWM(CAM_WS_READY);
  1430. break;
  1431. };
  1432. if (m_sCamData.ptz_axel)
  1433. { // Manual
  1434. if (m_nRelativeMoveCounter > m_sCamData.ptz_axel)
  1435. {
  1436. m_nRelativeMoveCounter = 0;
  1437. setWM(CAM_WS_READY);
  1438. };
  1439. }
  1440. else
  1441. {
  1442. // Stepping Axel by Zoom
  1443. uint8_t tVal = (uint8_t) (10.0f * (2.0f - (1.0f + m_fCP_Zoom)));
  1444. if (m_nRelativeMoveCounter > tVal)
  1445. {
  1446. m_nRelativeMoveCounter = 0;
  1447. setWM(CAM_WS_READY);
  1448. };
  1449. };
  1450. break;
  1451. };
  1452. case CAM_WS_STOP_MOVE:
  1453. {
  1454. m_nRelativeMoveCounter++;
  1455. m_fRM_Pan = .0f;
  1456. m_fRM_Pan = .0f;
  1457. m_fRM_Tilt = .0f;
  1458. m_fRM_Tilt = .0f;
  1459. m_fRM_Zoom = .0f;
  1460. m_fRM_Zoom = .0f;
  1461. if (relativeStop())
  1462. {
  1463. m_nHomingCounter = 0;
  1464. m_nRelativeMoveCounter = 0;
  1465. setWM(CAM_WS_READY);
  1466. break;
  1467. };
  1468. if (m_sCamData.ptz_axel)
  1469. {
  1470. if (m_nRelativeMoveCounter > m_sCamData.ptz_axel)
  1471. {
  1472. setWM(CAM_WS_READY);
  1473. }
  1474. }
  1475. else
  1476. {
  1477. uint8_t tVal = (uint8_t) (10.0f * (2.0f - (1.0f + m_fCP_Zoom)));
  1478. if (m_nRelativeMoveCounter > tVal)
  1479. {
  1480. setWM(CAM_WS_READY);
  1481. }
  1482. };
  1483. break;
  1484. };
  1485. case CAM_WS_GO_ABS_ROI:
  1486. case CAM_WS_GO_ABS_POSITION:
  1487. {
  1488. m_bCameraOnHomePosition = false;
  1489. setWM(CAM_WS_BUSY);
  1490. if (m_pCurrentROI != NULL)
  1491. {
  1492. // LOG("ROI Ok");
  1493. m_nHomingCounter = 0;
  1494. if (m_pBEH)
  1495. {
  1496. EVENT_CONTAINER * tEVT = createBasicEvent(m_sNodeData.node_id, EVT_CLASS_CAMERA_CTRL, EVT_CAMERA_MOVE_TO, NULL, NULL, NULL);
  1497. if (tEVT)
  1498. {
  1499. m_pBEH->broadcast(tEVT);
  1500. }
  1501. };
  1502. if (m_bNoAbsPositionning)
  1503. {
  1504. // LOG("ABS to PRESET");
  1505. uint8_t COL, ROW;
  1506. translateROI2PRESET(m_pCurrentROI->cam_orientation.pan, m_pCurrentROI->cam_orientation.tilt, m_pCurrentROI->cam_orientation.zoom, COL, ROW);
  1507. // char tB[64];
  1508. // sprintf( tB, "Get ABS Preset Col %d Row %d!", COL, ROW );
  1509. // LOG( tB );
  1510. // sprintf( tB, "Old Preset Col %d Row %d!", m_bCurrentPresetCol, m_bCurrentPresetRow );
  1511. // LOG( tB );
  1512. if ((m_bCurrentPresetCol == COL) && (m_bCurrentPresetRow == ROW))
  1513. {
  1514. m_fCP_Pan = m_pCurrentROI->cam_orientation.pan;
  1515. m_fCP_Tilt = m_pCurrentROI->cam_orientation.tilt;
  1516. m_fCP_Zoom = m_pCurrentROI->cam_orientation.zoom;
  1517. #ifdef DEBUG_EXTRA
  1518. LOG("INSTANT BCAST CAM ONPOS");
  1519. #endif
  1520. // Send Capture event
  1521. DBC_SNAPSHOT_DATA * tData = fusion_translate_ROI(m_pCurrentROI);
  1522. if (tData != NULL)
  1523. {
  1524. if (m_pBEH)
  1525. {
  1526. EVENT_CONTAINER * tEVT = createBasicEvent(m_sNodeData.node_id, EVT_CLASS_CAMERA_CTRL, EVT_CAMERA_ON_POSITION, NULL, NULL, tData);
  1527. if (tEVT)
  1528. {
  1529. m_pBEH->broadcast(tEVT);
  1530. }
  1531. else
  1532. {
  1533. free(tData);
  1534. };
  1535. }
  1536. else
  1537. {
  1538. free(tData);
  1539. };
  1540. };
  1541. setWM(CAM_WS_READY);
  1542. }
  1543. else
  1544. {
  1545. CSTR token = getPresetToken(COL, ROW);
  1546. if (token)
  1547. {
  1548. // update position Info
  1549. m_bCurrentPresetCol = COL;
  1550. m_bCurrentPresetRow = ROW;
  1551. m_fCP_Pan = m_pCurrentROI->cam_orientation.pan;
  1552. m_fCP_Tilt = m_pCurrentROI->cam_orientation.tilt;
  1553. m_fCP_Zoom = m_pCurrentROI->cam_orientation.zoom;
  1554. if (! m_pProtocol->goPreset(token))
  1555. {
  1556. LOG_ERROR("Cannot Issue goPreset Command!->Restart Camera?");
  1557. setWM(CAM_WS_END);
  1558. }
  1559. else
  1560. {
  1561. // Waiting Movement finish
  1562. if (m_bSkeepPositioningWait && 0)
  1563. {
  1564. #ifdef DEBUG_EXTRA
  1565. LOG("INSTANT BCAST CAM ONPOS");
  1566. #endif
  1567. // Send Capture event
  1568. DBC_SNAPSHOT_DATA * tData = fusion_translate_ROI(m_pCurrentROI);
  1569. if (tData != NULL)
  1570. {
  1571. if (m_pBEH)
  1572. {
  1573. EVENT_CONTAINER * tEVT = createBasicEvent(m_sNodeData.node_id, EVT_CLASS_CAMERA_CTRL, EVT_CAMERA_ON_POSITION, NULL, NULL, tData);
  1574. if (tEVT)
  1575. {
  1576. m_pBEH->broadcast(tEVT);
  1577. }
  1578. else
  1579. {
  1580. free(tData);
  1581. };
  1582. }
  1583. else
  1584. {
  1585. free(tData);
  1586. };
  1587. };
  1588. setWM(CAM_WS_READY);
  1589. }
  1590. else
  1591. {
  1592. // utilizeROI( m_pCurrentROI );
  1593. setWM(CAM_WS_WAIT_ONPOS);
  1594. };
  1595. };
  1596. }
  1597. else
  1598. {
  1599. #ifdef DEBUG_EXTRA
  1600. LOG("Not Found!");
  1601. #endif
  1602. utilizeROI(m_pCurrentROI);
  1603. setWM(CAM_WS_READY);
  1604. };
  1605. };
  1606. }
  1607. else
  1608. {
  1609. // update position Info
  1610. m_fCP_Pan = m_pCurrentROI->cam_orientation.pan;
  1611. m_fCP_Tilt = m_pCurrentROI->cam_orientation.tilt;
  1612. m_fCP_Zoom = m_pCurrentROI->cam_orientation.zoom;
  1613. // Do Movement
  1614. if (! m_pProtocol->goAbsolute(m_pCurrentROI->cam_orientation.pan, m_pCurrentROI->cam_orientation.tilt, m_pCurrentROI->cam_orientation.zoom * m_fZoomCorrection))
  1615. {
  1616. LOG_ERROR("Cannot Issue goAbsolute Command!->Restart Camera?");
  1617. setWM(CAM_WS_END);
  1618. }
  1619. else
  1620. {
  1621. // Waiting Movement finish
  1622. if (m_bSkeepPositioningWait)
  1623. {
  1624. #ifdef DEBUG_EXTRA
  1625. LOG("INSTANT BCAST CAM ONPOS");
  1626. #endif
  1627. // Send Capture event
  1628. DBC_SNAPSHOT_DATA * tData = fusion_translate_ROI(m_pCurrentROI);
  1629. if (tData != NULL)
  1630. {
  1631. if (m_pBEH)
  1632. {
  1633. EVENT_CONTAINER * tEVT = createBasicEvent(m_sNodeData.node_id, EVT_CLASS_CAMERA_CTRL, EVT_CAMERA_ON_POSITION, NULL, NULL, tData);
  1634. if (tEVT)
  1635. {
  1636. m_pBEH->broadcast(tEVT);
  1637. }
  1638. else
  1639. {
  1640. free(tData);
  1641. };
  1642. }
  1643. else
  1644. {
  1645. free(tData);
  1646. };
  1647. };
  1648. setWM(CAM_WS_READY);
  1649. }
  1650. else
  1651. {
  1652. setWM(CAM_WS_WAIT_ONPOS);
  1653. };
  1654. };
  1655. };
  1656. }
  1657. else
  1658. {
  1659. setWM(CAM_WS_READY);
  1660. }
  1661. break;
  1662. };
  1663. case CAM_WS_WAIT_ONPOS:
  1664. {
  1665. // LOG("WaitOnPos...");
  1666. DBC_SNAPSHOT_DATA * tData = NULL;
  1667. if (m_pCurrentROI != NULL)
  1668. {
  1669. // Send Capture event
  1670. tData = fusion_translate_ROI(m_pCurrentROI);
  1671. };
  1672. // To Accept any new ROI's ??
  1673. // setWM( CAM_WS_READY );
  1674. #ifdef DEBUG_EXTRA
  1675. LOG("Start Waiting Cam On Position!");
  1676. #endif
  1677. if (! m_bSkeepPositioningWait)
  1678. {
  1679. if (! m_pProtocol->waitMoveIsDone())
  1680. {
  1681. m_bSkeepPositioningWait = true;
  1682. }
  1683. };
  1684. if (m_bSkeepPositioningWait)
  1685. {
  1686. SLEEP(3000);
  1687. }
  1688. #ifdef DEBUG_EXTRA
  1689. LOG("Done Waiting Cam On Position!");
  1690. #endif
  1691. if (tData != NULL)
  1692. {
  1693. if (m_pBEH)
  1694. {
  1695. EVENT_CONTAINER * tEVT = createBasicEvent(m_sNodeData.node_id, EVT_CLASS_CAMERA_CTRL, EVT_CAMERA_ON_POSITION, NULL, NULL, tData);
  1696. if (tEVT)
  1697. {
  1698. #ifdef DEBUG_EXTRA
  1699. LOG("BCAST CAM ONPOS");
  1700. #endif
  1701. m_pBEH->broadcast(tEVT);
  1702. }
  1703. else
  1704. {
  1705. free(tData);
  1706. };
  1707. }
  1708. else
  1709. {
  1710. free(tData);
  1711. };
  1712. tData = NULL;
  1713. };
  1714. setWM(CAM_WS_READY);
  1715. break;
  1716. };
  1717. case CAM_WS_GO_HOMING_PRESET:
  1718. {
  1719. if (! m_bCameraOnHomePosition)
  1720. {
  1721. // For new Camera Presets
  1722. CSTR TK = getPresetToken(ARGUS_PRESET_TYPE_HOME);
  1723. if (TK)
  1724. {
  1725. #ifdef DEBUG_EXTRA
  1726. LOG("Go Home!");
  1727. LOG(TK);
  1728. #endif
  1729. if (! m_pProtocol->goPreset(TK))
  1730. {
  1731. LOG_ERROR("Cannot Issue goPreset Command!");
  1732. };
  1733. }
  1734. else
  1735. {
  1736. // Old Method
  1737. if (m_sCamData.home_token[0] != (char) 0)
  1738. {
  1739. #ifdef DEBUG_EXTRA
  1740. LOG("Go Home!");
  1741. LOG(m_sCamData.home_token);
  1742. #endif
  1743. if (! m_pProtocol->goPreset(m_sCamData.home_token))
  1744. {
  1745. LOG_ERROR("Cannot Issue goPreset Command!");
  1746. };
  1747. };
  1748. };
  1749. m_bCameraOnHomePosition = true;
  1750. m_bCurrentPresetCol = 0;
  1751. m_bCurrentPresetRow = 0;
  1752. };
  1753. setWM(CAM_WS_READY);
  1754. break;
  1755. };
  1756. case CAM_WS_GO_PRESET:
  1757. {
  1758. LOG("Go Preset!");
  1759. // if( m_sCamData.home_token[0] != (char)0 ) {
  1760. // LOG("Go Home!");
  1761. // LOG( m_sCamData.home_token );
  1762. // if( !m_pProtocol->goPreset( m_sCamData.home_token )){
  1763. // LOG_ERROR("Cannot Issue goPreset Command!");
  1764. // };
  1765. // };
  1766. setWM(CAM_WS_READY);
  1767. break;
  1768. };
  1769. case CAM_WS_SET_PRESET:
  1770. {
  1771. LOG("Set Preset!");
  1772. // if( m_sCamData.home_token[0] != (char)0 ) {
  1773. // LOG("Go Home!");
  1774. // LOG( m_sCamData.home_token );
  1775. // if( !m_pProtocol->goPreset( m_sCamData.home_token )){
  1776. // LOG_ERROR("Cannot Issue goPreset Command!");
  1777. // };
  1778. // };
  1779. setWM(CAM_WS_READY);
  1780. break;
  1781. };
  1782. case CAM_WS_BUSY:
  1783. {
  1784. // Handle Operation TO
  1785. setWM(CAM_WS_READY);
  1786. break;
  1787. };
  1788. case CAM_WS_READY:
  1789. {
  1790. m_pCurrentROI = fusion_fetchROI();
  1791. if (m_pCurrentROI != NULL)
  1792. {
  1793. #ifdef DEBUG_EXTRA
  1794. LOG("HAVE ROI to see!");
  1795. #endif
  1796. setWM(CAM_WS_GO_ABS_POSITION);
  1797. }
  1798. else
  1799. {
  1800. // Idle Work --->
  1801. if (m_sCamData.eco_ir)
  1802. {
  1803. m_mIRProtector.lock();
  1804. if (m_bIRIsOn)
  1805. {
  1806. if (m_bIRTO)
  1807. {
  1808. if (bNewSecondLoop)
  1809. {
  1810. m_bIRTO--;
  1811. }
  1812. }
  1813. else
  1814. {
  1815. CSTR TK = getPresetToken(ARGUS_PRESET_TYPE_IR_OFF);
  1816. if (TK)
  1817. {
  1818. if (m_pProtocol->goPreset(TK))
  1819. {
  1820. m_bIRIsOn = false;
  1821. }
  1822. }
  1823. };
  1824. };
  1825. m_mIRProtector.unlock();
  1826. };
  1827. if (m_sCamData.homing_enable)
  1828. {
  1829. if (bNewSecondLoop)
  1830. {
  1831. if (m_nHomingCounter < m_sCamData.homing_to)
  1832. {
  1833. m_nHomingCounter++;
  1834. }
  1835. else
  1836. {
  1837. // LOG("Go Home Position!");
  1838. m_nHomingCounter = 0;
  1839. setWM(CAM_WS_GO_HOMING_PRESET);
  1840. break;
  1841. };
  1842. };
  1843. };
  1844. CAM_TASK * tTASK = fetchCamTask();
  1845. if (tTASK != NULL)
  1846. {
  1847. if (tTASK->movemet_needed)
  1848. {
  1849. CAM_PTZ_CMD * tCMD = tTASK->CMD;
  1850. if (tCMD)
  1851. {
  1852. switch (tCMD->CMD)
  1853. {
  1854. case PTZ_STOP:
  1855. {
  1856. m_nHomingCounter = 0;
  1857. setWM(CAM_WS_STOP_MOVE);
  1858. break;
  1859. };
  1860. case PTZ_MOVE:
  1861. {
  1862. if (tState != CAM_WS_AUTOMATED_MOVE)
  1863. {
  1864. m_nRelativeMoveCounter = 0;
  1865. }
  1866. m_bRelativeMoveMatrix = 0;
  1867. m_fRM_Pan = tCMD->PAN;
  1868. m_fRM_Tilt = tCMD->TILT;
  1869. m_fRM_Zoom = tCMD->ZOOM;
  1870. m_nHomingCounter = 0;
  1871. setWM(CAM_WS_AUTOMATED_MOVE);
  1872. break;
  1873. };
  1874. case PTZ_ABSOLUTE:
  1875. {
  1876. m_pCurrentROI = createCAM_ROI();
  1877. if (m_pCurrentROI != NULL)
  1878. {
  1879. CAM_TRACK_RECORD * tTarget = (CAM_TRACK_RECORD *) malloc(sizeof(CAM_TRACK_RECORD));
  1880. if (tTarget != NULL)
  1881. {
  1882. memset(tTarget, 0, sizeof(CAM_TRACK_RECORD));
  1883. tTarget->evt_db_ID = tTASK->source_evt_id;
  1884. m_pCurrentROI->targets.push_back(tTarget);
  1885. m_pCurrentROI->cam_orientation.pan = tCMD->PAN;
  1886. m_pCurrentROI->cam_orientation.tilt = tCMD->TILT;
  1887. m_pCurrentROI->cam_orientation.zoom = tCMD->ZOOM;
  1888. m_nHomingCounter = 0;
  1889. setWM(CAM_WS_GO_ABS_POSITION);
  1890. }
  1891. else
  1892. {
  1893. utilizeROI(m_pCurrentROI);
  1894. };
  1895. };
  1896. break;
  1897. };
  1898. case PTZ_RELOAD_CAMERA:
  1899. {
  1900. LOG("Exit Command Rcpt");
  1901. m_bThreadStatus = TH_ENDED;
  1902. };
  1903. };
  1904. free(tCMD);
  1905. tTASK->CMD = NULL;
  1906. };
  1907. }
  1908. else
  1909. {
  1910. LOG("No Movement -> INSTCAP");
  1911. // No Movement -> Just Capture
  1912. if (m_pBEH)
  1913. {
  1914. EVENT_CONTAINER * tEVT = createBasicEvent(m_sNodeData.node_id, EVT_CLASS_CAMERA_CTRL, EVT_CAMERA_ON_POSITION, NULL, NULL, NULL);
  1915. if (tEVT)
  1916. {
  1917. tEVT->m_db_id = tTASK->source_evt_id;
  1918. m_pBEH->broadcast(tEVT);
  1919. };
  1920. };
  1921. };
  1922. free(tTASK);
  1923. }
  1924. else
  1925. {
  1926. // Lowest Priority to Web Client Commands
  1927. #ifdef DEBUG_EXTRA__
  1928. LOG("Check Web CMD!");
  1929. #endif
  1930. PUSHED = m_pDBC->getNodePushedCMD(m_sNodeData.node_id);
  1931. if (PUSHED != NULL)
  1932. {
  1933. #ifdef DEBUG_EXTRA__
  1934. LOG("HAVE Web CMD!");
  1935. #endif
  1936. m_pDBC->updateNodePushedCMD(PUSHED->id, DBC_CMD_READY_TO_CLEAN);
  1937. if ((PUSHED->CMD & ARGUS_CAM_PCMD_CMD_MASK) == ARGUS_CAM_PCMD_CMD_MASK)
  1938. {
  1939. switch (PUSHED->CMD)
  1940. {
  1941. case 0x80:
  1942. {
  1943. // Call Home Preset
  1944. LOG("CALL HOME PRESET!");
  1945. CSTR TK = getPresetToken(ARGUS_PRESET_TYPE_HOME);
  1946. if (TK)
  1947. {
  1948. LOG(TK);
  1949. if (m_pProtocol->goPreset(TK))
  1950. {
  1951. m_bCameraOnHomePosition = true;
  1952. m_nHomingCounter = 0;
  1953. };
  1954. };
  1955. break;
  1956. };
  1957. case 0x81:
  1958. {
  1959. // Call IR On Preset
  1960. LOG("CALL IR ON PRESET!");
  1961. m_mIRProtector.lock();
  1962. CSTR TK = getPresetToken(ARGUS_PRESET_TYPE_IR_ON);
  1963. if (TK)
  1964. {
  1965. LOG(TK);
  1966. if (m_pProtocol->goPreset(TK))
  1967. {
  1968. m_bIRIsOn = true;
  1969. m_bIRTO = DEFAULT_SHINE_ON_TARGET_TO;
  1970. };
  1971. }
  1972. else
  1973. {
  1974. LOG("PRESET NOT FOUND!");
  1975. }
  1976. m_mIRProtector.unlock();
  1977. break;
  1978. };
  1979. case 0x82:
  1980. {
  1981. // Call IR Off Preset
  1982. LOG("CALL IR OFF PRESET!");
  1983. m_mIRProtector.lock();
  1984. CSTR TK = (m_sCamData.eco_ir) ? getPresetToken(ARGUS_PRESET_TYPE_IR_OFF) : getPresetToken(ARGUS_PRESET_TYPE_IR_AUTO);
  1985. if (TK)
  1986. {
  1987. LOG(TK);
  1988. if (m_pProtocol->goPreset(TK))
  1989. {
  1990. m_bIRIsOn = false;
  1991. m_bIRTO = 0;
  1992. };
  1993. }
  1994. else
  1995. {
  1996. LOG("PRESET NOT FOUND!");
  1997. }
  1998. m_mIRProtector.unlock();
  1999. break;
  2000. };
  2001. case 0x83:
  2002. {
  2003. // Call IR Auto Preset
  2004. LOG("CALL IR AUTO PRESET!");
  2005. m_mIRProtector.lock();
  2006. CSTR TK = getPresetToken(ARGUS_PRESET_TYPE_IR_AUTO);
  2007. if (TK)
  2008. {
  2009. LOG(TK);
  2010. if (m_pProtocol->goPreset(TK))
  2011. {
  2012. m_bIRIsOn = true;
  2013. m_bIRTO = DEFAULT_SHINE_ON_TARGET_TO;
  2014. };
  2015. }
  2016. else
  2017. {
  2018. LOG("PRESET NOT FOUND!");
  2019. }
  2020. m_mIRProtector.unlock();
  2021. break;
  2022. };
  2023. case 0x84:
  2024. {
  2025. // Call Wisher Preset
  2026. LOG("CALL WIPER PRESET!");
  2027. CSTR TK = getPresetToken(ARGUS_PRESET_TYPE_IR_WISHER);
  2028. if (TK)
  2029. {
  2030. LOG(TK);
  2031. m_pProtocol->goPreset(TK);
  2032. };
  2033. break;
  2034. };
  2035. };
  2036. }
  2037. else if (tState == CAM_WS_READY || tState == CAM_WS_AUTOMATED_MOVE)
  2038. { // No Way to get here
  2039. // Direct Movment Mode
  2040. if (PUSHED->CMD == ARGUS_CAM_PCMD_STOP)
  2041. {
  2042. m_nHomingCounter = 0;
  2043. setWM(CAM_WS_STOP_MOVE);
  2044. }
  2045. else
  2046. {
  2047. char tB[64];
  2048. if (tState != CAM_WS_AUTOMATED_MOVE)
  2049. {
  2050. m_nRelativeMoveCounter = 0;
  2051. }
  2052. m_bRelativeMoveMatrix = (PUSHED->CMD) >> 1;
  2053. m_nHomingCounter = 0;
  2054. setWM(CAM_WS_AUTOMATED_MOVE);
  2055. };
  2056. };
  2057. free(PUSHED);
  2058. PUSHED = NULL;
  2059. };
  2060. };
  2061. };
  2062. break;
  2063. };
  2064. case CAM_WS_END:
  2065. {
  2066. LOG("Exit Command Rcpt");
  2067. m_bThreadStatus = TH_ENDED;
  2068. break;
  2069. };
  2070. };
  2071. SLEEP(DEFAULT_THREAD_SLEEP_TIME);
  2072. };
  2073. #ifdef DEBUG
  2074. LOG("CTRL Loop End!");
  2075. #endif
  2076. }
  2077. bool deviceNodeCamera::setNoPositionWait(bool FL)
  2078. {
  2079. m_bSkeepPositioningWait = FL;
  2080. return true;
  2081. }
  2082. bool deviceNodeCamera::testFOV(C3D_DECART L)
  2083. {
  2084. // System Coords -> trans to local and go to funct
  2085. systemAnchor * tSystem = systemAnchor::getInstance();
  2086. if (tSystem == NULL)
  2087. {
  2088. return false;
  2089. }
  2090. C3D_SPH_DEGREES SL;
  2091. #ifdef DEBUG_EXTRA
  2092. LOG("Test FOV local Decart coords");
  2093. #endif
  2094. if (! tSystem->convertToLocal(&m_oPosition, L, SL))
  2095. {
  2096. return false;
  2097. }
  2098. return testFOV(SL);
  2099. }
  2100. bool deviceNodeCamera::testFOV(C3D_WGS84_LOC L)
  2101. {
  2102. // Global Coords
  2103. systemAnchor * tSystem = systemAnchor::getInstance();
  2104. if (tSystem == NULL)
  2105. {
  2106. return false;
  2107. }
  2108. C3D_SPH_DEGREES SL;
  2109. #ifdef DEBUG_EXTRA
  2110. LOG("Test FOV WGS coords");
  2111. #endif
  2112. if (! tSystem->convertToLocal(&m_oPosition, L, SL))
  2113. {
  2114. return false;
  2115. }
  2116. return testFOV(SL);
  2117. }
  2118. bool deviceNodeCamera::testFOV(C3D_SPH_RADIANS L)
  2119. {
  2120. // Local Coords
  2121. C3D_SPH_DEGREES SL;
  2122. #ifdef DEBUG_EXTRA
  2123. LOG("Test FOV local Spherical(rad) coords");
  2124. #endif
  2125. spherical2spherical(L, SL);
  2126. return testFOV(SL);
  2127. }
  2128. bool deviceNodeCamera::testFOV(C3D_SPH_DEGREES SL)
  2129. {
  2130. #ifdef DEBUG
  2131. char tB[128];
  2132. #endif
  2133. #ifdef DEBUG_EXTRA
  2134. LOG("Test FOV local Spherical(deg) coords");
  2135. #endif
  2136. switch (m_sCamData.triggering_mode)
  2137. {
  2138. case CAM_TRIGGER_STRICT:
  2139. {
  2140. if (SL.r > m_fMaxRange || SL.r < m_fMinRange)
  2141. {
  2142. #ifdef DEBUG_EXTRA
  2143. sprintf(tB, "Target Range( %.3f.m ) is not in a Range %.3f.m <-> %.3f.m", SL.r, m_fMinRange, m_fMaxRange);
  2144. LOG(tB);
  2145. #endif
  2146. return false;
  2147. };
  2148. break;
  2149. };
  2150. case CAM_TRIGGER_ALL_NEAR:
  2151. {
  2152. if (SL.r > m_fMaxRange)
  2153. {
  2154. #ifdef DEBUG_EXTRA
  2155. sprintf(tB, "Target Range( %.3f.m ) is not in a Range %.3f.m <-> %.3f.m", SL.r, m_fMinRange, m_fMaxRange);
  2156. LOG(tB);
  2157. #endif
  2158. return false;
  2159. };
  2160. break;
  2161. };
  2162. case CAM_TRIGGER_ALL_FAR:
  2163. {
  2164. if (SL.r < m_fMinRange)
  2165. {
  2166. #ifdef DEBUG_EXTRA
  2167. sprintf(tB, "Target Range( %.3f.m ) is not in a Range %.3f.m <-> %.3f.m", SL.r, m_fMinRange, m_fMaxRange);
  2168. LOG(tB);
  2169. #endif
  2170. return false;
  2171. };
  2172. break;
  2173. };
  2174. case CAM_TRIGGER_ALL: return true;
  2175. };
  2176. float HS = AC_imager_width[m_sCamData.imager_type];
  2177. float VS = AC_imager_height[m_sCamData.imager_type];
  2178. float HS2 = HS * HS;
  2179. float VS2 = VS * VS;
  2180. float IR = HS / VS;
  2181. float IR2 = IR * IR;
  2182. if (m_sCamData.pan_angles == 0)
  2183. {
  2184. // Fixed Camera
  2185. float tA = fabsf(SL.fi);
  2186. float tZ = 0.5f * (m_fMinHFov + (SL.r - m_fMinRange) * m_fR2FL);
  2187. if (tA > tZ)
  2188. {
  2189. return false;
  2190. }
  2191. }
  2192. else
  2193. {
  2194. if (m_sCamData.pan_angles < 360)
  2195. { // If endless PAN -> PASSED
  2196. // Partial Pan
  2197. switch (m_sCamData.pan_model)
  2198. {
  2199. case 1:
  2200. {
  2201. // Regular Pan
  2202. float tFOV = 0.5f * (m_fMinHFov + (SL.r - m_fMinRange) * m_fR2FL);
  2203. float tRad = deg2rad(SL.fi);
  2204. #ifdef DEBUG_EXTRA
  2205. sprintf(tB, "FOV: %0.3f CC: %0.3f", tFOV, tRad);
  2206. LOG(tB);
  2207. #endif
  2208. if ((tRad < (-1.0 * tFOV)) || (tRad > (deg2rad(m_sCamData.pan_angles) + tFOV)))
  2209. {
  2210. return false;
  2211. }
  2212. break;
  2213. };
  2214. case 2:
  2215. {
  2216. break;
  2217. };
  2218. case 3:
  2219. {
  2220. break;
  2221. };
  2222. };
  2223. };
  2224. };
  2225. if (m_sCamData.tilt_angles == 0)
  2226. {
  2227. // Fixed Camera
  2228. if ((SL.tetta < (m_fMaxVFov / -2.0f)) || (SL.tetta > (m_fMaxVFov / 2.0f)))
  2229. {
  2230. return false;
  2231. }
  2232. }
  2233. else
  2234. {
  2235. if (m_sCamData.tilt_angles < 90)
  2236. { // if Cam CAN DO 90 -> Pass
  2237. // Partial Tilt
  2238. switch (m_sCamData.tilt_model)
  2239. {
  2240. case 1:
  2241. {
  2242. // Regular Tilt
  2243. float tFOV = 0.5f * (m_fMinVFov + (m_fMaxVFov - m_fMinVFov) * (SL.r / m_fMaxRange));
  2244. float tRad = deg2rad(SL.tetta);
  2245. if ((tRad < (-1.0 * tFOV)) || (tRad > (deg2rad(m_sCamData.tilt_angles) + tFOV)))
  2246. {
  2247. return false;
  2248. }
  2249. break;
  2250. };
  2251. case 2:
  2252. {
  2253. break;
  2254. };
  2255. };
  2256. };
  2257. };
  2258. return true;
  2259. }
  2260. bool deviceNodeCamera::lookAt(uint32_t TargetRefID, C3D_WGS84_LOC L)
  2261. {
  2262. systemAnchor * tSystem = systemAnchor::getInstance();
  2263. if (tSystem == NULL)
  2264. {
  2265. return false;
  2266. }
  2267. C3D_SPH_DEGREES SL;
  2268. #ifdef DEBUG_EXTRA
  2269. LOG("Look at WGS coords!");
  2270. #endif
  2271. tSystem->convertToLocal(&m_oPosition, L, SL);
  2272. return lookAt(TargetRefID, SL);
  2273. }
  2274. bool deviceNodeCamera::lookAt(uint32_t TargetRefID, C3D_DECART L)
  2275. {
  2276. systemAnchor * tSystem = systemAnchor::getInstance();
  2277. if (tSystem == NULL)
  2278. {
  2279. return false;
  2280. }
  2281. C3D_SPH_DEGREES SL;
  2282. #ifdef DEBUG_EXTRA
  2283. LOG("Look at System Decart coords!");
  2284. #endif
  2285. tSystem->convertToLocal(&m_oPosition, L, SL);
  2286. return lookAt(TargetRefID, SL);
  2287. }
  2288. bool deviceNodeCamera::lookAt(uint32_t TargetRefID, C3D_SPH_RADIANS L)
  2289. {
  2290. C3D_SPH_DEGREES SL;
  2291. #ifdef DEBUG_EXTRA
  2292. LOG("Look at spherical(rad) 2 spherical(deg) coords!");
  2293. #endif
  2294. spherical2spherical(L, SL);
  2295. return lookAt(TargetRefID, SL);
  2296. }
  2297. bool deviceNodeCamera::lookAt(uint32_t TargetRefID, C3D_SPH_DEGREES L)
  2298. {
  2299. if (m_bCameraIsStatic)
  2300. {
  2301. DBC_SNAPSHOT_DATA * tData = (DBC_SNAPSHOT_DATA *) malloc(sizeof(DBC_SNAPSHOT_DATA));
  2302. if (tData)
  2303. {
  2304. memset(tData, 0, sizeof(DBC_SNAPSHOT_DATA));
  2305. tData->stream_id = m_sNodeData.aux_parameter;
  2306. tData->assoc_event_ids.push_back(TargetRefID);
  2307. if (m_pBEH)
  2308. {
  2309. EVENT_CONTAINER * tEVT = createBasicEvent(m_sNodeData.node_id, EVT_CLASS_CAMERA_CTRL, EVT_CAMERA_ON_POSITION, NULL, NULL, tData);
  2310. if (tEVT)
  2311. {
  2312. #ifdef DEBUG_EXTRA
  2313. LOG("INSTANT BCAST CAM ONPOS");
  2314. #endif
  2315. m_pBEH->broadcast(tEVT);
  2316. }
  2317. else
  2318. {
  2319. free(tData);
  2320. };
  2321. }
  2322. else
  2323. {
  2324. free(tData);
  2325. };
  2326. };
  2327. return true;
  2328. };
  2329. // char tB[128];
  2330. // sprintf(tB, "Target On %.3f %.3f %.3f", L.r, L.fi, L.tetta );
  2331. // LOG( tB );
  2332. // sprintf( tB, "Cam GoTo %.3f %.3f %.3f", fP, fT, fZ );
  2333. // LOG( tB );
  2334. #ifdef DEBUG_EXTRA
  2335. LOG("Look at local spherical deg. coords!(final)");
  2336. #endif
  2337. // if( getWM() == CAM_WS_READY ){
  2338. // C3D_ORIENT_ONVIF LL;
  2339. // spherical2onvif( L, LL, m_fMaxRange, m_sCamData.pan_model, m_sCamData.pan_angles, m_sCamData.tilt_model, m_sCamData.tilt_angles );
  2340. // return absoluteMove( TargetRefID, LL );
  2341. // };
  2342. // Camera is Busy
  2343. if (m_bShineOnTarget)
  2344. {
  2345. m_mIRProtector.lock();
  2346. if (! m_bIRIsOn)
  2347. {
  2348. CSTR TK = getPresetToken(ARGUS_PRESET_TYPE_IR_AUTO);
  2349. if (TK)
  2350. {
  2351. if (m_pProtocol->goPreset(TK))
  2352. {
  2353. m_bIRIsOn = true;
  2354. m_bIRTO = DEFAULT_SHINE_ON_TARGET_TO;
  2355. };
  2356. };
  2357. }
  2358. else
  2359. {
  2360. m_bIRTO = DEFAULT_SHINE_ON_TARGET_TO;
  2361. }
  2362. m_mIRProtector.unlock();
  2363. };
  2364. // targeting by Presets -> instant ROI
  2365. if (m_bNoAbsPositionning)
  2366. {
  2367. CAM_ROI * tROI = createCAM_ROI();
  2368. if (tROI != NULL)
  2369. {
  2370. CAM_TRACK_RECORD * tTarget = (CAM_TRACK_RECORD *) malloc(sizeof(CAM_TRACK_RECORD));
  2371. if (tTarget != NULL)
  2372. {
  2373. memset(tTarget, 0, sizeof(CAM_TRACK_RECORD));
  2374. tTarget->evt_db_ID = TargetRefID;
  2375. tROI->targets.push_back(tTarget);
  2376. tROI->ts_ms = getMilliseconds();
  2377. spherical2onvif(L, tROI->cam_orientation, m_fMaxRange, m_sCamData.pan_model, m_sCamData.pan_angles, m_sCamData.tilt_model, m_sCamData.tilt_angles);
  2378. tROI->cam_position.fi = L.fi;
  2379. tROI->cam_position.tetta = L.tetta;
  2380. tROI->cam_position.r = L.r;
  2381. m_mROIsProtector.lock();
  2382. m_vROIS.push_back(tROI);
  2383. m_mROIsProtector.unlock();
  2384. }
  2385. else
  2386. {
  2387. LOG_ERROR("No MEM 4 TARGTEG DATA");
  2388. utilizeROI(tROI);
  2389. };
  2390. }
  2391. else
  2392. {
  2393. LOG_ERROR("No MEM 4 ROI");
  2394. }
  2395. return true;
  2396. };
  2397. return fusion_consumeData(TargetRefID, L);
  2398. }
  2399. bool deviceNodeCamera::makeSnap(uint32_t TargetRefID)
  2400. {
  2401. CAM_TASK * tTask = (CAM_TASK *) malloc(sizeof(CAM_TASK));
  2402. if (tTask)
  2403. {
  2404. memset(tTask, 0, sizeof(CAM_TASK));
  2405. tTask->CMD = (CAM_PTZ_CMD *) malloc(sizeof(CAM_PTZ_CMD));
  2406. if (tTask->CMD)
  2407. {
  2408. tTask->CMD->CMD = PTZ_CAPTURE_ONLY;
  2409. tTask->movemet_needed = false;
  2410. tTask->source_evt_id = TargetRefID;
  2411. return pushCamTask(tTask);
  2412. }
  2413. else
  2414. {
  2415. free(tTask);
  2416. };
  2417. };
  2418. return false;
  2419. }
  2420. bool deviceNodeCamera::connectToStream(uint32_t id)
  2421. {
  2422. /*
  2423. if( id ){
  2424. argusKernelModule * tKM = NULL;
  2425. argusKernel * tKRNL = NULL;
  2426. argusModuleFactory * tMF = NULL;
  2427. if( m_pDBC == NULL ) {
  2428. LOG_ERROR("No Database Connector!");
  2429. return false;
  2430. };
  2431. tKRNL = argusKernel::getInstance();
  2432. if( tKRNL ) {
  2433. tMF = tKRNL->getModuleFactory();
  2434. if( tMF ) {
  2435. tKM = tMF->getModule( ARGUS_MODULE_EMBED_VIDEO );
  2436. } else {
  2437. LOG_ERROR("No ModuleFactory Instance!");
  2438. return false;
  2439. };
  2440. } else {
  2441. LOG_ERROR("No Kernel Instance!");
  2442. return false;
  2443. };
  2444. if( tKM ) {
  2445. // New Module
  2446. DBC_MEDIA_STREAM_DATA *tDATA = m_pDBC->getMediaStreamData( id );
  2447. if( tDATA ) {
  2448. m_sMediaSource.mediaStreamId = id;
  2449. if( strncmp(tDATA->mrl_proto, "v4l2", 4) == 0 ) {
  2450. if (m_pSource != NULL) delete((videoAVLIBSource*)m_pSource);
  2451. m_pSource = (basicMediaSource *) new videoAVLIBSource(&m_sMediaSource);
  2452. } else {
  2453. if (m_pSource != NULL) delete((videoLIVE555Source*)m_pSource);
  2454. m_pSource = (basicMediaSource *) new videoLIVE555Source(&m_sMediaSource );
  2455. };
  2456. if( m_pSource != NULL ) {
  2457. char tMRL[1024];
  2458. #ifdef DEBUG
  2459. LOG("Source OK");
  2460. #endif
  2461. ((videoLIVE555Source*)m_pSource)->setWM( MS_MODE_STREAM_PROXY | MS_MODE_OVERLAYED_LOOPS | MS_MODE_DECODE_FRAMES );
  2462. int tW = ((argusModuleEmbedVideo*)tKM)->getWidh();
  2463. int tH = ((argusModuleEmbedVideo*)tKM)->getHeight();
  2464. int pix = -1;
  2465. bool in_16_9 = (((tDATA->width*100) / tDATA->height ) > 140 );
  2466. bool out_16_9 = (((tW*100) / tH ) > 140 );
  2467. updateOpticalParameters( tW, tH );
  2468. if( strlen( tDATA->c_type ) ){
  2469. };
  2470. //if( tW > 0 ){
  2471. // //m_pSource->setProportionalByWidth( tW );
  2472. // m_pSource->setSize( tW, tH );
  2473. //};
  2474. if( in_16_9 != out_16_9 ){
  2475. if( in_16_9 ){
  2476. tH = ((( tW * 1000 ) / 16 ) * 9 ) / 1000;
  2477. } else {
  2478. tW = ((( tH * 1000 ) / 3 ) * 4 ) / 1000;
  2479. };
  2480. };
  2481. if (strncmp(tDATA->mrl_proto, "v4l2", 4) == 0) {
  2482. sprintf( tMRL, tDATA->mrl );
  2483. } else {
  2484. DBC_CAM_DATA *tCAM = m_pDBC->getCamData( tDATA->source_id );
  2485. if( tCAM ) {
  2486. sprintf( tMRL, "%s://%s:%s@%s:%d%s", tDATA->mrl_proto, tCAM->username, tCAM->password, tCAM->ip, tDATA->mrl_port, tDATA->mrl );
  2487. free( tCAM );
  2488. } else {
  2489. LOG_ERROR("Error Getting CAM Data!");
  2490. delete( m_pSource );
  2491. m_pSource = NULL;
  2492. return false;
  2493. };
  2494. };
  2495. LOG( tMRL );
  2496. if( m_pSource->_open( tMRL, false, -1.0f, pix, tDATA->width, tDATA->height, tW, tH ) ){
  2497. m_bSourceConnected = m_pSource->attachDestination( (argusModuleEmbedVideo*) tKM );
  2498. if ( !m_bSourceConnected ) {
  2499. //delete( m_pSource );
  2500. if( strncmp(tDATA->mrl_proto, "v4l2", 4) == 0 ) {
  2501. delete((videoAVLIBSource*)m_pSource);
  2502. } else {
  2503. delete((videoLIVE555Source*)m_pSource);
  2504. };
  2505. m_pSource = NULL;
  2506. } else {
  2507. m_nActiveStreamID = id;
  2508. #ifdef DEBUG
  2509. LOG("Source is Connected to Output.");
  2510. #endif
  2511. };
  2512. } else {
  2513. LOG_ERROR("Can not Open Video Source!");
  2514. //delete( m_pSource );
  2515. if( strncmp(tDATA->mrl_proto, "v4l2", 4) == 0 ) {
  2516. delete((videoAVLIBSource*)m_pSource);
  2517. } else {
  2518. delete((videoLIVE555Source*)m_pSource);
  2519. };
  2520. m_pSource = NULL;
  2521. };
  2522. } else {
  2523. LOG_ERROR("Video Source Stream Failure!");
  2524. };
  2525. free( tDATA );
  2526. };
  2527. }
  2528. #ifdef DEBUG
  2529. else LOG("No Video Module!")
  2530. #endif
  2531. ;
  2532. };
  2533. LOG("Connect Finished!");
  2534. return m_bSourceConnected;
  2535. */
  2536. return true;
  2537. }
  2538. bool deviceNodeCamera::disconnectFromStream(void)
  2539. {
  2540. /*if( m_bSourceConnected ) {
  2541. if( m_pSource ){
  2542. delete( m_pSource );
  2543. m_pSource = NULL;
  2544. };
  2545. m_bSourceConnected = false;
  2546. };
  2547. return !m_bSourceConnected;
  2548. */
  2549. return true;
  2550. }
  2551. bool deviceNodeCamera::validateStream(void)
  2552. {
  2553. /*
  2554. if( m_bSourceConnected ) {
  2555. if( m_pSource ) {
  2556. if( m_pSource->isAttached() ){
  2557. if( m_pSource->getStatus() < MS_STOPED ) return true;
  2558. #ifdef DEBUG
  2559. LOG("Source is Finished!");
  2560. #endif
  2561. };
  2562. #ifdef DEBUG
  2563. LOG("Source is Disconnected!");
  2564. #endif
  2565. } else LOG_ERROR("No Source Object! Set Disconnected!");
  2566. disconnectFromStream();
  2567. };
  2568. return false;
  2569. */
  2570. return true;
  2571. }
  2572. bool deviceNodeCamera::validate()
  2573. {
  2574. if (m_pDBC == NULL)
  2575. {
  2576. return false;
  2577. }
  2578. DBC_CAM_DATA * tDATA = m_pDBC->getCamData(m_sCamData.id);
  2579. if (tDATA == NULL)
  2580. {
  2581. return false;
  2582. }
  2583. if (memcmp(tDATA, &m_sCamData, sizeof(DBC_CAM_DATA)) != 0)
  2584. {
  2585. // Settings Changed
  2586. // memcpy(&m_sCamData, tDATA, sizeof(DBC_CAM_DATA) );
  2587. free(tDATA);
  2588. return false;
  2589. };
  2590. free(tDATA);
  2591. if (! validateStream())
  2592. {
  2593. return false;
  2594. }
  2595. return deviceNode::validate();
  2596. }
  2597. bool deviceNodeCamera::load_presets(void)
  2598. {
  2599. bool bHaveIROFF = false;
  2600. bool bHaveIRON = false;
  2601. bool bHaveIRAUTO = false;
  2602. // Request PRESETS FROM CAMERA?! for some PTZ cameras without presets inpossible to
  2603. // operate by presets
  2604. if (m_pProtocol->getCameraPresets())
  2605. {
  2606. LOG("Camera Presets Loadded");
  2607. }
  2608. else
  2609. {
  2610. LOG_ERROR("Error while loadding Camera Presets!");
  2611. }
  2612. if (m_pDBC == NULL)
  2613. {
  2614. return false;
  2615. }
  2616. DBC_PRESETS_INFO * tData = m_pDBC->getCamPresetInfo(m_sCamData.id);
  2617. if (tData)
  2618. {
  2619. memcpy(&m_sCamPresets, tData, sizeof(DBC_PRESETS_INFO));
  2620. #ifdef DEBUG
  2621. char tB[128];
  2622. LOG("Loading Presets!");
  2623. sprintf(tB, "Cam Presets Info: CNT: %d Cols: %d Rows: %d ", m_sCamPresets.maxCount, m_sCamPresets.gCols, m_sCamPresets.gRows);
  2624. LOG(tB);
  2625. #endif
  2626. DBC_INT_LIST * tList = m_pDBC->listCamPresets(m_sCamData.id);
  2627. if (tList)
  2628. {
  2629. for (DBC_INT_LIST::iterator idx = tList->begin(); idx < tList->end(); idx++)
  2630. {
  2631. CAM_PRESET * tData = m_pDBC->getCamPreset(*idx);
  2632. if (tData)
  2633. {
  2634. m_sCamPresets.presets.push_back(tData);
  2635. bHaveIRON = (tData->role == ARGUS_PRESET_TYPE_IR_ON) ? true : bHaveIRON;
  2636. bHaveIROFF = (tData->role == ARGUS_PRESET_TYPE_IR_OFF) ? true : bHaveIROFF;
  2637. bHaveIRAUTO = (tData->role == ARGUS_PRESET_TYPE_IR_AUTO) ? true : bHaveIRAUTO;
  2638. };
  2639. };
  2640. delete(tList);
  2641. };
  2642. if (m_sCamData.eco_ir)
  2643. {
  2644. m_bShineOnTarget = (bHaveIROFF && bHaveIRAUTO);
  2645. }
  2646. else
  2647. {
  2648. m_bShineOnTarget = false;
  2649. }
  2650. return true;
  2651. #ifdef DEBUG
  2652. }
  2653. else
  2654. {
  2655. LOG("No Camera Presets!");
  2656. #endif
  2657. };
  2658. return false;
  2659. }
  2660. void deviceNodeCamera::translateROI2PRESET(float P, float T, float Z, uint8_t & col, uint8_t & row)
  2661. {
  2662. char tB[128];
  2663. row = (uint8_t) (Z * ((float) m_sCamPresets.gRows));
  2664. if (P < 0.5)
  2665. {
  2666. P = P + 1.5f;
  2667. }
  2668. else
  2669. {
  2670. P = P - 0.5f;
  2671. };
  2672. col = (uint8_t) (P * ((float) (m_sCamPresets.gCols * (1 << row)) / 2.0f));
  2673. row++;
  2674. }
  2675. CSTR deviceNodeCamera::getPresetToken(uint8_t CCOL, uint8_t CROW)
  2676. {
  2677. if (m_sCamPresets.presets.size() > 0)
  2678. {
  2679. for (V_PRESETS::iterator idx = m_sCamPresets.presets.begin(); idx < m_sCamPresets.presets.end(); idx++)
  2680. {
  2681. if ((*idx)->role == ARGUS_PRESET_TYPE_POSITION)
  2682. {
  2683. if (((*idx)->row == CROW) && ((*idx)->col == CCOL))
  2684. {
  2685. return (CSTR) (*idx)->token;
  2686. }
  2687. }
  2688. }
  2689. }
  2690. return NULL;
  2691. }
  2692. CSTR deviceNodeCamera::getPresetToken(uint8_t specialRole)
  2693. {
  2694. if (m_sCamPresets.presets.size() > 0)
  2695. {
  2696. for (V_PRESETS::iterator idx = m_sCamPresets.presets.begin(); idx < m_sCamPresets.presets.end(); idx++)
  2697. {
  2698. if ((*idx)->role == specialRole)
  2699. {
  2700. return (CSTR) (*idx)->token;
  2701. }
  2702. }
  2703. }
  2704. return NULL;
  2705. }
  2706. #undef LOG
  2707. #undef LOG_ERROR
  2708. // #undef DEBUG_EXTRA