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.

1398 lines
52 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
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  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. for( vCAM_TRACK_RECORDS::iterator idx = ptr->targets.begin(); idx < ptr->targets.end(); idx++ ) {
  616. rVal->assoc_event_ids.push_back( (*idx)->evt_db_ID );
  617. };
  618. };
  619. };
  620. ptr->targets.clear();
  621. };
  622. // Destruct ROI MEM
  623. delete( (CAM_ROI *) ptr );
  624. ptr = NULL;
  625. // Not Setting MD5 and TS -> will set at capture time
  626. return rVal;
  627. };
  628. void ARGUS_Camera::fusion_validate( void ){
  629. m_mROIsProtector.lock();
  630. if( m_vROIS.size() ){
  631. #ifdef DEBUG
  632. char tB[128];
  633. sprintf( tB, "Fusion: Have [%d] Regions Of Interes", m_vROIS.size() );
  634. LOG( tB );
  635. #endif
  636. uint64_t cts = getMilliseconds();
  637. for( vCAM_ROIS::iterator idx = m_vROIS.begin(); idx < m_vROIS.end(); idx++ ){
  638. if( !fusion_validateROI( cts, *idx ) ) {
  639. free( (*idx) );
  640. m_vROIS.erase( idx );
  641. };
  642. };
  643. };
  644. m_mROIsProtector.unlock();
  645. };
  646. bool ARGUS_Camera::absoluteMove( uint32_t TargetRefID, float PanPOS, float TiltPOS, float ZoomPOS ){
  647. if( m_sCamData.online ){
  648. CAM_TASK *tTask = (CAM_TASK *)malloc(sizeof(CAM_TASK));
  649. if( tTask ){
  650. memset(tTask, 0, sizeof(CAM_TASK));
  651. tTask->CMD = (CAM_PTZ_CMD *)malloc(sizeof(CAM_PTZ_CMD));
  652. if( tTask->CMD ){
  653. tTask->CMD->CMD = PTZ_ABSOLUTE;
  654. tTask->CMD->PAN = PanPOS;
  655. tTask->CMD->TILT = TiltPOS;
  656. tTask->CMD->ZOOM = ZoomPOS;
  657. tTask->movemet_needed = true;
  658. tTask->source_evt_id = TargetRefID;
  659. setWM( CAM_WS_BUSY );
  660. return pushCamTask( tTask );
  661. } else {
  662. LOG_ERROR("CMD malloc Error!");
  663. free( tTask );
  664. };
  665. } else {
  666. LOG_ERROR("Task malloc error!");
  667. };
  668. };
  669. return false;
  670. };
  671. bool ARGUS_Camera::absoluteMove( uint32_t TargetRefID, C3D_ORIENT_ONVIF O ){
  672. if( m_sCamData.online ){
  673. CAM_TASK *tTask = (CAM_TASK *)malloc(sizeof(CAM_TASK));
  674. if( tTask ){
  675. memset(tTask, 0, sizeof(CAM_TASK));
  676. tTask->CMD = (CAM_PTZ_CMD *)malloc(sizeof(CAM_PTZ_CMD));
  677. if( tTask->CMD ){
  678. tTask->CMD->CMD = PTZ_ABSOLUTE;
  679. tTask->CMD->PAN = O.pan;
  680. tTask->CMD->TILT = O.tilt;
  681. tTask->CMD->ZOOM = O.zoom;
  682. tTask->movemet_needed = true;
  683. tTask->source_evt_id = TargetRefID;
  684. setWM( CAM_WS_BUSY );
  685. return pushCamTask( tTask );
  686. } else {
  687. LOG_ERROR("CMD malloc Error!");
  688. free( tTask );
  689. };
  690. } else {
  691. LOG_ERROR("Task malloc error!");
  692. };
  693. };
  694. return false;
  695. };
  696. void ARGUS_Camera::m_fThreadProc( void ){
  697. DBC_PUSHED_CMD * PUSHED = NULL;
  698. m_bThreadStatus = TH_WORKING;
  699. while( m_bThreadStatus < TH_ENDED ) {
  700. //Local Recorder
  701. if( m_bSourceConnected ) {
  702. };
  703. uint8_t tState = getWM();
  704. char tB[128];
  705. //sprintf( tB, "CWM: 0x%02X", tState );
  706. //LOG( tB );
  707. fusion_validate();
  708. switch( tState ) {
  709. case CAM_WS_INIT:{
  710. #ifdef DEBUG
  711. LOG("Init Camera - PowerMode");
  712. #endif
  713. //setBusy( true );
  714. if( m_pProtocol->initCamera() ) setWM( CAM_WS_WHITE_BALLANCE );
  715. //setBusy( false );
  716. break;
  717. };
  718. case CAM_WS_WHITE_BALLANCE:{
  719. #ifdef DEBUG
  720. LOG("Set WB");
  721. #endif
  722. //setBusy( true );
  723. if( m_pProtocol->setWhiteBalanceMode( WB_AUTO ) ) setWM( CAM_WS_BACKLIGHT );
  724. //setBusy( false );
  725. break;
  726. };
  727. case CAM_WS_BACKLIGHT:{
  728. #ifdef DEBUG
  729. LOG("Set BLC");
  730. #endif
  731. //setBusy( true );
  732. if( m_pProtocol->setBackLightMode( true ) ) setWM( CAM_WS_AUTO_FOCUS );
  733. //setBusy( false );
  734. break;
  735. };
  736. case CAM_WS_AUTO_FOCUS:{
  737. #ifdef DEBUG
  738. LOG("Set AF");
  739. #endif
  740. //setBusy( true );
  741. if( m_pProtocol->setAutoFocus( true ) ) setWM( CAM_WS_INIT_GO_HOME );
  742. //setBusy( false );
  743. break;
  744. };
  745. case CAM_WS_INIT_GO_HOME:{
  746. #ifdef DEBUG
  747. LOG("INIT GO -1 -1 -1");
  748. #endif
  749. //setBusy( true );
  750. if( m_pProtocol->goAbsolute( -1, 0, -1 ) ) setWM( CAM_WS_READY );
  751. //setBusy( false );
  752. break;
  753. };
  754. case CAM_WS_AUTOMATED_MOVE:{
  755. m_nRelativeMoveCounter++;
  756. if( ( m_bRelativeMoveMatrix & 0x03 ) != 0 ) {
  757. //Pan Move
  758. if( ( m_bRelativeMoveMatrix & 0x01 ) == 0 ) {
  759. m_fRM_Pan -= m_fSpeedRise;
  760. } else {
  761. m_fRM_Pan += m_fSpeedRise;
  762. };
  763. };
  764. if( ( m_bRelativeMoveMatrix & 0x0C ) != 0 ) {
  765. //TILT Move
  766. if( ( m_bRelativeMoveMatrix & 0x04 ) == 0 ) {
  767. m_fRM_Tilt -= m_fSpeedRise;
  768. } else {
  769. m_fRM_Tilt += m_fSpeedRise;
  770. }
  771. };
  772. if( ( m_bRelativeMoveMatrix & 0x30 ) != 0 ) {
  773. //ZOOM Move
  774. if( ( m_bRelativeMoveMatrix & 0x10 ) == 0 ) {
  775. //Wide
  776. m_fRM_Zoom -= m_fSpeedRise;
  777. } else {
  778. //Tele
  779. m_fRM_Zoom += m_fSpeedRise;
  780. };
  781. };
  782. if( m_fRM_Pan < -1.0f ) m_fRM_Pan = -1.0f;
  783. if( m_fRM_Pan > 1.0f ) m_fRM_Pan = 1.0f;
  784. if( m_fRM_Tilt < -1.0f ) m_fRM_Tilt = -1.0f;
  785. if( m_fRM_Tilt > 1.0f ) m_fRM_Tilt = 1.0f;
  786. if( m_fRM_Zoom < -1.0f ) m_fRM_Zoom = -1.0f;
  787. if( m_fRM_Zoom > 1.0f ) m_fRM_Zoom = 1.0f;
  788. if( !relativeMove( m_fRM_Pan, m_fRM_Tilt, m_fRM_Zoom ) ){
  789. m_nRelativeMoveCounter = 0;
  790. setWM( CAM_WS_READY );
  791. break;
  792. };
  793. if( m_nRelativeMoveCounter > 10 ) {
  794. m_nRelativeMoveCounter = 0;
  795. setWM( CAM_WS_READY );
  796. };
  797. break;
  798. };
  799. case CAM_WS_STOP_MOVE:{
  800. m_nRelativeMoveCounter++;
  801. m_fRM_Pan = .0f;
  802. m_fRM_Pan = .0f;
  803. m_fRM_Tilt = .0f;
  804. m_fRM_Tilt = .0f;
  805. m_fRM_Zoom = .0f;
  806. m_fRM_Zoom = .0f;
  807. if( relativeStop() ) {
  808. m_nRelativeMoveCounter = 0;
  809. setWM( CAM_WS_READY );
  810. break;
  811. };
  812. if( m_nRelativeMoveCounter > 10 ) setWM( CAM_WS_READY );
  813. break;
  814. };
  815. case CAM_WS_GO_ABS_POSITION:{
  816. setWM( CAM_WS_BUSY );
  817. if ( m_pCurrentROI != NULL ) {
  818. if( !m_pProtocol->goAbsolute(
  819. m_pCurrentROI->cam_orientation.pan,
  820. m_pCurrentROI->cam_orientation.tilt,
  821. m_pCurrentROI->cam_orientation.zoom
  822. )){
  823. LOG_ERROR("Cannot Issue goAbsolute Command!");
  824. setWM( CAM_WS_READY );
  825. } else {
  826. // Send Capture event
  827. DBC_SNAPSHOT_DATA *tData = fusion_translate_ROI( m_pCurrentROI );
  828. if( tData != NULL ) {
  829. if( m_pBEH ){
  830. EVENT_CONTAINER *tEVT = createBasicEvent(
  831. m_sCamData.node_id,
  832. EVT_CLASS_CAMERA_CTRL,
  833. EVT_CAMERA_ON_POSITION,
  834. NULL, NULL, tData );
  835. if( tEVT ){
  836. //tEVT->m_db_id = tTASK->source_evt_id; // EVT ids embed in DATA
  837. m_pBEH->broadcast( tEVT );
  838. };
  839. };
  840. };
  841. // Waiting capture finish
  842. //setWM( CAM_WS_BUSY );
  843. };
  844. } else setWM( CAM_WS_READY );
  845. break;
  846. };
  847. case CAM_WS_BUSY:{
  848. //Handle Operation TO
  849. setWM( CAM_WS_READY );
  850. break;
  851. };
  852. case CAM_WS_READY:{
  853. /*
  854. uint32_t tSLEEP = DEFAULT_THREAD_SLEEP_TIME;
  855. CAM_TASK * tTASK = fetchCamTask();
  856. if ( tTASK != NULL ) {
  857. if( tTASK->movemet_needed ){
  858. CAM_PTZ_CMD *tCMD = tTASK->CMD;
  859. if( tCMD ) {
  860. switch( tCMD->CMD ) {
  861. case PTZ_STOP:{
  862. m_pProtocol->goStop();
  863. break;
  864. };
  865. case PTZ_MOVE:{
  866. uint32_t MoveMatrix = 0;
  867. if( m_pProtocol->setPTZSpeed( tCMD->PAN, tCMD->TILT, tCMD->ZOOM ) ){
  868. tSLEEP = 15;
  869. m_pProtocol->move( MoveMatrix );
  870. };
  871. break;
  872. };
  873. case PTZ_ABSOLUTE:{
  874. if( !m_pProtocol->goAbsolute( tCMD->PAN, tCMD->TILT, tCMD->ZOOM ) ) {
  875. LOG_ERROR("Cannot Issue goAbsolute Command!");
  876. tSLEEP= 100;
  877. } else {
  878. // Send Capture event
  879. //fusion_translate_ROI( CAM_ROI *ptr );
  880. if( m_pBEH ){
  881. EVENT_CONTAINER *tEVT = createBasicEvent(
  882. m_sCamData.node_id,
  883. EVT_CLASS_CAMERA_CTRL,
  884. EVT_CAMERA_ON_POSITION,
  885. NULL, NULL, NULL );
  886. if( tEVT ){
  887. tEVT->m_db_id = tTASK->source_evt_id;
  888. m_pBEH->broadcast(tEVT);
  889. };
  890. };
  891. };
  892. break;
  893. };
  894. case PTZ_RELOAD_CAMERA:{
  895. LOG("Exit Command Rcpt");
  896. m_bThreadStatus = TH_ENDED;
  897. };
  898. };
  899. free( tCMD );
  900. tTASK->CMD = NULL;
  901. };
  902. };
  903. free( tTASK );
  904. } else {
  905. // Lowest Priority to Web Client Commands
  906. PUSHED = m_pDBC->getNodePushedCMD( m_sCamData.node_id );
  907. if( PUSHED != NULL ) {
  908. if( ( PUSHED->CMD & ARGUS_CAM_PCMD_CMD_MASK ) == ARGUS_CAM_PCMD_CMD_MASK ) {
  909. switch( PUSHED->CMD ) {
  910. case 0x80:{
  911. //m_bWM = ANODE_WM_2D_ISSUE_FULL_RESET;
  912. break;
  913. };
  914. case 0x81:{
  915. //m_bWM = ANODE_WM_2D_GET_FAST_DRAW;
  916. break;
  917. };
  918. case 0x82:{
  919. break;
  920. };
  921. }; //?? Check if we don't miss something!!
  922. } else if ( tState == CAM_WS_READY || tState == CAM_WS_AUTOMATED_MOVE ){
  923. //Direct Movment Mode
  924. if( PUSHED->CMD == ARGUS_CAM_PCMD_STOP ) {
  925. setWM( CAM_WS_STOP_MOVE );
  926. } else {
  927. char tB[64];
  928. if( tState != CAM_WS_AUTOMATED_MOVE ) m_nRelativeMoveCounter = 0;
  929. m_bRelativeMoveMatrix = ( PUSHED->CMD ) >> 1;
  930. setWM( CAM_WS_AUTOMATED_MOVE );
  931. };
  932. };
  933. free( PUSHED );
  934. PUSHED = NULL;
  935. };
  936. };
  937. if( tSLEEP ) {
  938. SLEEP( tSLEEP );
  939. };
  940. */
  941. m_pCurrentROI = fusion_fetchROI();
  942. if( m_pCurrentROI != NULL ) {
  943. setWM( CAM_WS_GO_ABS_POSITION );
  944. } else {
  945. // Lowest Priority to Web Client Commands
  946. PUSHED = m_pDBC->getNodePushedCMD( m_sCamData.node_id );
  947. if( PUSHED != NULL ) {
  948. m_pDBC->updateNodePushedCMD( PUSHED->id, DBC_CMD_READY_TO_CLEAN );
  949. if( ( PUSHED->CMD & ARGUS_CAM_PCMD_CMD_MASK ) == ARGUS_CAM_PCMD_CMD_MASK ) {
  950. switch( PUSHED->CMD ) {
  951. case 0x80:{
  952. //m_bWM = ANODE_WM_2D_ISSUE_FULL_RESET;
  953. break;
  954. };
  955. case 0x81:{
  956. //m_bWM = ANODE_WM_2D_GET_FAST_DRAW;
  957. break;
  958. };
  959. case 0x82:{
  960. break;
  961. };
  962. }; //?? Check if we don't miss something!!
  963. } else if ( tState == CAM_WS_READY || tState == CAM_WS_AUTOMATED_MOVE ){
  964. //Direct Movment Mode
  965. if( PUSHED->CMD == ARGUS_CAM_PCMD_STOP ) {
  966. setWM( CAM_WS_STOP_MOVE );
  967. } else {
  968. char tB[64];
  969. if( tState != CAM_WS_AUTOMATED_MOVE ) m_nRelativeMoveCounter = 0;
  970. m_bRelativeMoveMatrix = ( PUSHED->CMD ) >> 1;
  971. setWM( CAM_WS_AUTOMATED_MOVE );
  972. };
  973. };
  974. free( PUSHED );
  975. PUSHED = NULL;
  976. };
  977. };
  978. break;
  979. };
  980. case CAM_WS_END:{
  981. LOG("Exit Command Rcpt");
  982. m_bThreadStatus = TH_ENDED;
  983. //setBusy( true );
  984. break;
  985. };
  986. };
  987. SLEEP( DEFAULT_THREAD_SLEEP_TIME );
  988. };
  989. //LOG("CTRL Loop End!");
  990. };
  991. bool ARGUS_Camera::testFOV( C3D_DECART L ){
  992. //System Coords -> trans to local and go to funct
  993. systemAnchor* tSystem = systemAnchor::getInstance();
  994. if ( tSystem == NULL ) return false;
  995. C3D_SPH_DEGREES SL;
  996. #ifdef DEBUG
  997. LOG("Test FOV local Decart coords");
  998. #endif
  999. if( !tSystem->convertToLocal( &m_oPosition, L, SL ) ) return false;
  1000. return testFOV( SL );
  1001. };
  1002. bool ARGUS_Camera::testFOV( C3D_WGS84_LOC L ){
  1003. //Global Coords
  1004. systemAnchor* tSystem = systemAnchor::getInstance();
  1005. if ( tSystem == NULL ) return false;
  1006. C3D_SPH_DEGREES SL;
  1007. #ifdef DEBUG
  1008. LOG("Test FOV WGS coords");
  1009. #endif
  1010. if( !tSystem->convertToLocal( &m_oPosition, L, SL ) ) return false;
  1011. return testFOV( SL );
  1012. };
  1013. bool ARGUS_Camera::testFOV( C3D_SPH_RADIANS L ){
  1014. //Local Coords
  1015. C3D_SPH_DEGREES SL;
  1016. #ifdef DEBUG
  1017. LOG("Test FOV local Spherical(rad) coords");
  1018. #endif
  1019. spherical2spherical( L, SL );
  1020. return testFOV( SL );
  1021. };
  1022. bool ARGUS_Camera::testFOV( C3D_SPH_DEGREES SL ){
  1023. #ifdef DEBUG
  1024. char tB[128];
  1025. #endif
  1026. #ifdef DEBUG
  1027. LOG("Test FOV local Spherical(deg) coords");
  1028. #endif
  1029. switch ( m_sCamData.triggering_mode ) {
  1030. case CAM_TRIGGER_STRICT:{
  1031. if( SL.r > m_fMaxRange || SL.r < m_fMinRange ){
  1032. #ifdef DEBUG
  1033. sprintf( tB, "Target Range( %.3f.m ) is not in a Range %.3f.m <-> %.3f.m", SL.r, m_fMinRange, m_fMaxRange );
  1034. LOG( tB );
  1035. #endif
  1036. return false;
  1037. };
  1038. break;
  1039. };
  1040. case CAM_TRIGGER_ALL_NEAR:{
  1041. if( SL.r > m_fMaxRange ){
  1042. #ifdef DEBUG
  1043. sprintf( tB, "Target Range( %.3f.m ) is not in a Range %.3f.m <-> %.3f.m", SL.r, m_fMinRange, m_fMaxRange );
  1044. LOG( tB );
  1045. #endif
  1046. return false;
  1047. };
  1048. break;
  1049. };
  1050. case CAM_TRIGGER_ALL_FAR:{
  1051. if( SL.r < m_fMinRange ){
  1052. #ifdef DEBUG
  1053. sprintf( tB, "Target Range( %.3f.m ) is not in a Range %.3f.m <-> %.3f.m", SL.r, m_fMinRange, m_fMaxRange );
  1054. LOG( tB );
  1055. #endif
  1056. return false;
  1057. };
  1058. break;
  1059. };
  1060. case CAM_TRIGGER_ALL: return true;
  1061. };
  1062. float HS = AC_imager_width[m_sCamData.imager_type];
  1063. float VS = AC_imager_height[m_sCamData.imager_type];
  1064. float HS2 = HS * HS;
  1065. float VS2 = VS * VS;
  1066. float IR = HS / VS;
  1067. float IR2 = IR * IR;
  1068. if( m_sCamData.pan_angles == 0 ) {
  1069. // Fixed Camera
  1070. float tA = fabsf( SL.fi );
  1071. float tZ = 0.5f * ( m_fMinHFov + ( SL.r - m_fMinRange ) * m_fR2FL );
  1072. if ( tA > tZ ) return false;
  1073. } else {
  1074. if( m_sCamData.pan_angles < 360 ) { // If endless PAN -> PASSED
  1075. // Partial Pan
  1076. switch( m_sCamData.pan_model ) {
  1077. case 1: {
  1078. // Regular Pan
  1079. float tFOV = 0.5f * ( m_fMinHFov + ( SL.r - m_fMinRange ) * m_fR2FL );
  1080. float tRad = deg2rad( SL.fi );
  1081. #ifdef DEBUG
  1082. sprintf( tB, "FOV: %0.3f CC: %0.3f", tFOV, tRad );
  1083. LOG( tB );
  1084. #endif
  1085. if( ( tRad < ( -1.0 * tFOV ) ) || ( tRad > ( deg2rad( m_sCamData.pan_angles ) + tFOV ) ) ) return false;
  1086. break;
  1087. };
  1088. case 2:{
  1089. break;
  1090. };
  1091. case 3:{
  1092. break;
  1093. };
  1094. };
  1095. };
  1096. };
  1097. if( m_sCamData.tilt_angles == 0 ){
  1098. //Fixed Camera
  1099. if ( ( SL.tetta < ( m_fMaxVFov / -2.0f ) ) || ( SL.tetta > ( m_fMaxVFov / 2.0f ) ) )return false;
  1100. } else {
  1101. if( m_sCamData.tilt_angles < 90 ) { // if Cam CAN DO 90 -> Pass
  1102. // Partial Tilt
  1103. switch( m_sCamData.tilt_model ) {
  1104. case 1: {
  1105. // Regular Tilt
  1106. float tFOV = 0.5f * ( m_fMinVFov + ( m_fMaxVFov - m_fMinVFov ) * ( SL.r / m_fMaxRange ) ) ;
  1107. float tRad = deg2rad( SL.tetta );
  1108. if( ( tRad < ( -1.0 * tFOV ) ) || ( tRad > ( deg2rad( m_sCamData.tilt_angles ) + tFOV ) ) )return false;
  1109. break;
  1110. };
  1111. case 2:{
  1112. break;
  1113. };
  1114. };
  1115. };
  1116. };
  1117. return true;
  1118. };
  1119. bool ARGUS_Camera::lookAt( uint32_t TargetRefID, C3D_WGS84_LOC L ){
  1120. systemAnchor* tSystem = systemAnchor::getInstance();
  1121. if( tSystem == NULL ) return false;
  1122. C3D_SPH_DEGREES SL;
  1123. #ifdef DEBUG
  1124. LOG("Look at WGS coords!");
  1125. #endif
  1126. tSystem->convertToLocal(&m_oPosition, L, SL );
  1127. return lookAt(TargetRefID, SL );
  1128. };
  1129. bool ARGUS_Camera::lookAt( uint32_t TargetRefID, C3D_DECART L ){
  1130. systemAnchor* tSystem = systemAnchor::getInstance();
  1131. if( tSystem == NULL ) return false;
  1132. C3D_SPH_DEGREES SL;
  1133. #ifdef DEBUG
  1134. LOG("Look at System Decart coords!");
  1135. #endif
  1136. tSystem->convertToLocal( &m_oPosition, L, SL );
  1137. return lookAt(TargetRefID, SL );
  1138. };
  1139. bool ARGUS_Camera::lookAt( uint32_t TargetRefID, C3D_SPH_RADIANS L ){
  1140. C3D_SPH_DEGREES SL;
  1141. #ifdef DEBUG
  1142. LOG("Look at spherical(rad) 2 spherical(deg) coords!");
  1143. #endif
  1144. spherical2spherical( L, SL );
  1145. return lookAt(TargetRefID, SL );
  1146. };
  1147. bool ARGUS_Camera::lookAt( uint32_t TargetRefID, C3D_SPH_DEGREES L ){
  1148. //char tB[128];
  1149. //sprintf(tB, "Target On %.3f %.3f %.3f", L.r, L.fi, L.tetta );
  1150. //LOG( tB );
  1151. //sprintf( tB, "Cam GoTo %.3f %.3f %.3f", fP, fT, fZ );
  1152. //LOG( tB );
  1153. #ifdef DEBUG
  1154. LOG("Look at local spherical deg. coords!(final)");
  1155. #endif
  1156. //if( getWM() == CAM_WS_READY ){
  1157. // C3D_ORIENT_ONVIF LL;
  1158. // spherical2onvif( L, LL, m_fMaxRange, m_sCamData.pan_model, m_sCamData.pan_angles, m_sCamData.tilt_model, m_sCamData.tilt_angles );
  1159. // return absoluteMove( TargetRefID, LL );
  1160. //};
  1161. // Camera is Busy
  1162. return fusion_consumeData( TargetRefID, L );
  1163. };
  1164. bool ARGUS_Camera::makeSnap( uint32_t TargetRefID ){
  1165. CAM_TASK *tTask = (CAM_TASK *)malloc(sizeof(CAM_TASK));
  1166. if( tTask ){
  1167. memset(tTask, 0, sizeof(CAM_TASK));
  1168. tTask->CMD = (CAM_PTZ_CMD *) malloc( sizeof(CAM_PTZ_CMD) );
  1169. if( tTask->CMD ){
  1170. tTask->CMD->CMD = PTZ_CAPTURE_ONLY;
  1171. tTask->source_evt_id = TargetRefID;
  1172. return pushCamTask( tTask );
  1173. } else {
  1174. free( tTask );
  1175. };
  1176. };
  1177. return false;
  1178. };
  1179. bool ARGUS_Camera::connectToStream( uint32_t id ){
  1180. if( id ){
  1181. argusKernelModule * tKM = NULL;
  1182. argusKernel * tKRNL = NULL;
  1183. argusModuleFactory * tMF = NULL;
  1184. if( m_pDBC == NULL ) {
  1185. LOG_ERROR("No Database Connector!");
  1186. return false;
  1187. };
  1188. tKRNL = argusKernel::getInstance();
  1189. if( tKRNL ) {
  1190. tMF = tKRNL->getModuleFactory();
  1191. if( tMF ) {
  1192. tKM = tMF->getModule( ARGUS_MODULE_EMBED_VIDEO );
  1193. } else {
  1194. LOG_ERROR("No ModuleFactory Instance!");
  1195. return false;
  1196. };
  1197. } else {
  1198. LOG_ERROR("No Kernel Instance!");
  1199. return false;
  1200. };
  1201. if( tKM ) {
  1202. DBC_MEDIA_STREAM_DATA *tDATA = m_pDBC->getMediaStreamData( id );
  1203. if( tDATA ) {
  1204. m_sMediaSource.mediaStreamId = id;
  1205. if( strncmp(tDATA->mrl_proto, "v4l2", 4) == 0 ) {
  1206. if (m_pSource != NULL) delete((videoAVLIBSource*)m_pSource);
  1207. m_pSource = (basicMediaSource *) new videoAVLIBSource(&m_sMediaSource);
  1208. } else {
  1209. if (m_pSource != NULL) delete((videoLIVE555Source*)m_pSource);
  1210. m_pSource = (basicMediaSource *) new videoLIVE555Source(&m_sMediaSource);
  1211. };
  1212. if( m_pSource != NULL ) {
  1213. char tMRL[1024];
  1214. #ifdef DEBUG
  1215. LOG("Source OK");
  1216. #endif
  1217. int tW = ((argusModuleEmbedVideo*)tKM)->getWidh();
  1218. int tH = ((argusModuleEmbedVideo*)tKM)->getHeight();
  1219. int pix = -1;
  1220. bool in_16_9 = (((tDATA->width*100) / tDATA->height ) > 140 );
  1221. bool out_16_9 = (((tW*100) / tH ) > 140 );
  1222. updateOpticalParameters( tW, tH );
  1223. if( strlen( tDATA->c_type ) ){
  1224. };
  1225. //if( tW > 0 ){
  1226. // //m_pSource->setProportionalByWidth( tW );
  1227. // m_pSource->setSize( tW, tH );
  1228. //};
  1229. if( in_16_9 != out_16_9 ){
  1230. if( in_16_9 ){
  1231. tH = ((( tW * 1000 ) / 16 ) * 9 ) / 1000;
  1232. } else {
  1233. tW = ((( tH * 1000 ) / 3 ) * 4 ) / 1000;
  1234. };
  1235. };
  1236. if (strncmp(tDATA->mrl_proto, "v4l2", 4) == 0) {
  1237. sprintf( tMRL, tDATA->mrl );
  1238. } else {
  1239. DBC_CAM_DATA *tCAM = m_pDBC->getCamData( tDATA->source_id );
  1240. if( tCAM ) {
  1241. sprintf( tMRL, "%s://%s:%s@%s:%d%s", tDATA->mrl_proto, tCAM->username, tCAM->password, tCAM->ip, tDATA->mrl_port, tDATA->mrl );
  1242. free( tCAM );
  1243. } else {
  1244. LOG_ERROR("Error Getting CAM Data!");
  1245. delete( m_pSource );
  1246. m_pSource = NULL;
  1247. return false;
  1248. };
  1249. };
  1250. LOG( tMRL );
  1251. if( m_pSource->_open( tMRL, false, -1.0f, pix, tDATA->width, tDATA->height, tW, tH ) ){
  1252. m_bSourceConnected = m_pSource->attachDestination( (argusModuleEmbedVideo*) tKM );
  1253. if ( !m_bSourceConnected ) {
  1254. delete( m_pSource );
  1255. m_pSource = NULL;
  1256. } else {
  1257. m_nActiveStreamID = id;
  1258. #ifdef DEBUG
  1259. LOG("Source is Connected to Output.");
  1260. #endif
  1261. };
  1262. } else {
  1263. LOG_ERROR("Can not Open Video Source!");
  1264. delete( m_pSource );
  1265. m_pSource = NULL;
  1266. };
  1267. } else {
  1268. LOG_ERROR("Video Source Stream Failure!");
  1269. };
  1270. free( tDATA );
  1271. };
  1272. }
  1273. #ifdef DEBUG
  1274. else LOG("No Video Module!")
  1275. #endif
  1276. ;
  1277. };
  1278. return m_bSourceConnected;
  1279. };
  1280. bool ARGUS_Camera::disconnectFromStream( void ){
  1281. if( m_bSourceConnected ) {
  1282. if( m_pSource ){
  1283. delete( m_pSource );
  1284. m_pSource = NULL;
  1285. };
  1286. m_bSourceConnected = false;
  1287. };
  1288. return !m_bSourceConnected;
  1289. };
  1290. bool ARGUS_Camera::validateStream( void ){
  1291. if( m_bSourceConnected ) {
  1292. if( m_pSource ) {
  1293. if( m_pSource->isAttached() ){
  1294. if( m_pSource->getStatus() < MS_STOPED ) return true;
  1295. #ifdef DEBUG
  1296. LOG("Source is Finished!");
  1297. #endif
  1298. };
  1299. #ifdef DEBUG
  1300. LOG("Source is Disconnected!");
  1301. #endif
  1302. } else LOG_ERROR("No Source Object! Set Disconnected!");
  1303. disconnectFromStream();
  1304. };
  1305. return false;
  1306. };
  1307. #undef LOG
  1308. #undef LOG_ERROR