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.

1432 lines
53 KiB

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