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.

1358 lines
50 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
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  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 );
  249. } else {
  250. int Zone = tSYS->getUTMZone();
  251. C3D_DECART tpp = tSYS->getXYZ();
  252. m_oPosition.setPositionXYZ( tpp.x + m_sCamData.x, tpp.y + m_sCamData.y, tpp.z + m_sCamData.z, Zone );
  253. #ifdef DEBUG
  254. sprintf( tB, "Finalized Sensor Location: Lat:%.3f Lon:%.3f Alt:%.3f", m_oPosition.getLat(), m_oPosition.getLon(), m_oPosition.getAlt() );
  255. LOG( tB );
  256. sprintf( tB, "Finalized Sensor Location: x:%.3f y:%.3f z:%.3f", m_oPosition.getX(), m_oPosition.getY(), m_oPosition.getZ() );
  257. LOG( tB );
  258. #endif
  259. };
  260. #ifdef DEBUG
  261. sprintf( tB, "Orientation: x:%.3f y:%.3f z:%.3f", m_sCamData.x_rot , m_sCamData.y_rot, m_sCamData.z_rot );
  262. LOG( tB );
  263. #endif
  264. m_oPosition.setOrientationDeg( m_sCamData.x_rot , m_sCamData.y_rot, m_sCamData.z_rot );
  265. if( m_pProtocol != NULL ){
  266. LOG_ERROR("Cannot Start: Service Already Running!");
  267. return false;
  268. };
  269. switch( m_sCamData.protocol ){
  270. case CAM_CTRL_PROTO_VISCA: {
  271. #ifdef DEBUG
  272. LOG("VISCA");
  273. #endif
  274. m_pProtocol = (protocolPTZ *) new protocolPTZ_VISCA( m_sCamData.protocol_aux );
  275. if( m_pProtocol!= NULL ) {
  276. argusKernel * tKRNL = argusKernel::getInstance();
  277. basicTransport * tTransport = NULL;
  278. if( tKRNL!= NULL ) {
  279. argusHardwareManager * tHM = tKRNL->getHardwareManager();
  280. if ( tHM ) {
  281. argusHardwareResource * tRES = tHM->requestResource( ARGUS_HARDWARE_PROVIDER_SERIAL, m_sCamData.uart_id );
  282. if( tRES != NULL ) {
  283. if( tRES->isValid()) {
  284. tTransport = static_cast<basicTransport *>(static_cast<argusHardwareResourceSerialProvider *>(tRES));
  285. if( m_pProtocol->setTransport( tTransport )){
  286. if( !m_pProtocol->create() ){
  287. LOG_ERROR("Protocol <<< Create >>> Failure!");
  288. };
  289. } else {
  290. LOG_ERROR("Binding Port Failure!");
  291. };
  292. };
  293. }
  294. #ifdef DEBUG
  295. else {
  296. LOG("HWM Has No UART PORT!");
  297. }
  298. #endif
  299. ;
  300. };
  301. }
  302. #ifdef DEBUG
  303. else LOG_ERROR("Can not get Kernel!");
  304. #endif
  305. if( tTransport!= NULL ) {
  306. LOG( "Bind Transport Port!" );
  307. } else {
  308. LOG_ERROR("Transport Creation Failure!");
  309. delete( ( protocolPTZ_VISCA * )m_pProtocol );
  310. m_pProtocol = NULL;
  311. }
  312. }
  313. #ifdef DEBUG
  314. else LOG_ERROR("Protocol NEW failure!");
  315. #endif
  316. break;
  317. };
  318. case CAM_CTRL_PROTO_PELCO:{
  319. #ifdef DEBUG
  320. LOG("PELCO");
  321. #endif
  322. m_pProtocol = (protocolPTZ *) new protocolPTZ_PELCO( m_sCamData.protocol_aux );
  323. if( m_pProtocol!= NULL ) {
  324. argusKernel * tKRNL = argusKernel::getInstance();
  325. basicTransport * tTransport = NULL;
  326. if( tKRNL!= NULL ) {
  327. argusHardwareManager * tHM = tKRNL->getHardwareManager();
  328. if ( tHM ) {
  329. argusHardwareResource * tRES = tHM->requestResource( ARGUS_HARDWARE_PROVIDER_SERIAL, m_sCamData.uart_id );
  330. if( tRES != NULL ) {
  331. if( tRES->isValid()) {
  332. tTransport = static_cast<basicTransport *>(static_cast<argusHardwareResourceSerialProvider *>(tRES));
  333. if( m_pProtocol->setTransport( tTransport )){
  334. if( !m_pProtocol->create() ){
  335. LOG_ERROR("Protocol <<< Create >>> Failure!");
  336. };
  337. } else {
  338. LOG_ERROR("Binding Port Failure!");
  339. };
  340. };
  341. }
  342. #ifdef DEBUG
  343. else {
  344. LOG("HWM Has No UART PORT!");
  345. }
  346. #endif
  347. ;
  348. };
  349. }
  350. #ifdef DEBUG
  351. else LOG_ERROR("Can not get Kernel!");
  352. #endif
  353. if( tTransport!= NULL ) {
  354. LOG( "Bind Transport Port!" );
  355. } else {
  356. LOG_ERROR("Transport Creation Failure!");
  357. delete( ( protocolPTZ_PELCO * )m_pProtocol );
  358. m_pProtocol = NULL;
  359. }
  360. }
  361. #ifdef DEBUG
  362. else LOG_ERROR("Protocol NEW failure!");
  363. #endif
  364. break;
  365. };
  366. case CAM_CTRL_PROTO_ONVIF:{
  367. #ifdef DEBUG
  368. LOG("ONVIF");
  369. #endif
  370. m_pProtocol = (protocolPTZ *) new protocolPTZ_ONVIF( m_sCamData.ip, m_sCamData.username, m_sCamData.password );
  371. if( m_pProtocol!=NULL ) {
  372. if( !m_pProtocol->create() ) {
  373. LOG_ERROR("Error Creating Proto!");
  374. delete( (protocolPTZ_ONVIF*) m_pProtocol );
  375. m_pProtocol = NULL;
  376. };
  377. };
  378. break;
  379. };
  380. default:{
  381. #ifdef DEBUG
  382. LOG("Camera Has no defined control protocol");
  383. #endif
  384. };
  385. };
  386. if( m_pProtocol == NULL ) {
  387. if(m_sCamData.protocol == 0 ) {
  388. m_sCamData.online = true;
  389. } else LOG_ERROR("No Proto Created!");
  390. } else {
  391. #ifdef DEBUG
  392. LOG("Proto is OK");
  393. #endif
  394. m_sCamData.online = init();
  395. };
  396. return m_sCamData.online;
  397. };
  398. //void ARGUS_Camera::setBusy( bool Status ){
  399. //m_bBusy = Status;
  400. //};
  401. bool ARGUS_Camera::relativeMove( float PanSPD, float TiltSPD, float ZoomSPD ){
  402. if( m_sCamData.online ) {
  403. uint32_t MoveMatrix = 0;
  404. if( PanSPD != .0f ){
  405. if( PanSPD > 0 ) {
  406. MoveMatrix |= PTZ_DRIVE_PAN_RIGHT;
  407. } else {
  408. MoveMatrix |= PTZ_DRIVE_PAN_LEFT;
  409. };
  410. };
  411. if( TiltSPD != .0f ){
  412. if( TiltSPD > 0 ) {
  413. MoveMatrix |= PTZ_DRIVE_TILT_UP;
  414. } else {
  415. MoveMatrix |= PTZ_DRIVE_TILT_DOWN;
  416. };
  417. };
  418. if( ZoomSPD != .0f ){
  419. if( ZoomSPD > 0 ) {
  420. MoveMatrix |= PTZ_DRIVE_ZOOM_WIDE;
  421. } else {
  422. MoveMatrix |= PTZ_DRIVE_ZOOM_TELE;
  423. };
  424. };
  425. if( MoveMatrix != 0 ) {
  426. m_pProtocol->setPTZSpeed( fabs(PanSPD), fabs(TiltSPD), fabs(ZoomSPD) );
  427. };
  428. return m_pProtocol->move( MoveMatrix );
  429. };
  430. return false;
  431. };
  432. bool ARGUS_Camera::relativeStop(){
  433. if( m_sCamData.online ) {
  434. return m_pProtocol->move( 0 );
  435. };
  436. return false;
  437. };
  438. CAM_TASK *ARGUS_Camera::fetchCamTask( void ){
  439. CAM_TASK *rVal = NULL;
  440. m_mQPROTECTOR.lock();
  441. if (m_qCAM_TASK.size()) {
  442. rVal = m_qCAM_TASK.front();
  443. m_qCAM_TASK.pop();
  444. };
  445. m_mQPROTECTOR.unlock();
  446. return rVal;
  447. };
  448. bool ARGUS_Camera::pushCamTask( CAM_TASK *ptr ){
  449. if ( ptr == NULL ) return false;
  450. m_mQPROTECTOR.lock();
  451. m_qCAM_TASK.push( ptr );
  452. m_mQPROTECTOR.unlock();
  453. return true;
  454. };
  455. bool ARGUS_Camera::fusion_compare( CAM_ROI *ptr, C3D_SPH_DEGREES tp ){
  456. if( ptr == NULL ) return false;
  457. float tFI = fabsf( ptr->cam_position.fi - tp.fi );
  458. float tTETTA = fabsf( ptr->cam_position.tetta - tp.tetta );
  459. #ifdef DEBUG
  460. char tB[128];
  461. 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 );
  462. LOG(tB);
  463. #endif
  464. if( tFI > ptr->h_fov ) return false;
  465. if( tTETTA > ptr->v_fov ) return false;
  466. if( tp.r < ptr->min_range ) return false;
  467. if( tp.r > ptr->max_range ) return false;
  468. return true;
  469. };
  470. bool ARGUS_Camera::fusion_consumeData( uint32_t db_id, C3D_SPH_DEGREES tOR ){
  471. if( db_id == 0 ) return false;
  472. CAM_TRACK_RECORD * tTrackRecords = (CAM_TRACK_RECORD * ) malloc( sizeof(CAM_TRACK_RECORD));
  473. if( tTrackRecords == NULL ) return false;
  474. #ifdef DEBUG
  475. LOG("Fusion: Consuming new Data!");
  476. #endif
  477. memset(tTrackRecords, 0, sizeof(CAM_TRACK_RECORD));
  478. tTrackRecords->target_direction.fi = tOR.fi;
  479. tTrackRecords->target_direction.r = tOR.r;
  480. tTrackRecords->target_direction.tetta = tOR.tetta;
  481. tTrackRecords->evt_db_ID = db_id;
  482. tTrackRecords->ts_ms = getMilliseconds();
  483. m_mROIsProtector.lock();
  484. if( m_vROIS.size() ){
  485. vCAM_ROIS::iterator idx = m_vROIS.begin();
  486. for( ; idx < m_vROIS.end(); idx++ ) if ( fusion_compare( *idx, tOR ) ) break;
  487. if ( idx != m_vROIS.end() ) {
  488. // have Match!
  489. LOG("ROI MATCHED!");
  490. (*idx)->targets.push_back( tTrackRecords );
  491. fusion_validateROI( tTrackRecords->ts_ms, (*idx) );
  492. } else {
  493. LOG("NO MATCH FOUND!");
  494. // No mathced ROI -> create new one
  495. CAM_ROI * tROI = (CAM_ROI *)malloc(sizeof(CAM_ROI));
  496. if ( tROI ) {
  497. memset( tROI, 0 , sizeof(CAM_ROI) );
  498. tROI->ts_ms = tTrackRecords->ts_ms;
  499. spherical2onvif( tOR, tROI->cam_orientation, m_fMaxRange, m_sCamData.pan_model, m_sCamData.pan_angles, m_sCamData.tilt_model, m_sCamData.tilt_angles );
  500. tROI->cam_position.fi = tOR.fi;
  501. tROI->cam_position.tetta = tOR.tetta;
  502. tROI->cam_position.r = tOR.r;
  503. float cFL = m_fMinFL + m_fR2FL * ( tROI->cam_position.r - m_fMinRange );
  504. tROI->max_range = calcMaxDistance_mm( AC_imager_diag[m_sCamData.imager_type], cFL, m_nRealFrameWidth );
  505. tROI->min_range = calcMinDistance_mm( AC_imager_diag[m_sCamData.imager_type], cFL, m_nRealFrameWidth );
  506. tROI->h_fov = rad2deg( calcFOV_mm( AC_imager_width[m_sCamData.imager_type], cFL ) ) / 2.0f; // div to no to div in comparation function
  507. tROI->v_fov = rad2deg( calcFOV_mm( AC_imager_height[m_sCamData.imager_type], cFL ) ) / 2.0f; // that will be called more ofthen
  508. tROI->targets.push_back( tTrackRecords );
  509. m_vROIS.push_back( tROI );
  510. };
  511. };
  512. } else {
  513. // empty ROIs -> create new one
  514. CAM_ROI * tROI = (CAM_ROI *)malloc(sizeof(CAM_ROI));
  515. if ( tROI ) {
  516. memset( tROI, 0 , sizeof(CAM_ROI) );
  517. tROI->ts_ms = tTrackRecords->ts_ms;
  518. //Set vector Cam Coordinates
  519. tROI->cam_position.fi = tOR.fi;
  520. tROI->cam_position.tetta = tOR.tetta;
  521. tROI->cam_position.r = tOR.r;
  522. // Set vector OnVIF coordinates
  523. spherical2onvif( tOR, tROI->cam_orientation, m_fMaxRange, m_sCamData.pan_model, m_sCamData.pan_angles, m_sCamData.tilt_model, m_sCamData.tilt_angles );
  524. // make fusion data BOX
  525. float cFL = m_fMinFL + m_fR2FL * ( tROI->cam_position.r );// - m_fMinRange );
  526. tROI->min_range = calcMinDistance_mm( AC_imager_diag[m_sCamData.imager_type], cFL, m_nRealFrameWidth );
  527. tROI->max_range = calcMaxDistance_mm( AC_imager_diag[m_sCamData.imager_type], cFL, m_nRealFrameWidth );
  528. tROI->h_fov = rad2deg( calcFOV_mm( AC_imager_width[m_sCamData.imager_type], cFL ) )/ 2.0f; // div to prevent division in comparation
  529. tROI->v_fov = rad2deg( calcFOV_mm( AC_imager_height[m_sCamData.imager_type], cFL ) )/ 2.0f; // function that will be called more ofthen
  530. // Add ROI to vector
  531. tROI->targets.push_back( tTrackRecords );
  532. m_vROIS.push_back( tROI );
  533. };
  534. };
  535. m_mROIsProtector.unlock();
  536. return true;
  537. };
  538. bool ARGUS_Camera::fusion_validateROI( uint64_t cts, CAM_ROI *ptr ){
  539. if( ptr == NULL ) return false;
  540. if( (cts - ptr->ts_ms) > LOSE_INTERES_TIME_MS ) return false;
  541. #ifdef DEBUG
  542. char tB[128];
  543. sprintf( tB, "\tROI [%d]", ptr->targets.size() );
  544. LOG( tB );
  545. #endif
  546. if( ptr->targets.size() ) {
  547. double tA = 0.0;
  548. double tB = 0.0;
  549. double tC = 0.0;
  550. uint32_t tCNT = 0;
  551. for( vCAM_TRACK_RECORDS::iterator idx = ptr->targets.begin(); idx < ptr->targets.end(); idx++ ) {
  552. if( ( cts - (*idx)->ts_ms ) > LOSE_INTERES_TIME_MS ) {
  553. free( (*idx) );
  554. ptr->targets.erase( idx );
  555. } else {
  556. tA = tA + (*idx)->target_direction.fi;
  557. tB = tB + (*idx)->target_direction.tetta;
  558. tC = tC + (*idx)->target_direction.r;
  559. tCNT++;
  560. };
  561. };
  562. if( tCNT ) {
  563. C3D_SPH_DEGREES L = { 0 };
  564. L.fi = (float) ( tA /(double) tCNT );
  565. L.tetta = (float) ( tB /(double) tCNT );
  566. L.r = (float) ( tC /(double) tCNT );
  567. spherical2onvif(L, ptr->cam_orientation, m_fMaxRange, m_sCamData.pan_model, m_sCamData.pan_angles, m_sCamData.tilt_model, m_sCamData.tilt_angles);
  568. ptr->ts_ms = cts;
  569. float cFL = m_fMinFL + m_fR2FL * ( ptr->cam_position.r - m_fMinRange );
  570. ptr->max_range = calcMaxDistance_mm( AC_imager_diag[m_sCamData.imager_type], cFL, m_nRealFrameWidth );
  571. ptr->min_range = calcMinDistance_mm( AC_imager_diag[m_sCamData.imager_type], cFL, m_nRealFrameWidth );
  572. ptr->h_fov = rad2deg( calcFOV_mm(AC_imager_width[m_sCamData.imager_type], cFL) )/ 2.0f; // div to prevent division in comparation
  573. ptr->v_fov = rad2deg( calcFOV_mm( AC_imager_height[m_sCamData.imager_type], cFL ) )/ 2.0f; // function that will be called more ofthen
  574. return true;
  575. };
  576. };
  577. return false;
  578. };
  579. CAM_ROI *ARGUS_Camera::fusion_fetchROI( void ){
  580. CAM_ROI *tVAL = NULL;
  581. uint64_t cts = 0;
  582. m_mROIsProtector.lock();
  583. vCAM_ROIS::iterator sRec;
  584. if( m_vROIS.size() ){
  585. for( vCAM_ROIS::iterator idx = m_vROIS.begin(); idx < m_vROIS.end(); idx++ ){
  586. if ( tVAL == NULL ) {
  587. sRec = idx;
  588. tVAL = (*idx);
  589. cts = tVAL->ts_ms;
  590. } else {
  591. #ifdef CAM_FETCH_ROI_NEWEST
  592. if ( (*idx)->ts_ms > cts )
  593. #else
  594. if ( (*idx)->ts_ms < cts )
  595. #endif
  596. {
  597. sRec = idx;
  598. tVAL = (*idx);
  599. cts = tVAL->ts_ms;
  600. };
  601. };
  602. };
  603. m_vROIS.erase( sRec );
  604. };
  605. m_mROIsProtector.unlock();
  606. return tVAL;
  607. };
  608. DBC_SNAPSHOT_DATA *ARGUS_Camera::fusion_translate_ROI( CAM_ROI *(&ptr) ){
  609. DBC_SNAPSHOT_DATA *rVal = NULL;
  610. if( ptr == NULL ) return NULL;
  611. if( ptr->targets.size() ){
  612. if( m_bSourceConnected ){
  613. rVal = (DBC_SNAPSHOT_DATA *)malloc(sizeof(DBC_SNAPSHOT_DATA));
  614. if( rVal ){
  615. memset( rVal, 0, sizeof(DBC_SNAPSHOT_DATA) );
  616. rVal->stream_id = m_nActiveStreamID;
  617. for( vCAM_TRACK_RECORDS::iterator idx = ptr->targets.begin(); idx < ptr->targets.end(); idx++ ) {
  618. rVal->assoc_event_ids.push_back( (*idx)->evt_db_ID );
  619. };
  620. };
  621. };
  622. ptr->targets.clear();
  623. };
  624. // Destruct ROI MEM
  625. delete( (CAM_ROI *) ptr );
  626. ptr = NULL;
  627. // Not Setting MD5 and TS -> will set at capture time
  628. return rVal;
  629. };
  630. void ARGUS_Camera::fusion_validate( void ){
  631. m_mROIsProtector.lock();
  632. if( m_vROIS.size() ){
  633. #ifdef DEBUG
  634. char tB[128];
  635. sprintf( tB, "Fusion: Have [%d] Regions Of Interes", m_vROIS.size() );
  636. LOG( tB );
  637. #endif
  638. uint64_t cts = getMilliseconds();
  639. for( vCAM_ROIS::iterator idx = m_vROIS.begin(); idx < m_vROIS.end(); idx++ ){
  640. if( !fusion_validateROI( cts, *idx ) ) {
  641. free( (*idx) );
  642. m_vROIS.erase( idx );
  643. };
  644. };
  645. };
  646. m_mROIsProtector.unlock();
  647. };
  648. bool ARGUS_Camera::absoluteMove( uint32_t TargetRefID, float PanPOS, float TiltPOS, float ZoomPOS ){
  649. if( m_sCamData.online ){
  650. CAM_TASK *tTask = (CAM_TASK *)malloc(sizeof(CAM_TASK));
  651. if( tTask ){
  652. memset(tTask, 0, sizeof(CAM_TASK));
  653. tTask->CMD = (CAM_PTZ_CMD *)malloc(sizeof(CAM_PTZ_CMD));
  654. if( tTask->CMD ){
  655. tTask->CMD->CMD = PTZ_ABSOLUTE;
  656. tTask->CMD->PAN = PanPOS;
  657. tTask->CMD->TILT = TiltPOS;
  658. tTask->CMD->ZOOM = ZoomPOS;
  659. tTask->movemet_needed = true;
  660. tTask->source_evt_id = TargetRefID;
  661. setWM( CAM_WS_BUSY );
  662. return pushCamTask( tTask );
  663. } else {
  664. LOG_ERROR("CMD malloc Error!");
  665. free( tTask );
  666. };
  667. } else {
  668. LOG_ERROR("Task malloc error!");
  669. };
  670. };
  671. return false;
  672. };
  673. bool ARGUS_Camera::absoluteMove( uint32_t TargetRefID, C3D_ORIENT_ONVIF O ){
  674. if( m_sCamData.online ){
  675. CAM_TASK *tTask = (CAM_TASK *)malloc(sizeof(CAM_TASK));
  676. if( tTask ){
  677. memset(tTask, 0, sizeof(CAM_TASK));
  678. tTask->CMD = (CAM_PTZ_CMD *)malloc(sizeof(CAM_PTZ_CMD));
  679. if( tTask->CMD ){
  680. tTask->CMD->CMD = PTZ_ABSOLUTE;
  681. tTask->CMD->PAN = O.pan;
  682. tTask->CMD->TILT = O.tilt;
  683. tTask->CMD->ZOOM = O.zoom;
  684. tTask->movemet_needed = true;
  685. tTask->source_evt_id = TargetRefID;
  686. setWM( CAM_WS_BUSY );
  687. return pushCamTask( tTask );
  688. } else {
  689. LOG_ERROR("CMD malloc Error!");
  690. free( tTask );
  691. };
  692. } else {
  693. LOG_ERROR("Task malloc error!");
  694. };
  695. };
  696. return false;
  697. };
  698. void ARGUS_Camera::m_fThreadProc( void ){
  699. DBC_PUSHED_CMD * PUSHED = NULL;
  700. m_bThreadStatus = TH_WORKING;
  701. while( m_bThreadStatus < TH_ENDED ) {
  702. //Local Recorder
  703. if( m_bSourceConnected ) {
  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, -1, -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. if( ( PUSHED->CMD & ARGUS_CAM_PCMD_CMD_MASK ) == ARGUS_CAM_PCMD_CMD_MASK ) {
  951. switch( PUSHED->CMD ) {
  952. case 0x80:{
  953. //m_bWM = ANODE_WM_2D_ISSUE_FULL_RESET;
  954. break;
  955. };
  956. case 0x81:{
  957. //m_bWM = ANODE_WM_2D_GET_FAST_DRAW;
  958. break;
  959. };
  960. case 0x82:{
  961. break;
  962. };
  963. }; //?? Check if we don't miss something!!
  964. } else if ( tState == CAM_WS_READY || tState == CAM_WS_AUTOMATED_MOVE ){
  965. //Direct Movment Mode
  966. if( PUSHED->CMD == ARGUS_CAM_PCMD_STOP ) {
  967. setWM( CAM_WS_STOP_MOVE );
  968. } else {
  969. char tB[64];
  970. if( tState != CAM_WS_AUTOMATED_MOVE ) m_nRelativeMoveCounter = 0;
  971. m_bRelativeMoveMatrix = ( PUSHED->CMD ) >> 1;
  972. setWM( CAM_WS_AUTOMATED_MOVE );
  973. };
  974. };
  975. free( PUSHED );
  976. PUSHED = NULL;
  977. };
  978. };
  979. break;
  980. };
  981. case CAM_WS_END:{
  982. LOG("Exit Command Rcpt");
  983. m_bThreadStatus = TH_ENDED;
  984. //setBusy( true );
  985. break;
  986. };
  987. };
  988. SLEEP( DEFAULT_THREAD_SLEEP_TIME );
  989. };
  990. //LOG("CTRL Loop End!");
  991. };
  992. bool ARGUS_Camera::testFOV( C3D_DECART L ){
  993. //System Coords -> trans to local and go to funct
  994. systemAnchor* tSystem = systemAnchor::getInstance();
  995. if ( tSystem == NULL ) return false;
  996. C3D_SPH_DEGREES SL;
  997. if( !tSystem->convertToLocal( &m_oPosition, L, SL ) ) return false;
  998. return testFOV( SL );
  999. };
  1000. bool ARGUS_Camera::testFOV( C3D_WGS84_LOC L ){
  1001. //Global Coords
  1002. systemAnchor* tSystem = systemAnchor::getInstance();
  1003. if ( tSystem == NULL ) return false;
  1004. C3D_SPH_DEGREES SL;
  1005. if( !tSystem->convertToLocal( &m_oPosition, L, SL ) ) return false;
  1006. return testFOV( SL );
  1007. };
  1008. bool ARGUS_Camera::testFOV( C3D_SPH_RADIANS L ){
  1009. //Local Coords
  1010. C3D_SPH_DEGREES SL;
  1011. spherical2spherical( L, SL );
  1012. return testFOV( SL );
  1013. };
  1014. bool ARGUS_Camera::testFOV( C3D_SPH_DEGREES SL ){
  1015. #ifdef DEBUG
  1016. char tB[128];
  1017. #endif
  1018. switch ( m_sCamData.triggering_mode ) {
  1019. case CAM_TRIGGER_STRICT:{
  1020. if( SL.r > m_fMaxRange || SL.r < m_fMinRange ){
  1021. #ifdef DEBUG
  1022. sprintf( tB, "Target Range( %.3f.m ) is not in a Range %.3f.m <-> %.3f.m", SL.r, m_fMinRange, m_fMaxRange );
  1023. LOG( tB );
  1024. #endif
  1025. return false;
  1026. };
  1027. break;
  1028. };
  1029. case CAM_TRIGGER_ALL_NEAR:{
  1030. if( SL.r > m_fMaxRange ){
  1031. #ifdef DEBUG
  1032. sprintf( tB, "Target Range( %.3f.m ) is not in a Range %.3f.m <-> %.3f.m", SL.r, m_fMinRange, m_fMaxRange );
  1033. LOG( tB );
  1034. #endif
  1035. return false;
  1036. };
  1037. break;
  1038. };
  1039. case CAM_TRIGGER_ALL_FAR:{
  1040. if( SL.r < m_fMinRange ){
  1041. #ifdef DEBUG
  1042. sprintf( tB, "Target Range( %.3f.m ) is not in a Range %.3f.m <-> %.3f.m", SL.r, m_fMinRange, m_fMaxRange );
  1043. LOG( tB );
  1044. #endif
  1045. return false;
  1046. };
  1047. break;
  1048. };
  1049. case CAM_TRIGGER_ALL: return true;
  1050. };
  1051. float HS = AC_imager_width[m_sCamData.imager_type];
  1052. float VS = AC_imager_height[m_sCamData.imager_type];
  1053. float HS2 = HS * HS;
  1054. float VS2 = VS * VS;
  1055. float IR = HS / VS;
  1056. float IR2 = IR * IR;
  1057. if( m_sCamData.pan_angles == 0 ) {
  1058. // Fixed Camera
  1059. float tA = fabsf( SL.fi );
  1060. float tZ = 0.5f * ( m_fMinHFov + ( SL.r - m_fMinRange ) * m_fR2FL );
  1061. if ( tA > tZ ) return false;
  1062. } else {
  1063. if( m_sCamData.pan_angles < 360 ) { // If endless PAN -> PASSED
  1064. // Partial Pan
  1065. switch( m_sCamData.pan_model ) {
  1066. case 1: {
  1067. // Regular Pan
  1068. float tFOV = 0.5f * ( m_fMinHFov + ( SL.r - m_fMinRange ) * m_fR2FL );
  1069. float tRad = deg2rad( SL.fi );
  1070. #ifdef DEBUG
  1071. sprintf( tB, "FOV: %0.3f CC: %0.3f", tFOV, tRad );
  1072. LOG( tB );
  1073. #endif
  1074. if( ( tRad < ( -1.0 * tFOV ) ) || ( tRad > ( deg2rad( m_sCamData.pan_angles ) + tFOV ) ) ) return false;
  1075. break;
  1076. };
  1077. case 2:{
  1078. break;
  1079. };
  1080. case 3:{
  1081. break;
  1082. };
  1083. };
  1084. };
  1085. };
  1086. if( m_sCamData.tilt_angles == 0 ){
  1087. //Fixed Camera
  1088. if ( ( SL.tetta < ( m_fMaxVFov / -2.0f ) ) || ( SL.tetta > ( m_fMaxVFov / 2.0f ) ) )return false;
  1089. } else {
  1090. if( m_sCamData.tilt_angles < 90 ) { // if Cam CAN DO 90 -> Pass
  1091. // Partial Tilt
  1092. switch( m_sCamData.tilt_model ) {
  1093. case 1: {
  1094. // Regular Tilt
  1095. float tFOV = 0.5f * ( m_fMinVFov + ( m_fMaxVFov - m_fMinVFov ) * ( SL.r / m_fMaxRange ) ) ;
  1096. float tRad = deg2rad( SL.tetta );
  1097. if( ( tRad < ( -1.0 * tFOV ) ) || ( tRad > ( deg2rad( m_sCamData.tilt_angles ) + tFOV ) ) )return false;
  1098. break;
  1099. };
  1100. case 2:{
  1101. break;
  1102. };
  1103. };
  1104. };
  1105. };
  1106. return true;
  1107. };
  1108. bool ARGUS_Camera::lookAt( uint32_t TargetRefID, C3D_WGS84_LOC L ){
  1109. systemAnchor* tSystem = systemAnchor::getInstance();
  1110. if( tSystem == NULL ) return false;
  1111. C3D_SPH_DEGREES SL;
  1112. tSystem->convertToLocal(&m_oPosition, L, SL );
  1113. return lookAt(TargetRefID, SL );
  1114. };
  1115. bool ARGUS_Camera::lookAt( uint32_t TargetRefID, C3D_DECART L ){
  1116. systemAnchor* tSystem = systemAnchor::getInstance();
  1117. if( tSystem == NULL ) return false;
  1118. C3D_SPH_DEGREES SL;
  1119. tSystem->convertToLocal( &m_oPosition, L, SL );
  1120. return lookAt(TargetRefID, SL );
  1121. };
  1122. bool ARGUS_Camera::lookAt( uint32_t TargetRefID, C3D_SPH_RADIANS L ){
  1123. C3D_SPH_DEGREES SL;
  1124. spherical2spherical( L, SL );
  1125. return lookAt(TargetRefID, SL );
  1126. };
  1127. bool ARGUS_Camera::lookAt( uint32_t TargetRefID, C3D_SPH_DEGREES L ){
  1128. //char tB[128];
  1129. //sprintf(tB, "Target On %.3f %.3f %.3f", L.r, L.fi, L.tetta );
  1130. //LOG( tB );
  1131. //sprintf( tB, "Cam GoTo %.3f %.3f %.3f", fP, fT, fZ );
  1132. //LOG( tB );
  1133. //if( getWM() == CAM_WS_READY ){
  1134. // C3D_ORIENT_ONVIF LL;
  1135. // spherical2onvif( L, LL, m_fMaxRange, m_sCamData.pan_model, m_sCamData.pan_angles, m_sCamData.tilt_model, m_sCamData.tilt_angles );
  1136. // return absoluteMove( TargetRefID, LL );
  1137. //};
  1138. // Camera is Busy
  1139. return fusion_consumeData( TargetRefID, L );
  1140. };
  1141. bool ARGUS_Camera::makeSnap( uint32_t TargetRefID ){
  1142. CAM_TASK *tTask = (CAM_TASK *)malloc(sizeof(CAM_TASK));
  1143. if( tTask ){
  1144. memset(tTask, 0, sizeof(CAM_TASK));
  1145. tTask->CMD = (CAM_PTZ_CMD *) malloc( sizeof(CAM_PTZ_CMD) );
  1146. if( tTask->CMD ){
  1147. tTask->CMD->CMD = PTZ_CAPTURE_ONLY;
  1148. tTask->source_evt_id = TargetRefID;
  1149. return pushCamTask( tTask );
  1150. } else {
  1151. free( tTask );
  1152. };
  1153. };
  1154. return false;
  1155. };
  1156. bool ARGUS_Camera::connectToStream( uint32_t id ){
  1157. if( id ){
  1158. argusKernelModule * tKM = NULL;
  1159. argusKernel * tKRNL = NULL;
  1160. argusModuleFactory * tMF = NULL;
  1161. if( m_pDBC == NULL ) {
  1162. LOG_ERROR("No Database Connector!");
  1163. return false;
  1164. };
  1165. tKRNL = argusKernel::getInstance();
  1166. if( tKRNL ) {
  1167. tMF = tKRNL->getModuleFactory();
  1168. if( tMF ) {
  1169. tKM = tMF->getModule( ARGUS_MODULE_EMBED_VIDEO );
  1170. } else {
  1171. LOG_ERROR("No ModuleFactory Instance!");
  1172. return false;
  1173. };
  1174. } else {
  1175. LOG_ERROR("No Kernel Instance!");
  1176. return false;
  1177. };
  1178. if( tKM ) {
  1179. DBC_MEDIA_STREAM_DATA *tDATA = m_pDBC->getMediaStreamData( id );
  1180. if( tDATA ) {
  1181. LOG( tDATA->mrl_proto );
  1182. m_sMediaSource.mediaStreamId = id;
  1183. m_pSource = new videoAVLIBSource( &m_sMediaSource );
  1184. if( m_pSource != NULL ) {
  1185. char tMRL[1024];
  1186. #ifdef DEBUG
  1187. LOG("Source OK");
  1188. #endif
  1189. int tW = ((argusModuleEmbedVideo*)tKM)->getWidh();
  1190. int tH = ((argusModuleEmbedVideo*)tKM)->getHeight();
  1191. updateOpticalParameters( tW, tH );
  1192. if( tW > 0 ) {
  1193. //m_pSource->setProportionalByWidth( tW );
  1194. m_pSource->setSize( tW, tH );
  1195. };
  1196. if( strncmp( tDATA->mrl_proto, "v4l2", 4 ) == 0 ){
  1197. sprintf( tMRL, tDATA->mrl );
  1198. } else {
  1199. DBC_CAM_DATA *tCAM = m_pDBC->getCamData( tDATA->source_id );
  1200. if( tCAM ) {
  1201. sprintf( tMRL, "%s://%s:%s@%s:%d%s", tDATA->mrl_proto, tCAM->username, tCAM->password, tCAM->ip, tDATA->mrl_port, tDATA->mrl );
  1202. free( tCAM );
  1203. } else {
  1204. LOG_ERROR("Error Getting CAM Data!");
  1205. delete( m_pSource );
  1206. m_pSource = NULL;
  1207. return false;
  1208. };
  1209. };
  1210. LOG( tMRL );
  1211. //m_pSource = new videoAVLIBSource( tMRL, &m_sMediaSource );
  1212. if( m_pSource->_open( tMRL ) ) {
  1213. m_bSourceConnected = m_pSource->attachDestination( (argusModuleEmbedVideo*) tKM );
  1214. if ( !m_bSourceConnected ) {
  1215. delete( m_pSource );
  1216. m_pSource = NULL;
  1217. } else {
  1218. m_nActiveStreamID = id;
  1219. #ifdef DEBUG
  1220. LOG("Source is Connected to Output.");
  1221. #endif
  1222. };
  1223. } else {
  1224. LOG_ERROR("Can not Open Video Source!");
  1225. delete( m_pSource );
  1226. m_pSource = NULL;
  1227. };
  1228. } else {
  1229. LOG_ERROR("Video Source Stream Failure!");
  1230. };
  1231. free( tDATA );
  1232. };
  1233. }
  1234. #ifdef DEBUG
  1235. else LOG("No Video Module!")
  1236. #endif
  1237. ;
  1238. };
  1239. return m_bSourceConnected;
  1240. };
  1241. bool ARGUS_Camera::disconnectFromStream( void ){
  1242. if( m_bSourceConnected ) {
  1243. if( m_pSource ){
  1244. delete( m_pSource );
  1245. m_pSource = NULL;
  1246. };
  1247. m_bSourceConnected = false;
  1248. };
  1249. return !m_bSourceConnected;
  1250. };
  1251. bool ARGUS_Camera::validateStream( void ){
  1252. if( m_bSourceConnected ) {
  1253. if( m_pSource ) {
  1254. if( !m_pSource->isFinished() ){
  1255. return true;
  1256. };
  1257. LOG("Source is Finished!");
  1258. } else {
  1259. LOG_ERROR("No Source Object! Set Disconnected!");
  1260. m_bSourceConnected = false;
  1261. };
  1262. };
  1263. return false;
  1264. };
  1265. #undef LOG
  1266. #undef LOG_ERROR