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.

1436 lines
53 KiB

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