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.

2828 lines
68 KiB

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