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.

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