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.

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