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.

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