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.

1930 lines
75 KiB

​
​
​
​
​
​
​
​
​
​
​
​
3 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
3 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
3 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
3 years ago
​
​
3 years ago
​
​
​
​
​
​
​
​
​
3 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
3 years ago
​
​
​
​
​
​
​
​
​
​
​
​
3 years ago
3 years ago
​
​
​
​
​
​
​
​
3 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
3 years ago
​
​
​
​
3 years ago
3 years ago
​
​
​
​
​
3 years ago
​
​
3 years ago
3 years ago
​
3 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
3 years ago
​
3 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
3 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. #include "deviceNodeCamera.h"
  2. #include "../../components/base/basicCameraInfo.h"
  3. #include "../../components/object/objectSystemAnchor.h"
  4. #include "../../hardware/uart/serialPort.h"
  5. #include "../../hardware/uart/serialSOL.h"
  6. #include "../../data/protocol/protocolPTZ_VISCA.h"
  7. #include "../../data/protocol/protocolPTZ_ONVIF.h"
  8. #include "../../data/protocol/protocolPTZ_PELCO.h"
  9. #include "hardwareManager.h"
  10. #include "hardwareResourceSerialProvider.h"
  11. #include "kernelModuleEmbedVideo.h"
  12. #include "../mojo/mojoKernel.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. char _tBfrErrMSG2w34[256];\
  18. sprintf( _tBfrErrMSG2w34, "In file %s[%d] in func:%s %s",__FILE__, __LINE__, __func__, X );\
  19. deviceNode::m_pLog->Log( "RCAM", this, _LOG_ERROR, _tBfrErrMSG2w34 );\
  20. }
  21. #else
  22. #define LOG_ERROR( X ) deviceNode::m_pLog->Log( "RCAM", this, _LOG_ERROR, X );
  23. #endif
  24. // all sizes in mm
  25. 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 };
  26. 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 };
  27. 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 };
  28. #define CAM_FOCAL_LENGTH_CNT 49
  29. static const float cam_predef_focal_length[CAM_FOCAL_LENGTH_CNT]={
  30. 2.7f,
  31. 3.0f,
  32. 3.2f,
  33. 3.5f,
  34. 3.7f,
  35. 3.9f,
  36. 4.0f,
  37. 4.3f,
  38. 4.8f,
  39. 5.0f,
  40. 5.1f,
  41. 5.5f,
  42. 5.8f,
  43. 6.0f,
  44. 6.2f,
  45. 6.6f,
  46. 7.0f,
  47. 7.1f,
  48. 7.5f,
  49. 7.6f,
  50. 8.0f,
  51. 8.6f,
  52. 8.9f,
  53. 9.0f,
  54. 9.2f,
  55. 9.3f,
  56. 10.0f,
  57. 10.5f,
  58. 10.6f,
  59. 10.7f,
  60. 11.0f,
  61. 11.3f,
  62. 12.0f,
  63. 12.9f,
  64. 13.0f,
  65. 13.5f,
  66. 13.6f,
  67. 14.0f,
  68. 14.6f,
  69. 15.0f,
  70. 15.1f,
  71. 15.5f,
  72. 16.0f,
  73. 17.0f,
  74. 17.5f,
  75. 17.7f,
  76. 18.0f,
  77. 18.2f,
  78. 19.0f
  79. };
  80. deviceNodeCamera::deviceNodeCamera( uint32_t NodeID ): deviceNode( NodeID ), basicThread(){
  81. //m_pLog = basicLogCollection::getInstance();
  82. m_pBEH = basicEventHeap::getInstance();
  83. m_bSkeepPositioningWait = true;
  84. m_bNoAbsPositionning = false;
  85. m_bCameraOnHomePosition = false;
  86. m_tTimeTSeconds = 0;
  87. m_pProtocol = NULL;
  88. m_pTransport = NULL;
  89. m_pCurrentROI = NULL;
  90. m_nRealFrameWidth = 640;
  91. //m_bState = 0;
  92. //m_bBusy = true;
  93. m_fMaxRange = m_fMinHFov = m_fMaxHFov = m_fMinVFov = m_fMaxVFov = .0f;
  94. m_nRelativeMoveCounter = 0;
  95. m_bRelativeMoveMatrix = 0;
  96. m_bCurrentPresetCol = 0;
  97. m_bCurrentPresetRow = 0;
  98. m_bShineOnTarget = false;
  99. m_bCameraIsStatic = false;
  100. m_bIRIsOn = false;
  101. m_bIRTO = 0;
  102. m_fSpeedRise = 0.03f;
  103. m_fZoomCorrection = 1.0f;
  104. m_fRM_Pan = m_fRM_Tilt = m_fRM_Zoom = .0f;
  105. //m_bSourceConnected = false;
  106. //m_pSource = NULL;
  107. };
  108. deviceNodeCamera::deviceNodeCamera( DBC_NODE_DATA *Data ): deviceNode( Data ), basicThread(){
  109. //m_pLog = basicLogCollection::getInstance();
  110. m_pBEH = basicEventHeap::getInstance();
  111. m_bSkeepPositioningWait = true;
  112. m_bNoAbsPositionning = false;
  113. m_bCameraOnHomePosition = false;
  114. m_tTimeTSeconds = 0;
  115. m_pProtocol = NULL;
  116. m_pTransport = NULL;
  117. m_pCurrentROI = NULL;
  118. m_nRealFrameWidth = 640;
  119. //m_bState = 0;
  120. //m_bBusy = true;
  121. m_fMaxRange = m_fMinHFov = m_fMaxHFov = m_fMinVFov = m_fMaxVFov = .0f;
  122. m_nRelativeMoveCounter = 0;
  123. m_bRelativeMoveMatrix = 0;
  124. m_bCurrentPresetCol = 0;
  125. m_bCurrentPresetRow = 0;
  126. m_bShineOnTarget = false;
  127. m_bIRIsOn = false;
  128. m_bIRTO = 0;
  129. m_fSpeedRise = 0.03f;
  130. m_fZoomCorrection = 1.0f;
  131. m_fRM_Pan = m_fRM_Tilt = m_fRM_Zoom = .0f;
  132. //m_bSourceConnected = false;
  133. //m_pSource = NULL;
  134. };
  135. deviceNodeCamera::~deviceNodeCamera(){
  136. LOG("Destruct");
  137. //m_mQPROTECTOR.unlock();
  138. waitThreadEnd( true );
  139. LOG("Thread Del OK");
  140. if( m_pTransport!= NULL ) {
  141. if( m_sCamData.uart_id ) {
  142. DBC_UART_DATA *tData = NULL;
  143. tData = m_pDBC->getUartData( m_sCamData.uart_id );
  144. switch( tData->type ) {
  145. case UART_TYPE_HARDWARE:{
  146. delete((serialPort*)m_pTransport);
  147. break;
  148. };
  149. case UART_TYPE_SOL:{
  150. delete((serialSOL*)m_pTransport);
  151. break;
  152. };
  153. };
  154. };
  155. };
  156. LOG("Transport Del OK");
  157. if( m_pProtocol != NULL ) {
  158. switch( m_sCamData.protocol ) {
  159. case CAM_CTRL_PROTO_VISCA:
  160. delete( (protocolPTZ_VISCA *) m_pProtocol );
  161. break;
  162. case CAM_CTRL_PROTO_PELCO:
  163. delete( (protocolPTZ_PELCO *) m_pProtocol );
  164. break;
  165. case CAM_CTRL_PROTO_ONVIF:
  166. delete( (protocolPTZ_ONVIF *) m_pProtocol );
  167. break;
  168. default:
  169. delete( m_pProtocol );
  170. };
  171. };
  172. LOG("Proto Del OK");
  173. //if( m_pPosition != NULL ) delete( m_pPosition );
  174. //LOG("Pos Del OK");
  175. //if( m_bSourceConnected ){
  176. // if( m_pSource != NULL ){
  177. // m_pSource->_close();
  178. // delete( m_pSource );
  179. // };
  180. //};
  181. //LOG("Stream Del OK");
  182. };
  183. void deviceNodeCamera::updateOpticalParameters( uint16_t frame_width, uint16_t frame_heigth ){
  184. #ifdef DEBUG
  185. char tB[128];
  186. LOG("Set Optical Parameters");
  187. sprintf( tB, "\tCapturing Frame: %dx%d ", frame_width, frame_heigth );
  188. LOG(tB);
  189. #endif
  190. if( frame_width > 0 && frame_heigth > 0 ) {
  191. m_nRealFrameWidth = frame_width;
  192. m_nRealFrameHeight = frame_heigth;
  193. float tFrameDiag = sqrtf( m_nRealFrameHeight*m_nRealFrameHeight+m_nRealFrameWidth*m_nRealFrameWidth );
  194. switch( m_sCamData.zoom_model ) {
  195. case 0:{
  196. #ifdef DEBUG
  197. LOG("\tFixed Zoom");
  198. #endif
  199. m_fMaxFL = m_fMinFL = cam_predef_focal_length[m_sCamData.lens_f];
  200. m_fMinRange = calcMinDistance_mm( AC_imager_diag[m_sCamData.imager_type], m_fMinFL, tFrameDiag );
  201. m_fMaxRange = calcMaxDistance_mm( AC_imager_diag[m_sCamData.imager_type], m_fMaxFL, tFrameDiag );
  202. m_fR2FL = .0f;
  203. m_fZoomCorrection = .0f;
  204. m_pDBC->updateCamRange( m_sCamData.id, m_fMaxRange );
  205. break;
  206. };
  207. case 1:{
  208. #ifdef DEBUG
  209. LOG("\tAutomated Zoom");
  210. #endif
  211. m_fMinFL = cam_predef_focal_length[m_sCamData.lens_f];
  212. m_fMinRange = calcMinDistance_mm( AC_imager_diag[m_sCamData.imager_type], m_fMinFL, tFrameDiag );
  213. m_fMaxFL = cam_predef_focal_length[m_sCamData.lens_f] * m_sCamData.zoom;
  214. m_fMaxRange = calcMaxDistance_mm( AC_imager_diag[m_sCamData.imager_type], m_fMaxFL, tFrameDiag );
  215. if( m_fMaxRange > m_fMinRange ){
  216. m_fR2FL = ((m_fMaxFL - m_fMinFL) / (m_fMaxRange - m_fMinRange));
  217. } else {
  218. m_fR2FL = .0f;
  219. };
  220. m_fZoomCorrection = 1.0f;
  221. m_pDBC->updateCamRange(m_sCamData.id, m_fMaxRange);
  222. break;
  223. };
  224. case 2:{
  225. #ifdef DEBUG
  226. LOG("\tManual Zoom");
  227. #endif
  228. m_fMinRange = 5.0f; //Minimal 5 meters
  229. if ( m_sCamData.max_range < m_fMinRange ) {
  230. m_fMaxRange = m_fMinRange;
  231. } else {
  232. m_fMaxRange = m_sCamData.max_range;
  233. };
  234. m_fZoomCorrection = m_sCamData.zoom * 0.01f;
  235. m_fMinFL = calcCFL( AC_imager_width[m_sCamData.imager_type], m_fMinRange, tFrameDiag );
  236. m_fMaxFL = calcCFL( AC_imager_width[m_sCamData.imager_type], m_fMaxRange, tFrameDiag );
  237. break;
  238. };
  239. };
  240. m_fMinHFov = rad2deg(calcFOV_mm( AC_imager_width[m_sCamData.imager_type], m_fMaxFL ));
  241. m_fMaxHFov = rad2deg(calcFOV_mm( AC_imager_width[m_sCamData.imager_type], m_fMinFL ));
  242. m_fMinVFov = rad2deg(calcFOV_mm( AC_imager_height[m_sCamData.imager_type], m_fMaxFL ));
  243. m_fMaxVFov = rad2deg(calcFOV_mm( AC_imager_height[m_sCamData.imager_type], m_fMinFL ));
  244. #ifdef DEBUG
  245. sprintf( tB, "\tMinimal FL: %0.3fmm. Maximal FL: %0.3fmm. ", m_fMinFL, m_fMaxFL );
  246. LOG(tB);
  247. sprintf( tB, "\tMinimal Range: %0.2fm. Maximal Range: %0.2fm. ", m_fMinRange, m_fMaxRange );
  248. LOG(tB);
  249. sprintf( tB, "\tMinimal FOVs, Horizontal: %0.2f Vertical: %0.2f", m_fMinHFov, m_fMinVFov );
  250. LOG(tB);
  251. sprintf( tB, "\tMaximal FOVs, Horizontal: %0.2f Vertical: %0.2f", m_fMaxHFov, m_fMaxVFov );
  252. LOG(tB);
  253. #endif
  254. };
  255. };
  256. bool deviceNodeCamera::create(){
  257. setWM( CAM_WS_INIT );
  258. if( m_sNodeData.aux_id == 0 ) return false;
  259. DBC_CAM_DATA * tData = m_pDBC->getCamData( m_sNodeData.aux_id );
  260. if( tData != NULL ) {
  261. memcpy( &m_sCamData, tData, sizeof(DBC_CAM_DATA) );
  262. free( tData );
  263. } else {
  264. LOG_ERROR("Cannot Fetch CAM Data from Database!");
  265. return false;
  266. };
  267. if( !( m_sCamData.lens_f < CAM_FOCAL_LENGTH_CNT ) ) m_sCamData.lens_f = CAM_FOCAL_LENGTH_CNT - 1;
  268. updateOpticalParameters( 1280, 720 ); //HD Ready
  269. if( m_sCamData.ptz_speed_step > .0f ) {
  270. m_fSpeedRise = m_sCamData.ptz_speed_step;
  271. };
  272. //m_pPosition = new basicPosition();// m_sCamData.lat, m_sCamData.lon, m_sCamData.alt, m_sCamData.roll, m_sCamData.pitch, m_sCamData.yaw );
  273. //if( m_pPosition == NULL ) {
  274. // LOG_ERROR("Position Constructor Failure!");
  275. // return false;
  276. //};
  277. //m_oPosition.setPositionXYZ( m_sCamData.x, m_sCamData.y, m_sCamData.z );
  278. //m_oPosition.setOrientationDeg( m_sCamData.x_rot, m_sCamData.y_rot, m_sCamData.z_rot );
  279. systemAnchor * SA = systemAnchor::getInstance();
  280. if( SA ){
  281. LOG("SysAnchor OK");
  282. if ( SA->beLocal() ) {
  283. LOG("SysAnchor beLocal!");
  284. m_oPosition.setPositionXYZ( m_sCamData.x, m_sCamData.y, m_sCamData.z, 0 ); //SA->getUTMZone() );
  285. m_oPosition.setOrientationDeg( m_sCamData.x_rot , m_sCamData.y_rot, m_sCamData.z_rot );
  286. } else {
  287. LOG("SysAnchor is Global Position System!");
  288. C3D_DECART LD = SA->getXYZ();
  289. m_oPosition.setPositionXYZ( LD.x + m_sCamData.x, LD.y + m_sCamData.y, LD.z + m_sCamData.z, SA->getUTMZone() );
  290. m_oPosition.setOrientationDeg( m_sCamData.x_rot , m_sCamData.y_rot, m_sCamData.z_rot );
  291. };
  292. } else {
  293. LOG("No SysAnchor!");
  294. m_oPosition.setPositionXYZ( m_sCamData.x , m_sCamData.y, m_sCamData.z );
  295. m_oPosition.setOrientationDeg( m_sCamData.x_rot , m_sCamData.y_rot, m_sCamData.z_rot );
  296. };
  297. m_oPosition.updateMatrix();
  298. #ifdef DEBUG
  299. char tB[128];
  300. sprintf( tB, "Finalized Camera Location: Lat:%.3f Lon:%.3f Alt:%.3f", m_oPosition.getLat(), m_oPosition.getLon(), m_oPosition.getAlt() );
  301. LOG( tB );
  302. sprintf( tB, "Finalized Camera Location: x:%.3f y:%.3f z:%.3f", m_oPosition.getX(), m_oPosition.getY(), m_oPosition.getZ() );
  303. LOG( tB );
  304. sprintf( tB, "Orientation: x:%.3f y:%.3f z:%.3f", m_oPosition.getRotX_deg(), m_oPosition.getRotY_deg(), m_oPosition.getRotZ_deg() );
  305. LOG( tB );
  306. #endif
  307. if( m_pProtocol != NULL ){
  308. LOG_ERROR("Cannot Start: Service Already Running!");
  309. return false;
  310. };
  311. switch( m_sCamData.protocol ){
  312. case CAM_CTRL_PROTO_VISCA: {
  313. #ifdef DEBUG
  314. LOG("VISCA");
  315. #endif
  316. m_pProtocol = (protocolPTZ *) new protocolPTZ_VISCA( m_sCamData.protocol_aux );
  317. if( m_pProtocol!= NULL ) {
  318. basicTransport *tTransport = NULL;
  319. hardwareManager *tHM = hardwareManager::getInstance();
  320. if( tHM ){
  321. hardwareResource *tRES = tHM->requestResource( HARDWARE_PROVIDER_SERIAL, m_sCamData.uart_id );
  322. if( tRES != NULL ) {
  323. if( tRES->isValid()) {
  324. tTransport = static_cast<basicTransport *>(static_cast<hardwareResourceSerialProvider *>(tRES));
  325. if( m_pProtocol->setTransport( tTransport )){
  326. if( !m_pProtocol->create() ){
  327. LOG_ERROR("Protocol <<< Create >>> Failure!");
  328. };
  329. } else {
  330. LOG_ERROR("Binding Port Failure!");
  331. };
  332. };
  333. #ifdef DEBUG
  334. } else {
  335. LOG("HWM Has No UART PORT!");
  336. #endif
  337. };
  338. #ifdef DEBUG
  339. } else {
  340. LOG_ERROR("Can not get Hardware Manager!");
  341. #endif
  342. };
  343. if( tTransport!= NULL ) {
  344. LOG( "Bind Transport Port!" );
  345. } else {
  346. LOG_ERROR("Transport Creation Failure!");
  347. delete( ( protocolPTZ_VISCA * )m_pProtocol );
  348. m_pProtocol = NULL;
  349. };
  350. #ifdef DEBUG
  351. } else {
  352. LOG_ERROR("Protocol Creation failure!");
  353. #endif
  354. };
  355. break;
  356. };
  357. case CAM_CTRL_PROTO_PELCO:{
  358. #ifdef DEBUG
  359. LOG("PELCO");
  360. #endif
  361. m_pProtocol = (protocolPTZ *) new protocolPTZ_PELCO( m_sCamData.protocol_aux );
  362. if( m_pProtocol!= NULL ) {
  363. basicTransport *tTransport = NULL;
  364. hardwareManager *tHM = hardwareManager::getInstance();
  365. if ( tHM ) {
  366. hardwareResource * tRES = tHM->requestResource( HARDWARE_PROVIDER_SERIAL, m_sCamData.uart_id );
  367. if( tRES != NULL ) {
  368. if( tRES->isValid()) {
  369. tTransport = static_cast<basicTransport *>(static_cast<hardwareResourceSerialProvider *>(tRES));
  370. if( m_pProtocol->setTransport( tTransport )){
  371. if( !m_pProtocol->create() ){
  372. LOG_ERROR("Protocol <<< Create >>> Failure!");
  373. };
  374. } else {
  375. LOG_ERROR("Binding Port Failure!");
  376. };
  377. };
  378. #ifdef DEBUG
  379. } else {
  380. LOG("HWM Has No UART PORT!");
  381. #endif
  382. };
  383. #ifdef DEBUG
  384. } else {
  385. LOG_ERROR("Can not get Hardware Manager!");
  386. #endif
  387. };
  388. if( tTransport!= NULL ) {
  389. LOG( "Bind Transport Port!" );
  390. } else {
  391. LOG_ERROR("Transport Creation Failure!");
  392. delete( ( protocolPTZ_PELCO * )m_pProtocol );
  393. m_pProtocol = NULL;
  394. };
  395. #ifdef DEBUG
  396. } else {
  397. LOG_ERROR("Protocol Creation failure!");
  398. #endif
  399. };
  400. break;
  401. };
  402. case CAM_CTRL_PROTO_ONVIF:{
  403. #ifdef DEBUG
  404. LOG("ONVIF");
  405. #endif
  406. //m_pProtocol = (protocolPTZ *) new protocolPTZ_ONVIF( m_sCamData.ip, m_sCamData.port, m_sCamData.username, m_sCamData.password, m_sCamData.auth_mode );
  407. if( strstr( m_sCamData.onvif_ctrl, "http" ) == NULL ) {
  408. char tURI[256];
  409. if ( m_sCamData.port ) {
  410. sprintf(tURI, "http://%s:%d%s", m_sCamData.ip, m_sCamData.port, m_sCamData.onvif_ctrl);
  411. } else {
  412. sprintf(tURI, "http://%s%s", m_sCamData.ip, m_sCamData.onvif_ctrl);
  413. };
  414. m_pProtocol = (protocolPTZ *)new protocolPTZ_ONVIF( tURI, m_sCamData.username, m_sCamData.password, m_sCamData.auth_mode);
  415. } else {
  416. m_pProtocol = (protocolPTZ *)new protocolPTZ_ONVIF(m_sCamData.onvif_ctrl, m_sCamData.username, m_sCamData.password, m_sCamData.auth_mode);
  417. };
  418. #ifdef DEBUG
  419. LOG("ONVIF-END");
  420. #endif
  421. if( m_pProtocol != NULL ) {
  422. if( !m_pProtocol->create() ) {
  423. LOG_ERROR("Error Creating Proto!");
  424. delete( (protocolPTZ_ONVIF*) m_pProtocol );
  425. m_pProtocol = NULL;
  426. };
  427. #ifdef DEBUG
  428. LOG("PROTO CREATE DONE");
  429. #endif
  430. };
  431. break;
  432. };
  433. default:{
  434. #ifdef DEBUG
  435. LOG("Camera Has no defined control protocol");
  436. #endif
  437. };
  438. };
  439. //Check - No ABS Movement!
  440. if( ( m_sCamData.type == ARGUS_IP_PRESET_CAMERA ) || ( m_sCamData.type == ARGUS_ANALOG_PRESET_CAMERA ) ) m_bNoAbsPositionning = true;
  441. if (m_pProtocol == NULL) {
  442. if( m_sCamData.protocol == 0 ) {
  443. m_bCameraIsStatic = true;
  444. m_bSkeepPositioningWait = true;
  445. m_sCamData.online = true;
  446. } else LOG_ERROR("No Proto Created!");
  447. } else {
  448. #ifdef DEBUG
  449. LOG("Proto is OK");
  450. #endif
  451. //Load Presets only if we have protocol
  452. load_presets();
  453. mojoKernel *tKRNL = mojoKernel::getInstance();
  454. if( tKRNL ) {
  455. m_bSkeepPositioningWait = (( tKRNL->getMediaMode() & EVE_OUTPUT_VIDEO ) == EVE_OUTPUT_VIDEO );
  456. #ifdef DEBUG
  457. char ttTT[128];
  458. sprintf( ttTT, "Kernel Media Mode:%d", tKRNL->getMediaMode() );
  459. LOG( ttTT );
  460. if ( m_bSkeepPositioningWait ) {
  461. LOG("Camera No Wait On Pos!");
  462. } else {
  463. LOG("Camera Will Wait On Pos!");
  464. };
  465. #endif
  466. } else {
  467. LOG_ERROR("No Kernel Instance!");
  468. };
  469. m_sCamData.online = init();
  470. };
  471. m_pDBC->updateCamOnlineStatus( m_sCamData.id, m_sCamData.online );
  472. return m_sCamData.online;
  473. };
  474. //void deviceNodeCamera::setBusy( bool Status ){
  475. //m_bBusy = Status;
  476. //};
  477. bool deviceNodeCamera::relativeMove( float PanSPD, float TiltSPD, float ZoomSPD ){
  478. if( m_sCamData.online ) {
  479. uint32_t MoveMatrix = 0;
  480. if( PanSPD != .0f ){
  481. if( PanSPD > 0 ) {
  482. MoveMatrix |= PTZ_DRIVE_PAN_RIGHT;
  483. } else {
  484. MoveMatrix |= PTZ_DRIVE_PAN_LEFT;
  485. };
  486. };
  487. if( TiltSPD != .0f ){
  488. if( TiltSPD > 0 ) {
  489. MoveMatrix |= PTZ_DRIVE_TILT_UP;
  490. } else {
  491. MoveMatrix |= PTZ_DRIVE_TILT_DOWN;
  492. };
  493. };
  494. if( ZoomSPD != .0f ){
  495. if( ZoomSPD > 0 ) {
  496. MoveMatrix |= PTZ_DRIVE_ZOOM_WIDE;
  497. } else {
  498. MoveMatrix |= PTZ_DRIVE_ZOOM_TELE;
  499. };
  500. };
  501. if( MoveMatrix != 0 ) {
  502. m_pProtocol->setPTZSpeed( fabs(PanSPD), fabs(TiltSPD), fabs(ZoomSPD) );
  503. };
  504. return m_pProtocol->move( MoveMatrix );
  505. };
  506. return false;
  507. };
  508. bool deviceNodeCamera::relativeStop(){
  509. if( m_sCamData.online ) {
  510. return m_pProtocol->move( 0 );
  511. };
  512. return false;
  513. };
  514. CAM_TASK *deviceNodeCamera::fetchCamTask( void ){
  515. CAM_TASK *rVal = NULL;
  516. m_mQPROTECTOR.lock();
  517. if (m_qCAM_TASK.size()) {
  518. rVal = m_qCAM_TASK.front();
  519. m_qCAM_TASK.pop();
  520. };
  521. m_mQPROTECTOR.unlock();
  522. return rVal;
  523. };
  524. bool deviceNodeCamera::pushCamTask( CAM_TASK *ptr ){
  525. if ( ptr == NULL ) return false;
  526. m_mQPROTECTOR.lock();
  527. m_qCAM_TASK.push( ptr );
  528. m_mQPROTECTOR.unlock();
  529. return true;
  530. };
  531. bool deviceNodeCamera::fusion_compare( CAM_ROI *ptr, C3D_SPH_DEGREES tp ){
  532. if( ptr == NULL ) return false;
  533. float tFI = fabsf( ptr->cam_position.fi - tp.fi );
  534. float tTETTA = fabsf( ptr->cam_position.tetta - tp.tetta );
  535. #ifdef DEBUG
  536. char tB[128];
  537. 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 );
  538. LOG(tB);
  539. #endif
  540. if( tFI > ptr->h_fov ) return false;
  541. if( tTETTA > ptr->v_fov ) return false;
  542. if( tp.r < ptr->min_range ) return false;
  543. if( tp.r > ptr->max_range ) return false;
  544. return true;
  545. };
  546. bool deviceNodeCamera::fusion_consumeData( uint32_t db_id, C3D_SPH_DEGREES tOR ){
  547. if( db_id == 0 ) return false;
  548. CAM_TRACK_RECORD *tTrackRecord = (CAM_TRACK_RECORD *)malloc(sizeof(CAM_TRACK_RECORD));
  549. if( tTrackRecord == NULL ) return false;
  550. #ifdef DEBUG_EXTRA
  551. LOG("Fusion: Consuming new Data!");
  552. #endif
  553. memset(tTrackRecord, 0, sizeof(CAM_TRACK_RECORD));
  554. tTrackRecord->target_direction.fi = tOR.fi;
  555. tTrackRecord->target_direction.r = tOR.r;
  556. tTrackRecord->target_direction.tetta = tOR.tetta;
  557. tTrackRecord->evt_db_ID = db_id;
  558. tTrackRecord->ts_ms = getMilliseconds();
  559. m_mROIsProtector.lock();
  560. if( m_vROIS.size() ){
  561. vCAM_ROIS::iterator idx = m_vROIS.begin();
  562. for( ; idx < m_vROIS.end(); idx++ ) if ( fusion_compare( *idx, tOR ) ) break;
  563. if ( idx != m_vROIS.end() ) {
  564. // have Match!
  565. #ifdef DEBUG_EXTRA
  566. LOG("ROI MATCHED!");
  567. #endif
  568. (*idx)->targets.push_back( tTrackRecord );
  569. fusion_validateROI( tTrackRecord->ts_ms, (*idx) );
  570. } else {
  571. #ifdef DEBUG_EXTRA
  572. LOG("NO MATCH FOUND!");
  573. #endif
  574. // No mathced ROI -> create new one
  575. CAM_ROI * tROI = (CAM_ROI *)malloc(sizeof(CAM_ROI));
  576. if ( tROI ) {
  577. memset( tROI, 0 , sizeof(CAM_ROI) );
  578. tROI->ts_ms = tTrackRecord->ts_ms;
  579. spherical2onvif( tOR, tROI->cam_orientation, m_fMaxRange, m_sCamData.pan_model, m_sCamData.pan_angles, m_sCamData.tilt_model, m_sCamData.tilt_angles );
  580. tROI->cam_position.fi = tOR.fi;
  581. tROI->cam_position.tetta = tOR.tetta;
  582. tROI->cam_position.r = tOR.r;
  583. float cFL = m_fMinFL + m_fR2FL * ( tROI->cam_position.r - m_fMinRange );
  584. tROI->max_range = calcMaxDistance_mm( AC_imager_diag[m_sCamData.imager_type], cFL, m_nRealFrameWidth );
  585. tROI->min_range = calcMinDistance_mm( AC_imager_diag[m_sCamData.imager_type], cFL, m_nRealFrameWidth );
  586. tROI->h_fov = rad2deg( calcFOV_mm( AC_imager_width[m_sCamData.imager_type], cFL ) ) / 2.0f; // div to no to div in comparation function
  587. tROI->v_fov = rad2deg( calcFOV_mm( AC_imager_height[m_sCamData.imager_type], cFL ) ) / 2.0f; // that will be called more ofthen
  588. tROI->targets.push_back( tTrackRecord );
  589. m_vROIS.push_back( tROI );
  590. };
  591. };
  592. } else {
  593. #ifdef DEBUG_EXTRA
  594. LOG("Fusion: NO ROI -> CREATE!");
  595. #endif
  596. //
  597. CAM_ROI * tROI = (CAM_ROI *)malloc(sizeof(CAM_ROI));
  598. if ( tROI ) {
  599. memset( tROI, 0 , sizeof(CAM_ROI) );
  600. tROI->ts_ms = tTrackRecord->ts_ms;
  601. //Set vector Cam Coordinates
  602. tROI->cam_position.fi = tOR.fi;
  603. tROI->cam_position.tetta = tOR.tetta;
  604. tROI->cam_position.r = tOR.r;
  605. // Set vector OnVIF coordinates
  606. spherical2onvif( tOR, tROI->cam_orientation, m_fMaxRange, m_sCamData.pan_model, m_sCamData.pan_angles, m_sCamData.tilt_model, m_sCamData.tilt_angles );
  607. // make fusion data BOX
  608. float cFL = m_fMinFL + m_fR2FL * ( tROI->cam_position.r );// - m_fMinRange );
  609. tROI->min_range = calcMinDistance_mm( AC_imager_diag[m_sCamData.imager_type], cFL, m_nRealFrameWidth );
  610. tROI->max_range = calcMaxDistance_mm( AC_imager_diag[m_sCamData.imager_type], cFL, m_nRealFrameWidth );
  611. tROI->h_fov = rad2deg( calcFOV_mm( AC_imager_width[m_sCamData.imager_type], cFL ) )/ 2.0f; // div to prevent division in comparation
  612. tROI->v_fov = rad2deg( calcFOV_mm( AC_imager_height[m_sCamData.imager_type], cFL ) )/ 2.0f; // function that will be called more ofthen
  613. // Add ROI to vector
  614. tROI->targets.push_back( tTrackRecord );
  615. m_vROIS.push_back( tROI );
  616. } else LOG_ERROR("No MEM4ROI");
  617. };
  618. m_mROIsProtector.unlock();
  619. return true;
  620. };
  621. bool deviceNodeCamera::fusion_validateROI( uint64_t cts, CAM_ROI *ptr ){
  622. if( ptr == NULL ) return false;
  623. if( (cts - ptr->ts_ms) > LOSE_INTERES_TIME_MS ) return false;
  624. #ifdef DEBUG_EXTRA
  625. char tB[128];
  626. sprintf( tB, "\tROI [%d]", ptr->targets.size() );
  627. LOG( tB );
  628. #endif
  629. if( ptr->targets.size() ) {
  630. double tA = 0.0;
  631. double tB = 0.0;
  632. double tC = 0.0;
  633. uint32_t tCNT = 0;
  634. for( vCAM_TRACK_RECORDS::iterator idx = ptr->targets.begin(); idx < ptr->targets.end(); idx++ ) {
  635. if( ( cts - (*idx)->ts_ms ) > LOSE_INTERES_TIME_MS ) {
  636. free( (*idx) );
  637. ptr->targets.erase( idx );
  638. } else {
  639. tA = tA + (*idx)->target_direction.fi;
  640. tB = tB + (*idx)->target_direction.tetta;
  641. tC = tC + (*idx)->target_direction.r;
  642. tCNT++;
  643. };
  644. };
  645. if( tCNT ) {
  646. C3D_SPH_DEGREES L = { 0 };
  647. L.fi = (float) ( tA /(double) tCNT );
  648. L.tetta = (float) ( tB /(double) tCNT );
  649. L.r = (float) ( tC /(double) tCNT );
  650. spherical2onvif(L, ptr->cam_orientation, m_fMaxRange, m_sCamData.pan_model, m_sCamData.pan_angles, m_sCamData.tilt_model, m_sCamData.tilt_angles);
  651. ptr->ts_ms = cts;
  652. float cFL = m_fMinFL + m_fR2FL * ( ptr->cam_position.r - m_fMinRange );
  653. ptr->max_range = calcMaxDistance_mm( AC_imager_diag[m_sCamData.imager_type], cFL, m_nRealFrameWidth );
  654. ptr->min_range = calcMinDistance_mm( AC_imager_diag[m_sCamData.imager_type], cFL, m_nRealFrameWidth );
  655. ptr->h_fov = rad2deg( calcFOV_mm(AC_imager_width[m_sCamData.imager_type], cFL) )/ 2.0f; // div to prevent division in comparation
  656. ptr->v_fov = rad2deg( calcFOV_mm( AC_imager_height[m_sCamData.imager_type], cFL ) )/ 2.0f; // function that will be called more ofthen
  657. return true;
  658. };
  659. };
  660. return false;
  661. };
  662. CAM_ROI *deviceNodeCamera::fusion_fetchROI( void ){
  663. CAM_ROI *tVAL = NULL;
  664. uint64_t cts = 0;
  665. m_mROIsProtector.lock();
  666. vCAM_ROIS::iterator sRec;
  667. if( m_vROIS.size() ){
  668. for( vCAM_ROIS::iterator idx = m_vROIS.begin(); idx < m_vROIS.end(); idx++ ){
  669. if ( tVAL == NULL ) {
  670. sRec = idx;
  671. tVAL = (*idx);
  672. cts = tVAL->ts_ms;
  673. } else {
  674. #ifdef CAM_FETCH_ROI_NEWEST
  675. if ( (*idx)->ts_ms > cts )
  676. #else
  677. if ( (*idx)->ts_ms < cts )
  678. #endif
  679. {
  680. sRec = idx;
  681. tVAL = (*idx);
  682. cts = tVAL->ts_ms;
  683. };
  684. };
  685. };
  686. m_vROIS.erase( sRec );
  687. };
  688. m_mROIsProtector.unlock();
  689. return tVAL;
  690. };
  691. DBC_SNAPSHOT_DATA *deviceNodeCamera::fusion_translate_ROI( CAM_ROI *(&ptr) ){
  692. DBC_SNAPSHOT_DATA *rVal = NULL;
  693. if( ptr == NULL ) return NULL;
  694. if( ptr->targets.size() ){
  695. //if( m_bSourceConnected ){
  696. rVal = (DBC_SNAPSHOT_DATA *)malloc(sizeof(DBC_SNAPSHOT_DATA));
  697. if( rVal ){
  698. memset( rVal, 0, sizeof(DBC_SNAPSHOT_DATA) );
  699. //rVal->stream_id = m_nActiveStreamID;
  700. rVal->stream_id = m_sNodeData.aux_parameter;
  701. for( vCAM_TRACK_RECORDS::iterator idx = ptr->targets.begin(); idx < ptr->targets.end(); idx++ ) {
  702. LOG("Added Event ID");
  703. rVal->assoc_event_ids.push_back( (*idx)->evt_db_ID );
  704. };
  705. };
  706. //};
  707. ptr->targets.clear();
  708. } else {
  709. LOG("No Events ID to ROI");
  710. };
  711. // Destruct ROI MEM
  712. delete( (CAM_ROI *) ptr );
  713. ptr = NULL;
  714. // Not Setting MD5 and TS -> will set at capture time
  715. return rVal;
  716. };
  717. void deviceNodeCamera::fusion_validate( void ){
  718. m_mROIsProtector.lock();
  719. if( m_vROIS.size() ){
  720. #ifdef DEBUG_EXTRA
  721. char tB[128];
  722. sprintf( tB, "Fusion: Have [%d] Regions Of Interes", m_vROIS.size() );
  723. LOG( tB );
  724. #endif
  725. uint64_t cts = getMilliseconds();
  726. for( vCAM_ROIS::iterator idx = m_vROIS.begin(); idx < m_vROIS.end(); idx++ ){
  727. if( !fusion_validateROI( cts, *idx ) ) {
  728. free( (*idx) );
  729. m_vROIS.erase( idx );
  730. };
  731. };
  732. };
  733. m_mROIsProtector.unlock();
  734. };
  735. bool deviceNodeCamera::absoluteMove( uint32_t TargetRefID, float PanPOS, float TiltPOS, float ZoomPOS ){
  736. if( m_sCamData.online ){
  737. CAM_TASK *tTask = (CAM_TASK *)malloc(sizeof(CAM_TASK));
  738. if( tTask ){
  739. memset(tTask, 0, sizeof(CAM_TASK));
  740. tTask->CMD = (CAM_PTZ_CMD *)malloc(sizeof(CAM_PTZ_CMD));
  741. if( tTask->CMD ){
  742. tTask->CMD->CMD = PTZ_ABSOLUTE;
  743. tTask->CMD->PAN = PanPOS;
  744. tTask->CMD->TILT = TiltPOS;
  745. tTask->CMD->ZOOM = ZoomPOS;
  746. tTask->movemet_needed = true;
  747. tTask->source_evt_id = TargetRefID;
  748. setWM( CAM_WS_BUSY );
  749. return pushCamTask( tTask );
  750. } else {
  751. LOG_ERROR("CMD malloc Error!");
  752. free( tTask );
  753. };
  754. } else LOG_ERROR("Task malloc error!");
  755. };
  756. return false;
  757. };
  758. bool deviceNodeCamera::absoluteMove( uint32_t TargetRefID, C3D_ORIENT_ONVIF O ){
  759. if( m_sCamData.online ){
  760. CAM_TASK *tTask = (CAM_TASK *)malloc(sizeof(CAM_TASK));
  761. if( tTask ){
  762. memset(tTask, 0, sizeof(CAM_TASK));
  763. tTask->CMD = (CAM_PTZ_CMD *)malloc(sizeof(CAM_PTZ_CMD));
  764. if( tTask->CMD ){
  765. tTask->CMD->CMD = PTZ_ABSOLUTE;
  766. tTask->CMD->PAN = O.pan;
  767. tTask->CMD->TILT = O.tilt;
  768. tTask->CMD->ZOOM = O.zoom;
  769. tTask->movemet_needed = true;
  770. tTask->source_evt_id = TargetRefID;
  771. setWM( CAM_WS_BUSY );
  772. return pushCamTask( tTask );
  773. } else {
  774. LOG_ERROR("CMD malloc Error!");
  775. free( tTask );
  776. };
  777. } else LOG_ERROR("Task malloc error!");
  778. };
  779. return false;
  780. };
  781. void deviceNodeCamera::m_fThreadProc( void ){
  782. DBC_PUSHED_CMD * PUSHED = NULL;
  783. m_bThreadStatus = TH_WORKING;
  784. bool bNewSecondLoop = false;
  785. uint32_t cTimeSeconds = 0;
  786. uint32_t oTimeSeconds = 0;
  787. while( m_bThreadStatus < TH_ENDED ) {
  788. cTimeSeconds = (uint32_t) time( NULL );
  789. if ( bNewSecondLoop = ( cTimeSeconds != oTimeSeconds ) ) oTimeSeconds = cTimeSeconds;
  790. uint8_t tState = getWM();
  791. char tB[128];
  792. fusion_validate();
  793. switch( tState ) {
  794. case CAM_WS_INIT:{
  795. #ifdef DEBUG
  796. LOG("Init Camera - PowerMode");
  797. #endif
  798. //setBusy( true );
  799. if( m_pProtocol->initCamera() ) setWM( CAM_WS_WHITE_BALLANCE );
  800. //setBusy( false );
  801. break;
  802. };
  803. case CAM_WS_WHITE_BALLANCE:{
  804. #ifdef DEBUG
  805. LOG("Set WB");
  806. #endif
  807. //setBusy( true );
  808. if( m_pProtocol->setWhiteBalanceMode( WB_AUTO ) ) setWM( CAM_WS_BACKLIGHT );
  809. //setBusy( false );
  810. break;
  811. };
  812. case CAM_WS_BACKLIGHT:{
  813. #ifdef DEBUG
  814. LOG("Set BLC");
  815. #endif
  816. //setBusy( true );
  817. if( m_pProtocol->setBackLightMode( true ) ) setWM( CAM_WS_AUTO_FOCUS );
  818. //setBusy( false );
  819. break;
  820. };
  821. case CAM_WS_AUTO_FOCUS:{
  822. #ifdef DEBUG
  823. LOG("Set AF");
  824. #endif
  825. //setBusy( true );
  826. if( m_pProtocol->setAutoFocus( true ) ) setWM( CAM_WS_INIT_GO_HOME );
  827. //setBusy( false );
  828. break;
  829. };
  830. case CAM_WS_INIT_GO_HOME:{
  831. m_nHomingCounter = 0;
  832. if( !m_bNoAbsPositionning ) {
  833. #ifdef DEBUG
  834. LOG("INIT GO -1 0 -1");
  835. #endif
  836. if (m_pProtocol->goAbsolute(-1, 0, -1)) {
  837. m_fCP_Pan = -1;
  838. m_fCP_Tilt = 0;
  839. m_fCP_Zoom = -1;
  840. setWM( CAM_WS_READY );
  841. };
  842. } else {
  843. #ifdef DEBUG
  844. LOG("Skip! Go Home!");
  845. #endif
  846. CSTR TK = getPresetToken( ARGUS_PRESET_TYPE_HOME );
  847. if( TK ) {
  848. if ( !m_bCameraOnHomePosition ) {
  849. if( !m_pProtocol->goPreset( TK )){
  850. LOG_ERROR("Cannot Issue goPreset Command!");
  851. };
  852. m_bCameraOnHomePosition = true;
  853. };
  854. };
  855. setWM( CAM_WS_READY );
  856. };
  857. break;
  858. };
  859. case CAM_WS_AUTOMATED_MOVE:{
  860. m_bCameraOnHomePosition = false;
  861. m_nRelativeMoveCounter++;
  862. if( ( m_bRelativeMoveMatrix & 0x03 ) != 0 ) {
  863. //Pan Move
  864. if( ( m_bRelativeMoveMatrix & 0x01 ) == 0 ) {
  865. m_fRM_Pan -= m_fSpeedRise;
  866. } else {
  867. m_fRM_Pan += m_fSpeedRise;
  868. };
  869. };
  870. if( ( m_bRelativeMoveMatrix & 0x0C ) != 0 ) {
  871. //TILT Move
  872. if( ( m_bRelativeMoveMatrix & 0x04 ) == 0 ) {
  873. m_fRM_Tilt -= m_fSpeedRise;
  874. } else {
  875. m_fRM_Tilt += m_fSpeedRise;
  876. }
  877. };
  878. if( ( m_bRelativeMoveMatrix & 0x30 ) != 0 ) {
  879. //ZOOM Move
  880. if( ( m_bRelativeMoveMatrix & 0x10 ) == 0 ) {
  881. //Wide
  882. m_fRM_Zoom -= m_fSpeedRise;
  883. } else {
  884. //Tele
  885. m_fRM_Zoom += m_fSpeedRise;
  886. };
  887. };
  888. if( m_fRM_Pan < -1.0f ) m_fRM_Pan = -1.0f;
  889. if( m_fRM_Pan > 1.0f ) m_fRM_Pan = 1.0f;
  890. if( m_fRM_Tilt < -1.0f ) m_fRM_Tilt = -1.0f;
  891. if( m_fRM_Tilt > 1.0f ) m_fRM_Tilt = 1.0f;
  892. if( m_fRM_Zoom < -1.0f ) m_fRM_Zoom = -1.0f;
  893. if( m_fRM_Zoom > 1.0f ) m_fRM_Zoom = 1.0f;
  894. m_fCP_Zoom += 0.5f*m_fRM_Zoom;
  895. if( m_fCP_Zoom < -1.0f ) m_fCP_Zoom = -1.0f;
  896. if( m_fCP_Zoom > 1.0f ) m_fCP_Zoom = 1.0f;
  897. if( !relativeMove( m_fRM_Pan, m_fRM_Tilt, m_fRM_Zoom ) ){
  898. m_nRelativeMoveCounter = 0;
  899. setWM( CAM_WS_READY );
  900. break;
  901. };
  902. if( m_sCamData.ptz_axel ) { //Manual
  903. if (m_nRelativeMoveCounter > m_sCamData.ptz_axel) {
  904. m_nRelativeMoveCounter = 0;
  905. setWM( CAM_WS_READY );
  906. };
  907. } else {
  908. // Stepping Axel by Zoom
  909. uint8_t tVal = (uint8_t)( 10.0f * ( 2.0f - ( 1.0f + m_fCP_Zoom ) ));
  910. if( m_nRelativeMoveCounter > tVal ) {
  911. m_nRelativeMoveCounter = 0;
  912. setWM( CAM_WS_READY );
  913. };
  914. };
  915. break;
  916. };
  917. case CAM_WS_STOP_MOVE:{
  918. m_nRelativeMoveCounter++;
  919. m_fRM_Pan = .0f;
  920. m_fRM_Pan = .0f;
  921. m_fRM_Tilt = .0f;
  922. m_fRM_Tilt = .0f;
  923. m_fRM_Zoom = .0f;
  924. m_fRM_Zoom = .0f;
  925. if( relativeStop() ) {
  926. m_nHomingCounter = 0;
  927. m_nRelativeMoveCounter = 0;
  928. setWM( CAM_WS_READY );
  929. break;
  930. };
  931. if( m_sCamData.ptz_axel ) {
  932. if (m_nRelativeMoveCounter > m_sCamData.ptz_axel) setWM(CAM_WS_READY);
  933. } else {
  934. uint8_t tVal = (uint8_t)( 10.0f * ( 2.0f - ( 1.0f + m_fCP_Zoom ) ));
  935. if (m_nRelativeMoveCounter > tVal) setWM(CAM_WS_READY);
  936. };
  937. break;
  938. };
  939. case CAM_WS_GO_ABS_ROI:
  940. case CAM_WS_GO_ABS_POSITION:{
  941. m_bCameraOnHomePosition = false;
  942. setWM( CAM_WS_BUSY );
  943. if( m_pCurrentROI != NULL ){
  944. m_nHomingCounter = 0;
  945. if( m_pBEH ){
  946. EVENT_CONTAINER *tEVT = createBasicEvent(
  947. m_sNodeData.node_id,
  948. EVT_CLASS_CAMERA_CTRL,
  949. EVT_CAMERA_MOVE_TO,
  950. NULL, NULL, NULL );
  951. if( tEVT ){
  952. //tEVT->m_db_id = tTASK->source_evt_id; // EVT ids embed in DATA
  953. m_pBEH->broadcast( tEVT );
  954. };
  955. };
  956. if( m_bNoAbsPositionning ){
  957. uint8_t COL, ROW;
  958. translateROI2PRESET(
  959. m_pCurrentROI->cam_orientation.pan,
  960. m_pCurrentROI->cam_orientation.tilt,
  961. m_pCurrentROI->cam_orientation.zoom, COL, ROW );
  962. if ( ( m_bCurrentPresetCol == COL ) && ( m_bCurrentPresetRow == ROW ) ) {
  963. m_fCP_Pan = m_pCurrentROI->cam_orientation.pan;
  964. m_fCP_Tilt = m_pCurrentROI->cam_orientation.tilt;
  965. m_fCP_Zoom = m_pCurrentROI->cam_orientation.zoom;
  966. // Send Capture event
  967. DBC_SNAPSHOT_DATA *tData = fusion_translate_ROI( m_pCurrentROI );
  968. utilizeROI( m_pCurrentROI );
  969. setWM( CAM_WS_READY );
  970. if( tData != NULL ) {
  971. if( m_pBEH ){
  972. EVENT_CONTAINER *tEVT = createBasicEvent(
  973. m_sNodeData.node_id,
  974. EVT_CLASS_CAMERA_CTRL,
  975. EVT_CAMERA_ON_POSITION,
  976. NULL, NULL, tData );
  977. if( tEVT ){
  978. #ifdef DEBUG
  979. LOG("INSTANT BCAST CAM ONPOS");
  980. #endif
  981. //tEVT->m_db_id = tTASK->source_evt_id; // EVT ids embed in DATA
  982. m_pBEH->broadcast( tEVT );
  983. } else {
  984. free( tData );
  985. };
  986. } else {
  987. free( tData );
  988. };
  989. };
  990. } else {
  991. LOG("Get ABS Preset!");
  992. CSTR token = getPresetToken( COL, ROW );
  993. if ( token ) {
  994. LOG("Have One!");
  995. //update position Info
  996. m_bCurrentPresetCol = COL;
  997. m_bCurrentPresetRow = ROW;
  998. m_fCP_Pan = m_pCurrentROI->cam_orientation.pan;
  999. m_fCP_Tilt = m_pCurrentROI->cam_orientation.tilt;
  1000. m_fCP_Zoom = m_pCurrentROI->cam_orientation.zoom;
  1001. if( !m_pProtocol->goPreset( token ) ){
  1002. LOG_ERROR("Cannot Issue goPreset Command!->Restart Camera?");
  1003. setWM( CAM_WS_END );
  1004. } else {
  1005. // Waiting Movement finish
  1006. if( m_bSkeepPositioningWait ){
  1007. // Send Capture event
  1008. DBC_SNAPSHOT_DATA *tData = fusion_translate_ROI( m_pCurrentROI );
  1009. utilizeROI( m_pCurrentROI );
  1010. setWM( CAM_WS_READY );
  1011. if( tData != NULL ) {
  1012. if( m_pBEH ){
  1013. EVENT_CONTAINER *tEVT = createBasicEvent(
  1014. m_sNodeData.node_id,
  1015. EVT_CLASS_CAMERA_CTRL,
  1016. EVT_CAMERA_ON_POSITION,
  1017. NULL, NULL, tData );
  1018. if( tEVT ){
  1019. #ifdef DEBUG
  1020. LOG("INSTANT BCAST CAM ONPOS");
  1021. #endif
  1022. //tEVT->m_db_id = tTASK->source_evt_id; // EVT ids embed in DATA
  1023. m_pBEH->broadcast( tEVT );
  1024. } else {
  1025. free( tData );
  1026. };
  1027. } else {
  1028. free( tData );
  1029. };
  1030. };
  1031. } else {
  1032. setWM(CAM_WS_WAIT_ONPOS);
  1033. };
  1034. };
  1035. } else {
  1036. utilizeROI( m_pCurrentROI );
  1037. setWM( CAM_WS_READY );
  1038. };
  1039. };
  1040. } else {
  1041. //update position Info
  1042. m_fCP_Pan = m_pCurrentROI->cam_orientation.pan;
  1043. m_fCP_Tilt = m_pCurrentROI->cam_orientation.tilt;
  1044. m_fCP_Zoom = m_pCurrentROI->cam_orientation.zoom;
  1045. // Do Movement
  1046. if( !m_pProtocol->goAbsolute(
  1047. m_pCurrentROI->cam_orientation.pan,
  1048. m_pCurrentROI->cam_orientation.tilt,
  1049. ( m_pCurrentROI->cam_orientation.zoom * m_fZoomCorrection )
  1050. )){
  1051. LOG_ERROR("Cannot Issue goAbsolute Command!->Restart Camera?");
  1052. setWM( CAM_WS_END );
  1053. } else {
  1054. // Waiting Movement finish
  1055. if( m_bSkeepPositioningWait ){
  1056. // Send Capture event
  1057. DBC_SNAPSHOT_DATA *tData = fusion_translate_ROI( m_pCurrentROI );
  1058. utilizeROI( m_pCurrentROI );
  1059. setWM( CAM_WS_READY );
  1060. if( tData != NULL ) {
  1061. if( m_pBEH ){
  1062. EVENT_CONTAINER *tEVT = createBasicEvent(
  1063. m_sNodeData.node_id,
  1064. EVT_CLASS_CAMERA_CTRL,
  1065. EVT_CAMERA_ON_POSITION,
  1066. NULL, NULL, tData );
  1067. if( tEVT ){
  1068. #ifdef DEBUG
  1069. LOG("INSTANT BCAST CAM ONPOS");
  1070. #endif
  1071. //tEVT->m_db_id = tTASK->source_evt_id; // EVT ids embed in DATA
  1072. m_pBEH->broadcast( tEVT );
  1073. } else {
  1074. free( tData );
  1075. };
  1076. } else {
  1077. free( tData );
  1078. };
  1079. };
  1080. } else {
  1081. setWM(CAM_WS_WAIT_ONPOS);
  1082. };
  1083. };
  1084. };
  1085. } else setWM( CAM_WS_READY );
  1086. break;
  1087. };
  1088. case CAM_WS_WAIT_ONPOS:{
  1089. DBC_SNAPSHOT_DATA *tData = NULL;
  1090. if( m_pCurrentROI != NULL ){
  1091. // Send Capture event
  1092. tData = fusion_translate_ROI( m_pCurrentROI );
  1093. utilizeROI(m_pCurrentROI);
  1094. };
  1095. // To Accept any new ROI's ??
  1096. setWM( CAM_WS_READY );
  1097. #ifdef DEBUG
  1098. LOG("Start Waiting Cam On Position!");
  1099. #endif
  1100. if (!m_pProtocol->waitMoveIsDone()) {
  1101. //Some Cameras does not support that :(
  1102. //LOG_ERROR("Cannot Issue getPosition Command!->Restart Camera?");
  1103. //setWM( CAM_WS_END );
  1104. m_bSkeepPositioningWait = true;
  1105. };
  1106. if( tData != NULL ) {
  1107. if( m_pBEH ){
  1108. EVENT_CONTAINER *tEVT = createBasicEvent(
  1109. m_sNodeData.node_id,
  1110. EVT_CLASS_CAMERA_CTRL,
  1111. EVT_CAMERA_ON_POSITION,
  1112. NULL, NULL, tData );
  1113. if( tEVT ){
  1114. #ifdef DEBUG
  1115. LOG("BCAST CAM ONPOS");
  1116. #endif
  1117. //tEVT->m_db_id = tTASK->source_evt_id; // EVT ids embed in DATA
  1118. m_pBEH->broadcast( tEVT );
  1119. } else {
  1120. free( tData );
  1121. };
  1122. } else {
  1123. free( tData );
  1124. };
  1125. tData = NULL;
  1126. };
  1127. break;
  1128. };
  1129. case CAM_WS_GO_HOMING_PRESET:{
  1130. CSTR TK = getPresetToken( ARGUS_PRESET_TYPE_HOME );
  1131. if( TK ) {
  1132. if ( !m_bCameraOnHomePosition ) {
  1133. LOG("Go Home!");
  1134. LOG( TK );
  1135. if( !m_pProtocol->goPreset( TK )){
  1136. LOG_ERROR("Cannot Issue goPreset Command!");
  1137. };
  1138. m_bCameraOnHomePosition = true;
  1139. };
  1140. };
  1141. setWM( CAM_WS_READY );
  1142. break;
  1143. };
  1144. case CAM_WS_GO_PRESET:{
  1145. LOG("Go Preset!");
  1146. //if( m_sCamData.home_token[0] != (char)0 ) {
  1147. // LOG("Go Home!");
  1148. // LOG( m_sCamData.home_token );
  1149. // if( !m_pProtocol->goPreset( m_sCamData.home_token )){
  1150. // LOG_ERROR("Cannot Issue goPreset Command!");
  1151. // };
  1152. //};
  1153. setWM( CAM_WS_READY );
  1154. break;
  1155. };
  1156. case CAM_WS_SET_PRESET:{
  1157. LOG("Set Preset!");
  1158. //if( m_sCamData.home_token[0] != (char)0 ) {
  1159. // LOG("Go Home!");
  1160. // LOG( m_sCamData.home_token );
  1161. // if( !m_pProtocol->goPreset( m_sCamData.home_token )){
  1162. // LOG_ERROR("Cannot Issue goPreset Command!");
  1163. // };
  1164. //};
  1165. setWM( CAM_WS_READY );
  1166. break;
  1167. };
  1168. case CAM_WS_BUSY:{
  1169. //Handle Operation TO
  1170. setWM( CAM_WS_READY );
  1171. break;
  1172. };
  1173. case CAM_WS_READY:{
  1174. m_pCurrentROI = fusion_fetchROI();
  1175. if( m_pCurrentROI != NULL ) {
  1176. #ifdef DEBUG_EXTRA__
  1177. LOG("HAVE ROI to see!");
  1178. #endif
  1179. setWM( CAM_WS_GO_ABS_POSITION );
  1180. } else {
  1181. //Idle Work --->
  1182. if ( m_sCamData.eco_ir ) {
  1183. m_mIRProtector.lock();
  1184. if( m_bIRIsOn ){
  1185. if ( m_bIRTO ) {
  1186. if ( bNewSecondLoop ) m_bIRTO--;
  1187. } else {
  1188. CSTR TK = getPresetToken( ARGUS_PRESET_TYPE_IR_OFF );
  1189. if ( TK ) if( m_pProtocol->goPreset( TK ) ) m_bIRIsOn = false;
  1190. };
  1191. };
  1192. m_mIRProtector.unlock();
  1193. };
  1194. if (m_sCamData.homing_enable) {
  1195. if ( bNewSecondLoop ) {
  1196. if ( m_nHomingCounter < m_sCamData.homing_to ) {
  1197. m_nHomingCounter++;
  1198. } else {
  1199. //LOG("Go Home Position!");
  1200. m_nHomingCounter = 0;
  1201. setWM( CAM_WS_GO_HOMING_PRESET );
  1202. break;
  1203. };
  1204. };
  1205. };
  1206. CAM_TASK * tTASK = fetchCamTask();
  1207. if ( tTASK != NULL ) {
  1208. if( tTASK->movemet_needed ){
  1209. CAM_PTZ_CMD *tCMD = tTASK->CMD;
  1210. if( tCMD ) {
  1211. switch( tCMD->CMD ) {
  1212. case PTZ_STOP:{
  1213. m_nHomingCounter = 0;
  1214. setWM( CAM_WS_STOP_MOVE );
  1215. break;
  1216. };
  1217. case PTZ_MOVE:{
  1218. if( tState != CAM_WS_AUTOMATED_MOVE ) m_nRelativeMoveCounter = 0;
  1219. m_bRelativeMoveMatrix = 0;
  1220. m_fRM_Pan = tCMD->PAN;
  1221. m_fRM_Tilt = tCMD->TILT;
  1222. m_fRM_Zoom = tCMD->ZOOM;
  1223. m_nHomingCounter = 0;
  1224. setWM( CAM_WS_AUTOMATED_MOVE );
  1225. break;
  1226. };
  1227. case PTZ_ABSOLUTE:{
  1228. m_pCurrentROI = createCAM_ROI();
  1229. if( m_pCurrentROI != NULL ){
  1230. CAM_TRACK_RECORD * tTarget = (CAM_TRACK_RECORD*)malloc(sizeof(CAM_TRACK_RECORD));
  1231. if ( tTarget != NULL ) {
  1232. memset( tTarget, 0, sizeof(CAM_TRACK_RECORD) );
  1233. tTarget->evt_db_ID = tTASK->source_evt_id;
  1234. m_pCurrentROI->targets.push_back( tTarget );
  1235. m_pCurrentROI->cam_orientation.pan = tCMD->PAN;
  1236. m_pCurrentROI->cam_orientation.tilt = tCMD->TILT;
  1237. m_pCurrentROI->cam_orientation.zoom = tCMD->ZOOM;
  1238. m_nHomingCounter = 0;
  1239. setWM(CAM_WS_GO_ABS_POSITION);
  1240. } else {
  1241. utilizeROI( m_pCurrentROI );
  1242. };
  1243. };
  1244. break;
  1245. };
  1246. case PTZ_RELOAD_CAMERA:{
  1247. LOG("Exit Command Rcpt");
  1248. m_bThreadStatus = TH_ENDED;
  1249. };
  1250. };
  1251. free( tCMD );
  1252. tTASK->CMD = NULL;
  1253. };
  1254. } else {
  1255. LOG("No Movement -> INSTCAP" );
  1256. // No Movement -> Just Capture
  1257. if( m_pBEH ){
  1258. EVENT_CONTAINER *tEVT = createBasicEvent(
  1259. m_sNodeData.node_id,
  1260. EVT_CLASS_CAMERA_CTRL,
  1261. EVT_CAMERA_ON_POSITION,
  1262. NULL, NULL, NULL );
  1263. if( tEVT ){
  1264. tEVT->m_db_id = tTASK->source_evt_id;
  1265. m_pBEH->broadcast(tEVT);
  1266. };
  1267. };
  1268. };
  1269. free( tTASK );
  1270. } else {
  1271. // Lowest Priority to Web Client Commands
  1272. #ifdef DEBUG_EXTRA__
  1273. LOG("Check Web CMD!");
  1274. #endif
  1275. PUSHED = m_pDBC->getNodePushedCMD( m_sNodeData.node_id );
  1276. if( PUSHED != NULL ) {
  1277. #ifdef DEBUG_EXTRA__
  1278. LOG("HAVE Web CMD!");
  1279. #endif
  1280. m_pDBC->updateNodePushedCMD( PUSHED->id, DBC_CMD_READY_TO_CLEAN );
  1281. if( ( PUSHED->CMD & ARGUS_CAM_PCMD_CMD_MASK ) == ARGUS_CAM_PCMD_CMD_MASK ) {
  1282. switch( PUSHED->CMD ) {
  1283. case 0x80:{
  1284. //Call Home Preset
  1285. LOG("CALL HOME PRESET!");
  1286. CSTR TK = getPresetToken(ARGUS_PRESET_TYPE_HOME);
  1287. if ( TK ) {
  1288. LOG(TK);
  1289. if( m_pProtocol->goPreset( TK )){
  1290. m_bCameraOnHomePosition = true;
  1291. m_nHomingCounter = 0;
  1292. };
  1293. };
  1294. break;
  1295. };
  1296. case 0x81:{
  1297. //Call IR On Preset
  1298. LOG("CALL IR ON PRESET!");
  1299. m_mIRProtector.lock();
  1300. CSTR TK = getPresetToken(ARGUS_PRESET_TYPE_IR_ON);
  1301. if ( TK ) {
  1302. LOG(TK);
  1303. if( m_pProtocol->goPreset( TK )){
  1304. m_bIRIsOn = true;
  1305. m_bIRTO = DEFAULT_SHINE_ON_TARGET_TO;
  1306. };
  1307. } else LOG("PRESET NOT FOUND!");
  1308. m_mIRProtector.unlock();
  1309. break;
  1310. };
  1311. case 0x82:{
  1312. //Call IR Off Preset
  1313. LOG("CALL IR OFF PRESET!");
  1314. m_mIRProtector.lock();
  1315. CSTR TK = ( m_sCamData.eco_ir )?getPresetToken(ARGUS_PRESET_TYPE_IR_OFF):getPresetToken(ARGUS_PRESET_TYPE_IR_AUTO);
  1316. if ( TK ) {
  1317. LOG(TK);
  1318. if( m_pProtocol->goPreset( TK )){
  1319. m_bIRIsOn = false;
  1320. m_bIRTO = 0;
  1321. };
  1322. } else LOG("PRESET NOT FOUND!");
  1323. m_mIRProtector.unlock();
  1324. break;
  1325. };
  1326. case 0x83:{
  1327. //Call IR Auto Preset
  1328. LOG("CALL IR AUTO PRESET!");
  1329. m_mIRProtector.lock();
  1330. CSTR TK = getPresetToken(ARGUS_PRESET_TYPE_IR_AUTO);
  1331. if ( TK ) {
  1332. LOG(TK);
  1333. if( m_pProtocol->goPreset( TK )){
  1334. m_bIRIsOn = true;
  1335. m_bIRTO = DEFAULT_SHINE_ON_TARGET_TO;
  1336. };
  1337. } else LOG("PRESET NOT FOUND!");
  1338. m_mIRProtector.unlock();
  1339. break;
  1340. };
  1341. case 0x84:{
  1342. //Call Wisher Preset
  1343. LOG("CALL WIPER PRESET!");
  1344. CSTR TK = getPresetToken(ARGUS_PRESET_TYPE_IR_WISHER);
  1345. if ( TK ) {
  1346. LOG(TK);
  1347. m_pProtocol->goPreset( TK );
  1348. };
  1349. break;
  1350. };
  1351. };
  1352. } else if ( tState == CAM_WS_READY || tState == CAM_WS_AUTOMATED_MOVE ){ //No Way to get here
  1353. //Direct Movment Mode
  1354. if( PUSHED->CMD == ARGUS_CAM_PCMD_STOP ) {
  1355. m_nHomingCounter = 0;
  1356. setWM( CAM_WS_STOP_MOVE );
  1357. } else {
  1358. char tB[64];
  1359. if( tState != CAM_WS_AUTOMATED_MOVE ) m_nRelativeMoveCounter = 0;
  1360. m_bRelativeMoveMatrix = ( PUSHED->CMD ) >> 1;
  1361. m_nHomingCounter = 0;
  1362. setWM( CAM_WS_AUTOMATED_MOVE );
  1363. };
  1364. };
  1365. free( PUSHED );
  1366. PUSHED = NULL;
  1367. };
  1368. };
  1369. };
  1370. break;
  1371. };
  1372. case CAM_WS_END:{
  1373. LOG("Exit Command Rcpt");
  1374. m_bThreadStatus = TH_ENDED;
  1375. break;
  1376. };
  1377. };
  1378. SLEEP( DEFAULT_THREAD_SLEEP_TIME );
  1379. };
  1380. #ifdef DEBUG
  1381. LOG("CTRL Loop End!");
  1382. #endif
  1383. };
  1384. bool deviceNodeCamera::setNoPositionWait( bool FL ){
  1385. m_bSkeepPositioningWait = FL;
  1386. return true;
  1387. };
  1388. bool deviceNodeCamera::testFOV( C3D_DECART L ){
  1389. //System Coords -> trans to local and go to funct
  1390. systemAnchor* tSystem = systemAnchor::getInstance();
  1391. if ( tSystem == NULL ) return false;
  1392. C3D_SPH_DEGREES SL;
  1393. #ifdef DEBUG_EXTRA
  1394. LOG("Test FOV local Decart coords");
  1395. #endif
  1396. if( !tSystem->convertToLocal( &m_oPosition, L, SL ) ) return false;
  1397. return testFOV( SL );
  1398. };
  1399. bool deviceNodeCamera::testFOV( C3D_WGS84_LOC L ){
  1400. //Global Coords
  1401. systemAnchor* tSystem = systemAnchor::getInstance();
  1402. if ( tSystem == NULL ) return false;
  1403. C3D_SPH_DEGREES SL;
  1404. #ifdef DEBUG_EXTRA
  1405. LOG("Test FOV WGS coords");
  1406. #endif
  1407. if( !tSystem->convertToLocal( &m_oPosition, L, SL ) ) return false;
  1408. return testFOV( SL );
  1409. };
  1410. bool deviceNodeCamera::testFOV( C3D_SPH_RADIANS L ){
  1411. //Local Coords
  1412. C3D_SPH_DEGREES SL;
  1413. #ifdef DEBUG_EXTRA
  1414. LOG("Test FOV local Spherical(rad) coords");
  1415. #endif
  1416. spherical2spherical( L, SL );
  1417. return testFOV( SL );
  1418. };
  1419. bool deviceNodeCamera::testFOV( C3D_SPH_DEGREES SL ){
  1420. #ifdef DEBUG
  1421. char tB[128];
  1422. #endif
  1423. #ifdef DEBUG_EXTRA
  1424. LOG("Test FOV local Spherical(deg) coords");
  1425. #endif
  1426. switch ( m_sCamData.triggering_mode ) {
  1427. case CAM_TRIGGER_STRICT:{
  1428. if( SL.r > m_fMaxRange || SL.r < m_fMinRange ){
  1429. #ifdef DEBUG_EXTRA
  1430. sprintf( tB, "Target Range( %.3f.m ) is not in a Range %.3f.m <-> %.3f.m", SL.r, m_fMinRange, m_fMaxRange );
  1431. LOG( tB );
  1432. #endif
  1433. return false;
  1434. };
  1435. break;
  1436. };
  1437. case CAM_TRIGGER_ALL_NEAR:{
  1438. if( SL.r > m_fMaxRange ){
  1439. #ifdef DEBUG_EXTRA
  1440. sprintf( tB, "Target Range( %.3f.m ) is not in a Range %.3f.m <-> %.3f.m", SL.r, m_fMinRange, m_fMaxRange );
  1441. LOG( tB );
  1442. #endif
  1443. return false;
  1444. };
  1445. break;
  1446. };
  1447. case CAM_TRIGGER_ALL_FAR:{
  1448. if( SL.r < m_fMinRange ){
  1449. #ifdef DEBUG_EXTRA
  1450. sprintf( tB, "Target Range( %.3f.m ) is not in a Range %.3f.m <-> %.3f.m", SL.r, m_fMinRange, m_fMaxRange );
  1451. LOG( tB );
  1452. #endif
  1453. return false;
  1454. };
  1455. break;
  1456. };
  1457. case CAM_TRIGGER_ALL: return true;
  1458. };
  1459. float HS = AC_imager_width[m_sCamData.imager_type];
  1460. float VS = AC_imager_height[m_sCamData.imager_type];
  1461. float HS2 = HS * HS;
  1462. float VS2 = VS * VS;
  1463. float IR = HS / VS;
  1464. float IR2 = IR * IR;
  1465. if( m_sCamData.pan_angles == 0 ) {
  1466. // Fixed Camera
  1467. float tA = fabsf( SL.fi );
  1468. float tZ = 0.5f * ( m_fMinHFov + ( SL.r - m_fMinRange ) * m_fR2FL );
  1469. if ( tA > tZ ) return false;
  1470. } else {
  1471. if( m_sCamData.pan_angles < 360 ) { // If endless PAN -> PASSED
  1472. // Partial Pan
  1473. switch( m_sCamData.pan_model ) {
  1474. case 1: {
  1475. // Regular Pan
  1476. float tFOV = 0.5f * ( m_fMinHFov + ( SL.r - m_fMinRange ) * m_fR2FL );
  1477. float tRad = deg2rad( SL.fi );
  1478. #ifdef DEBUG_EXTRA
  1479. sprintf( tB, "FOV: %0.3f CC: %0.3f", tFOV, tRad );
  1480. LOG( tB );
  1481. #endif
  1482. if( ( tRad < ( -1.0 * tFOV ) ) || ( tRad > ( deg2rad( m_sCamData.pan_angles ) + tFOV ) ) ) return false;
  1483. break;
  1484. };
  1485. case 2:{
  1486. break;
  1487. };
  1488. case 3:{
  1489. break;
  1490. };
  1491. };
  1492. };
  1493. };
  1494. if( m_sCamData.tilt_angles == 0 ){
  1495. //Fixed Camera
  1496. if ( ( SL.tetta < ( m_fMaxVFov / -2.0f ) ) || ( SL.tetta > ( m_fMaxVFov / 2.0f ) ) )return false;
  1497. } else {
  1498. if( m_sCamData.tilt_angles < 90 ) { // if Cam CAN DO 90 -> Pass
  1499. // Partial Tilt
  1500. switch( m_sCamData.tilt_model ) {
  1501. case 1: {
  1502. // Regular Tilt
  1503. float tFOV = 0.5f * ( m_fMinVFov + ( m_fMaxVFov - m_fMinVFov ) * ( SL.r / m_fMaxRange ) ) ;
  1504. float tRad = deg2rad( SL.tetta );
  1505. if( ( tRad < ( -1.0 * tFOV ) ) || ( tRad > ( deg2rad( m_sCamData.tilt_angles ) + tFOV ) ) )return false;
  1506. break;
  1507. };
  1508. case 2:{
  1509. break;
  1510. };
  1511. };
  1512. };
  1513. };
  1514. return true;
  1515. };
  1516. bool deviceNodeCamera::lookAt( uint32_t TargetRefID, C3D_WGS84_LOC L ){
  1517. systemAnchor* tSystem = systemAnchor::getInstance();
  1518. if( tSystem == NULL ) return false;
  1519. C3D_SPH_DEGREES SL;
  1520. #ifdef DEBUG_EXTRA
  1521. LOG("Look at WGS coords!");
  1522. #endif
  1523. tSystem->convertToLocal(&m_oPosition, L, SL );
  1524. return lookAt(TargetRefID, SL );
  1525. };
  1526. bool deviceNodeCamera::lookAt( uint32_t TargetRefID, C3D_DECART L ){
  1527. systemAnchor* tSystem = systemAnchor::getInstance();
  1528. if( tSystem == NULL ) return false;
  1529. C3D_SPH_DEGREES SL;
  1530. #ifdef DEBUG_EXTRA
  1531. LOG("Look at System Decart coords!");
  1532. #endif
  1533. tSystem->convertToLocal( &m_oPosition, L, SL );
  1534. return lookAt(TargetRefID, SL );
  1535. };
  1536. bool deviceNodeCamera::lookAt( uint32_t TargetRefID, C3D_SPH_RADIANS L ){
  1537. C3D_SPH_DEGREES SL;
  1538. #ifdef DEBUG_EXTRA
  1539. LOG("Look at spherical(rad) 2 spherical(deg) coords!");
  1540. #endif
  1541. spherical2spherical( L, SL );
  1542. return lookAt(TargetRefID, SL );
  1543. };
  1544. bool deviceNodeCamera::lookAt( uint32_t TargetRefID, C3D_SPH_DEGREES L ){
  1545. if ( m_bCameraIsStatic ) {
  1546. DBC_SNAPSHOT_DATA *tData = (DBC_SNAPSHOT_DATA*)malloc(sizeof(DBC_SNAPSHOT_DATA));
  1547. if ( tData ) {
  1548. memset( tData, 0, sizeof( DBC_SNAPSHOT_DATA ));
  1549. tData->stream_id = m_sNodeData.aux_parameter;
  1550. tData->assoc_event_ids.push_back( TargetRefID );
  1551. if (m_pBEH) {
  1552. EVENT_CONTAINER *tEVT = createBasicEvent(
  1553. m_sNodeData.node_id,
  1554. EVT_CLASS_CAMERA_CTRL,
  1555. EVT_CAMERA_ON_POSITION,
  1556. NULL, NULL, tData );
  1557. if( tEVT ){
  1558. #ifdef DEBUG
  1559. LOG("INSTANT BCAST CAM ONPOS");
  1560. #endif
  1561. //tEVT->m_db_id = tTASK->source_evt_id; // EVT ids embed in DATA
  1562. m_pBEH->broadcast( tEVT );
  1563. } else {
  1564. free( tData );
  1565. };
  1566. } else {
  1567. free( tData );
  1568. };
  1569. };
  1570. return true;
  1571. };
  1572. //char tB[128];
  1573. //sprintf(tB, "Target On %.3f %.3f %.3f", L.r, L.fi, L.tetta );
  1574. //LOG( tB );
  1575. //sprintf( tB, "Cam GoTo %.3f %.3f %.3f", fP, fT, fZ );
  1576. //LOG( tB );
  1577. #ifdef DEBUG_EXTRA
  1578. LOG("Look at local spherical deg. coords!(final)");
  1579. #endif
  1580. //if( getWM() == CAM_WS_READY ){
  1581. // C3D_ORIENT_ONVIF LL;
  1582. // spherical2onvif( L, LL, m_fMaxRange, m_sCamData.pan_model, m_sCamData.pan_angles, m_sCamData.tilt_model, m_sCamData.tilt_angles );
  1583. // return absoluteMove( TargetRefID, LL );
  1584. //};
  1585. // Camera is Busy
  1586. if( m_bShineOnTarget ){
  1587. m_mIRProtector.lock();
  1588. if ( !m_bIRIsOn ) {
  1589. CSTR TK = getPresetToken(ARGUS_PRESET_TYPE_IR_AUTO);
  1590. if ( TK ) {
  1591. if( m_pProtocol->goPreset( TK )){
  1592. m_bIRIsOn = true;
  1593. m_bIRTO = DEFAULT_SHINE_ON_TARGET_TO;
  1594. };
  1595. };
  1596. } else m_bIRTO = DEFAULT_SHINE_ON_TARGET_TO;
  1597. m_mIRProtector.unlock();
  1598. };
  1599. return fusion_consumeData(TargetRefID, L);
  1600. };
  1601. bool deviceNodeCamera::makeSnap( uint32_t TargetRefID ){
  1602. CAM_TASK *tTask = (CAM_TASK *)malloc(sizeof(CAM_TASK));
  1603. if( tTask ){
  1604. memset(tTask, 0, sizeof(CAM_TASK));
  1605. tTask->CMD = (CAM_PTZ_CMD *) malloc( sizeof(CAM_PTZ_CMD) );
  1606. if( tTask->CMD ){
  1607. tTask->CMD->CMD = PTZ_CAPTURE_ONLY;
  1608. tTask->movemet_needed = false;
  1609. tTask->source_evt_id = TargetRefID;
  1610. return pushCamTask( tTask );
  1611. } else {
  1612. free( tTask );
  1613. };
  1614. };
  1615. return false;
  1616. };
  1617. bool deviceNodeCamera::connectToStream( uint32_t id ){
  1618. /*
  1619. if( id ){
  1620. argusKernelModule * tKM = NULL;
  1621. argusKernel * tKRNL = NULL;
  1622. argusModuleFactory * tMF = NULL;
  1623. if( m_pDBC == NULL ) {
  1624. LOG_ERROR("No Database Connector!");
  1625. return false;
  1626. };
  1627. tKRNL = argusKernel::getInstance();
  1628. if( tKRNL ) {
  1629. tMF = tKRNL->getModuleFactory();
  1630. if( tMF ) {
  1631. tKM = tMF->getModule( ARGUS_MODULE_EMBED_VIDEO );
  1632. } else {
  1633. LOG_ERROR("No ModuleFactory Instance!");
  1634. return false;
  1635. };
  1636. } else {
  1637. LOG_ERROR("No Kernel Instance!");
  1638. return false;
  1639. };
  1640. if( tKM ) {
  1641. // New Module
  1642. DBC_MEDIA_STREAM_DATA *tDATA = m_pDBC->getMediaStreamData( id );
  1643. if( tDATA ) {
  1644. m_sMediaSource.mediaStreamId = id;
  1645. if( strncmp(tDATA->mrl_proto, "v4l2", 4) == 0 ) {
  1646. if (m_pSource != NULL) delete((videoAVLIBSource*)m_pSource);
  1647. m_pSource = (basicMediaSource *) new videoAVLIBSource(&m_sMediaSource);
  1648. } else {
  1649. if (m_pSource != NULL) delete((videoLIVE555Source*)m_pSource);
  1650. m_pSource = (basicMediaSource *) new videoLIVE555Source(&m_sMediaSource );
  1651. };
  1652. if( m_pSource != NULL ) {
  1653. char tMRL[1024];
  1654. #ifdef DEBUG
  1655. LOG("Source OK");
  1656. #endif
  1657. ((videoLIVE555Source*)m_pSource)->setWM( MS_MODE_STREAM_PROXY | MS_MODE_OVERLAYED_LOOPS | MS_MODE_DECODE_FRAMES );
  1658. int tW = ((argusModuleEmbedVideo*)tKM)->getWidh();
  1659. int tH = ((argusModuleEmbedVideo*)tKM)->getHeight();
  1660. int pix = -1;
  1661. bool in_16_9 = (((tDATA->width*100) / tDATA->height ) > 140 );
  1662. bool out_16_9 = (((tW*100) / tH ) > 140 );
  1663. updateOpticalParameters( tW, tH );
  1664. if( strlen( tDATA->c_type ) ){
  1665. };
  1666. //if( tW > 0 ){
  1667. // //m_pSource->setProportionalByWidth( tW );
  1668. // m_pSource->setSize( tW, tH );
  1669. //};
  1670. if( in_16_9 != out_16_9 ){
  1671. if( in_16_9 ){
  1672. tH = ((( tW * 1000 ) / 16 ) * 9 ) / 1000;
  1673. } else {
  1674. tW = ((( tH * 1000 ) / 3 ) * 4 ) / 1000;
  1675. };
  1676. };
  1677. if (strncmp(tDATA->mrl_proto, "v4l2", 4) == 0) {
  1678. sprintf( tMRL, tDATA->mrl );
  1679. } else {
  1680. DBC_CAM_DATA *tCAM = m_pDBC->getCamData( tDATA->source_id );
  1681. if( tCAM ) {
  1682. sprintf( tMRL, "%s://%s:%s@%s:%d%s", tDATA->mrl_proto, tCAM->username, tCAM->password, tCAM->ip, tDATA->mrl_port, tDATA->mrl );
  1683. free( tCAM );
  1684. } else {
  1685. LOG_ERROR("Error Getting CAM Data!");
  1686. delete( m_pSource );
  1687. m_pSource = NULL;
  1688. return false;
  1689. };
  1690. };
  1691. LOG( tMRL );
  1692. if( m_pSource->_open( tMRL, false, -1.0f, pix, tDATA->width, tDATA->height, tW, tH ) ){
  1693. m_bSourceConnected = m_pSource->attachDestination( (argusModuleEmbedVideo*) tKM );
  1694. if ( !m_bSourceConnected ) {
  1695. //delete( m_pSource );
  1696. if( strncmp(tDATA->mrl_proto, "v4l2", 4) == 0 ) {
  1697. delete((videoAVLIBSource*)m_pSource);
  1698. } else {
  1699. delete((videoLIVE555Source*)m_pSource);
  1700. };
  1701. m_pSource = NULL;
  1702. } else {
  1703. m_nActiveStreamID = id;
  1704. #ifdef DEBUG
  1705. LOG("Source is Connected to Output.");
  1706. #endif
  1707. };
  1708. } else {
  1709. LOG_ERROR("Can not Open Video Source!");
  1710. //delete( m_pSource );
  1711. if( strncmp(tDATA->mrl_proto, "v4l2", 4) == 0 ) {
  1712. delete((videoAVLIBSource*)m_pSource);
  1713. } else {
  1714. delete((videoLIVE555Source*)m_pSource);
  1715. };
  1716. m_pSource = NULL;
  1717. };
  1718. } else {
  1719. LOG_ERROR("Video Source Stream Failure!");
  1720. };
  1721. free( tDATA );
  1722. };
  1723. }
  1724. #ifdef DEBUG
  1725. else LOG("No Video Module!")
  1726. #endif
  1727. ;
  1728. };
  1729. LOG("Connect Finished!");
  1730. return m_bSourceConnected;
  1731. */
  1732. return true;
  1733. };
  1734. bool deviceNodeCamera::disconnectFromStream( void ){
  1735. /*if( m_bSourceConnected ) {
  1736. if( m_pSource ){
  1737. delete( m_pSource );
  1738. m_pSource = NULL;
  1739. };
  1740. m_bSourceConnected = false;
  1741. };
  1742. return !m_bSourceConnected;
  1743. */
  1744. return true;
  1745. };
  1746. bool deviceNodeCamera::validateStream( void ){
  1747. /*
  1748. if( m_bSourceConnected ) {
  1749. if( m_pSource ) {
  1750. if( m_pSource->isAttached() ){
  1751. if( m_pSource->getStatus() < MS_STOPED ) return true;
  1752. #ifdef DEBUG
  1753. LOG("Source is Finished!");
  1754. #endif
  1755. };
  1756. #ifdef DEBUG
  1757. LOG("Source is Disconnected!");
  1758. #endif
  1759. } else LOG_ERROR("No Source Object! Set Disconnected!");
  1760. disconnectFromStream();
  1761. };
  1762. return false;
  1763. */
  1764. return true;
  1765. };
  1766. bool deviceNodeCamera::validate(){
  1767. if ( m_pDBC == NULL ) return false;
  1768. DBC_CAM_DATA *tDATA = m_pDBC->getCamData( m_sCamData.id );
  1769. if ( tDATA == NULL ) return false;
  1770. if ( memcmp( tDATA, &m_sCamData, sizeof(DBC_CAM_DATA) ) != 0 ) {
  1771. //Settings Changed
  1772. memcpy(&m_sCamData, tDATA, sizeof(DBC_CAM_DATA) );
  1773. };
  1774. free( tDATA );
  1775. if (!validateStream()) return false;
  1776. return deviceNode::validate();
  1777. };
  1778. bool deviceNodeCamera::load_presets( void ){
  1779. bool bHaveIROFF = false;
  1780. bool bHaveIRON = false;
  1781. bool bHaveIRAUTO = false;
  1782. if ( m_pDBC == NULL ) return false;
  1783. LOG("Loading Presets!");
  1784. DBC_PRESETS_INFO *tData = m_pDBC->getCamPresetInfo(m_sCamData.id);
  1785. if ( tData ) {
  1786. memcpy( &m_sCamPresets, tData, sizeof(DBC_PRESETS_INFO));
  1787. DBC_INT_LIST *tList = m_pDBC->listCamPresets( m_sCamData.id );
  1788. if ( tList ) {
  1789. for( DBC_INT_LIST::iterator idx= tList->begin(); idx < tList->end(); idx++ ) {
  1790. CAM_PRESET *tData = m_pDBC->getCamPreset( *idx );
  1791. if ( tData ) {
  1792. m_sCamPresets.presets.push_back(tData);
  1793. bHaveIRON = ( tData->role == ARGUS_PRESET_TYPE_IR_ON )?true:bHaveIRON;
  1794. bHaveIROFF = ( tData->role == ARGUS_PRESET_TYPE_IR_OFF )?true:bHaveIROFF;
  1795. bHaveIRAUTO = ( tData->role == ARGUS_PRESET_TYPE_IR_AUTO )?true:bHaveIRAUTO;
  1796. };
  1797. };
  1798. delete(tList);
  1799. };
  1800. if ( m_sCamData.eco_ir ) {
  1801. m_bShineOnTarget = (bHaveIROFF && bHaveIRAUTO);
  1802. } else m_bShineOnTarget = false;
  1803. return true;
  1804. };
  1805. return false;
  1806. };
  1807. void deviceNodeCamera::translateROI2PRESET( float P, float T, float Z, uint8_t &col, uint8_t &row ){
  1808. float kRange = 1.0f / ((float)m_sCamPresets.gRows);
  1809. row = (uint8_t) ( Z / kRange );
  1810. float kAz = 2.0f / ( (float) (m_sCamPresets.gCols * ( 1 << row )) );
  1811. row++;
  1812. P = 0.5f + ( -1.0f * P );
  1813. if ( P > 1.0f ) P = P - 2.0f;
  1814. P = P + 1.0f;
  1815. col = (uint8_t) ( P / kAz );
  1816. };
  1817. CSTR deviceNodeCamera::getPresetToken( uint8_t CCOL, uint8_t CROW ){
  1818. if( m_sCamPresets.presets.size() > 0 )
  1819. for (V_PRESETS::iterator idx = m_sCamPresets.presets.begin(); idx < m_sCamPresets.presets.end(); idx++)
  1820. if ( (*idx)->role == ARGUS_PRESET_TYPE_POSITION )
  1821. if ( ( (*idx)->row == CROW ) && ( (*idx)->col == CCOL ) ) return (CSTR)(*idx)->token;
  1822. return NULL;
  1823. };
  1824. CSTR deviceNodeCamera::getPresetToken( uint8_t specialRole ){
  1825. if( m_sCamPresets.presets.size() > 0 )
  1826. for ( V_PRESETS::iterator idx = m_sCamPresets.presets.begin(); idx < m_sCamPresets.presets.end(); idx++ )
  1827. if ( (*idx)->role == specialRole ) return (CSTR)(*idx)->token;
  1828. return NULL;
  1829. };
  1830. #undef LOG
  1831. #undef LOG_ERROR
  1832. #undef DEBUG_EXTRA