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.

1421 lines
53 KiB

​
5 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
5 years ago
​
​
5 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
5 years ago
​
​
​
​
​
​
​
​
​
​
​
​
5 years ago
5 years ago
5 years ago
​
5 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
5 years ago
​
​
​
​
​
​
​
​
5 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
5 years ago
​
​
​
​
​
​
5 years ago
​
​
​
​
5 years ago
​
5 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
5 years ago
​
​
​
​
5 years ago
​
5 years ago
​
5 years ago
​
​
​
​
5 years ago
​
​
​
​
​
​
5 years ago
5 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
5 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
5 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
5 years ago
​
​
​
5 years ago
​
​
​
5 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
5 years ago
​
5 years ago
​
5 years ago
​
5 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
5 years ago
​
​
​
​
​
​
​
​
​
​
5 years ago
​
​
​
​
​
​
5 years ago
​
​
​
​
​
​
5 years ago
​
​
​
​
​
​
5 years ago
​
​
​
​
5 years ago
5 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
5 years ago
​
​
​
​
5 years ago
​
​
​
​
5 years ago
​
​
​
​
​
5 years ago
​
​
​
​
​
5 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
5 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
5 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
5 years ago
5 years ago
​
​
​
​
5 years ago
​
5 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
5 years ago
​
5 years ago
​
​
​
​
​
​
​
5 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
5 years ago
​
​
5 years ago
5 years ago
​
​
​
​
​
​
5 years ago
​
​
5 years ago
​
​
​
​
​
​
5 years ago
​
​
5 years ago
​
​
​
​
5 years ago
​
​
5 years ago
​
​
​
​
​
5 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
5 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
5 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
5 years ago
​
5 years ago
5 years ago
​
​
​
5 years ago
​
​
5 years ago
​
​
​
​
​
5 years ago
​
​
5 years ago
​
​
​
5 years ago
​
​
5 years ago
​
​
​
​
​
​
5 years ago
​
​
​
​
​
​
​
​
​
5 years ago
​
​
5 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
5 years ago
5 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
5 years ago
​
5 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. #include "rgooseCamera.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 "rgooseKernel.h"
  10. #include "rgooseHardwareResourceSerialProvider.h"
  11. #include "rgooseModuleEmbedVideo.h"
  12. #define LOG( X ) m_pLog->Log( "RCAM", this, X )
  13. #ifdef DEBUG
  14. #define LOG_ERROR( X ) {\
  15. char _tBfrErrMSG2w34[256];\
  16. sprintf( _tBfrErrMSG2w34, "In file %s[%d] in func:%s %s",__FILE__, __LINE__, __func__, X );\
  17. m_pLog->Log( "RCAM", this, _LOG_ERROR, _tBfrErrMSG2w34 );\
  18. }
  19. #else
  20. #define LOG_ERROR( X ) m_pLog->Log( "RCAM", this, _LOG_ERROR, X );
  21. #endif
  22. // all sizes in mm
  23. 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 };
  24. 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 };
  25. 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 };
  26. #define CAM_FOCAL_LENGTH_CNT 49
  27. static const float cam_predef_focal_length[CAM_FOCAL_LENGTH_CNT]={
  28. 2.7f,
  29. 3.0f,
  30. 3.2f,
  31. 3.5f,
  32. 3.7f,
  33. 3.9f,
  34. 4.0f,
  35. 4.3f,
  36. 4.8f,
  37. 5.0f,
  38. 5.1f,
  39. 5.5f,
  40. 5.8f,
  41. 6.0f,
  42. 6.2f,
  43. 6.6f,
  44. 7.0f,
  45. 7.1f,
  46. 7.5f,
  47. 7.6f,
  48. 8.0f,
  49. 8.6f,
  50. 8.9f,
  51. 9.0f,
  52. 9.2f,
  53. 9.3f,
  54. 10.0f,
  55. 10.5f,
  56. 10.6f,
  57. 10.7f,
  58. 11.0f,
  59. 11.3f,
  60. 12.0f,
  61. 12.9f,
  62. 13.0f,
  63. 13.5f,
  64. 13.6f,
  65. 14.0f,
  66. 14.6f,
  67. 15.0f,
  68. 15.1f,
  69. 15.5f,
  70. 16.0f,
  71. 17.0f,
  72. 17.5f,
  73. 17.7f,
  74. 18.0f,
  75. 18.2f,
  76. 19.0f
  77. };
  78. ARGUS_Camera::ARGUS_Camera( uint32_t NodeID ): argusNetNode( NodeID ), basicThread(){
  79. m_pLog = basicLogCollection::getInstance();
  80. m_pBEH = basicEventHeap::getInstance();
  81. m_tTimeTSeconds = 0;
  82. m_pProtocol = NULL;
  83. m_pTransport = NULL;
  84. m_pCurrentROI = NULL;
  85. m_nRealFrameWidth = 640;
  86. //m_bState = 0;
  87. //m_bBusy = true;
  88. m_fMaxRange = m_fMinHFov = m_fMaxHFov = m_fMinVFov = m_fMaxVFov = .0f;
  89. m_nRelativeMoveCounter = 0;
  90. m_bRelativeMoveMatrix = 0;
  91. m_fSpeedRise = 0.03f;
  92. m_fRM_Pan = m_fRM_Tilt = m_fRM_Zoom = .0f;
  93. //m_bSourceConnected = false;
  94. //m_pSource = NULL;
  95. };
  96. ARGUS_Camera::ARGUS_Camera( DBC_NODE_DATA *Data ): argusNetNode( Data ), basicThread(){
  97. m_pLog = basicLogCollection::getInstance();
  98. m_pBEH = basicEventHeap::getInstance();
  99. m_tTimeTSeconds = 0;
  100. m_pProtocol = NULL;
  101. m_pTransport = NULL;
  102. m_pCurrentROI = NULL;
  103. m_nRealFrameWidth = 640;
  104. //m_bState = 0;
  105. //m_bBusy = true;
  106. m_fMaxRange = m_fMinHFov = m_fMaxHFov = m_fMinVFov = m_fMaxVFov = .0f;
  107. m_nRelativeMoveCounter = 0;
  108. m_bRelativeMoveMatrix = 0;
  109. m_fSpeedRise = 0.03f;
  110. m_fRM_Pan = m_fRM_Tilt = m_fRM_Zoom = .0f;
  111. //m_bSourceConnected = false;
  112. //m_pSource = NULL;
  113. };
  114. ARGUS_Camera::~ARGUS_Camera(){
  115. LOG("Destruct");
  116. //m_mQPROTECTOR.unlock();
  117. waitThreadEnd( true );
  118. LOG("Thread Del OK");
  119. if( m_pTransport!= NULL ) {
  120. if( m_sCamData.uart_id ) {
  121. DBC_UART_DATA *tData = NULL;
  122. tData = m_pDBC->getUartData( m_sCamData.uart_id );
  123. switch( tData->type ) {
  124. case UART_TYPE_HARDWARE:{
  125. delete((serialPort*)m_pTransport);
  126. break;
  127. };
  128. case UART_TYPE_SOL:{
  129. delete((serialSOL*)m_pTransport);
  130. break;
  131. };
  132. };
  133. }
  134. };
  135. LOG("Transport Del OK");
  136. if( m_pProtocol != NULL ) {
  137. switch( m_sCamData.protocol ) {
  138. case CAM_CTRL_PROTO_VISCA:
  139. delete( (protocolPTZ_VISCA *) m_pProtocol );
  140. break;
  141. case CAM_CTRL_PROTO_PELCO:
  142. delete( (protocolPTZ_PELCO *) m_pProtocol );
  143. break;
  144. case CAM_CTRL_PROTO_ONVIF:
  145. delete( (protocolPTZ_ONVIF *) m_pProtocol );
  146. break;
  147. default:
  148. delete( m_pProtocol );
  149. };
  150. };
  151. LOG("Proto Del OK");
  152. //if( m_pPosition != NULL ) delete( m_pPosition );
  153. //LOG("Pos Del OK");
  154. //if( m_bSourceConnected ){
  155. // if( m_pSource != NULL ){
  156. // m_pSource->_close();
  157. // delete( m_pSource );
  158. // };
  159. //};
  160. //LOG("Stream Del OK");
  161. };
  162. void ARGUS_Camera::updateOpticalParameters( uint16_t frame_width, uint16_t frame_heigth ){
  163. #ifdef DEBUG
  164. char tB[128];
  165. #endif
  166. if( frame_width > 0 && frame_heigth > 0 ) {
  167. m_nRealFrameWidth = frame_width;
  168. m_nRealFrameHeight = frame_heigth;
  169. float tFrameDiag = sqrtf( m_nRealFrameHeight*m_nRealFrameHeight+m_nRealFrameWidth*m_nRealFrameWidth );
  170. switch( m_sCamData.zoom_model ) {
  171. case 0:{
  172. m_fMinFL = cam_predef_focal_length[m_sCamData.lens_f];
  173. m_fMinRange = calcMinDistance_mm( AC_imager_diag[m_sCamData.imager_type], m_fMinFL, tFrameDiag );
  174. m_fMaxFL = cam_predef_focal_length[m_sCamData.lens_f] * m_sCamData.zoom;
  175. m_fMaxRange = calcMaxDistance_mm( AC_imager_diag[m_sCamData.imager_type], m_fMaxFL, tFrameDiag );
  176. if( m_fMaxRange > m_fMinRange ){
  177. m_fR2FL = ((m_fMaxFL - m_fMinFL) / (m_fMaxRange - m_fMinRange));
  178. } else {
  179. m_fR2FL = .0f;
  180. };
  181. break;
  182. };
  183. case 1:{
  184. // Fixed Zoom
  185. m_fMaxFL = m_fMinFL = cam_predef_focal_length[m_sCamData.lens_f];
  186. m_fMinRange = calcMinDistance_mm( AC_imager_diag[m_sCamData.imager_type], m_fMinFL, tFrameDiag );
  187. m_fMaxRange = calcMaxDistance_mm( AC_imager_diag[m_sCamData.imager_type], m_fMaxFL, tFrameDiag );
  188. m_fR2FL = .0f;
  189. break;
  190. };
  191. };
  192. m_pDBC->updateCamRange( m_sCamData.id, m_fMaxRange );
  193. m_fMinHFov = rad2deg(calcFOV_mm( AC_imager_width[m_sCamData.imager_type], m_fMinFL ));
  194. m_fMaxHFov = rad2deg(calcFOV_mm( AC_imager_width[m_sCamData.imager_type], m_fMaxFL ));
  195. m_fMinVFov = rad2deg(calcFOV_mm( AC_imager_height[m_sCamData.imager_type], m_fMinFL ));
  196. m_fMaxVFov = rad2deg(calcFOV_mm( AC_imager_height[m_sCamData.imager_type], m_fMaxFL ));
  197. #ifdef DEBUG
  198. LOG("Set Optical Parameters");
  199. sprintf( tB, "\tCapturing Frame: %dx%d ", frame_width, frame_heigth );
  200. LOG(tB);
  201. sprintf( tB, "\tMinimal FL: %0.3fmm. Maximal FL: %0.3fmm. ", m_fMinFL, m_fMaxFL );
  202. LOG(tB);
  203. sprintf( tB, "\tMinimal Range: %0.2fm. Maximal Range: %0.2fm. ", m_fMinRange, m_fMaxRange );
  204. LOG(tB);
  205. sprintf( tB, "\tMaximal FOVs, Horizontal: %0.2f Vertical: %0.2f", m_fMinHFov, m_fMinVFov );
  206. LOG(tB);
  207. sprintf( tB, "\tMinimal FOVs, Horizontal: %0.2f Vertical: %0.2f", m_fMaxHFov, m_fMaxVFov );
  208. LOG(tB);
  209. #endif
  210. };
  211. };
  212. bool ARGUS_Camera::create(){
  213. #ifdef DEBUG
  214. char tB[128];
  215. #endif
  216. setWM( CAM_WS_INIT );
  217. if( m_sNodeData.aux_id == 0 ) return false;
  218. DBC_CAM_DATA * tData = m_pDBC->getCamData( m_sNodeData.aux_id );
  219. if( tData != NULL ) {
  220. memcpy( &m_sCamData, tData, sizeof(DBC_CAM_DATA) );
  221. free( tData );
  222. } else {
  223. LOG_ERROR("Cannot Fetch CAM Data from Database!");
  224. return false;
  225. };
  226. if( !( m_sCamData.lens_f < CAM_FOCAL_LENGTH_CNT ) ) m_sCamData.lens_f = CAM_FOCAL_LENGTH_CNT - 1;
  227. updateOpticalParameters( 640,480 );
  228. if( m_sCamData.ptz_speed_step > .0f ) {
  229. m_fSpeedRise = m_sCamData.ptz_speed_step;
  230. };
  231. //m_pPosition = new basicPosition();// m_sCamData.lat, m_sCamData.lon, m_sCamData.alt, m_sCamData.roll, m_sCamData.pitch, m_sCamData.yaw );
  232. //if( m_pPosition == NULL ) {
  233. // LOG_ERROR("Position Constructor Failure!");
  234. // return false;
  235. //};
  236. //m_oPosition.setPositionXYZ( m_sCamData.x, m_sCamData.y, m_sCamData.z );
  237. //m_oPosition.setOrientationDeg( m_sCamData.x_rot, m_sCamData.y_rot, m_sCamData.z_rot );
  238. systemAnchor *tSYS = systemAnchor::getInstance();
  239. if ( tSYS == NULL ) {
  240. LOG_ERROR("Can not get System Anchor!");
  241. return false;
  242. };
  243. #ifdef DEBUG
  244. sprintf( tB, "Pos: x:%.3f y:%.3f z:%.3f", m_sCamData.x , m_sCamData.y, m_sCamData.z );
  245. LOG( tB );
  246. #endif
  247. if( tSYS->beLocal() ){
  248. m_oPosition.setPositionXYZ( m_sCamData.x, m_sCamData.y, m_sCamData.z, 0 );
  249. } else {
  250. C3D_DECART tpp = tSYS->getXYZ();
  251. m_oPosition.setPositionXYZ( tpp.x + m_sCamData.x, tpp.y + m_sCamData.y, tpp.z + m_sCamData.z, tSYS->getUTMZone() );
  252. };
  253. m_oPosition.setOrientationDeg( m_sCamData.x_rot , m_sCamData.y_rot, m_sCamData.z_rot );
  254. m_oPosition.updateMatrix();
  255. #ifdef DEBUG
  256. sprintf( tB, "Finalized Sensor Location: Lat:%.3f Lon:%.3f Alt:%.3f", m_oPosition.getLat(), m_oPosition.getLon(), m_oPosition.getAlt() );
  257. LOG( tB );
  258. sprintf( tB, "Finalized Sensor Location: x:%.3f y:%.3f z:%.3f", m_oPosition.getX(), m_oPosition.getY(), m_oPosition.getZ() );
  259. LOG( tB );
  260. sprintf( tB, "Orientation: x:%.3f y:%.3f z:%.3f", m_oPosition.getRotX_deg(), m_oPosition.getRotY_deg(), m_oPosition.getRotZ_deg() );
  261. LOG( tB );
  262. #endif
  263. if( m_pProtocol != NULL ){
  264. LOG_ERROR("Cannot Start: Service Already Running!");
  265. return false;
  266. };
  267. switch( m_sCamData.protocol ){
  268. case CAM_CTRL_PROTO_VISCA: {
  269. #ifdef DEBUG
  270. LOG("VISCA");
  271. #endif
  272. m_pProtocol = (protocolPTZ *) new protocolPTZ_VISCA( m_sCamData.protocol_aux );
  273. if( m_pProtocol!= NULL ) {
  274. argusKernel * tKRNL = argusKernel::getInstance();
  275. basicTransport * tTransport = NULL;
  276. if( tKRNL!= NULL ) {
  277. argusHardwareManager * tHM = tKRNL->getHardwareManager();
  278. if ( tHM ) {
  279. argusHardwareResource * tRES = tHM->requestResource( ARGUS_HARDWARE_PROVIDER_SERIAL, m_sCamData.uart_id );
  280. if( tRES != NULL ) {
  281. if( tRES->isValid()) {
  282. tTransport = static_cast<basicTransport *>(static_cast<argusHardwareResourceSerialProvider *>(tRES));
  283. if( m_pProtocol->setTransport( tTransport )){
  284. if( !m_pProtocol->create() ){
  285. LOG_ERROR("Protocol <<< Create >>> Failure!");
  286. };
  287. } else {
  288. LOG_ERROR("Binding Port Failure!");
  289. };
  290. };
  291. }
  292. #ifdef DEBUG
  293. else {
  294. LOG("HWM Has No UART PORT!");
  295. }
  296. #endif
  297. ;
  298. };
  299. }
  300. #ifdef DEBUG
  301. else LOG_ERROR("Can not get Kernel!");
  302. #endif
  303. if( tTransport!= NULL ) {
  304. LOG( "Bind Transport Port!" );
  305. } else {
  306. LOG_ERROR("Transport Creation Failure!");
  307. delete( ( protocolPTZ_VISCA * )m_pProtocol );
  308. m_pProtocol = NULL;
  309. }
  310. }
  311. #ifdef DEBUG
  312. else LOG_ERROR("Protocol NEW failure!");
  313. #endif
  314. break;
  315. };
  316. case CAM_CTRL_PROTO_PELCO:{
  317. #ifdef DEBUG
  318. LOG("PELCO");
  319. #endif
  320. m_pProtocol = (protocolPTZ *) new protocolPTZ_PELCO( m_sCamData.protocol_aux );
  321. if( m_pProtocol!= NULL ) {
  322. argusKernel * tKRNL = argusKernel::getInstance();
  323. basicTransport * tTransport = NULL;
  324. if( tKRNL!= NULL ) {
  325. argusHardwareManager * tHM = tKRNL->getHardwareManager();
  326. if ( tHM ) {
  327. argusHardwareResource * tRES = tHM->requestResource( ARGUS_HARDWARE_PROVIDER_SERIAL, m_sCamData.uart_id );
  328. if( tRES != NULL ) {
  329. if( tRES->isValid()) {
  330. tTransport = static_cast<basicTransport *>(static_cast<argusHardwareResourceSerialProvider *>(tRES));
  331. if( m_pProtocol->setTransport( tTransport )){
  332. if( !m_pProtocol->create() ){
  333. LOG_ERROR("Protocol <<< Create >>> Failure!");
  334. };
  335. } else {
  336. LOG_ERROR("Binding Port Failure!");
  337. };
  338. };
  339. }
  340. #ifdef DEBUG
  341. else {
  342. LOG("HWM Has No UART PORT!");
  343. }
  344. #endif
  345. ;
  346. };
  347. }
  348. #ifdef DEBUG
  349. else LOG_ERROR("Can not get Kernel!");
  350. #endif
  351. if( tTransport!= NULL ) {
  352. LOG( "Bind Transport Port!" );
  353. } else {
  354. LOG_ERROR("Transport Creation Failure!");
  355. delete( ( protocolPTZ_PELCO * )m_pProtocol );
  356. m_pProtocol = NULL;
  357. }
  358. }
  359. #ifdef DEBUG
  360. else LOG_ERROR("Protocol NEW failure!");
  361. #endif
  362. break;
  363. };
  364. case CAM_CTRL_PROTO_ONVIF:{
  365. #ifdef DEBUG
  366. LOG("ONVIF");
  367. #endif
  368. m_pProtocol = (protocolPTZ *) new protocolPTZ_ONVIF( m_sCamData.ip, m_sCamData.username, m_sCamData.password, m_sCamData.auth_mode );
  369. if( m_pProtocol!=NULL ) {
  370. if( !m_pProtocol->create() ) {
  371. LOG_ERROR("Error Creating Proto!");
  372. delete( (protocolPTZ_ONVIF*) m_pProtocol );
  373. m_pProtocol = NULL;
  374. };
  375. };
  376. break;
  377. };
  378. default:{
  379. #ifdef DEBUG
  380. LOG("Camera Has no defined control protocol");
  381. #endif
  382. };
  383. };
  384. if( m_pProtocol == NULL ) {
  385. if(m_sCamData.protocol == 0 ) {
  386. m_sCamData.online = true;
  387. } else LOG_ERROR("No Proto Created!");
  388. } else {
  389. #ifdef DEBUG
  390. LOG("Proto is OK");
  391. #endif
  392. m_sCamData.online = init();
  393. };
  394. return m_sCamData.online;
  395. };
  396. //void ARGUS_Camera::setBusy( bool Status ){
  397. //m_bBusy = Status;
  398. //};
  399. bool ARGUS_Camera::relativeMove( float PanSPD, float TiltSPD, float ZoomSPD ){
  400. if( m_sCamData.online ) {
  401. uint32_t MoveMatrix = 0;
  402. if( PanSPD != .0f ){
  403. if( PanSPD > 0 ) {
  404. MoveMatrix |= PTZ_DRIVE_PAN_RIGHT;
  405. } else {
  406. MoveMatrix |= PTZ_DRIVE_PAN_LEFT;
  407. };
  408. };
  409. if( TiltSPD != .0f ){
  410. if( TiltSPD > 0 ) {
  411. MoveMatrix |= PTZ_DRIVE_TILT_UP;
  412. } else {
  413. MoveMatrix |= PTZ_DRIVE_TILT_DOWN;
  414. };
  415. };
  416. if( ZoomSPD != .0f ){
  417. if( ZoomSPD > 0 ) {
  418. MoveMatrix |= PTZ_DRIVE_ZOOM_WIDE;
  419. } else {
  420. MoveMatrix |= PTZ_DRIVE_ZOOM_TELE;
  421. };
  422. };
  423. if( MoveMatrix != 0 ) {
  424. m_pProtocol->setPTZSpeed( fabs(PanSPD), fabs(TiltSPD), fabs(ZoomSPD) );
  425. };
  426. return m_pProtocol->move( MoveMatrix );
  427. };
  428. return false;
  429. };
  430. bool ARGUS_Camera::relativeStop(){
  431. if( m_sCamData.online ) {
  432. return m_pProtocol->move( 0 );
  433. };
  434. return false;
  435. };
  436. CAM_TASK *ARGUS_Camera::fetchCamTask( void ){
  437. CAM_TASK *rVal = NULL;
  438. m_mQPROTECTOR.lock();
  439. if (m_qCAM_TASK.size()) {
  440. rVal = m_qCAM_TASK.front();
  441. m_qCAM_TASK.pop();
  442. };
  443. m_mQPROTECTOR.unlock();
  444. return rVal;
  445. };
  446. bool ARGUS_Camera::pushCamTask( CAM_TASK *ptr ){
  447. if ( ptr == NULL ) return false;
  448. m_mQPROTECTOR.lock();
  449. m_qCAM_TASK.push( ptr );
  450. m_mQPROTECTOR.unlock();
  451. return true;
  452. };
  453. bool ARGUS_Camera::fusion_compare( CAM_ROI *ptr, C3D_SPH_DEGREES tp ){
  454. if( ptr == NULL ) return false;
  455. float tFI = fabsf( ptr->cam_position.fi - tp.fi );
  456. float tTETTA = fabsf( ptr->cam_position.tetta - tp.tetta );
  457. #ifdef DEBUG
  458. char tB[128];
  459. 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 );
  460. LOG(tB);
  461. #endif
  462. if( tFI > ptr->h_fov ) return false;
  463. if( tTETTA > ptr->v_fov ) return false;
  464. if( tp.r < ptr->min_range ) return false;
  465. if( tp.r > ptr->max_range ) return false;
  466. return true;
  467. };
  468. bool ARGUS_Camera::fusion_consumeData( uint32_t db_id, C3D_SPH_DEGREES tOR ){
  469. if( db_id == 0 ) return false;
  470. CAM_TRACK_RECORD * tTrackRecords = (CAM_TRACK_RECORD * ) malloc( sizeof(CAM_TRACK_RECORD));
  471. if( tTrackRecords == NULL ) return false;
  472. #ifdef DEBUG
  473. LOG("Fusion: Consuming new Data!");
  474. #endif
  475. memset(tTrackRecords, 0, sizeof(CAM_TRACK_RECORD));
  476. tTrackRecords->target_direction.fi = tOR.fi;
  477. tTrackRecords->target_direction.r = tOR.r;
  478. tTrackRecords->target_direction.tetta = tOR.tetta;
  479. tTrackRecords->evt_db_ID = db_id;
  480. tTrackRecords->ts_ms = getMilliseconds();
  481. m_mROIsProtector.lock();
  482. if( m_vROIS.size() ){
  483. vCAM_ROIS::iterator idx = m_vROIS.begin();
  484. for( ; idx < m_vROIS.end(); idx++ ) if ( fusion_compare( *idx, tOR ) ) break;
  485. if ( idx != m_vROIS.end() ) {
  486. // have Match!
  487. LOG("ROI MATCHED!");
  488. (*idx)->targets.push_back( tTrackRecords );
  489. fusion_validateROI( tTrackRecords->ts_ms, (*idx) );
  490. } else {
  491. LOG("NO MATCH FOUND!");
  492. // No mathced ROI -> create new one
  493. CAM_ROI * tROI = (CAM_ROI *)malloc(sizeof(CAM_ROI));
  494. if ( tROI ) {
  495. memset( tROI, 0 , sizeof(CAM_ROI) );
  496. tROI->ts_ms = tTrackRecords->ts_ms;
  497. spherical2onvif( tOR, tROI->cam_orientation, m_fMaxRange, m_sCamData.pan_model, m_sCamData.pan_angles, m_sCamData.tilt_model, m_sCamData.tilt_angles );
  498. tROI->cam_position.fi = tOR.fi;
  499. tROI->cam_position.tetta = tOR.tetta;
  500. tROI->cam_position.r = tOR.r;
  501. float cFL = m_fMinFL + m_fR2FL * ( tROI->cam_position.r - m_fMinRange );
  502. tROI->max_range = calcMaxDistance_mm( AC_imager_diag[m_sCamData.imager_type], cFL, m_nRealFrameWidth );
  503. tROI->min_range = calcMinDistance_mm( AC_imager_diag[m_sCamData.imager_type], cFL, m_nRealFrameWidth );
  504. tROI->h_fov = rad2deg( calcFOV_mm( AC_imager_width[m_sCamData.imager_type], cFL ) ) / 2.0f; // div to no to div in comparation function
  505. tROI->v_fov = rad2deg( calcFOV_mm( AC_imager_height[m_sCamData.imager_type], cFL ) ) / 2.0f; // that will be called more ofthen
  506. tROI->targets.push_back( tTrackRecords );
  507. m_vROIS.push_back( tROI );
  508. };
  509. };
  510. } else {
  511. // empty ROIs -> create new one
  512. CAM_ROI * tROI = (CAM_ROI *)malloc(sizeof(CAM_ROI));
  513. if ( tROI ) {
  514. memset( tROI, 0 , sizeof(CAM_ROI) );
  515. tROI->ts_ms = tTrackRecords->ts_ms;
  516. //Set vector Cam Coordinates
  517. tROI->cam_position.fi = tOR.fi;
  518. tROI->cam_position.tetta = tOR.tetta;
  519. tROI->cam_position.r = tOR.r;
  520. // Set vector OnVIF coordinates
  521. spherical2onvif( tOR, tROI->cam_orientation, m_fMaxRange, m_sCamData.pan_model, m_sCamData.pan_angles, m_sCamData.tilt_model, m_sCamData.tilt_angles );
  522. // make fusion data BOX
  523. float cFL = m_fMinFL + m_fR2FL * ( tROI->cam_position.r );// - m_fMinRange );
  524. tROI->min_range = calcMinDistance_mm( AC_imager_diag[m_sCamData.imager_type], cFL, m_nRealFrameWidth );
  525. tROI->max_range = calcMaxDistance_mm( AC_imager_diag[m_sCamData.imager_type], cFL, m_nRealFrameWidth );
  526. tROI->h_fov = rad2deg( calcFOV_mm( AC_imager_width[m_sCamData.imager_type], cFL ) )/ 2.0f; // div to prevent division in comparation
  527. tROI->v_fov = rad2deg( calcFOV_mm( AC_imager_height[m_sCamData.imager_type], cFL ) )/ 2.0f; // function that will be called more ofthen
  528. // Add ROI to vector
  529. tROI->targets.push_back( tTrackRecords );
  530. m_vROIS.push_back( tROI );
  531. };
  532. };
  533. m_mROIsProtector.unlock();
  534. return true;
  535. };
  536. bool ARGUS_Camera::fusion_validateROI( uint64_t cts, CAM_ROI *ptr ){
  537. if( ptr == NULL ) return false;
  538. if( (cts - ptr->ts_ms) > LOSE_INTERES_TIME_MS ) return false;
  539. #ifdef DEBUG
  540. char tB[128];
  541. sprintf( tB, "\tROI [%d]", ptr->targets.size() );
  542. LOG( tB );
  543. #endif
  544. if( ptr->targets.size() ) {
  545. double tA = 0.0;
  546. double tB = 0.0;
  547. double tC = 0.0;
  548. uint32_t tCNT = 0;
  549. for( vCAM_TRACK_RECORDS::iterator idx = ptr->targets.begin(); idx < ptr->targets.end(); idx++ ) {
  550. if( ( cts - (*idx)->ts_ms ) > LOSE_INTERES_TIME_MS ) {
  551. free( (*idx) );
  552. ptr->targets.erase( idx );
  553. } else {
  554. tA = tA + (*idx)->target_direction.fi;
  555. tB = tB + (*idx)->target_direction.tetta;
  556. tC = tC + (*idx)->target_direction.r;
  557. tCNT++;
  558. };
  559. };
  560. if( tCNT ) {
  561. C3D_SPH_DEGREES L = { 0 };
  562. L.fi = (float) ( tA /(double) tCNT );
  563. L.tetta = (float) ( tB /(double) tCNT );
  564. L.r = (float) ( tC /(double) tCNT );
  565. spherical2onvif(L, ptr->cam_orientation, m_fMaxRange, m_sCamData.pan_model, m_sCamData.pan_angles, m_sCamData.tilt_model, m_sCamData.tilt_angles);
  566. ptr->ts_ms = cts;
  567. float cFL = m_fMinFL + m_fR2FL * ( ptr->cam_position.r - m_fMinRange );
  568. ptr->max_range = calcMaxDistance_mm( AC_imager_diag[m_sCamData.imager_type], cFL, m_nRealFrameWidth );
  569. ptr->min_range = calcMinDistance_mm( AC_imager_diag[m_sCamData.imager_type], cFL, m_nRealFrameWidth );
  570. ptr->h_fov = rad2deg( calcFOV_mm(AC_imager_width[m_sCamData.imager_type], cFL) )/ 2.0f; // div to prevent division in comparation
  571. ptr->v_fov = rad2deg( calcFOV_mm( AC_imager_height[m_sCamData.imager_type], cFL ) )/ 2.0f; // function that will be called more ofthen
  572. return true;
  573. };
  574. };
  575. return false;
  576. };
  577. CAM_ROI *ARGUS_Camera::fusion_fetchROI( void ){
  578. CAM_ROI *tVAL = NULL;
  579. uint64_t cts = 0;
  580. m_mROIsProtector.lock();
  581. vCAM_ROIS::iterator sRec;
  582. if( m_vROIS.size() ){
  583. for( vCAM_ROIS::iterator idx = m_vROIS.begin(); idx < m_vROIS.end(); idx++ ){
  584. if ( tVAL == NULL ) {
  585. sRec = idx;
  586. tVAL = (*idx);
  587. cts = tVAL->ts_ms;
  588. } else {
  589. #ifdef CAM_FETCH_ROI_NEWEST
  590. if ( (*idx)->ts_ms > cts )
  591. #else
  592. if ( (*idx)->ts_ms < cts )
  593. #endif
  594. {
  595. sRec = idx;
  596. tVAL = (*idx);
  597. cts = tVAL->ts_ms;
  598. };
  599. };
  600. };
  601. m_vROIS.erase( sRec );
  602. };
  603. m_mROIsProtector.unlock();
  604. return tVAL;
  605. };
  606. DBC_SNAPSHOT_DATA *ARGUS_Camera::fusion_translate_ROI( CAM_ROI *(&ptr) ){
  607. DBC_SNAPSHOT_DATA *rVal = NULL;
  608. if( ptr == NULL ) return NULL;
  609. if( ptr->targets.size() ){
  610. //if( m_bSourceConnected ){
  611. rVal = (DBC_SNAPSHOT_DATA *)malloc(sizeof(DBC_SNAPSHOT_DATA));
  612. if( rVal ){
  613. memset( rVal, 0, sizeof(DBC_SNAPSHOT_DATA) );
  614. //rVal->stream_id = m_nActiveStreamID;
  615. rVal->stream_id = m_sNodeData.aux_parameter;
  616. for( vCAM_TRACK_RECORDS::iterator idx = ptr->targets.begin(); idx < ptr->targets.end(); idx++ ) {
  617. rVal->assoc_event_ids.push_back( (*idx)->evt_db_ID );
  618. };
  619. };
  620. //};
  621. ptr->targets.clear();
  622. };
  623. // Destruct ROI MEM
  624. delete( (CAM_ROI *) ptr );
  625. ptr = NULL;
  626. // Not Setting MD5 and TS -> will set at capture time
  627. return rVal;
  628. };
  629. void ARGUS_Camera::fusion_validate( void ){
  630. m_mROIsProtector.lock();
  631. if( m_vROIS.size() ){
  632. #ifdef DEBUG
  633. char tB[128];
  634. sprintf( tB, "Fusion: Have [%d] Regions Of Interes", m_vROIS.size() );
  635. LOG( tB );
  636. #endif
  637. uint64_t cts = getMilliseconds();
  638. for( vCAM_ROIS::iterator idx = m_vROIS.begin(); idx < m_vROIS.end(); idx++ ){
  639. if( !fusion_validateROI( cts, *idx ) ) {
  640. free( (*idx) );
  641. m_vROIS.erase( idx );
  642. };
  643. };
  644. };
  645. m_mROIsProtector.unlock();
  646. };
  647. bool ARGUS_Camera::absoluteMove( uint32_t TargetRefID, float PanPOS, float TiltPOS, float ZoomPOS ){
  648. if( m_sCamData.online ){
  649. CAM_TASK *tTask = (CAM_TASK *)malloc(sizeof(CAM_TASK));
  650. if( tTask ){
  651. memset(tTask, 0, sizeof(CAM_TASK));
  652. tTask->CMD = (CAM_PTZ_CMD *)malloc(sizeof(CAM_PTZ_CMD));
  653. if( tTask->CMD ){
  654. tTask->CMD->CMD = PTZ_ABSOLUTE;
  655. tTask->CMD->PAN = PanPOS;
  656. tTask->CMD->TILT = TiltPOS;
  657. tTask->CMD->ZOOM = ZoomPOS;
  658. tTask->movemet_needed = true;
  659. tTask->source_evt_id = TargetRefID;
  660. setWM( CAM_WS_BUSY );
  661. return pushCamTask( tTask );
  662. } else {
  663. LOG_ERROR("CMD malloc Error!");
  664. free( tTask );
  665. };
  666. } else {
  667. LOG_ERROR("Task malloc error!");
  668. };
  669. };
  670. return false;
  671. };
  672. bool ARGUS_Camera::absoluteMove( uint32_t TargetRefID, C3D_ORIENT_ONVIF O ){
  673. if( m_sCamData.online ){
  674. CAM_TASK *tTask = (CAM_TASK *)malloc(sizeof(CAM_TASK));
  675. if( tTask ){
  676. memset(tTask, 0, sizeof(CAM_TASK));
  677. tTask->CMD = (CAM_PTZ_CMD *)malloc(sizeof(CAM_PTZ_CMD));
  678. if( tTask->CMD ){
  679. tTask->CMD->CMD = PTZ_ABSOLUTE;
  680. tTask->CMD->PAN = O.pan;
  681. tTask->CMD->TILT = O.tilt;
  682. tTask->CMD->ZOOM = O.zoom;
  683. tTask->movemet_needed = true;
  684. tTask->source_evt_id = TargetRefID;
  685. setWM( CAM_WS_BUSY );
  686. return pushCamTask( tTask );
  687. } else {
  688. LOG_ERROR("CMD malloc Error!");
  689. free( tTask );
  690. };
  691. } else {
  692. LOG_ERROR("Task malloc error!");
  693. };
  694. };
  695. return false;
  696. };
  697. void ARGUS_Camera::m_fThreadProc( void ){
  698. DBC_PUSHED_CMD * PUSHED = NULL;
  699. m_bThreadStatus = TH_WORKING;
  700. while( m_bThreadStatus < TH_ENDED ) {
  701. //Local Recorder
  702. //if( m_bSourceConnected ) {
  703. //
  704. // };
  705. uint8_t tState = getWM();
  706. char tB[128];
  707. //sprintf( tB, "CWM: 0x%02X", tState );
  708. //LOG( tB );
  709. fusion_validate();
  710. switch( tState ) {
  711. case CAM_WS_INIT:{
  712. #ifdef DEBUG
  713. LOG("Init Camera - PowerMode");
  714. #endif
  715. //setBusy( true );
  716. if( m_pProtocol->initCamera() ) setWM( CAM_WS_WHITE_BALLANCE );
  717. //setBusy( false );
  718. break;
  719. };
  720. case CAM_WS_WHITE_BALLANCE:{
  721. #ifdef DEBUG
  722. LOG("Set WB");
  723. #endif
  724. //setBusy( true );
  725. if( m_pProtocol->setWhiteBalanceMode( WB_AUTO ) ) setWM( CAM_WS_BACKLIGHT );
  726. //setBusy( false );
  727. break;
  728. };
  729. case CAM_WS_BACKLIGHT:{
  730. #ifdef DEBUG
  731. LOG("Set BLC");
  732. #endif
  733. //setBusy( true );
  734. if( m_pProtocol->setBackLightMode( true ) ) setWM( CAM_WS_AUTO_FOCUS );
  735. //setBusy( false );
  736. break;
  737. };
  738. case CAM_WS_AUTO_FOCUS:{
  739. #ifdef DEBUG
  740. LOG("Set AF");
  741. #endif
  742. //setBusy( true );
  743. if( m_pProtocol->setAutoFocus( true ) ) setWM( CAM_WS_INIT_GO_HOME );
  744. //setBusy( false );
  745. break;
  746. };
  747. case CAM_WS_INIT_GO_HOME:{
  748. #ifdef DEBUG
  749. LOG("INIT GO -1 -1 -1");
  750. #endif
  751. //setBusy( true );
  752. if( m_pProtocol->goAbsolute( -1, 0, -1 ) ) setWM( CAM_WS_READY );
  753. //setBusy( false );
  754. break;
  755. };
  756. case CAM_WS_AUTOMATED_MOVE:{
  757. m_nRelativeMoveCounter++;
  758. if( ( m_bRelativeMoveMatrix & 0x03 ) != 0 ) {
  759. //Pan Move
  760. if( ( m_bRelativeMoveMatrix & 0x01 ) == 0 ) {
  761. m_fRM_Pan -= m_fSpeedRise;
  762. } else {
  763. m_fRM_Pan += m_fSpeedRise;
  764. };
  765. };
  766. if( ( m_bRelativeMoveMatrix & 0x0C ) != 0 ) {
  767. //TILT Move
  768. if( ( m_bRelativeMoveMatrix & 0x04 ) == 0 ) {
  769. m_fRM_Tilt -= m_fSpeedRise;
  770. } else {
  771. m_fRM_Tilt += m_fSpeedRise;
  772. }
  773. };
  774. if( ( m_bRelativeMoveMatrix & 0x30 ) != 0 ) {
  775. //ZOOM Move
  776. if( ( m_bRelativeMoveMatrix & 0x10 ) == 0 ) {
  777. //Wide
  778. m_fRM_Zoom -= m_fSpeedRise;
  779. } else {
  780. //Tele
  781. m_fRM_Zoom += m_fSpeedRise;
  782. };
  783. };
  784. if( m_fRM_Pan < -1.0f ) m_fRM_Pan = -1.0f;
  785. if( m_fRM_Pan > 1.0f ) m_fRM_Pan = 1.0f;
  786. if( m_fRM_Tilt < -1.0f ) m_fRM_Tilt = -1.0f;
  787. if( m_fRM_Tilt > 1.0f ) m_fRM_Tilt = 1.0f;
  788. if( m_fRM_Zoom < -1.0f ) m_fRM_Zoom = -1.0f;
  789. if( m_fRM_Zoom > 1.0f ) m_fRM_Zoom = 1.0f;
  790. if( !relativeMove( m_fRM_Pan, m_fRM_Tilt, m_fRM_Zoom ) ){
  791. m_nRelativeMoveCounter = 0;
  792. setWM( CAM_WS_READY );
  793. break;
  794. };
  795. if( m_nRelativeMoveCounter > 10 ) {
  796. m_nRelativeMoveCounter = 0;
  797. setWM( CAM_WS_READY );
  798. };
  799. break;
  800. };
  801. case CAM_WS_STOP_MOVE:{
  802. m_nRelativeMoveCounter++;
  803. m_fRM_Pan = .0f;
  804. m_fRM_Pan = .0f;
  805. m_fRM_Tilt = .0f;
  806. m_fRM_Tilt = .0f;
  807. m_fRM_Zoom = .0f;
  808. m_fRM_Zoom = .0f;
  809. if( relativeStop() ) {
  810. m_nRelativeMoveCounter = 0;
  811. setWM( CAM_WS_READY );
  812. break;
  813. };
  814. if( m_nRelativeMoveCounter > 10 ) setWM( CAM_WS_READY );
  815. break;
  816. };
  817. case CAM_WS_GO_ABS_POSITION:{
  818. setWM( CAM_WS_BUSY );
  819. if ( m_pCurrentROI != NULL ) {
  820. if( !m_pProtocol->goAbsolute(
  821. m_pCurrentROI->cam_orientation.pan,
  822. m_pCurrentROI->cam_orientation.tilt,
  823. m_pCurrentROI->cam_orientation.zoom
  824. )){
  825. LOG_ERROR("Cannot Issue goAbsolute Command!");
  826. setWM( CAM_WS_READY );
  827. } else {
  828. // Send Capture event
  829. DBC_SNAPSHOT_DATA *tData = fusion_translate_ROI( m_pCurrentROI );
  830. if( tData != NULL ) {
  831. if( m_pBEH ){
  832. EVENT_CONTAINER *tEVT = createBasicEvent(
  833. m_sCamData.node_id,
  834. EVT_CLASS_CAMERA_CTRL,
  835. EVT_CAMERA_ON_POSITION,
  836. NULL, NULL, tData );
  837. if( tEVT ){
  838. //tEVT->m_db_id = tTASK->source_evt_id; // EVT ids embed in DATA
  839. m_pBEH->broadcast( tEVT );
  840. };
  841. };
  842. };
  843. // Waiting capture finish
  844. //setWM( CAM_WS_BUSY );
  845. };
  846. } else setWM( CAM_WS_READY );
  847. break;
  848. };
  849. case CAM_WS_BUSY:{
  850. //Handle Operation TO
  851. setWM( CAM_WS_READY );
  852. break;
  853. };
  854. case CAM_WS_READY:{
  855. /*
  856. uint32_t tSLEEP = DEFAULT_THREAD_SLEEP_TIME;
  857. CAM_TASK * tTASK = fetchCamTask();
  858. if ( tTASK != NULL ) {
  859. if( tTASK->movemet_needed ){
  860. CAM_PTZ_CMD *tCMD = tTASK->CMD;
  861. if( tCMD ) {
  862. switch( tCMD->CMD ) {
  863. case PTZ_STOP:{
  864. m_pProtocol->goStop();
  865. break;
  866. };
  867. case PTZ_MOVE:{
  868. uint32_t MoveMatrix = 0;
  869. if( m_pProtocol->setPTZSpeed( tCMD->PAN, tCMD->TILT, tCMD->ZOOM ) ){
  870. tSLEEP = 15;
  871. m_pProtocol->move( MoveMatrix );
  872. };
  873. break;
  874. };
  875. case PTZ_ABSOLUTE:{
  876. if( !m_pProtocol->goAbsolute( tCMD->PAN, tCMD->TILT, tCMD->ZOOM ) ) {
  877. LOG_ERROR("Cannot Issue goAbsolute Command!");
  878. tSLEEP= 100;
  879. } else {
  880. // Send Capture event
  881. //fusion_translate_ROI( CAM_ROI *ptr );
  882. if( m_pBEH ){
  883. EVENT_CONTAINER *tEVT = createBasicEvent(
  884. m_sCamData.node_id,
  885. EVT_CLASS_CAMERA_CTRL,
  886. EVT_CAMERA_ON_POSITION,
  887. NULL, NULL, NULL );
  888. if( tEVT ){
  889. tEVT->m_db_id = tTASK->source_evt_id;
  890. m_pBEH->broadcast(tEVT);
  891. };
  892. };
  893. };
  894. break;
  895. };
  896. case PTZ_RELOAD_CAMERA:{
  897. LOG("Exit Command Rcpt");
  898. m_bThreadStatus = TH_ENDED;
  899. };
  900. };
  901. free( tCMD );
  902. tTASK->CMD = NULL;
  903. };
  904. };
  905. free( tTASK );
  906. } else {
  907. // Lowest Priority to Web Client Commands
  908. PUSHED = m_pDBC->getNodePushedCMD( m_sCamData.node_id );
  909. if( PUSHED != NULL ) {
  910. if( ( PUSHED->CMD & ARGUS_CAM_PCMD_CMD_MASK ) == ARGUS_CAM_PCMD_CMD_MASK ) {
  911. switch( PUSHED->CMD ) {
  912. case 0x80:{
  913. //m_bWM = ANODE_WM_2D_ISSUE_FULL_RESET;
  914. break;
  915. };
  916. case 0x81:{
  917. //m_bWM = ANODE_WM_2D_GET_FAST_DRAW;
  918. break;
  919. };
  920. case 0x82:{
  921. break;
  922. };
  923. }; //?? Check if we don't miss something!!
  924. } else if ( tState == CAM_WS_READY || tState == CAM_WS_AUTOMATED_MOVE ){
  925. //Direct Movment Mode
  926. if( PUSHED->CMD == ARGUS_CAM_PCMD_STOP ) {
  927. setWM( CAM_WS_STOP_MOVE );
  928. } else {
  929. char tB[64];
  930. if( tState != CAM_WS_AUTOMATED_MOVE ) m_nRelativeMoveCounter = 0;
  931. m_bRelativeMoveMatrix = ( PUSHED->CMD ) >> 1;
  932. setWM( CAM_WS_AUTOMATED_MOVE );
  933. };
  934. };
  935. free( PUSHED );
  936. PUSHED = NULL;
  937. };
  938. };
  939. if( tSLEEP ) {
  940. SLEEP( tSLEEP );
  941. };
  942. */
  943. m_pCurrentROI = fusion_fetchROI();
  944. if( m_pCurrentROI != NULL ) {
  945. setWM( CAM_WS_GO_ABS_POSITION );
  946. } else {
  947. // Lowest Priority to Web Client Commands
  948. PUSHED = m_pDBC->getNodePushedCMD( m_sCamData.node_id );
  949. if( PUSHED != NULL ) {
  950. m_pDBC->updateNodePushedCMD( PUSHED->id, DBC_CMD_READY_TO_CLEAN );
  951. if( ( PUSHED->CMD & ARGUS_CAM_PCMD_CMD_MASK ) == ARGUS_CAM_PCMD_CMD_MASK ) {
  952. switch( PUSHED->CMD ) {
  953. case 0x80:{
  954. //m_bWM = ANODE_WM_2D_ISSUE_FULL_RESET;
  955. break;
  956. };
  957. case 0x81:{
  958. //m_bWM = ANODE_WM_2D_GET_FAST_DRAW;
  959. break;
  960. };
  961. case 0x82:{
  962. break;
  963. };
  964. }; //?? Check if we don't miss something!!
  965. } else if ( tState == CAM_WS_READY || tState == CAM_WS_AUTOMATED_MOVE ){
  966. //Direct Movment Mode
  967. if( PUSHED->CMD == ARGUS_CAM_PCMD_STOP ) {
  968. setWM( CAM_WS_STOP_MOVE );
  969. } else {
  970. char tB[64];
  971. if( tState != CAM_WS_AUTOMATED_MOVE ) m_nRelativeMoveCounter = 0;
  972. m_bRelativeMoveMatrix = ( PUSHED->CMD ) >> 1;
  973. setWM( CAM_WS_AUTOMATED_MOVE );
  974. };
  975. };
  976. free( PUSHED );
  977. PUSHED = NULL;
  978. };
  979. };
  980. break;
  981. };
  982. case CAM_WS_END:{
  983. LOG("Exit Command Rcpt");
  984. m_bThreadStatus = TH_ENDED;
  985. //setBusy( true );
  986. break;
  987. };
  988. };
  989. SLEEP( DEFAULT_THREAD_SLEEP_TIME );
  990. };
  991. //LOG("CTRL Loop End!");
  992. };
  993. bool ARGUS_Camera::testFOV( C3D_DECART L ){
  994. //System Coords -> trans to local and go to funct
  995. systemAnchor* tSystem = systemAnchor::getInstance();
  996. if ( tSystem == NULL ) return false;
  997. C3D_SPH_DEGREES SL;
  998. #ifdef DEBUG
  999. LOG("Test FOV local Decart coords");
  1000. #endif
  1001. if( !tSystem->convertToLocal( &m_oPosition, L, SL ) ) return false;
  1002. return testFOV( SL );
  1003. };
  1004. bool ARGUS_Camera::testFOV( C3D_WGS84_LOC L ){
  1005. //Global Coords
  1006. systemAnchor* tSystem = systemAnchor::getInstance();
  1007. if ( tSystem == NULL ) return false;
  1008. C3D_SPH_DEGREES SL;
  1009. #ifdef DEBUG
  1010. LOG("Test FOV WGS coords");
  1011. #endif
  1012. if( !tSystem->convertToLocal( &m_oPosition, L, SL ) ) return false;
  1013. return testFOV( SL );
  1014. };
  1015. bool ARGUS_Camera::testFOV( C3D_SPH_RADIANS L ){
  1016. //Local Coords
  1017. C3D_SPH_DEGREES SL;
  1018. #ifdef DEBUG
  1019. LOG("Test FOV local Spherical(rad) coords");
  1020. #endif
  1021. spherical2spherical( L, SL );
  1022. return testFOV( SL );
  1023. };
  1024. bool ARGUS_Camera::testFOV( C3D_SPH_DEGREES SL ){
  1025. #ifdef DEBUG
  1026. char tB[128];
  1027. #endif
  1028. #ifdef DEBUG
  1029. LOG("Test FOV local Spherical(deg) coords");
  1030. #endif
  1031. switch ( m_sCamData.triggering_mode ) {
  1032. case CAM_TRIGGER_STRICT:{
  1033. if( SL.r > m_fMaxRange || SL.r < m_fMinRange ){
  1034. #ifdef DEBUG
  1035. sprintf( tB, "Target Range( %.3f.m ) is not in a Range %.3f.m <-> %.3f.m", SL.r, m_fMinRange, m_fMaxRange );
  1036. LOG( tB );
  1037. #endif
  1038. return false;
  1039. };
  1040. break;
  1041. };
  1042. case CAM_TRIGGER_ALL_NEAR:{
  1043. if( SL.r > m_fMaxRange ){
  1044. #ifdef DEBUG
  1045. sprintf( tB, "Target Range( %.3f.m ) is not in a Range %.3f.m <-> %.3f.m", SL.r, m_fMinRange, m_fMaxRange );
  1046. LOG( tB );
  1047. #endif
  1048. return false;
  1049. };
  1050. break;
  1051. };
  1052. case CAM_TRIGGER_ALL_FAR:{
  1053. if( SL.r < m_fMinRange ){
  1054. #ifdef DEBUG
  1055. sprintf( tB, "Target Range( %.3f.m ) is not in a Range %.3f.m <-> %.3f.m", SL.r, m_fMinRange, m_fMaxRange );
  1056. LOG( tB );
  1057. #endif
  1058. return false;
  1059. };
  1060. break;
  1061. };
  1062. case CAM_TRIGGER_ALL: return true;
  1063. };
  1064. float HS = AC_imager_width[m_sCamData.imager_type];
  1065. float VS = AC_imager_height[m_sCamData.imager_type];
  1066. float HS2 = HS * HS;
  1067. float VS2 = VS * VS;
  1068. float IR = HS / VS;
  1069. float IR2 = IR * IR;
  1070. if( m_sCamData.pan_angles == 0 ) {
  1071. // Fixed Camera
  1072. float tA = fabsf( SL.fi );
  1073. float tZ = 0.5f * ( m_fMinHFov + ( SL.r - m_fMinRange ) * m_fR2FL );
  1074. if ( tA > tZ ) return false;
  1075. } else {
  1076. if( m_sCamData.pan_angles < 360 ) { // If endless PAN -> PASSED
  1077. // Partial Pan
  1078. switch( m_sCamData.pan_model ) {
  1079. case 1: {
  1080. // Regular Pan
  1081. float tFOV = 0.5f * ( m_fMinHFov + ( SL.r - m_fMinRange ) * m_fR2FL );
  1082. float tRad = deg2rad( SL.fi );
  1083. #ifdef DEBUG
  1084. sprintf( tB, "FOV: %0.3f CC: %0.3f", tFOV, tRad );
  1085. LOG( tB );
  1086. #endif
  1087. if( ( tRad < ( -1.0 * tFOV ) ) || ( tRad > ( deg2rad( m_sCamData.pan_angles ) + tFOV ) ) ) return false;
  1088. break;
  1089. };
  1090. case 2:{
  1091. break;
  1092. };
  1093. case 3:{
  1094. break;
  1095. };
  1096. };
  1097. };
  1098. };
  1099. if( m_sCamData.tilt_angles == 0 ){
  1100. //Fixed Camera
  1101. if ( ( SL.tetta < ( m_fMaxVFov / -2.0f ) ) || ( SL.tetta > ( m_fMaxVFov / 2.0f ) ) )return false;
  1102. } else {
  1103. if( m_sCamData.tilt_angles < 90 ) { // if Cam CAN DO 90 -> Pass
  1104. // Partial Tilt
  1105. switch( m_sCamData.tilt_model ) {
  1106. case 1: {
  1107. // Regular Tilt
  1108. float tFOV = 0.5f * ( m_fMinVFov + ( m_fMaxVFov - m_fMinVFov ) * ( SL.r / m_fMaxRange ) ) ;
  1109. float tRad = deg2rad( SL.tetta );
  1110. if( ( tRad < ( -1.0 * tFOV ) ) || ( tRad > ( deg2rad( m_sCamData.tilt_angles ) + tFOV ) ) )return false;
  1111. break;
  1112. };
  1113. case 2:{
  1114. break;
  1115. };
  1116. };
  1117. };
  1118. };
  1119. return true;
  1120. };
  1121. bool ARGUS_Camera::lookAt( uint32_t TargetRefID, C3D_WGS84_LOC L ){
  1122. systemAnchor* tSystem = systemAnchor::getInstance();
  1123. if( tSystem == NULL ) return false;
  1124. C3D_SPH_DEGREES SL;
  1125. #ifdef DEBUG
  1126. LOG("Look at WGS coords!");
  1127. #endif
  1128. tSystem->convertToLocal(&m_oPosition, L, SL );
  1129. return lookAt(TargetRefID, SL );
  1130. };
  1131. bool ARGUS_Camera::lookAt( uint32_t TargetRefID, C3D_DECART L ){
  1132. systemAnchor* tSystem = systemAnchor::getInstance();
  1133. if( tSystem == NULL ) return false;
  1134. C3D_SPH_DEGREES SL;
  1135. #ifdef DEBUG
  1136. LOG("Look at System Decart coords!");
  1137. #endif
  1138. tSystem->convertToLocal( &m_oPosition, L, SL );
  1139. return lookAt(TargetRefID, SL );
  1140. };
  1141. bool ARGUS_Camera::lookAt( uint32_t TargetRefID, C3D_SPH_RADIANS L ){
  1142. C3D_SPH_DEGREES SL;
  1143. #ifdef DEBUG
  1144. LOG("Look at spherical(rad) 2 spherical(deg) coords!");
  1145. #endif
  1146. spherical2spherical( L, SL );
  1147. return lookAt(TargetRefID, SL );
  1148. };
  1149. bool ARGUS_Camera::lookAt( uint32_t TargetRefID, C3D_SPH_DEGREES L ){
  1150. //char tB[128];
  1151. //sprintf(tB, "Target On %.3f %.3f %.3f", L.r, L.fi, L.tetta );
  1152. //LOG( tB );
  1153. //sprintf( tB, "Cam GoTo %.3f %.3f %.3f", fP, fT, fZ );
  1154. //LOG( tB );
  1155. #ifdef DEBUG
  1156. LOG("Look at local spherical deg. coords!(final)");
  1157. #endif
  1158. //if( getWM() == CAM_WS_READY ){
  1159. // C3D_ORIENT_ONVIF LL;
  1160. // spherical2onvif( L, LL, m_fMaxRange, m_sCamData.pan_model, m_sCamData.pan_angles, m_sCamData.tilt_model, m_sCamData.tilt_angles );
  1161. // return absoluteMove( TargetRefID, LL );
  1162. //};
  1163. // Camera is Busy
  1164. return fusion_consumeData( TargetRefID, L );
  1165. };
  1166. bool ARGUS_Camera::makeSnap( uint32_t TargetRefID ){
  1167. CAM_TASK *tTask = (CAM_TASK *)malloc(sizeof(CAM_TASK));
  1168. if( tTask ){
  1169. memset(tTask, 0, sizeof(CAM_TASK));
  1170. tTask->CMD = (CAM_PTZ_CMD *) malloc( sizeof(CAM_PTZ_CMD) );
  1171. if( tTask->CMD ){
  1172. tTask->CMD->CMD = PTZ_CAPTURE_ONLY;
  1173. tTask->source_evt_id = TargetRefID;
  1174. return pushCamTask( tTask );
  1175. } else {
  1176. free( tTask );
  1177. };
  1178. };
  1179. return false;
  1180. };
  1181. bool ARGUS_Camera::connectToStream( uint32_t id ){
  1182. /*
  1183. if( id ){
  1184. argusKernelModule * tKM = NULL;
  1185. argusKernel * tKRNL = NULL;
  1186. argusModuleFactory * tMF = NULL;
  1187. if( m_pDBC == NULL ) {
  1188. LOG_ERROR("No Database Connector!");
  1189. return false;
  1190. };
  1191. tKRNL = argusKernel::getInstance();
  1192. if( tKRNL ) {
  1193. tMF = tKRNL->getModuleFactory();
  1194. if( tMF ) {
  1195. tKM = tMF->getModule( ARGUS_MODULE_EMBED_VIDEO );
  1196. } else {
  1197. LOG_ERROR("No ModuleFactory Instance!");
  1198. return false;
  1199. };
  1200. } else {
  1201. LOG_ERROR("No Kernel Instance!");
  1202. return false;
  1203. };
  1204. if( tKM ) {
  1205. // New Module
  1206. DBC_MEDIA_STREAM_DATA *tDATA = m_pDBC->getMediaStreamData( id );
  1207. if( tDATA ) {
  1208. m_sMediaSource.mediaStreamId = id;
  1209. if( strncmp(tDATA->mrl_proto, "v4l2", 4) == 0 ) {
  1210. if (m_pSource != NULL) delete((videoAVLIBSource*)m_pSource);
  1211. m_pSource = (basicMediaSource *) new videoAVLIBSource(&m_sMediaSource);
  1212. } else {
  1213. if (m_pSource != NULL) delete((videoLIVE555Source*)m_pSource);
  1214. m_pSource = (basicMediaSource *) new videoLIVE555Source(&m_sMediaSource );
  1215. };
  1216. if( m_pSource != NULL ) {
  1217. char tMRL[1024];
  1218. #ifdef DEBUG
  1219. LOG("Source OK");
  1220. #endif
  1221. ((videoLIVE555Source*)m_pSource)->setWM( MS_MODE_STREAM_PROXY | MS_MODE_OVERLAYED_LOOPS | MS_MODE_DECODE_FRAMES );
  1222. int tW = ((argusModuleEmbedVideo*)tKM)->getWidh();
  1223. int tH = ((argusModuleEmbedVideo*)tKM)->getHeight();
  1224. int pix = -1;
  1225. bool in_16_9 = (((tDATA->width*100) / tDATA->height ) > 140 );
  1226. bool out_16_9 = (((tW*100) / tH ) > 140 );
  1227. updateOpticalParameters( tW, tH );
  1228. if( strlen( tDATA->c_type ) ){
  1229. };
  1230. //if( tW > 0 ){
  1231. // //m_pSource->setProportionalByWidth( tW );
  1232. // m_pSource->setSize( tW, tH );
  1233. //};
  1234. if( in_16_9 != out_16_9 ){
  1235. if( in_16_9 ){
  1236. tH = ((( tW * 1000 ) / 16 ) * 9 ) / 1000;
  1237. } else {
  1238. tW = ((( tH * 1000 ) / 3 ) * 4 ) / 1000;
  1239. };
  1240. };
  1241. if (strncmp(tDATA->mrl_proto, "v4l2", 4) == 0) {
  1242. sprintf( tMRL, tDATA->mrl );
  1243. } else {
  1244. DBC_CAM_DATA *tCAM = m_pDBC->getCamData( tDATA->source_id );
  1245. if( tCAM ) {
  1246. sprintf( tMRL, "%s://%s:%s@%s:%d%s", tDATA->mrl_proto, tCAM->username, tCAM->password, tCAM->ip, tDATA->mrl_port, tDATA->mrl );
  1247. free( tCAM );
  1248. } else {
  1249. LOG_ERROR("Error Getting CAM Data!");
  1250. delete( m_pSource );
  1251. m_pSource = NULL;
  1252. return false;
  1253. };
  1254. };
  1255. LOG( tMRL );
  1256. if( m_pSource->_open( tMRL, false, -1.0f, pix, tDATA->width, tDATA->height, tW, tH ) ){
  1257. m_bSourceConnected = m_pSource->attachDestination( (argusModuleEmbedVideo*) tKM );
  1258. if ( !m_bSourceConnected ) {
  1259. //delete( m_pSource );
  1260. if( strncmp(tDATA->mrl_proto, "v4l2", 4) == 0 ) {
  1261. delete((videoAVLIBSource*)m_pSource);
  1262. } else {
  1263. delete((videoLIVE555Source*)m_pSource);
  1264. };
  1265. m_pSource = NULL;
  1266. } else {
  1267. m_nActiveStreamID = id;
  1268. #ifdef DEBUG
  1269. LOG("Source is Connected to Output.");
  1270. #endif
  1271. };
  1272. } else {
  1273. LOG_ERROR("Can not Open Video Source!");
  1274. //delete( m_pSource );
  1275. if( strncmp(tDATA->mrl_proto, "v4l2", 4) == 0 ) {
  1276. delete((videoAVLIBSource*)m_pSource);
  1277. } else {
  1278. delete((videoLIVE555Source*)m_pSource);
  1279. };
  1280. m_pSource = NULL;
  1281. };
  1282. } else {
  1283. LOG_ERROR("Video Source Stream Failure!");
  1284. };
  1285. free( tDATA );
  1286. };
  1287. }
  1288. #ifdef DEBUG
  1289. else LOG("No Video Module!")
  1290. #endif
  1291. ;
  1292. };
  1293. LOG("Connect Finished!");
  1294. return m_bSourceConnected;
  1295. */
  1296. return true;
  1297. };
  1298. bool ARGUS_Camera::disconnectFromStream( void ){
  1299. /*if( m_bSourceConnected ) {
  1300. if( m_pSource ){
  1301. delete( m_pSource );
  1302. m_pSource = NULL;
  1303. };
  1304. m_bSourceConnected = false;
  1305. };
  1306. return !m_bSourceConnected;
  1307. */
  1308. return true;
  1309. };
  1310. bool ARGUS_Camera::validateStream( void ){
  1311. /*
  1312. if( m_bSourceConnected ) {
  1313. if( m_pSource ) {
  1314. if( m_pSource->isAttached() ){
  1315. if( m_pSource->getStatus() < MS_STOPED ) return true;
  1316. #ifdef DEBUG
  1317. LOG("Source is Finished!");
  1318. #endif
  1319. };
  1320. #ifdef DEBUG
  1321. LOG("Source is Disconnected!");
  1322. #endif
  1323. } else LOG_ERROR("No Source Object! Set Disconnected!");
  1324. disconnectFromStream();
  1325. };
  1326. return false;
  1327. */
  1328. return true;
  1329. };
  1330. #undef LOG
  1331. #undef LOG_ERROR