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.

2821 lines
67 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[512]; \
  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. CAM_ROI * tROI = new CAM_ROI();
  731. if (tROI)
  732. {
  733. //memset(tROI, 0, sizeof(CAM_ROI));
  734. tROI->ts_ms = tTrackRecord->ts_ms;
  735. spherical2onvif(tOR, tROI->cam_orientation, m_fMaxRange, m_sCamData.pan_model, m_sCamData.pan_angles, m_sCamData.tilt_model, m_sCamData.tilt_angles);
  736. tROI->cam_position.fi = tOR.fi;
  737. tROI->cam_position.tetta = tOR.tetta;
  738. tROI->cam_position.r = tOR.r;
  739. float cFL = m_fMinFL + m_fR2FL * (tROI->cam_position.r - m_fMinRange);
  740. tROI->max_range = calcMaxDistance_mm(AC_imager_diag[m_sCamData.imager_type], cFL, m_nRealFrameWidth);
  741. tROI->min_range = calcMinDistance_mm(AC_imager_diag[m_sCamData.imager_type], cFL, m_nRealFrameWidth);
  742. tROI->h_fov = rad2deg(calcFOV_mm(AC_imager_width[m_sCamData.imager_type], cFL)) / 2.0f; // div to no to div in comparation function
  743. tROI->v_fov = rad2deg(calcFOV_mm(AC_imager_height[m_sCamData.imager_type], cFL)) / 2.0f; // that will be called more ofthen
  744. tROI->targets.push_back(tTrackRecord);
  745. m_vROIS.push_back(tROI);
  746. };
  747. };
  748. }
  749. else
  750. {
  751. #ifdef DEBUG_EXTRA
  752. LOG("Fusion: NO ROI -> CREATE!");
  753. #endif
  754. //
  755. //CAM_ROI * tROI = (CAM_ROI *) malloc(sizeof(CAM_ROI));
  756. CAM_ROI * tROI = new CAM_ROI();
  757. if (tROI)
  758. {
  759. //memset(tROI, 0, sizeof(CAM_ROI));
  760. tROI->ts_ms = tTrackRecord->ts_ms;
  761. // Set vector Cam Coordinates
  762. tROI->cam_position.fi = tOR.fi;
  763. tROI->cam_position.tetta = tOR.tetta;
  764. tROI->cam_position.r = tOR.r;
  765. // Set vector OnVIF coordinates
  766. spherical2onvif(tOR, tROI->cam_orientation, m_fMaxRange, m_sCamData.pan_model, m_sCamData.pan_angles, m_sCamData.tilt_model, m_sCamData.tilt_angles);
  767. // make fusion data BOX
  768. float cFL = m_fMinFL + m_fR2FL * (tROI->cam_position.r); // - m_fMinRange );
  769. tROI->min_range = calcMinDistance_mm(AC_imager_diag[m_sCamData.imager_type], cFL, m_nRealFrameWidth);
  770. tROI->max_range = calcMaxDistance_mm(AC_imager_diag[m_sCamData.imager_type], cFL, m_nRealFrameWidth);
  771. tROI->h_fov = rad2deg(calcFOV_mm(AC_imager_width[m_sCamData.imager_type], cFL)) / 2.0f; // div to prevent division in comparation
  772. tROI->v_fov = rad2deg(calcFOV_mm(AC_imager_height[m_sCamData.imager_type], cFL)) / 2.0f; // function that will be called more ofthen
  773. // Add ROI to vector
  774. tROI->targets.push_back(tTrackRecord);
  775. m_vROIS.push_back(tROI);
  776. }
  777. else
  778. {
  779. LOG_ERROR("No MEM4ROI");
  780. }
  781. };
  782. m_mROIsProtector.unlock();
  783. return true;
  784. }
  785. bool deviceNodeCamera::fusion_validateROI(uint64_t cts, CAM_ROI * ptr)
  786. {
  787. if (ptr == NULL)
  788. {
  789. return false;
  790. }
  791. if ((cts - ptr->ts_ms) > LOSE_INTERES_TIME_MS)
  792. {
  793. return false;
  794. }
  795. #ifdef DEBUG_EXTRA
  796. char tB[128];
  797. sprintf(tB, "\tROI [%d]", ptr->targets.size());
  798. LOG(tB);
  799. #endif
  800. if (ptr->targets.size())
  801. {
  802. double tA = 0.0;
  803. double tB = 0.0;
  804. double tC = 0.0;
  805. uint32_t tCNT = 0;
  806. for (vCAM_TRACK_RECORDS::iterator idx = ptr->targets.begin(); idx < ptr->targets.end();)
  807. {
  808. if ((cts - (*idx)->ts_ms) > LOSE_INTERES_TIME_MS)
  809. {
  810. free(*idx);
  811. ptr->targets.erase(idx);
  812. }
  813. else
  814. {
  815. tA = tA + (*idx)->target_direction.fi;
  816. tB = tB + (*idx)->target_direction.tetta;
  817. tC = tC + (*idx)->target_direction.r;
  818. tCNT++;
  819. idx++;
  820. };
  821. };
  822. if (tCNT)
  823. {
  824. C3D_SPH_DEGREES L;// = {0};
  825. //memset( &L, 0, sizeof(C3D_SPH_DEGREES));
  826. L.fi = (float) (tA / (double) tCNT);
  827. L.tetta = (float) (tB / (double) tCNT);
  828. L.r = (float) (tC / (double) tCNT);
  829. spherical2onvif(L, ptr->cam_orientation, m_fMaxRange, m_sCamData.pan_model, m_sCamData.pan_angles, m_sCamData.tilt_model, m_sCamData.tilt_angles);
  830. ptr->ts_ms = cts;
  831. float cFL = m_fMinFL + m_fR2FL * (ptr->cam_position.r - m_fMinRange);
  832. ptr->max_range = calcMaxDistance_mm(AC_imager_diag[m_sCamData.imager_type], cFL, m_nRealFrameWidth);
  833. ptr->min_range = calcMinDistance_mm(AC_imager_diag[m_sCamData.imager_type], cFL, m_nRealFrameWidth);
  834. ptr->h_fov = rad2deg(calcFOV_mm(AC_imager_width[m_sCamData.imager_type], cFL)) / 2.0f; // div to prevent division in comparation
  835. ptr->v_fov = rad2deg(calcFOV_mm(AC_imager_height[m_sCamData.imager_type], cFL)) / 2.0f; // function that will be called more ofthen
  836. return true;
  837. };
  838. };
  839. return false;
  840. }
  841. CAM_ROI * deviceNodeCamera::fusion_fetchROI(void)
  842. {
  843. CAM_ROI * tVAL = NULL;
  844. m_mROIsProtector.lock();
  845. if (m_vROIS.size())
  846. {
  847. vCAM_ROIS::iterator sRec;
  848. uint64_t cts = 0;
  849. for (vCAM_ROIS::iterator idx = m_vROIS.begin(); idx != m_vROIS.end(); idx++)
  850. {
  851. if (tVAL == NULL)
  852. {
  853. sRec = idx;
  854. tVAL = (*idx);
  855. cts = tVAL->ts_ms;
  856. }
  857. else
  858. {
  859. #ifdef CAM_FETCH_ROI_NEWEST
  860. if ((*idx)->ts_ms > cts)
  861. #else
  862. if ((*idx)->ts_ms < cts)
  863. #endif
  864. {
  865. sRec = idx;
  866. tVAL = (*idx);
  867. cts = tVAL->ts_ms;
  868. };
  869. };
  870. };
  871. m_vROIS.erase(sRec);
  872. };
  873. m_mROIsProtector.unlock();
  874. if (m_bNoAbsPositionning && tVAL)
  875. {
  876. // Skip targets older 3 seconds
  877. if (getMilliseconds() - tVAL->ts_ms > 3000)
  878. #ifdef DEBUG_EXTRA
  879. {
  880. LOG("Targetd too old! ---> Skiping");
  881. utilizeROI(tVAL);
  882. };
  883. #else
  884. utilizeROI(tVAL);
  885. #endif
  886. };
  887. return tVAL;
  888. }
  889. DBC_SNAPSHOT_DATA * deviceNodeCamera::fusion_translate_ROI(CAM_ROI *(&ptr))
  890. {
  891. DBC_SNAPSHOT_DATA * rVal = NULL;
  892. if (ptr == NULL)
  893. {
  894. return NULL;
  895. }
  896. if (ptr->targets.size())
  897. {
  898. //rVal = (DBC_SNAPSHOT_DATA *) malloc(sizeof(DBC_SNAPSHOT_DATA));
  899. rVal = new DBC_SNAPSHOT_DATA();
  900. if (rVal)
  901. {
  902. //memset(rVal, 0, sizeof(DBC_SNAPSHOT_DATA));
  903. rVal->stream_id = m_sNodeData.aux_parameter;
  904. for (vCAM_TRACK_RECORDS::iterator idx = ptr->targets.begin(); idx < ptr->targets.end(); idx++)
  905. {
  906. rVal->assoc_event_ids.push_back((*idx)->evt_db_ID);
  907. }
  908. };
  909. ptr->targets.clear();
  910. }
  911. else
  912. {
  913. LOG_ERROR("No Events ID to ROI");
  914. };
  915. utilizeROI(ptr);
  916. // Not Setting MD5 and TS -> will set at capture time
  917. return rVal;
  918. }
  919. void deviceNodeCamera::fusion_validate(void)
  920. {
  921. if (m_bNoAbsPositionning)
  922. {
  923. return;
  924. }
  925. m_mROIsProtector.lock();
  926. if (m_vROIS.size())
  927. {
  928. #ifdef DEBUG_EXTRA
  929. char tB[128];
  930. sprintf(tB, "Fusion: Have [%d] Regions Of Interes", m_vROIS.size());
  931. LOG(tB);
  932. #endif
  933. uint64_t cts = getMilliseconds();
  934. for (vCAM_ROIS::iterator idx = m_vROIS.begin(); idx != m_vROIS.end();)
  935. {
  936. if (! fusion_validateROI(cts, *idx))
  937. {
  938. utilizeROI(*idx);
  939. m_vROIS.erase(idx);
  940. }
  941. else
  942. {
  943. idx++;
  944. }
  945. };
  946. };
  947. m_mROIsProtector.unlock();
  948. }
  949. bool deviceNodeCamera::absoluteMove(uint32_t TargetRefID, float PanPOS, float TiltPOS, float ZoomPOS)
  950. {
  951. if (m_sCamData.online)
  952. {
  953. CAM_TASK * tTask = (CAM_TASK *) malloc(sizeof(CAM_TASK));
  954. if (tTask)
  955. {
  956. memset(tTask, 0, sizeof(CAM_TASK));
  957. tTask->CMD = (CAM_PTZ_CMD *) malloc(sizeof(CAM_PTZ_CMD));
  958. if (tTask->CMD)
  959. {
  960. tTask->CMD->CMD = PTZ_ABSOLUTE;
  961. tTask->CMD->PAN = PanPOS;
  962. tTask->CMD->TILT = TiltPOS;
  963. tTask->CMD->ZOOM = ZoomPOS;
  964. tTask->movemet_needed = true;
  965. tTask->source_evt_id = TargetRefID;
  966. setWM(CAM_WS_BUSY);
  967. return pushCamTask(tTask);
  968. }
  969. else
  970. {
  971. LOG_ERROR("CMD malloc Error!");
  972. free(tTask);
  973. };
  974. }
  975. else
  976. {
  977. LOG_ERROR("Task malloc error!");
  978. }
  979. };
  980. return false;
  981. }
  982. bool deviceNodeCamera::absoluteMove(uint32_t TargetRefID, C3D_ORIENT_ONVIF O)
  983. {
  984. if (m_sCamData.online)
  985. {
  986. CAM_TASK * tTask = (CAM_TASK *) malloc(sizeof(CAM_TASK));
  987. if (tTask)
  988. {
  989. memset(tTask, 0, sizeof(CAM_TASK));
  990. tTask->CMD = (CAM_PTZ_CMD *) malloc(sizeof(CAM_PTZ_CMD));
  991. if (tTask->CMD)
  992. {
  993. tTask->CMD->CMD = PTZ_ABSOLUTE;
  994. tTask->CMD->PAN = O.pan;
  995. tTask->CMD->TILT = O.tilt;
  996. tTask->CMD->ZOOM = O.zoom;
  997. tTask->movemet_needed = true;
  998. tTask->source_evt_id = TargetRefID;
  999. setWM(CAM_WS_BUSY);
  1000. return pushCamTask(tTask);
  1001. }
  1002. else
  1003. {
  1004. LOG_ERROR("CMD malloc Error!");
  1005. free(tTask);
  1006. };
  1007. }
  1008. else
  1009. {
  1010. LOG_ERROR("Task malloc error!");
  1011. }
  1012. };
  1013. return false;
  1014. }
  1015. uint8_t deviceNodeCamera::getPtzStatus()
  1016. {
  1017. if (m_sCamData.protocol == 0)
  1018. {
  1019. return 1;
  1020. }
  1021. if (getWM() > CAM_WS_INIT)
  1022. {
  1023. return 1;
  1024. }
  1025. return 0;
  1026. }
  1027. uint8_t deviceNodeCamera::getStreamStatus()
  1028. {
  1029. return 1;
  1030. }
  1031. void deviceNodeCamera::m_fThreadProc(void)
  1032. {
  1033. DBC_PUSHED_CMD * PUSHED = NULL;
  1034. m_bThreadStatus = TH_WORKING;
  1035. bool bNewSecondLoop = false;
  1036. uint32_t cTimeSeconds = 0;
  1037. uint32_t oTimeSeconds = 0;
  1038. while (m_bThreadStatus < TH_ENDED)
  1039. {
  1040. cTimeSeconds = (uint32_t) time(NULL);
  1041. if ((bNewSecondLoop = (cTimeSeconds != oTimeSeconds))) oTimeSeconds = cTimeSeconds;
  1042. uint8_t tState = getWM();
  1043. //char tB[128];
  1044. fusion_validate();
  1045. switch (tState)
  1046. {
  1047. case CAM_WS_INIT:
  1048. {
  1049. if (m_pProtocol != NULL)
  1050. {
  1051. switch (m_sCamData.protocol)
  1052. {
  1053. case CAM_CTRL_PROTO_VISCA: delete((protocolPTZ_VISCA *) m_pProtocol); break;
  1054. case CAM_CTRL_PROTO_PELCO: delete((protocolPTZ_PELCO *) m_pProtocol); break;
  1055. case CAM_CTRL_PROTO_ONVIF: delete((protocolPTZ_ONVIF *) m_pProtocol); break;
  1056. default : delete(m_pProtocol);
  1057. };
  1058. m_pProtocol = NULL;
  1059. m_sCamData.online = false;
  1060. };
  1061. switch (m_sCamData.protocol)
  1062. {
  1063. case CAM_CTRL_PROTO_VISCA:
  1064. {
  1065. #ifdef DEBUG
  1066. LOG("VISCA");
  1067. #endif
  1068. m_pProtocol = (protocolPTZ *) new protocolPTZ_VISCA(m_sCamData.protocol_aux);
  1069. if (m_pProtocol != NULL)
  1070. {
  1071. basicTransport * tTransport = NULL;
  1072. hardwareManager * tHM = hardwareManager::getInstance();
  1073. if (tHM)
  1074. {
  1075. hardwareResource * tRES = tHM->requestResource(HARDWARE_PROVIDER_SERIAL, m_sCamData.uart_id);
  1076. if (tRES != NULL)
  1077. {
  1078. if (tRES->isValid())
  1079. {
  1080. tTransport = static_cast<basicTransport *>(static_cast<hardwareResourceSerialProvider *>(tRES));
  1081. if (m_pProtocol->setTransport(tTransport))
  1082. {
  1083. if (! m_pProtocol->create())
  1084. {
  1085. LOG_ERROR("Protocol <<< Create >>> Failure!");
  1086. };
  1087. }
  1088. else
  1089. {
  1090. LOG_ERROR("Binding Port Failure!");
  1091. };
  1092. };
  1093. #ifdef DEBUG
  1094. }
  1095. else
  1096. {
  1097. LOG("HWM Has No UART PORT!");
  1098. #endif
  1099. };
  1100. #ifdef DEBUG
  1101. }
  1102. else
  1103. {
  1104. LOG_ERROR("Can not get Hardware Manager!");
  1105. #endif
  1106. };
  1107. if (tTransport != NULL)
  1108. {
  1109. LOG("Bind Transport Port!");
  1110. }
  1111. else
  1112. {
  1113. LOG_ERROR("Transport Creation Failure!");
  1114. delete((protocolPTZ_VISCA *) m_pProtocol);
  1115. m_pProtocol = NULL;
  1116. };
  1117. #ifdef DEBUG
  1118. }
  1119. else
  1120. {
  1121. LOG_ERROR("Protocol Creation failure!");
  1122. #endif
  1123. };
  1124. break;
  1125. };
  1126. case CAM_CTRL_PROTO_PELCO:
  1127. {
  1128. #ifdef DEBUG
  1129. LOG("PELCO");
  1130. #endif
  1131. m_pProtocol = (protocolPTZ *) new protocolPTZ_PELCO(m_sCamData.protocol_aux);
  1132. if (m_pProtocol != NULL)
  1133. {
  1134. basicTransport * tTransport = NULL;
  1135. hardwareManager * tHM = hardwareManager::getInstance();
  1136. if (tHM)
  1137. {
  1138. hardwareResource * tRES = tHM->requestResource(HARDWARE_PROVIDER_SERIAL, m_sCamData.uart_id);
  1139. if (tRES != NULL)
  1140. {
  1141. if (tRES->isValid())
  1142. {
  1143. tTransport = static_cast<basicTransport *>(static_cast<hardwareResourceSerialProvider *>(tRES));
  1144. if (m_pProtocol->setTransport(tTransport))
  1145. {
  1146. if (! m_pProtocol->create())
  1147. {
  1148. LOG_ERROR("Protocol <<< Create >>> Failure!");
  1149. };
  1150. }
  1151. else
  1152. {
  1153. LOG_ERROR("Binding Port Failure!");
  1154. };
  1155. };
  1156. #ifdef DEBUG
  1157. }
  1158. else
  1159. {
  1160. LOG("HWM Has No UART PORT!");
  1161. #endif
  1162. };
  1163. #ifdef DEBUG
  1164. }
  1165. else
  1166. {
  1167. LOG_ERROR("Can not get Hardware Manager!");
  1168. #endif
  1169. };
  1170. if (tTransport != NULL)
  1171. {
  1172. LOG("Bind Transport Port!");
  1173. }
  1174. else
  1175. {
  1176. LOG_ERROR("Transport Creation Failure!");
  1177. delete((protocolPTZ_PELCO *) m_pProtocol);
  1178. m_pProtocol = NULL;
  1179. };
  1180. #ifdef DEBUG
  1181. }
  1182. else
  1183. {
  1184. LOG_ERROR("Protocol Creation failure!");
  1185. #endif
  1186. };
  1187. break;
  1188. };
  1189. case CAM_CTRL_PROTO_ONVIF:
  1190. {
  1191. #ifdef DEBUG_EXTRA
  1192. LOG("ONVIF");
  1193. #endif
  1194. // m_pProtocol = (protocolPTZ *) new protocolPTZ_ONVIF( m_sCamData.ip, m_sCamData.port, m_sCamData.username, m_sCamData.password, m_sCamData.auth_mode );
  1195. if (strstr(m_sCamData.onvif_ctrl, "http") == NULL)
  1196. {
  1197. char tURI[512];
  1198. if (m_sCamData.port)
  1199. {
  1200. sprintf(tURI, "http://%s:%d%s", m_sCamData.ip, m_sCamData.port, m_sCamData.onvif_ctrl);
  1201. }
  1202. else
  1203. {
  1204. sprintf(tURI, "http://%s%s", m_sCamData.ip, m_sCamData.onvif_ctrl);
  1205. };
  1206. m_pProtocol = (protocolPTZ *) new protocolPTZ_ONVIF(tURI, m_sCamData.username, m_sCamData.password, m_sCamData.auth_mode);
  1207. }
  1208. else
  1209. {
  1210. m_pProtocol = (protocolPTZ *) new protocolPTZ_ONVIF(m_sCamData.onvif_ctrl, m_sCamData.username, m_sCamData.password, m_sCamData.auth_mode);
  1211. };
  1212. #ifdef DEBUG_EXTRA
  1213. LOG("ONVIF-END");
  1214. #endif
  1215. if (m_pProtocol != NULL)
  1216. {
  1217. if (! m_pProtocol->create())
  1218. {
  1219. LOG_ERROR("Error Creating Proto!");
  1220. delete((protocolPTZ_ONVIF *) m_pProtocol);
  1221. m_pProtocol = NULL;
  1222. };
  1223. #ifdef DEBUG_EXTRA
  1224. LOG("PROTO CREATE DONE");
  1225. #endif
  1226. };
  1227. break;
  1228. };
  1229. default:
  1230. {
  1231. #ifdef DEBUG
  1232. LOG("Camera Has no defined control protocol");
  1233. #endif
  1234. };
  1235. };
  1236. if (m_pProtocol != NULL)
  1237. {
  1238. // Load Presets only if we have protocol
  1239. load_presets();
  1240. mojoKernel * tKRNL = mojoKernel::getInstance();
  1241. if (tKRNL)
  1242. {
  1243. m_bSkeepPositioningWait = ((tKRNL->getMediaMode() & EVE_OUTPUT_VIDEO) == EVE_OUTPUT_VIDEO);
  1244. #ifdef DEBUG_EXTRA
  1245. char ttTT[128];
  1246. sprintf(ttTT, "Kernel Media Mode:%d", tKRNL->getMediaMode());
  1247. LOG(ttTT);
  1248. if (m_bSkeepPositioningWait)
  1249. {
  1250. LOG("Camera No Wait On Pos!");
  1251. }
  1252. else
  1253. {
  1254. LOG("Camera Will Wait On Pos!");
  1255. };
  1256. #endif
  1257. }
  1258. else
  1259. {
  1260. LOG_ERROR("No Kernel Instance!");
  1261. };
  1262. m_sCamData.online = true;
  1263. setWM(CAM_WS_POWER_INIT);
  1264. };
  1265. m_pDBC->updateCamOnlineStatus(m_sCamData.id, m_sCamData.online);
  1266. break;
  1267. };
  1268. case CAM_WS_POWER_INIT:
  1269. {
  1270. #ifdef DEBUG
  1271. LOG("Init Camera - PowerMode");
  1272. #endif
  1273. // setBusy( true );
  1274. if (m_pProtocol->initCamera())
  1275. {
  1276. setWM(CAM_WS_WHITE_BALLANCE);
  1277. }
  1278. // setBusy( false );
  1279. break;
  1280. };
  1281. case CAM_WS_WHITE_BALLANCE:
  1282. {
  1283. #ifdef DEBUG
  1284. LOG("Set WB");
  1285. #endif
  1286. // setBusy( true );
  1287. if (m_pProtocol->setWhiteBalanceMode(WB_AUTO))
  1288. {
  1289. setWM(CAM_WS_BACKLIGHT);
  1290. }
  1291. // setBusy( false );
  1292. break;
  1293. };
  1294. case CAM_WS_BACKLIGHT:
  1295. {
  1296. #ifdef DEBUG
  1297. LOG("Set BLC");
  1298. #endif
  1299. // setBusy( true );
  1300. if (m_pProtocol->setBackLightMode(true))
  1301. {
  1302. setWM(CAM_WS_AUTO_FOCUS);
  1303. }
  1304. // setBusy( false );
  1305. break;
  1306. };
  1307. case CAM_WS_AUTO_FOCUS:
  1308. {
  1309. #ifdef DEBUG
  1310. LOG("Set AF");
  1311. #endif
  1312. // setBusy( true );
  1313. if (m_pProtocol->setAutoFocus(true))
  1314. {
  1315. setWM(CAM_WS_INIT_GO_HOME);
  1316. }
  1317. // setBusy( false );
  1318. break;
  1319. };
  1320. case CAM_WS_INIT_GO_HOME:
  1321. {
  1322. m_nHomingCounter = 0;
  1323. /* Skip For Restartin bug
  1324. if( !m_bNoAbsPositionning ) {
  1325. #ifdef DEBUG
  1326. LOG("INIT GO -1 0 -1");
  1327. #endif
  1328. if (m_pProtocol->goAbsolute(-1, 0, -1)) {
  1329. m_fCP_Pan = -1;
  1330. m_fCP_Tilt = 0;
  1331. m_fCP_Zoom = -1;
  1332. setWM( CAM_WS_READY );
  1333. };
  1334. } else {
  1335. #ifdef DEBUG
  1336. LOG("Go Home!");
  1337. #endif
  1338. CSTR TK = getPresetToken( ARGUS_PRESET_TYPE_HOME );
  1339. if( TK ) {
  1340. if ( !m_bCameraOnHomePosition ) {
  1341. if( !m_pProtocol->goPreset( TK )){
  1342. LOG_ERROR("Cannot Issue goPreset Command!");
  1343. };
  1344. m_bCameraOnHomePosition = true;
  1345. };
  1346. };
  1347. setWM( CAM_WS_READY );
  1348. };*/
  1349. setWM(CAM_WS_READY);
  1350. break;
  1351. };
  1352. case CAM_WS_AUTOMATED_MOVE:
  1353. {
  1354. m_bCameraOnHomePosition = false;
  1355. m_nRelativeMoveCounter++;
  1356. if ((m_bRelativeMoveMatrix & 0x03) != 0)
  1357. {
  1358. // Pan Move
  1359. if ((m_bRelativeMoveMatrix & 0x01) == 0)
  1360. {
  1361. m_fRM_Pan -= m_fSpeedRise;
  1362. }
  1363. else
  1364. {
  1365. m_fRM_Pan += m_fSpeedRise;
  1366. };
  1367. };
  1368. if ((m_bRelativeMoveMatrix & 0x0C) != 0)
  1369. {
  1370. // TILT Move
  1371. if ((m_bRelativeMoveMatrix & 0x04) == 0)
  1372. {
  1373. m_fRM_Tilt -= m_fSpeedRise;
  1374. }
  1375. else
  1376. {
  1377. m_fRM_Tilt += m_fSpeedRise;
  1378. }
  1379. };
  1380. if ((m_bRelativeMoveMatrix & 0x30) != 0)
  1381. {
  1382. // ZOOM Move
  1383. if ((m_bRelativeMoveMatrix & 0x10) == 0)
  1384. {
  1385. // Wide
  1386. m_fRM_Zoom -= m_fSpeedRise;
  1387. }
  1388. else
  1389. {
  1390. // Tele
  1391. m_fRM_Zoom += m_fSpeedRise;
  1392. };
  1393. };
  1394. if (m_fRM_Pan < -1.0f)
  1395. {
  1396. m_fRM_Pan = -1.0f;
  1397. }
  1398. if (m_fRM_Pan > 1.0f)
  1399. {
  1400. m_fRM_Pan = 1.0f;
  1401. }
  1402. if (m_fRM_Tilt < -1.0f)
  1403. {
  1404. m_fRM_Tilt = -1.0f;
  1405. }
  1406. if (m_fRM_Tilt > 1.0f)
  1407. {
  1408. m_fRM_Tilt = 1.0f;
  1409. }
  1410. if (m_fRM_Zoom < -1.0f)
  1411. {
  1412. m_fRM_Zoom = -1.0f;
  1413. }
  1414. if (m_fRM_Zoom > 1.0f)
  1415. {
  1416. m_fRM_Zoom = 1.0f;
  1417. }
  1418. m_fCP_Zoom += 0.5f * m_fRM_Zoom;
  1419. if (m_fCP_Zoom < -1.0f)
  1420. {
  1421. m_fCP_Zoom = -1.0f;
  1422. }
  1423. if (m_fCP_Zoom > 1.0f)
  1424. {
  1425. m_fCP_Zoom = 1.0f;
  1426. }
  1427. if (! relativeMove(m_fRM_Pan, m_fRM_Tilt, m_fRM_Zoom))
  1428. {
  1429. m_nRelativeMoveCounter = 0;
  1430. setWM(CAM_WS_READY);
  1431. break;
  1432. };
  1433. if (m_sCamData.ptz_axel)
  1434. { // Manual
  1435. if (m_nRelativeMoveCounter > m_sCamData.ptz_axel)
  1436. {
  1437. m_nRelativeMoveCounter = 0;
  1438. setWM(CAM_WS_READY);
  1439. };
  1440. }
  1441. else
  1442. {
  1443. // Stepping Axel by Zoom
  1444. uint8_t tVal = (uint8_t) (10.0f * (2.0f - (1.0f + m_fCP_Zoom)));
  1445. if (m_nRelativeMoveCounter > tVal)
  1446. {
  1447. m_nRelativeMoveCounter = 0;
  1448. setWM(CAM_WS_READY);
  1449. };
  1450. };
  1451. break;
  1452. };
  1453. case CAM_WS_STOP_MOVE:
  1454. {
  1455. m_nRelativeMoveCounter++;
  1456. m_fRM_Pan = .0f;
  1457. m_fRM_Pan = .0f;
  1458. m_fRM_Tilt = .0f;
  1459. m_fRM_Tilt = .0f;
  1460. m_fRM_Zoom = .0f;
  1461. m_fRM_Zoom = .0f;
  1462. if (relativeStop())
  1463. {
  1464. m_nHomingCounter = 0;
  1465. m_nRelativeMoveCounter = 0;
  1466. setWM(CAM_WS_READY);
  1467. break;
  1468. };
  1469. if (m_sCamData.ptz_axel)
  1470. {
  1471. if (m_nRelativeMoveCounter > m_sCamData.ptz_axel)
  1472. {
  1473. setWM(CAM_WS_READY);
  1474. }
  1475. }
  1476. else
  1477. {
  1478. uint8_t tVal = (uint8_t) (10.0f * (2.0f - (1.0f + m_fCP_Zoom)));
  1479. if (m_nRelativeMoveCounter > tVal)
  1480. {
  1481. setWM(CAM_WS_READY);
  1482. }
  1483. };
  1484. break;
  1485. };
  1486. case CAM_WS_GO_ABS_ROI:
  1487. case CAM_WS_GO_ABS_POSITION:
  1488. {
  1489. m_bCameraOnHomePosition = false;
  1490. setWM(CAM_WS_BUSY);
  1491. if (m_pCurrentROI != NULL)
  1492. {
  1493. // LOG("ROI Ok");
  1494. m_nHomingCounter = 0;
  1495. if (m_pBEH)
  1496. {
  1497. EVENT_CONTAINER * tEVT = createBasicEvent(m_sNodeData.node_id, EVT_CLASS_CAMERA_CTRL, EVT_CAMERA_MOVE_TO, NULL, NULL, NULL);
  1498. if (tEVT)
  1499. {
  1500. m_pBEH->broadcast(tEVT);
  1501. }
  1502. };
  1503. if (m_bNoAbsPositionning)
  1504. {
  1505. // LOG("ABS to PRESET");
  1506. uint8_t COL, ROW;
  1507. translateROI2PRESET(m_pCurrentROI->cam_orientation.pan, m_pCurrentROI->cam_orientation.tilt, m_pCurrentROI->cam_orientation.zoom, COL, ROW);
  1508. // char tB[64];
  1509. // sprintf( tB, "Get ABS Preset Col %d Row %d!", COL, ROW );
  1510. // LOG( tB );
  1511. // sprintf( tB, "Old Preset Col %d Row %d!", m_bCurrentPresetCol, m_bCurrentPresetRow );
  1512. // LOG( tB );
  1513. if ((m_bCurrentPresetCol == COL) && (m_bCurrentPresetRow == ROW))
  1514. {
  1515. m_fCP_Pan = m_pCurrentROI->cam_orientation.pan;
  1516. m_fCP_Tilt = m_pCurrentROI->cam_orientation.tilt;
  1517. m_fCP_Zoom = m_pCurrentROI->cam_orientation.zoom;
  1518. #ifdef DEBUG_EXTRA
  1519. LOG("INSTANT BCAST CAM ONPOS");
  1520. #endif
  1521. // Send Capture event
  1522. DBC_SNAPSHOT_DATA * tData = fusion_translate_ROI(m_pCurrentROI);
  1523. if (tData != NULL)
  1524. {
  1525. if (m_pBEH)
  1526. {
  1527. EVENT_CONTAINER * tEVT = createBasicEvent(m_sNodeData.node_id, EVT_CLASS_CAMERA_CTRL, EVT_CAMERA_ON_POSITION, NULL, NULL, tData);
  1528. if (tEVT)
  1529. {
  1530. m_pBEH->broadcast(tEVT);
  1531. }
  1532. else
  1533. {
  1534. //free(tData);
  1535. delete(tData);
  1536. };
  1537. }
  1538. else
  1539. {
  1540. //free(tData);
  1541. delete(tData);
  1542. };
  1543. };
  1544. setWM(CAM_WS_READY);
  1545. }
  1546. else
  1547. {
  1548. CSTR token = getPresetToken(COL, ROW);
  1549. if (token)
  1550. {
  1551. // update position Info
  1552. m_bCurrentPresetCol = COL;
  1553. m_bCurrentPresetRow = ROW;
  1554. m_fCP_Pan = m_pCurrentROI->cam_orientation.pan;
  1555. m_fCP_Tilt = m_pCurrentROI->cam_orientation.tilt;
  1556. m_fCP_Zoom = m_pCurrentROI->cam_orientation.zoom;
  1557. if (! m_pProtocol->goPreset(token))
  1558. {
  1559. LOG_ERROR("Cannot Issue goPreset Command!->Restart Camera?");
  1560. setWM(CAM_WS_END);
  1561. }
  1562. else
  1563. {
  1564. // Waiting Movement finish
  1565. if (m_bSkeepPositioningWait && 0)
  1566. {
  1567. #ifdef DEBUG_EXTRA
  1568. LOG("INSTANT BCAST CAM ONPOS");
  1569. #endif
  1570. // Send Capture event
  1571. DBC_SNAPSHOT_DATA * tData = fusion_translate_ROI(m_pCurrentROI);
  1572. if (tData != NULL)
  1573. {
  1574. if (m_pBEH)
  1575. {
  1576. EVENT_CONTAINER * tEVT = createBasicEvent(m_sNodeData.node_id, EVT_CLASS_CAMERA_CTRL, EVT_CAMERA_ON_POSITION, NULL, NULL, tData);
  1577. if (tEVT)
  1578. {
  1579. m_pBEH->broadcast(tEVT);
  1580. }
  1581. else
  1582. {
  1583. //free(tData);
  1584. delete(tData);
  1585. };
  1586. }
  1587. else
  1588. {
  1589. //free(tData);
  1590. delete(tData);
  1591. };
  1592. };
  1593. setWM(CAM_WS_READY);
  1594. }
  1595. else
  1596. {
  1597. // utilizeROI( m_pCurrentROI );
  1598. setWM(CAM_WS_WAIT_ONPOS);
  1599. };
  1600. };
  1601. }
  1602. else
  1603. {
  1604. #ifdef DEBUG_EXTRA
  1605. LOG("Not Found!");
  1606. #endif
  1607. utilizeROI(m_pCurrentROI);
  1608. setWM(CAM_WS_READY);
  1609. };
  1610. };
  1611. }
  1612. else
  1613. {
  1614. // update position Info
  1615. m_fCP_Pan = m_pCurrentROI->cam_orientation.pan;
  1616. m_fCP_Tilt = m_pCurrentROI->cam_orientation.tilt;
  1617. m_fCP_Zoom = m_pCurrentROI->cam_orientation.zoom;
  1618. // Do Movement
  1619. if (! m_pProtocol->goAbsolute(m_pCurrentROI->cam_orientation.pan, m_pCurrentROI->cam_orientation.tilt, m_pCurrentROI->cam_orientation.zoom * m_fZoomCorrection))
  1620. {
  1621. LOG_ERROR("Cannot Issue goAbsolute Command!->Restart Camera?");
  1622. setWM(CAM_WS_END);
  1623. }
  1624. else
  1625. {
  1626. // Waiting Movement finish
  1627. if (m_bSkeepPositioningWait)
  1628. {
  1629. #ifdef DEBUG_EXTRA
  1630. LOG("INSTANT BCAST CAM ONPOS");
  1631. #endif
  1632. // Send Capture event
  1633. DBC_SNAPSHOT_DATA * tData = fusion_translate_ROI(m_pCurrentROI);
  1634. if (tData != NULL)
  1635. {
  1636. if (m_pBEH)
  1637. {
  1638. EVENT_CONTAINER * tEVT = createBasicEvent(m_sNodeData.node_id, EVT_CLASS_CAMERA_CTRL, EVT_CAMERA_ON_POSITION, NULL, NULL, tData);
  1639. if (tEVT)
  1640. {
  1641. m_pBEH->broadcast(tEVT);
  1642. }
  1643. else
  1644. {
  1645. //free(tData);
  1646. delete(tData);
  1647. };
  1648. }
  1649. else
  1650. {
  1651. //free(tData);
  1652. delete(tData);
  1653. };
  1654. };
  1655. setWM(CAM_WS_READY);
  1656. }
  1657. else
  1658. {
  1659. setWM(CAM_WS_WAIT_ONPOS);
  1660. };
  1661. };
  1662. };
  1663. }
  1664. else
  1665. {
  1666. setWM(CAM_WS_READY);
  1667. }
  1668. break;
  1669. };
  1670. case CAM_WS_WAIT_ONPOS:
  1671. {
  1672. // LOG("WaitOnPos...");
  1673. DBC_SNAPSHOT_DATA * tData = NULL;
  1674. if (m_pCurrentROI != NULL)
  1675. {
  1676. // Send Capture event
  1677. tData = fusion_translate_ROI(m_pCurrentROI);
  1678. };
  1679. // To Accept any new ROI's ??
  1680. // setWM( CAM_WS_READY );
  1681. #ifdef DEBUG_EXTRA
  1682. LOG("Start Waiting Cam On Position!");
  1683. #endif
  1684. if (! m_bSkeepPositioningWait)
  1685. {
  1686. if (! m_pProtocol->waitMoveIsDone())
  1687. {
  1688. m_bSkeepPositioningWait = true;
  1689. }
  1690. };
  1691. if (m_bSkeepPositioningWait)
  1692. {
  1693. SLEEP(3000);
  1694. }
  1695. #ifdef DEBUG_EXTRA
  1696. LOG("Done Waiting Cam On Position!");
  1697. #endif
  1698. if (tData != NULL)
  1699. {
  1700. if (m_pBEH)
  1701. {
  1702. EVENT_CONTAINER * tEVT = createBasicEvent(m_sNodeData.node_id, EVT_CLASS_CAMERA_CTRL, EVT_CAMERA_ON_POSITION, NULL, NULL, tData);
  1703. if (tEVT)
  1704. {
  1705. #ifdef DEBUG_EXTRA
  1706. LOG("BCAST CAM ONPOS");
  1707. #endif
  1708. m_pBEH->broadcast(tEVT);
  1709. }
  1710. else
  1711. {
  1712. //free(tData);
  1713. delete(tData);
  1714. };
  1715. }
  1716. else
  1717. {
  1718. //free(tData);
  1719. delete(tData);
  1720. };
  1721. tData = NULL;
  1722. };
  1723. setWM(CAM_WS_READY);
  1724. break;
  1725. };
  1726. case CAM_WS_GO_HOMING_PRESET:
  1727. {
  1728. if (! m_bCameraOnHomePosition)
  1729. {
  1730. // For new Camera Presets
  1731. CSTR TK = getPresetToken(ARGUS_PRESET_TYPE_HOME);
  1732. if (TK)
  1733. {
  1734. #ifdef DEBUG_EXTRA
  1735. LOG("Go Home!");
  1736. LOG(TK);
  1737. #endif
  1738. if (! m_pProtocol->goPreset(TK))
  1739. {
  1740. LOG_ERROR("Cannot Issue goPreset Command!");
  1741. };
  1742. }
  1743. else
  1744. {
  1745. // Old Method
  1746. if (m_sCamData.home_token[0] != (char) 0)
  1747. {
  1748. #ifdef DEBUG_EXTRA
  1749. LOG("Go Home!");
  1750. LOG(m_sCamData.home_token);
  1751. #endif
  1752. if (! m_pProtocol->goPreset(m_sCamData.home_token))
  1753. {
  1754. LOG_ERROR("Cannot Issue goPreset Command!");
  1755. };
  1756. };
  1757. };
  1758. m_bCameraOnHomePosition = true;
  1759. m_bCurrentPresetCol = 0;
  1760. m_bCurrentPresetRow = 0;
  1761. };
  1762. setWM(CAM_WS_READY);
  1763. break;
  1764. };
  1765. case CAM_WS_GO_PRESET:
  1766. {
  1767. LOG("Go Preset!");
  1768. // if( m_sCamData.home_token[0] != (char)0 ) {
  1769. // LOG("Go Home!");
  1770. // LOG( m_sCamData.home_token );
  1771. // if( !m_pProtocol->goPreset( m_sCamData.home_token )){
  1772. // LOG_ERROR("Cannot Issue goPreset Command!");
  1773. // };
  1774. // };
  1775. setWM(CAM_WS_READY);
  1776. break;
  1777. };
  1778. case CAM_WS_SET_PRESET:
  1779. {
  1780. LOG("Set Preset!");
  1781. // if( m_sCamData.home_token[0] != (char)0 ) {
  1782. // LOG("Go Home!");
  1783. // LOG( m_sCamData.home_token );
  1784. // if( !m_pProtocol->goPreset( m_sCamData.home_token )){
  1785. // LOG_ERROR("Cannot Issue goPreset Command!");
  1786. // };
  1787. // };
  1788. setWM(CAM_WS_READY);
  1789. break;
  1790. };
  1791. case CAM_WS_BUSY:
  1792. {
  1793. // Handle Operation TO
  1794. setWM(CAM_WS_READY);
  1795. break;
  1796. };
  1797. case CAM_WS_READY:
  1798. {
  1799. m_pCurrentROI = fusion_fetchROI();
  1800. if (m_pCurrentROI != NULL)
  1801. {
  1802. #ifdef DEBUG_EXTRA
  1803. LOG("HAVE ROI to see!");
  1804. #endif
  1805. setWM(CAM_WS_GO_ABS_POSITION);
  1806. }
  1807. else
  1808. {
  1809. // Idle Work --->
  1810. if (m_sCamData.eco_ir)
  1811. {
  1812. m_mIRProtector.lock();
  1813. if (m_bIRIsOn)
  1814. {
  1815. if (m_bIRTO)
  1816. {
  1817. if (bNewSecondLoop)
  1818. {
  1819. m_bIRTO--;
  1820. }
  1821. }
  1822. else
  1823. {
  1824. CSTR TK = getPresetToken(ARGUS_PRESET_TYPE_IR_OFF);
  1825. if (TK)
  1826. {
  1827. if (m_pProtocol->goPreset(TK))
  1828. {
  1829. m_bIRIsOn = false;
  1830. }
  1831. }
  1832. };
  1833. };
  1834. m_mIRProtector.unlock();
  1835. };
  1836. if (m_sCamData.homing_enable)
  1837. {
  1838. if (bNewSecondLoop)
  1839. {
  1840. if (m_nHomingCounter < m_sCamData.homing_to)
  1841. {
  1842. m_nHomingCounter++;
  1843. }
  1844. else
  1845. {
  1846. // LOG("Go Home Position!");
  1847. m_nHomingCounter = 0;
  1848. setWM(CAM_WS_GO_HOMING_PRESET);
  1849. break;
  1850. };
  1851. };
  1852. };
  1853. CAM_TASK * tTASK = fetchCamTask();
  1854. if (tTASK != NULL)
  1855. {
  1856. if (tTASK->movemet_needed)
  1857. {
  1858. CAM_PTZ_CMD * tCMD = tTASK->CMD;
  1859. if (tCMD)
  1860. {
  1861. switch (tCMD->CMD)
  1862. {
  1863. case PTZ_STOP:
  1864. {
  1865. m_nHomingCounter = 0;
  1866. setWM(CAM_WS_STOP_MOVE);
  1867. break;
  1868. };
  1869. case PTZ_MOVE:
  1870. {
  1871. if (tState != CAM_WS_AUTOMATED_MOVE)
  1872. {
  1873. m_nRelativeMoveCounter = 0;
  1874. }
  1875. m_bRelativeMoveMatrix = 0;
  1876. m_fRM_Pan = tCMD->PAN;
  1877. m_fRM_Tilt = tCMD->TILT;
  1878. m_fRM_Zoom = tCMD->ZOOM;
  1879. m_nHomingCounter = 0;
  1880. setWM(CAM_WS_AUTOMATED_MOVE);
  1881. break;
  1882. };
  1883. case PTZ_ABSOLUTE:
  1884. {
  1885. m_pCurrentROI = createCAM_ROI();
  1886. if (m_pCurrentROI != NULL)
  1887. {
  1888. CAM_TRACK_RECORD * tTarget = (CAM_TRACK_RECORD *) malloc(sizeof(CAM_TRACK_RECORD));
  1889. if (tTarget != NULL)
  1890. {
  1891. memset(tTarget, 0, sizeof(CAM_TRACK_RECORD));
  1892. tTarget->evt_db_ID = tTASK->source_evt_id;
  1893. m_pCurrentROI->targets.push_back(tTarget);
  1894. m_pCurrentROI->cam_orientation.pan = tCMD->PAN;
  1895. m_pCurrentROI->cam_orientation.tilt = tCMD->TILT;
  1896. m_pCurrentROI->cam_orientation.zoom = tCMD->ZOOM;
  1897. m_nHomingCounter = 0;
  1898. setWM(CAM_WS_GO_ABS_POSITION);
  1899. }
  1900. else
  1901. {
  1902. utilizeROI(m_pCurrentROI);
  1903. };
  1904. };
  1905. break;
  1906. };
  1907. case PTZ_RELOAD_CAMERA:
  1908. {
  1909. LOG("Exit Command Rcpt");
  1910. m_bThreadStatus = TH_ENDED;
  1911. };
  1912. };
  1913. free(tCMD);
  1914. tTASK->CMD = NULL;
  1915. };
  1916. }
  1917. else
  1918. {
  1919. LOG("No Movement -> INSTCAP");
  1920. // No Movement -> Just Capture
  1921. if (m_pBEH)
  1922. {
  1923. EVENT_CONTAINER * tEVT = createBasicEvent(m_sNodeData.node_id, EVT_CLASS_CAMERA_CTRL, EVT_CAMERA_ON_POSITION, NULL, NULL, NULL);
  1924. if (tEVT)
  1925. {
  1926. tEVT->m_db_id = tTASK->source_evt_id;
  1927. m_pBEH->broadcast(tEVT);
  1928. };
  1929. };
  1930. };
  1931. free(tTASK);
  1932. }
  1933. else
  1934. {
  1935. // Lowest Priority to Web Client Commands
  1936. #ifdef DEBUG_EXTRA__
  1937. LOG("Check Web CMD!");
  1938. #endif
  1939. PUSHED = m_pDBC->getNodePushedCMD(m_sNodeData.node_id);
  1940. if (PUSHED != NULL)
  1941. {
  1942. #ifdef DEBUG_EXTRA__
  1943. LOG("HAVE Web CMD!");
  1944. #endif
  1945. m_pDBC->updateNodePushedCMD(PUSHED->id, DBC_CMD_READY_TO_CLEAN);
  1946. if ((PUSHED->CMD & ARGUS_CAM_PCMD_CMD_MASK) == ARGUS_CAM_PCMD_CMD_MASK)
  1947. {
  1948. switch (PUSHED->CMD)
  1949. {
  1950. case 0x80:
  1951. {
  1952. // Call Home Preset
  1953. LOG("CALL HOME PRESET!");
  1954. CSTR TK = getPresetToken(ARGUS_PRESET_TYPE_HOME);
  1955. if (TK)
  1956. {
  1957. LOG(TK);
  1958. if (m_pProtocol->goPreset(TK))
  1959. {
  1960. m_bCameraOnHomePosition = true;
  1961. m_nHomingCounter = 0;
  1962. };
  1963. };
  1964. break;
  1965. };
  1966. case 0x81:
  1967. {
  1968. // Call IR On Preset
  1969. LOG("CALL IR ON PRESET!");
  1970. m_mIRProtector.lock();
  1971. CSTR TK = getPresetToken(ARGUS_PRESET_TYPE_IR_ON);
  1972. if (TK)
  1973. {
  1974. LOG(TK);
  1975. if (m_pProtocol->goPreset(TK))
  1976. {
  1977. m_bIRIsOn = true;
  1978. m_bIRTO = DEFAULT_SHINE_ON_TARGET_TO;
  1979. };
  1980. }
  1981. else
  1982. {
  1983. LOG("PRESET NOT FOUND!");
  1984. }
  1985. m_mIRProtector.unlock();
  1986. break;
  1987. };
  1988. case 0x82:
  1989. {
  1990. // Call IR Off Preset
  1991. LOG("CALL IR OFF PRESET!");
  1992. m_mIRProtector.lock();
  1993. CSTR TK = (m_sCamData.eco_ir) ? getPresetToken(ARGUS_PRESET_TYPE_IR_OFF) : getPresetToken(ARGUS_PRESET_TYPE_IR_AUTO);
  1994. if (TK)
  1995. {
  1996. LOG(TK);
  1997. if (m_pProtocol->goPreset(TK))
  1998. {
  1999. m_bIRIsOn = false;
  2000. m_bIRTO = 0;
  2001. };
  2002. }
  2003. else
  2004. {
  2005. LOG("PRESET NOT FOUND!");
  2006. }
  2007. m_mIRProtector.unlock();
  2008. break;
  2009. };
  2010. case 0x83:
  2011. {
  2012. // Call IR Auto Preset
  2013. LOG("CALL IR AUTO PRESET!");
  2014. m_mIRProtector.lock();
  2015. CSTR TK = getPresetToken(ARGUS_PRESET_TYPE_IR_AUTO);
  2016. if (TK)
  2017. {
  2018. LOG(TK);
  2019. if (m_pProtocol->goPreset(TK))
  2020. {
  2021. m_bIRIsOn = true;
  2022. m_bIRTO = DEFAULT_SHINE_ON_TARGET_TO;
  2023. };
  2024. }
  2025. else
  2026. {
  2027. LOG("PRESET NOT FOUND!");
  2028. }
  2029. m_mIRProtector.unlock();
  2030. break;
  2031. };
  2032. case 0x84:
  2033. {
  2034. // Call Wisher Preset
  2035. LOG("CALL WIPER PRESET!");
  2036. CSTR TK = getPresetToken(ARGUS_PRESET_TYPE_IR_WISHER);
  2037. if (TK)
  2038. {
  2039. LOG(TK);
  2040. m_pProtocol->goPreset(TK);
  2041. };
  2042. break;
  2043. };
  2044. };
  2045. }
  2046. else if (tState == CAM_WS_READY || tState == CAM_WS_AUTOMATED_MOVE)
  2047. { // No Way to get here
  2048. // Direct Movment Mode
  2049. if (PUSHED->CMD == ARGUS_CAM_PCMD_STOP)
  2050. {
  2051. m_nHomingCounter = 0;
  2052. setWM(CAM_WS_STOP_MOVE);
  2053. }
  2054. else
  2055. {
  2056. //char tB[64];
  2057. if (tState != CAM_WS_AUTOMATED_MOVE)
  2058. {
  2059. m_nRelativeMoveCounter = 0;
  2060. }
  2061. m_bRelativeMoveMatrix = (PUSHED->CMD) >> 1;
  2062. m_nHomingCounter = 0;
  2063. setWM(CAM_WS_AUTOMATED_MOVE);
  2064. };
  2065. };
  2066. free(PUSHED);
  2067. PUSHED = NULL;
  2068. };
  2069. };
  2070. };
  2071. break;
  2072. };
  2073. case CAM_WS_END:
  2074. {
  2075. LOG("Exit Command Rcpt");
  2076. m_bThreadStatus = TH_ENDED;
  2077. break;
  2078. };
  2079. };
  2080. SLEEP(DEFAULT_THREAD_SLEEP_TIME);
  2081. };
  2082. #ifdef DEBUG
  2083. LOG("CTRL Loop End!");
  2084. #endif
  2085. }
  2086. bool deviceNodeCamera::setNoPositionWait(bool FL)
  2087. {
  2088. m_bSkeepPositioningWait = FL;
  2089. return true;
  2090. }
  2091. bool deviceNodeCamera::testFOV(C3D_DECART L)
  2092. {
  2093. // System Coords -> trans to local and go to funct
  2094. systemAnchor * tSystem = systemAnchor::getInstance();
  2095. if (tSystem == NULL)
  2096. {
  2097. return false;
  2098. }
  2099. C3D_SPH_DEGREES SL;
  2100. #ifdef DEBUG_EXTRA
  2101. LOG("Test FOV local Decart coords");
  2102. #endif
  2103. if (! tSystem->convertToLocal(&m_oPosition, L, SL))
  2104. {
  2105. return false;
  2106. }
  2107. return testFOV(SL);
  2108. }
  2109. bool deviceNodeCamera::testFOV(C3D_WGS84_LOC L)
  2110. {
  2111. // Global Coords
  2112. systemAnchor * tSystem = systemAnchor::getInstance();
  2113. if (tSystem == NULL)
  2114. {
  2115. return false;
  2116. }
  2117. C3D_SPH_DEGREES SL;
  2118. #ifdef DEBUG_EXTRA
  2119. LOG("Test FOV WGS coords");
  2120. #endif
  2121. if (! tSystem->convertToLocal(&m_oPosition, L, SL))
  2122. {
  2123. return false;
  2124. }
  2125. return testFOV(SL);
  2126. }
  2127. bool deviceNodeCamera::testFOV(C3D_SPH_RADIANS L)
  2128. {
  2129. // Local Coords
  2130. C3D_SPH_DEGREES SL;
  2131. #ifdef DEBUG_EXTRA
  2132. LOG("Test FOV local Spherical(rad) coords");
  2133. #endif
  2134. spherical2spherical(L, SL);
  2135. return testFOV(SL);
  2136. }
  2137. bool deviceNodeCamera::testFOV(C3D_SPH_DEGREES SL)
  2138. {
  2139. #ifdef DEBUG_EXTRA
  2140. char tB[128];
  2141. #endif
  2142. #ifdef DEBUG_EXTRA
  2143. LOG("Test FOV local Spherical(deg) coords");
  2144. #endif
  2145. switch (m_sCamData.triggering_mode)
  2146. {
  2147. case CAM_TRIGGER_STRICT:
  2148. {
  2149. if (SL.r > m_fMaxRange || SL.r < m_fMinRange)
  2150. {
  2151. #ifdef DEBUG_EXTRA
  2152. sprintf(tB, "Target Range( %.3f.m ) is not in a Range %.3f.m <-> %.3f.m", SL.r, m_fMinRange, m_fMaxRange);
  2153. LOG(tB);
  2154. #endif
  2155. return false;
  2156. };
  2157. break;
  2158. };
  2159. case CAM_TRIGGER_ALL_NEAR:
  2160. {
  2161. if (SL.r > m_fMaxRange)
  2162. {
  2163. #ifdef DEBUG_EXTRA
  2164. sprintf(tB, "Target Range( %.3f.m ) is not in a Range %.3f.m <-> %.3f.m", SL.r, m_fMinRange, m_fMaxRange);
  2165. LOG(tB);
  2166. #endif
  2167. return false;
  2168. };
  2169. break;
  2170. };
  2171. case CAM_TRIGGER_ALL_FAR:
  2172. {
  2173. if (SL.r < m_fMinRange)
  2174. {
  2175. #ifdef DEBUG_EXTRA
  2176. sprintf(tB, "Target Range( %.3f.m ) is not in a Range %.3f.m <-> %.3f.m", SL.r, m_fMinRange, m_fMaxRange);
  2177. LOG(tB);
  2178. #endif
  2179. return false;
  2180. };
  2181. break;
  2182. };
  2183. case CAM_TRIGGER_ALL: return true;
  2184. };
  2185. // float HS = AC_imager_width[m_sCamData.imager_type];
  2186. // float VS = AC_imager_height[m_sCamData.imager_type];
  2187. // float HS2 = HS * HS;
  2188. // float VS2 = VS * VS;
  2189. // float IR = HS / VS;
  2190. // float IR2 = IR * IR;
  2191. if (m_sCamData.pan_angles == 0)
  2192. {
  2193. // Fixed Camera
  2194. float tA = fabsf(SL.fi);
  2195. float tZ = 0.5f * (m_fMinHFov + (SL.r - m_fMinRange) * m_fR2FL);
  2196. if (tA > tZ)
  2197. {
  2198. return false;
  2199. }
  2200. }
  2201. else
  2202. {
  2203. if (m_sCamData.pan_angles < 360)
  2204. { // If endless PAN -> PASSED
  2205. // Partial Pan
  2206. switch (m_sCamData.pan_model)
  2207. {
  2208. case 1:
  2209. {
  2210. // Regular Pan
  2211. float tFOV = 0.5f * (m_fMinHFov + (SL.r - m_fMinRange) * m_fR2FL);
  2212. float tRad = deg2rad(SL.fi);
  2213. #ifdef DEBUG_EXTRA
  2214. sprintf(tB, "FOV: %0.3f CC: %0.3f", tFOV, tRad);
  2215. LOG(tB);
  2216. #endif
  2217. if ((tRad < (-1.0 * tFOV)) || (tRad > (deg2rad(m_sCamData.pan_angles) + tFOV)))
  2218. {
  2219. return false;
  2220. }
  2221. break;
  2222. };
  2223. case 2:
  2224. {
  2225. break;
  2226. };
  2227. case 3:
  2228. {
  2229. break;
  2230. };
  2231. };
  2232. };
  2233. };
  2234. if (m_sCamData.tilt_angles == 0)
  2235. {
  2236. // Fixed Camera
  2237. if ((SL.tetta < (m_fMaxVFov / -2.0f)) || (SL.tetta > (m_fMaxVFov / 2.0f)))
  2238. {
  2239. return false;
  2240. }
  2241. }
  2242. else
  2243. {
  2244. if (m_sCamData.tilt_angles < 90)
  2245. { // if Cam CAN DO 90 -> Pass
  2246. // Partial Tilt
  2247. switch (m_sCamData.tilt_model)
  2248. {
  2249. case 1:
  2250. {
  2251. // Regular Tilt
  2252. float tFOV = 0.5f * (m_fMinVFov + (m_fMaxVFov - m_fMinVFov) * (SL.r / m_fMaxRange));
  2253. float tRad = deg2rad(SL.tetta);
  2254. if ((tRad < (-1.0 * tFOV)) || (tRad > (deg2rad(m_sCamData.tilt_angles) + tFOV)))
  2255. {
  2256. return false;
  2257. }
  2258. break;
  2259. };
  2260. case 2:
  2261. {
  2262. break;
  2263. };
  2264. };
  2265. };
  2266. };
  2267. return true;
  2268. }
  2269. bool deviceNodeCamera::lookAt(uint32_t TargetRefID, C3D_WGS84_LOC L)
  2270. {
  2271. systemAnchor * tSystem = systemAnchor::getInstance();
  2272. if (tSystem == NULL)
  2273. {
  2274. return false;
  2275. }
  2276. C3D_SPH_DEGREES SL;
  2277. #ifdef DEBUG_EXTRA
  2278. LOG("Look at WGS coords!");
  2279. #endif
  2280. tSystem->convertToLocal(&m_oPosition, L, SL);
  2281. return lookAt(TargetRefID, SL);
  2282. }
  2283. bool deviceNodeCamera::lookAt(uint32_t TargetRefID, C3D_DECART L)
  2284. {
  2285. systemAnchor * tSystem = systemAnchor::getInstance();
  2286. if (tSystem == NULL)
  2287. {
  2288. return false;
  2289. }
  2290. C3D_SPH_DEGREES SL;
  2291. #ifdef DEBUG_EXTRA
  2292. LOG("Look at System Decart coords!");
  2293. #endif
  2294. tSystem->convertToLocal(&m_oPosition, L, SL);
  2295. return lookAt(TargetRefID, SL);
  2296. }
  2297. bool deviceNodeCamera::lookAt(uint32_t TargetRefID, C3D_SPH_RADIANS L)
  2298. {
  2299. C3D_SPH_DEGREES SL;
  2300. #ifdef DEBUG_EXTRA
  2301. LOG("Look at spherical(rad) 2 spherical(deg) coords!");
  2302. #endif
  2303. spherical2spherical(L, SL);
  2304. return lookAt(TargetRefID, SL);
  2305. }
  2306. bool deviceNodeCamera::lookAt(uint32_t TargetRefID, C3D_SPH_DEGREES L)
  2307. {
  2308. if (m_bCameraIsStatic)
  2309. {
  2310. //DBC_SNAPSHOT_DATA * tData = (DBC_SNAPSHOT_DATA *) malloc(sizeof(DBC_SNAPSHOT_DATA));
  2311. DBC_SNAPSHOT_DATA * tData = new DBC_SNAPSHOT_DATA();
  2312. if (tData)
  2313. {
  2314. //memset(tData, 0, sizeof(DBC_SNAPSHOT_DATA));
  2315. tData->stream_id = m_sNodeData.aux_parameter;
  2316. tData->assoc_event_ids.push_back(TargetRefID);
  2317. if (m_pBEH)
  2318. {
  2319. EVENT_CONTAINER * tEVT = createBasicEvent(m_sNodeData.node_id, EVT_CLASS_CAMERA_CTRL, EVT_CAMERA_ON_POSITION, NULL, NULL, tData);
  2320. if (tEVT)
  2321. {
  2322. #ifdef DEBUG_EXTRA
  2323. LOG("INSTANT BCAST CAM ONPOS");
  2324. #endif
  2325. m_pBEH->broadcast(tEVT);
  2326. }
  2327. else
  2328. {
  2329. //free(tData);
  2330. delete(tData);
  2331. };
  2332. }
  2333. else
  2334. {
  2335. //free(tData);
  2336. delete(tData);
  2337. };
  2338. };
  2339. return true;
  2340. };
  2341. // char tB[128];
  2342. // sprintf(tB, "Target On %.3f %.3f %.3f", L.r, L.fi, L.tetta );
  2343. // LOG( tB );
  2344. // sprintf( tB, "Cam GoTo %.3f %.3f %.3f", fP, fT, fZ );
  2345. // LOG( tB );
  2346. #ifdef DEBUG_EXTRA
  2347. LOG("Look at local spherical deg. coords!(final)");
  2348. #endif
  2349. // if( getWM() == CAM_WS_READY ){
  2350. // C3D_ORIENT_ONVIF LL;
  2351. // spherical2onvif( L, LL, m_fMaxRange, m_sCamData.pan_model, m_sCamData.pan_angles, m_sCamData.tilt_model, m_sCamData.tilt_angles );
  2352. // return absoluteMove( TargetRefID, LL );
  2353. // };
  2354. // Camera is Busy
  2355. if (m_bShineOnTarget)
  2356. {
  2357. m_mIRProtector.lock();
  2358. if (! m_bIRIsOn)
  2359. {
  2360. CSTR TK = getPresetToken(ARGUS_PRESET_TYPE_IR_AUTO);
  2361. if (TK)
  2362. {
  2363. if (m_pProtocol->goPreset(TK))
  2364. {
  2365. m_bIRIsOn = true;
  2366. m_bIRTO = DEFAULT_SHINE_ON_TARGET_TO;
  2367. };
  2368. };
  2369. }
  2370. else
  2371. {
  2372. m_bIRTO = DEFAULT_SHINE_ON_TARGET_TO;
  2373. }
  2374. m_mIRProtector.unlock();
  2375. };
  2376. // targeting by Presets -> instant ROI
  2377. if (m_bNoAbsPositionning)
  2378. {
  2379. CAM_ROI * tROI = createCAM_ROI();
  2380. if (tROI != NULL)
  2381. {
  2382. CAM_TRACK_RECORD * tTarget = (CAM_TRACK_RECORD *) malloc(sizeof(CAM_TRACK_RECORD));
  2383. if (tTarget != NULL)
  2384. {
  2385. memset(tTarget, 0, sizeof(CAM_TRACK_RECORD));
  2386. tTarget->evt_db_ID = TargetRefID;
  2387. tROI->targets.push_back(tTarget);
  2388. tROI->ts_ms = getMilliseconds();
  2389. spherical2onvif(L, tROI->cam_orientation, m_fMaxRange, m_sCamData.pan_model, m_sCamData.pan_angles, m_sCamData.tilt_model, m_sCamData.tilt_angles);
  2390. tROI->cam_position.fi = L.fi;
  2391. tROI->cam_position.tetta = L.tetta;
  2392. tROI->cam_position.r = L.r;
  2393. m_mROIsProtector.lock();
  2394. m_vROIS.push_back(tROI);
  2395. m_mROIsProtector.unlock();
  2396. }
  2397. else
  2398. {
  2399. LOG_ERROR("No MEM 4 TARGTEG DATA");
  2400. utilizeROI(tROI);
  2401. };
  2402. }
  2403. else
  2404. {
  2405. LOG_ERROR("No MEM 4 ROI");
  2406. }
  2407. return true;
  2408. };
  2409. return fusion_consumeData(TargetRefID, L);
  2410. }
  2411. bool deviceNodeCamera::makeSnap(uint32_t TargetRefID)
  2412. {
  2413. CAM_TASK * tTask = (CAM_TASK *) malloc(sizeof(CAM_TASK));
  2414. if (tTask)
  2415. {
  2416. memset(tTask, 0, sizeof(CAM_TASK));
  2417. tTask->CMD = (CAM_PTZ_CMD *) malloc(sizeof(CAM_PTZ_CMD));
  2418. if (tTask->CMD)
  2419. {
  2420. tTask->CMD->CMD = PTZ_CAPTURE_ONLY;
  2421. tTask->movemet_needed = false;
  2422. tTask->source_evt_id = TargetRefID;
  2423. return pushCamTask(tTask);
  2424. }
  2425. else
  2426. {
  2427. free(tTask);
  2428. };
  2429. };
  2430. return false;
  2431. }
  2432. bool deviceNodeCamera::connectToStream(uint32_t id)
  2433. {
  2434. /*
  2435. if( id ){
  2436. argusKernelModule * tKM = NULL;
  2437. argusKernel * tKRNL = NULL;
  2438. argusModuleFactory * tMF = NULL;
  2439. if( m_pDBC == NULL ) {
  2440. LOG_ERROR("No Database Connector!");
  2441. return false;
  2442. };
  2443. tKRNL = argusKernel::getInstance();
  2444. if( tKRNL ) {
  2445. tMF = tKRNL->getModuleFactory();
  2446. if( tMF ) {
  2447. tKM = tMF->getModule( ARGUS_MODULE_EMBED_VIDEO );
  2448. } else {
  2449. LOG_ERROR("No ModuleFactory Instance!");
  2450. return false;
  2451. };
  2452. } else {
  2453. LOG_ERROR("No Kernel Instance!");
  2454. return false;
  2455. };
  2456. if( tKM ) {
  2457. // New Module
  2458. DBC_MEDIA_STREAM_DATA *tDATA = m_pDBC->getMediaStreamData( id );
  2459. if( tDATA ) {
  2460. m_sMediaSource.mediaStreamId = id;
  2461. if( strncmp(tDATA->mrl_proto, "v4l2", 4) == 0 ) {
  2462. if (m_pSource != NULL) delete((videoAVLIBSource*)m_pSource);
  2463. m_pSource = (basicMediaSource *) new videoAVLIBSource(&m_sMediaSource);
  2464. } else {
  2465. if (m_pSource != NULL) delete((videoLIVE555Source*)m_pSource);
  2466. m_pSource = (basicMediaSource *) new videoLIVE555Source(&m_sMediaSource );
  2467. };
  2468. if( m_pSource != NULL ) {
  2469. char tMRL[1024];
  2470. #ifdef DEBUG
  2471. LOG("Source OK");
  2472. #endif
  2473. ((videoLIVE555Source*)m_pSource)->setWM( MS_MODE_STREAM_PROXY | MS_MODE_OVERLAYED_LOOPS | MS_MODE_DECODE_FRAMES );
  2474. int tW = ((argusModuleEmbedVideo*)tKM)->getWidh();
  2475. int tH = ((argusModuleEmbedVideo*)tKM)->getHeight();
  2476. int pix = -1;
  2477. bool in_16_9 = (((tDATA->width*100) / tDATA->height ) > 140 );
  2478. bool out_16_9 = (((tW*100) / tH ) > 140 );
  2479. updateOpticalParameters( tW, tH );
  2480. if( strlen( tDATA->c_type ) ){
  2481. };
  2482. //if( tW > 0 ){
  2483. // //m_pSource->setProportionalByWidth( tW );
  2484. // m_pSource->setSize( tW, tH );
  2485. //};
  2486. if( in_16_9 != out_16_9 ){
  2487. if( in_16_9 ){
  2488. tH = ((( tW * 1000 ) / 16 ) * 9 ) / 1000;
  2489. } else {
  2490. tW = ((( tH * 1000 ) / 3 ) * 4 ) / 1000;
  2491. };
  2492. };
  2493. if (strncmp(tDATA->mrl_proto, "v4l2", 4) == 0) {
  2494. sprintf( tMRL, tDATA->mrl );
  2495. } else {
  2496. DBC_CAM_DATA *tCAM = m_pDBC->getCamData( tDATA->source_id );
  2497. if( tCAM ) {
  2498. sprintf( tMRL, "%s://%s:%s@%s:%d%s", tDATA->mrl_proto, tCAM->username, tCAM->password, tCAM->ip, tDATA->mrl_port, tDATA->mrl );
  2499. free( tCAM );
  2500. } else {
  2501. LOG_ERROR("Error Getting CAM Data!");
  2502. delete( m_pSource );
  2503. m_pSource = NULL;
  2504. return false;
  2505. };
  2506. };
  2507. LOG( tMRL );
  2508. if( m_pSource->_open( tMRL, false, -1.0f, pix, tDATA->width, tDATA->height, tW, tH ) ){
  2509. m_bSourceConnected = m_pSource->attachDestination( (argusModuleEmbedVideo*) tKM );
  2510. if ( !m_bSourceConnected ) {
  2511. //delete( m_pSource );
  2512. if( strncmp(tDATA->mrl_proto, "v4l2", 4) == 0 ) {
  2513. delete((videoAVLIBSource*)m_pSource);
  2514. } else {
  2515. delete((videoLIVE555Source*)m_pSource);
  2516. };
  2517. m_pSource = NULL;
  2518. } else {
  2519. m_nActiveStreamID = id;
  2520. #ifdef DEBUG
  2521. LOG("Source is Connected to Output.");
  2522. #endif
  2523. };
  2524. } else {
  2525. LOG_ERROR("Can not Open Video Source!");
  2526. //delete( m_pSource );
  2527. if( strncmp(tDATA->mrl_proto, "v4l2", 4) == 0 ) {
  2528. delete((videoAVLIBSource*)m_pSource);
  2529. } else {
  2530. delete((videoLIVE555Source*)m_pSource);
  2531. };
  2532. m_pSource = NULL;
  2533. };
  2534. } else {
  2535. LOG_ERROR("Video Source Stream Failure!");
  2536. };
  2537. free( tDATA );
  2538. };
  2539. }
  2540. #ifdef DEBUG
  2541. else LOG("No Video Module!")
  2542. #endif
  2543. ;
  2544. };
  2545. LOG("Connect Finished!");
  2546. return m_bSourceConnected;
  2547. */
  2548. if ( id ) return true;
  2549. return true;
  2550. }
  2551. bool deviceNodeCamera::disconnectFromStream(void)
  2552. {
  2553. /*if( m_bSourceConnected ) {
  2554. if( m_pSource ){
  2555. delete( m_pSource );
  2556. m_pSource = NULL;
  2557. };
  2558. m_bSourceConnected = false;
  2559. };
  2560. return !m_bSourceConnected;
  2561. */
  2562. return true;
  2563. }
  2564. bool deviceNodeCamera::validateStream(void)
  2565. {
  2566. /*
  2567. if( m_bSourceConnected ) {
  2568. if( m_pSource ) {
  2569. if( m_pSource->isAttached() ){
  2570. if( m_pSource->getStatus() < MS_STOPED ) return true;
  2571. #ifdef DEBUG
  2572. LOG("Source is Finished!");
  2573. #endif
  2574. };
  2575. #ifdef DEBUG
  2576. LOG("Source is Disconnected!");
  2577. #endif
  2578. } else LOG_ERROR("No Source Object! Set Disconnected!");
  2579. disconnectFromStream();
  2580. };
  2581. return false;
  2582. */
  2583. return true;
  2584. }
  2585. bool deviceNodeCamera::validate()
  2586. {
  2587. if (m_pDBC == NULL)
  2588. {
  2589. return false;
  2590. }
  2591. DBC_CAM_DATA * tDATA = m_pDBC->getCamData(m_sCamData.id);
  2592. if (tDATA == NULL)
  2593. {
  2594. return false;
  2595. }
  2596. if (memcmp(tDATA, &m_sCamData, sizeof(DBC_CAM_DATA)) != 0)
  2597. {
  2598. // Settings Changed
  2599. // memcpy(&m_sCamData, tDATA, sizeof(DBC_CAM_DATA) );
  2600. free(tDATA);
  2601. return false;
  2602. };
  2603. free(tDATA);
  2604. if (! validateStream())
  2605. {
  2606. return false;
  2607. }
  2608. return deviceNode::validate();
  2609. }
  2610. bool deviceNodeCamera::load_presets(void)
  2611. {
  2612. bool bHaveIROFF = false;
  2613. bool bHaveIRON = false;
  2614. bool bHaveIRAUTO = false;
  2615. // Request PRESETS FROM CAMERA?! for some PTZ cameras without presets inpossible to
  2616. // operate by presets
  2617. if (m_pProtocol->getCameraPresets())
  2618. {
  2619. LOG("Camera Presets Loadded");
  2620. }
  2621. else
  2622. {
  2623. LOG_ERROR("Error while loadding Camera Presets!");
  2624. }
  2625. if (m_pDBC == NULL)
  2626. {
  2627. return false;
  2628. }
  2629. DBC_PRESETS_INFO * tData = m_pDBC->getCamPresetInfo(m_sCamData.id);
  2630. if (tData)
  2631. {
  2632. //memcpy(&m_sCamPresets, tData, sizeof(DBC_PRESETS_INFO));
  2633. m_sCamPresets.maxCount = tData->maxCount;
  2634. m_sCamPresets.gRows = tData->gRows;
  2635. m_sCamPresets.gCols = tData->gCols;
  2636. m_sCamPresets.presets.clear();
  2637. #ifdef DEBUG
  2638. char tB[128];
  2639. LOG("Loading Presets!");
  2640. sprintf(tB, "Cam Presets Info: CNT: %d Cols: %d Rows: %d ", m_sCamPresets.maxCount, m_sCamPresets.gCols, m_sCamPresets.gRows);
  2641. LOG(tB);
  2642. #endif
  2643. DBC_INT_LIST * tList = m_pDBC->listCamPresets(m_sCamData.id);
  2644. if (tList)
  2645. {
  2646. for (DBC_INT_LIST::iterator idx = tList->begin(); idx < tList->end(); idx++)
  2647. {
  2648. CAM_PRESET * tData = m_pDBC->getCamPreset(*idx);
  2649. if (tData)
  2650. {
  2651. m_sCamPresets.presets.push_back(tData);
  2652. bHaveIRON = (tData->role == ARGUS_PRESET_TYPE_IR_ON) ? true : bHaveIRON;
  2653. bHaveIROFF = (tData->role == ARGUS_PRESET_TYPE_IR_OFF) ? true : bHaveIROFF;
  2654. bHaveIRAUTO = (tData->role == ARGUS_PRESET_TYPE_IR_AUTO) ? true : bHaveIRAUTO;
  2655. };
  2656. };
  2657. delete(tList);
  2658. };
  2659. if (m_sCamData.eco_ir)
  2660. {
  2661. m_bShineOnTarget = (bHaveIROFF && bHaveIRAUTO);
  2662. }
  2663. else
  2664. {
  2665. m_bShineOnTarget = false;
  2666. }
  2667. return true;
  2668. #ifdef DEBUG
  2669. }
  2670. else
  2671. {
  2672. LOG("No Camera Presets!");
  2673. #endif
  2674. };
  2675. return false;
  2676. }
  2677. void deviceNodeCamera::translateROI2PRESET(float P, float T, float Z, uint8_t & col, uint8_t & row)
  2678. {
  2679. //char tB[128];
  2680. if ( T ) Z = Z;
  2681. row = (uint8_t)(Z * ((float)m_sCamPresets.gRows));
  2682. if (P < 0.5)
  2683. {
  2684. P = P + 1.5f;
  2685. }
  2686. else
  2687. {
  2688. P = P - 0.5f;
  2689. };
  2690. col = (uint8_t) (P * ((float) (m_sCamPresets.gCols * (1 << row)) / 2.0f));
  2691. row++;
  2692. }
  2693. CSTR deviceNodeCamera::getPresetToken(uint8_t CCOL, uint8_t CROW)
  2694. {
  2695. if (m_sCamPresets.presets.size() > 0)
  2696. {
  2697. for (V_PRESETS::iterator idx = m_sCamPresets.presets.begin(); idx < m_sCamPresets.presets.end(); idx++)
  2698. {
  2699. if ((*idx)->role == ARGUS_PRESET_TYPE_POSITION)
  2700. {
  2701. if (((*idx)->row == CROW) && ((*idx)->col == CCOL))
  2702. {
  2703. return (CSTR) (*idx)->token;
  2704. }
  2705. }
  2706. }
  2707. }
  2708. return NULL;
  2709. }
  2710. CSTR deviceNodeCamera::getPresetToken(uint8_t specialRole)
  2711. {
  2712. if (m_sCamPresets.presets.size() > 0)
  2713. {
  2714. for (V_PRESETS::iterator idx = m_sCamPresets.presets.begin(); idx < m_sCamPresets.presets.end(); idx++)
  2715. {
  2716. if ((*idx)->role == specialRole)
  2717. {
  2718. return (CSTR) (*idx)->token;
  2719. }
  2720. }
  2721. }
  2722. return NULL;
  2723. }
  2724. #undef LOG
  2725. #undef LOG_ERROR
  2726. // #undef DEBUG_EXTRA