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.

1386 lines
52 KiB

​
​
6 years ago
​
​
​
​
​
​
​
​
​
​
​
6 years ago
​
6 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
6 years ago
​
​
​
​
​
​
​
​
​
​
​
​
6 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. #include "rgooseNode3D.h"
  2. #define LOG( X ) m_pLog->Log( "NODE3D", this, X )
  3. #define LOG_ERROR( X ) m_pLog->Log( "NODE3D", this, _LOG_ERROR, X )
  4. argus3DLIDAR::argus3DLIDAR( uint32_t ID ): argusNetNode( ID ){
  5. m_pLog = basicLogCollection::getInstance();
  6. m_pEventHeap = basicEventHeap::getInstance();
  7. DBC_LRF_DATA *tDATA = m_pDBC->getLRFData( m_sNodeData.aux_id );
  8. //m_pThresholdData = NULL;
  9. m_pThresholdData = (thresholdNode *) malloc( sizeof(thresholdNode) * TH_SIZE_NODES );
  10. m_nFullBorderDataIndex = 0;
  11. m_nFullBorderKernelIndex = 0;
  12. m_bTWINC = false;
  13. m_pShadow = NULL;
  14. if( tDATA != NULL ) {
  15. memcpy( &m_sDATA, tDATA, sizeof(DBC_LRF_DATA));
  16. free( tDATA );
  17. } else {
  18. memset( &m_sDATA, 0, sizeof(DBC_LRF_DATA));
  19. };
  20. };
  21. argus3DLIDAR::~argus3DLIDAR(){
  22. stopService();
  23. };
  24. bool argus3DLIDAR::startService(){
  25. #ifdef DEBUG
  26. LOG("START SERVICE");
  27. #endif
  28. if( m_pDBC == NULL ) {
  29. LOG_ERROR("No Database Connector Defined!");
  30. return false;
  31. };
  32. LOG("1");
  33. m_oPosition.setPositionXYZ( m_sDATA.x , m_sDATA.y, m_sDATA.z );
  34. m_oPosition.setOrientationDeg( m_sDATA.x_rot , m_sDATA.y_rot, m_sDATA.z_rot );
  35. LOG("2");
  36. // Check if Allredy Working
  37. if( m_bThreadStatus > TH_ARMED ){
  38. if( m_bThreadStatus == TH_WORKING ) return true;
  39. return false;
  40. };
  41. LOG("3");
  42. m_pShadow = new ShadowMask();
  43. if( m_pShadow ) {
  44. m_pShadow->disableAutoSave();
  45. m_pShadow->clear();
  46. };
  47. LOG("4");
  48. return init();
  49. };
  50. bool argus3DLIDAR::stopService(){
  51. LOG("STOP SERVICE");
  52. LOG("unlock all mutexes");
  53. m_mDataMutex.unlock();
  54. m_mStatusMutex.unlock();
  55. m_mPackageMutex.unlock();
  56. LOG("Kill Thread");
  57. waitThreadEnd( true );
  58. LOG("Clean Data");
  59. cleanDataTH();
  60. LOG("Remove Shadow");
  61. if( m_pShadow ) delete( m_pShadow );
  62. LOG("Done!");
  63. return true;
  64. };
  65. bool argus3DLIDAR::validate(){
  66. DBC_PUSHED_CMD *tCMD = m_pDBC->getNodePushedCMD( m_sNodeData.node_id );
  67. if ( tCMD!= NULL ) {
  68. switch( tCMD->CMD ){
  69. case ARGUS_LRF_PCMD_FULL_RESET:{
  70. #ifdef DEBUG
  71. LOG("Full Reset Push CMD!");
  72. #endif
  73. NODE_COMMAND *tNCMD = (NODE_COMMAND *) malloc( sizeof(NODE_COMMAND));
  74. if (tNCMD) {
  75. tNCMD->cmd = NODE_CMD_FULL_RESET;
  76. tNCMD->parameter = NULL;
  77. LOG("B E");
  78. enqueueCommand( tNCMD );
  79. LOG("A E");
  80. };
  81. break;
  82. };
  83. case ARGUS_LRF_PCMD_DRAW:{
  84. #ifdef DEBUG
  85. LOG("Get Draw Push CMD!");
  86. #endif
  87. NODE_COMMAND *tNCMD = (NODE_COMMAND *) malloc( sizeof(NODE_COMMAND));
  88. if (tNCMD) {
  89. tNCMD->cmd = NODE_CMD_GET_DEVICE_DRAW;
  90. tNCMD->parameter = NULL;
  91. enqueueCommand( tNCMD );
  92. }
  93. break;
  94. };
  95. case ARGUS_LRF_PCMD_UPLOAD_FILE:{
  96. #ifdef DEBUG
  97. LOG("Upload File CMD!");
  98. #endif
  99. uint16_t sz = strlen(tCMD->PARAM);
  100. if( sz ){
  101. NODE_COMMAND *tNCMD = (NODE_COMMAND *)malloc(sizeof(NODE_COMMAND));
  102. if( tNCMD ){
  103. tNCMD->cmd = NODE_CMD_UPLOAD_FILE;
  104. tNCMD->parameter = malloc( sz+1 );
  105. if ( tNCMD->parameter != NULL ) {
  106. memcpy(tNCMD->parameter, tCMD->PARAM, sz);
  107. ((uint8_t *)tNCMD->parameter)[sz] = 0;
  108. enqueueCommand( tNCMD );
  109. } else {
  110. free( tNCMD );
  111. };
  112. };
  113. };
  114. break;
  115. };
  116. case ARGUS_LRF_PCMD_SET_MASK_1:
  117. case ARGUS_LRF_PCMD_SET_MASK_2:
  118. case ARGUS_LRF_PCMD_SET_MASK_3:
  119. case ARGUS_LRF_PCMD_SET_MASK_4:
  120. case ARGUS_LRF_PCMD_SET_MASK_5:{
  121. //Rasterize Masks and upload
  122. m_nCurrentMaskID = m_pDBC->getLRFMaskID( m_sNodeData.aux_id ); // 3DLRF has onli one mask
  123. if( m_nCurrentMaskID ){
  124. if( m_pShadow == NULL ){
  125. m_pShadow = new ShadowMask();
  126. };
  127. if ( m_pShadow != NULL ) {
  128. m_pShadow->clear();
  129. DBC_INT_LIST *tVertexes = m_pDBC->listLRFMaskVertexes( m_nCurrentMaskID );
  130. if ( tVertexes ){
  131. if( tVertexes->size() ){
  132. LOG("Has POLY");
  133. for ( int vidx=0; vidx < tVertexes->size(); vidx++ ) {
  134. simplePolygone *tPoly = m_pDBC->loadLRFMaskVertex( tVertexes->at(vidx) );
  135. if( tPoly ) {
  136. LOG("POLY LOADDED!");
  137. m_pShadow->addPoly( tPoly );
  138. LOG("POLY ADDED!");
  139. if( m_pShadow->rasterizeVertexes() ){
  140. LOG("Vertexes Rasterized!");
  141. } else {
  142. LOG("Vertexes ARE NOT Rasterized!");
  143. };
  144. };
  145. };
  146. LOG("Create Command!");
  147. NODE_COMMAND *tNCMD = (NODE_COMMAND *)malloc(sizeof(NODE_COMMAND));
  148. if( tNCMD ){
  149. tNCMD->cmd = NODE_CMD_UPLOAD_SHADOW;
  150. tNCMD->parameter = NULL;
  151. m_nCurrentPolyID = 0;
  152. enqueueCommand( tNCMD );
  153. LOG("Command Queued!");
  154. };
  155. }
  156. delete( tVertexes );
  157. };
  158. };
  159. };
  160. };
  161. };
  162. free(tCMD);
  163. };
  164. return true;
  165. };
  166. void argus3DLIDAR::simulateTarget( void ){
  167. TARGET_DATA * tData;
  168. CLIENT_EVENT cEVT;
  169. cEVT.range = 1050;
  170. cEVT.type = EVT_TARGET_3D_WHITE;
  171. cEVT.th_id = 1211;
  172. cEVT.a_th_node = 1211;
  173. cEVT.k_id = 5;
  174. cEVT.w_cnt = 5;
  175. //tData = m_pMasterNode->
  176. tData = prepareTargetData( &cEVT );
  177. if( tData != NULL ){
  178. EVENT_CONTAINER * bEVT = createBasicEvent( getDbId(), EVT_CLASS_SENSOR_3D, (basicEventTypeID)cEVT.type, NULL, NULL, tData );
  179. if (bEVT != NULL) {
  180. m_pEventHeap->broadcast( bEVT );
  181. };
  182. };
  183. };
  184. void argus3DLIDAR::m_fThreadProc(void){
  185. uint32_t nTimeOut = 20;
  186. clientPort * tPort = NULL;
  187. m_bThreadStatus = TH_WORKING;
  188. while( m_bThreadStatus < TH_ENDED ){
  189. uint8_t cWM = getWM();
  190. switch( cWM ) {
  191. case CWM_INIT:{
  192. if ( tPort!= NULL ) {
  193. delete( tPort );
  194. tPort = NULL;
  195. EVENT_CONTAINER *tEVT;
  196. //ToDo: Change Device Name!
  197. m_pDBC->setNodeOffline( m_sNodeData.node_id );
  198. tEVT = createBasicEvent( m_sNodeData.node_id, EVT_CLASS_SENSOR_3D, EVT_ERROR, NULL, "Device is Disconnected");
  199. m_pEventHeap->broadcast( tEVT );
  200. SLEEP(100);
  201. } else {
  202. if( strlen( m_sDATA.slave_host ) < 7 ) { //minimal size of "X.X.X.X"
  203. tPort = new clientPort( m_sDATA.master_host, m_sDATA.master_port );
  204. } else {
  205. tPort = new clientPort( m_sDATA.slave_host, m_sDATA.slave_port );
  206. };
  207. if( tPort!= NULL ) {
  208. setWM(CWM_CONNECT);
  209. };
  210. };
  211. break;
  212. };
  213. case CWM_CONNECT:{
  214. if( CS_CONNECTED == getConnectionStatus() ) {
  215. EVENT_CONTAINER *tEVT;
  216. #ifdef DEBUG
  217. LOG("Set Device Connected!");
  218. #endif
  219. m_pDBC->setNodeOnline( m_sNodeData.node_id );
  220. tEVT = createBasicEvent( m_sNodeData.node_id, EVT_CLASS_SENSOR_3D, EVT_READY, NULL, "Device is connected");
  221. m_pEventHeap->broadcast( tEVT );
  222. setWM( CWM_INITIAL_GET_TWINC );
  223. } else {
  224. if( tPort!= NULL ) {
  225. if ( tPort->create() ) {
  226. if( setTransport( tPort , false ) ){
  227. break;
  228. } else {
  229. LOG_ERROR("Transport is not Attached to Parser!");
  230. };
  231. } else {
  232. LOG_ERROR("Transport is not Created!");
  233. };
  234. };
  235. SLEEP(1000);
  236. setWM( CWM_INIT );
  237. };
  238. break;
  239. };
  240. case CWM_INITIAL_GET_TWINC:{
  241. if( !getTwincSupport() ) {
  242. #ifdef DEBUG
  243. LOG_ERROR("Get TWINC Support");
  244. #endif
  245. setWM( CWM_INIT );
  246. } else {
  247. LOG("Waiting ACK Twinc");
  248. setWM( CWM_INITIAL_WAIT_TWINC );
  249. };
  250. break;
  251. };
  252. case CWM_INITIAL_GET_PARAM:{
  253. if( !getParameters() ) {
  254. #ifdef DEBUG
  255. LOG_ERROR("Get Param Failure");
  256. #endif
  257. setWM( CWM_INIT );
  258. } else {
  259. LOG("Waiting ACK Param");
  260. setWM( CWM_INITIAL_WAIT_PARAM );
  261. };
  262. break;
  263. };
  264. case CWM_INITIAL_GET_MASKS_INFO:{
  265. if( !getShadowInfo() ){
  266. LOG_ERROR("Error INITIAL CMD_GET_SHADOW_INFO!");
  267. setWM( CWM_INIT );
  268. } else {
  269. setWM( CWM_INITIAL_WAIT_MASKS_INFO );
  270. };
  271. break;
  272. };
  273. case CWM_INITIAL_GET_VERTEX_DATA:{
  274. char tB[64];
  275. sprintf(tB, "Ini Get Vertex[%d]", m_nCurrentPolyID );
  276. LOG( tB );
  277. if( !getShadowPoly( m_nCurrentPolyID ) ){
  278. if( CS_CONNECTED != getConnectionStatus() ) {
  279. setWM( CWM_INIT );
  280. } else LOG_ERROR("Error INITIAL CMD_GET_SHADOW_DATA!");
  281. } else {
  282. setWM( CWM_INITIAL_WAIT_VERTEX_DATA );
  283. };
  284. break;
  285. };
  286. case CWM_GET_BORDER:{
  287. if( !getDraw( m_nFullBorderDataIndex, m_nFullBorderKernelIndex ) ){
  288. if( CS_CONNECTED != getConnectionStatus() ) {
  289. setWM( CWM_INIT );
  290. } else LOG_ERROR("Error Sending CMD_FULL_BORDER!");
  291. } else {
  292. setWM( CWM_WAIT_BORDER );
  293. };
  294. break;
  295. };
  296. case CWM_GET_MASK:{
  297. if( !setShadowPoly(NULL) ){
  298. if( CS_CONNECTED != getConnectionStatus() ) {
  299. setWM( CWM_INIT );
  300. } else LOG_ERROR("Error Sending CMD_SET_HZ_POLY!");
  301. } else {
  302. setWM( CWM_WAIT_MASK );
  303. };
  304. break;
  305. };
  306. case CWM_SET_MASK:{
  307. m_pShadow->getPolyCount();
  308. if( !setShadowPoly(NULL) ){
  309. if( CS_CONNECTED != getConnectionStatus() ) {
  310. setWM( CWM_INIT );
  311. } else LOG_ERROR("Error Sending CMD_SET_HZ_POLY!");
  312. } else {
  313. setWM( CWM_WAIT_MASK );
  314. };
  315. break;
  316. };
  317. case CWM_GET_PARAMS:{
  318. if( !getParameters() ){
  319. if( CS_CONNECTED != getConnectionStatus() ) {
  320. setWM( CWM_INIT );
  321. } else LOG_ERROR("Error Sending CMD_GET_PARAMETERS!");
  322. } else {
  323. setWM( CWM_WAIT_PARAMS );
  324. };
  325. break;
  326. };
  327. case CWM_SET_PARAMS:{
  328. //if( !setParameters( &m_sLGS_MASTER_PARAMETERS ) ){
  329. // setWM( CWM_INIT );
  330. // LOG_ERROR("Error Sending CMD_SET_PARAMETERS!");
  331. //} else {
  332. // setWM( CWM_WAIT_ACK_PARAMS );
  333. //};
  334. break;
  335. };
  336. case CWM_GET_STATUS:{
  337. if( !getState2() ){
  338. setWM( CWM_INIT );
  339. LOG_ERROR("Error Sending CMD_GET_STATUS2!");
  340. } else {
  341. setWM( CWM_WAIT_STATUS );
  342. };
  343. break;
  344. };
  345. case CWM_GET_EVENTS:{
  346. if( !getEvents() ){
  347. setWM( CWM_INIT );
  348. LOG_ERROR("Error Sending CMD_GET_EVENTS!");
  349. } else {
  350. LOG("GetEVENTS sent");
  351. setWM( CWM_WAIT_EVENTS );
  352. };
  353. break;
  354. };
  355. case CWM_SET_MODE_DETECTOR:{
  356. if( !setMode( MODE_DETECTOR )){
  357. setWM( CWM_INIT );
  358. LOG_ERROR("Error Sending CMD_GET_EVENTS!");
  359. } else {
  360. setWM( CWM_WAIT_MODE_ACK );
  361. };
  362. break;
  363. };
  364. case CWM_IDLE:
  365. default:{
  366. if( NODE_COMMAND * tCMD = fetchCommand() ) {
  367. switch( tCMD->cmd ) {
  368. case NODE_CMD_FULL_RESET:{
  369. setWM( CWM_ISSUE_FULL_RESET );
  370. break;
  371. };
  372. case NODE_CMD_UPLOAD_FILE:{
  373. //m_nUploadID = atoi( (CSTR) tCMD->parameter );
  374. //m_sCurrentFile, m_pDBC->get
  375. break;
  376. };
  377. case NODE_CMD_GET_DEVICE_DRAW:{
  378. m_nFullBorderDataIndex = 0;
  379. if( tCMD->parameter != NULL ) {
  380. m_nFullBorderKernelIndex = *((uint8_t *)tCMD->parameter);
  381. } else m_nFullBorderKernelIndex = m_sDATA.draw_lrf_id;
  382. setWM( CWM_GET_BORDER );
  383. break;
  384. };
  385. case NODE_CMD_UPLOAD_SHADOW:{
  386. setWM( CWM_SET_MASK );
  387. break;
  388. };
  389. };
  390. if( tCMD->parameter ) free( tCMD->parameter );
  391. free( tCMD );
  392. break;
  393. };
  394. //Time Driven Logic
  395. if( nTimeOut ){
  396. nTimeOut--;
  397. } else {
  398. nTimeOut = 20;
  399. setWM( CWM_GET_STATUS );
  400. };
  401. break;
  402. };
  403. };
  404. SLEEP( 50 );
  405. };
  406. if( tPort != NULL ){
  407. delete( tPort );
  408. };
  409. };
  410. thresholdNode *argus3DLIDAR::grabDataAccess( void ){
  411. if( m_mDataMutex.try_lock() ){
  412. if(( m_pThresholdData != NULL ) && ( m_nFullBorderDataIndex == TH_SIZE_NODES )){
  413. return m_pThresholdData;
  414. };
  415. m_mDataMutex.unlock();
  416. };
  417. return NULL;
  418. };
  419. void argus3DLIDAR::cleanDataTH( void ){
  420. m_mDataMutex.lock();
  421. if( m_pThresholdData != NULL ){
  422. free( m_pThresholdData );
  423. m_pThresholdData = NULL;
  424. m_nFullBorderDataIndex = 0;
  425. };
  426. m_mDataMutex.unlock();
  427. };
  428. void argus3DLIDAR::releaseDataAccess( void ){
  429. m_mDataMutex.unlock();
  430. };
  431. TARGET_DATA *argus3DLIDAR::prepareTargetData(CLIENT_EVENT* CE) {
  432. TARGET_DATA* tVal = NULL;
  433. if (CE != NULL) {
  434. #ifdef DEBUG_EXTRA
  435. char tB[256];
  436. sprintf( tB, "TRAW Type[%d] K[%d] R[%d] TH[%d] AA[%d] W[%d]",
  437. CE->type,
  438. CE->k_id,
  439. CE->range,
  440. CE->th_id,
  441. TH_SIZE_NODES,
  442. CE->w_cnt
  443. );
  444. LOG( tB );
  445. #endif
  446. tVal = (TARGET_DATA*)malloc(sizeof(TARGET_DATA));
  447. if (tVal != NULL) {
  448. systemAnchor* tSystem = systemAnchor::getInstance();
  449. uint16_t cX, cY;
  450. memset(tVal, 0, sizeof(TARGET_DATA));
  451. tVal->source_position.lat = m_oPosition.getLat();
  452. tVal->source_position.lon = m_oPosition.getLon();
  453. tVal->source_position.alt = m_oPosition.getAlt();
  454. makeXY(CE->th_id, cX, cY, (CE->k_id < 8));
  455. tVal->local_target.r = m_sDATA.max_range / 4095.0f * (float)CE->range;
  456. tVal->local_target.fi = 360.0f - (360.0f / (float)TH_LINE_NODES) * (float)cX;
  457. tVal->local_target.tetta = /* 90.0f - */ ((float)Q_DEGREE_FOV / (float)Q_SCAN_COUNT) * (float)( cY + Q_SHIFT_DOWN_FOV );
  458. if( tVal->local_target.tetta > 90.0f ) {
  459. tVal->local_target.tetta = 180.0f - tVal->local_target.tetta;
  460. tVal->local_target.fi = tVal->local_target.fi + 180.0f;
  461. if( tVal->local_target.fi > 360.0f ) tVal->local_target.fi = tVal->local_target.fi - 360.0f;
  462. };
  463. tVal->digital = CE->range;
  464. tVal->lrf_id = CE->k_id;
  465. //CE->w_cnt * CE->range
  466. tVal->area = CE->w_cnt * tVal->local_target.r;
  467. // local - C3D_SPH_RADIANS/C3D_SPH_DEGREES
  468. // system - C3D_DECART
  469. // global - C3D_WGS84_LOC
  470. tSystem->convertToSystem( &m_oPosition, tVal->local_target, tVal->system_target );
  471. tSystem->convertToGlobal( &m_oPosition, tVal->local_target, tVal->global_target );
  472. };
  473. };
  474. return tVal;
  475. };
  476. #define SIZE_IDX_A 1
  477. #define SIZE_IDX_B 2
  478. #define DEV_ID_IDX 3
  479. #define CMD_IDX 4
  480. #define SUB_CMD_IDX 5
  481. #define DATA_BEGIN_IDX 6
  482. bool argus3DLIDAR::packetHandler( protocolPackage* Package ) {
  483. uint8_t * pReceivedPackage = NULL;
  484. size_t nReceivedPackageSize = 0;
  485. bool bPackageIsArived = false;
  486. EVENT_CONTAINER * tEVT;
  487. CLIENT_EVENT * tData;
  488. if( Package == NULL ) return true;
  489. if( Package->m_pData == NULL ) return true;
  490. pReceivedPackage = Package->m_pData;
  491. nReceivedPackageSize = Package->m_nSize;
  492. uint8_t CUR_CMD;
  493. uint16_t PKG_LEN;
  494. uint8_t index;
  495. uint8_t SUB_LEN;
  496. uint8_t SUB_CMD;
  497. uint16_t Range;
  498. uint32_t Angle;
  499. uint32_t Width;
  500. bool State;
  501. uint8_t Status;
  502. uint8_t Code;
  503. PKG_LEN = ((pReceivedPackage[SIZE_IDX_A] & 0x7F) << 8) | pReceivedPackage[SIZE_IDX_B];
  504. CUR_CMD = pReceivedPackage[CMD_IDX];
  505. SUB_CMD = pReceivedPackage[SUB_CMD_IDX];
  506. #ifdef DEBUG
  507. char tZ[128];
  508. sprintf(tZ,"CMD: 0x%02X SUB: 0x%02X WM:0x%02X", CUR_CMD, SUB_CMD, getWM() );
  509. LOG( tZ );
  510. #endif
  511. m_nBusyCounter = 0;
  512. switch( CUR_CMD ){
  513. case CMD_STATE:{
  514. #ifdef DEBUG
  515. LOG("CMD_STATE");
  516. #endif
  517. if( SUB_CMD > WM_INIT ){
  518. SOFTWARE_INFO tINFO;
  519. uint8_t* ptr;
  520. ptr = (uint8_t*)&tINFO;
  521. for (uint8_t i = 0; i < sizeof( SOFTWARE_INFO ); i++) ptr[i] = pReceivedPackage[DATA_BEGIN_IDX + i];
  522. #ifdef DEBUG
  523. LOG("SOFT INFO FILLED!");
  524. #endif
  525. char tB[65];
  526. sprintf(tB, "%08X", tINFO.SW_MODE_FLAGS );
  527. m_pDBC->updateLRFSerial(m_sNodeData.aux_id, tB );
  528. sprintf(tB, "%08X", tINFO.SW_BUILD_DATE );
  529. m_pDBC->updateLRFVerison( m_sNodeData.aux_id, tB );
  530. sprintf(tB, "%08X%08X%08X%08X", tINFO.FW_CORE1_TYP, tINFO.FW_CORE2_TYP, tINFO.FW_CORE3_TYP, tINFO.FW_CORE4_TYP);
  531. m_pDBC->setLRFAltSerial( m_sNodeData.aux_id, tB );
  532. sprintf(tB, "%08X%08X%08X%08X", tINFO.FW_CORE1_VER, tINFO.FW_CORE2_VER, tINFO.FW_CORE3_VER, tINFO.FW_CORE4_VER );
  533. m_pDBC->setLRFAltVerison( m_sNodeData.aux_id, tB );
  534. };
  535. //setWM( CWM_INITIAL_GET_PARAM );
  536. setWM( CWM_INITIAL_GET_TWINC );
  537. break;
  538. };
  539. case CMD_QUIT:
  540. break;
  541. case CMD_RESET:
  542. #ifdef DEBUG
  543. LOG("Reset ACK");
  544. #endif
  545. break;
  546. case CMD_FULL_BORDER:
  547. case CMD_BORDER:
  548. #ifdef DEBUG_EXTRA
  549. LOG("CMD_BORDER");
  550. #endif
  551. if (SUB_CMD == BD_ERRORMODE) {
  552. LOG_ERROR("BOARD DATA ERROR!");
  553. m_nFullBorderDataIndex = 0;
  554. setWM( CWM_IDLE );
  555. } else if (SUB_CMD == BD_NOTREADY) {
  556. LOG_ERROR("BOARD DATA NOT READY!");
  557. m_nFullBorderDataIndex = 0;
  558. setWM( CWM_IDLE );
  559. } else {
  560. uint32_t index = 6;
  561. while (index < (nReceivedPackageSize - 7) && m_nFullBorderDataIndex < TH_SIZE_NODES) {
  562. m_pThresholdData[m_nFullBorderDataIndex].data = pReceivedPackage[index++];
  563. m_pThresholdData[m_nFullBorderDataIndex].data = (m_pThresholdData[m_nFullBorderDataIndex].data << 8) | pReceivedPackage[index++];
  564. m_pThresholdData[m_nFullBorderDataIndex].data = (m_pThresholdData[m_nFullBorderDataIndex].data << 8) | pReceivedPackage[index++];
  565. m_pThresholdData[m_nFullBorderDataIndex].data = (m_pThresholdData[m_nFullBorderDataIndex].data << 8) | pReceivedPackage[index++];
  566. m_nFullBorderDataIndex++;
  567. };
  568. if (m_nFullBorderDataIndex < TH_SIZE_NODES) {
  569. setWM( CWM_GET_BORDER );
  570. } else {
  571. #ifdef DEBUG
  572. LOG("FINISH GET BOARD DATA");
  573. #endif
  574. if( m_pThresholdData!= NULL ){
  575. if( m_nFullBorderDataIndex > 0 ){
  576. CSTR tID = getUniqID_MD5();
  577. if( tID!= NULL ) {
  578. if( saveTHPicture( m_pThresholdData, tID, ((m_nFullBorderKernelIndex < 16 )?true:false), true ) ){
  579. m_pDBC->addDraw( m_sNodeData.node_id, TH_SIZE_NODES, tID, m_nFullBorderKernelIndex, 1, true );
  580. } else {
  581. char cPATH[256];
  582. FILE *fd;
  583. sprintf( cPATH, "data/device_draws/%s.raw", tID );
  584. fd = fopen( cPATH, "wb");
  585. if( fd != NULL ) {
  586. fwrite( &m_nFullBorderDataIndex, sizeof(uint32_t), 1, fd );
  587. fwrite( m_pThresholdData, sizeof( thresholdNode ), m_nFullBorderDataIndex, fd );
  588. fclose( fd );
  589. m_pDBC->addDraw( m_sNodeData.node_id, TH_SIZE_NODES, tID, m_nFullBorderKernelIndex, 1 );
  590. } else {
  591. LOG_ERROR("Cannot open DRAW file!");
  592. };
  593. };
  594. free( tID );
  595. };
  596. };
  597. };
  598. #ifdef DEBUG
  599. LOG("FINISH SAVE BOARD DATA");
  600. #endif
  601. m_nFullBorderDataIndex = 0;
  602. setWM( CWM_SET_MODE_DETECTOR );
  603. };
  604. };
  605. break;
  606. case CMD_GETFILE:
  607. break;
  608. case CMD_RESTART:
  609. break;
  610. case CMD_GET_PARAMETERS:
  611. case CMD_PARAMETERS:
  612. case CMD_SET_PARAMETERS: {
  613. uint8_t cWM = getWM();
  614. if( cWM == CWM_WAIT_PARAMS || cWM == CWM_INITIAL_WAIT_PARAM ){
  615. #ifdef DEBUG
  616. LOG("Finish Get Params");
  617. #endif
  618. if( SUB_CMD == SUB_DATA_INTEGRITY_VALID ){
  619. PLGS_3D_PARAMETERS tParam = (PLGS_3D_PARAMETERS)(pReceivedPackage + 6);
  620. //memcpy( &m_sLGS_MASTER_PARAMETERS, tParam, sizeof(LGS_3D_PARAMETERS) );
  621. if( cWM == CWM_INITIAL_WAIT_PARAM ) {
  622. setWM( CWM_INITIAL_GET_MASKS_INFO );
  623. } else {
  624. setWM( CWM_IDLE );
  625. };
  626. } else {
  627. LOG_ERROR("Data Integrity Error(on Get Parameters)!");
  628. if( cWM == CWM_INITIAL_WAIT_PARAM ) {
  629. setWM( CWM_INITIAL_GET_PARAM );
  630. } else {
  631. setWM( CWM_GET_PARAMS );
  632. };
  633. };
  634. } else {
  635. LOG_ERROR("Not Requested Parameters Package!");
  636. };
  637. break;
  638. };
  639. case CMD_SET_HZ_INFO: {
  640. uint8_t cWM = getWM();
  641. if( cWM == CWM_WAIT_MASK_INFO || cWM == CWM_INITIAL_WAIT_MASKS_INFO ) {
  642. #ifdef DEBUG
  643. LOG("Finish Get HZ INFO");
  644. #endif
  645. m_nDevicePolyCount = pReceivedPackage[6];
  646. m_nCurrentMaskID = m_pDBC->getLRFMaskID( m_sNodeData.aux_id );
  647. if ( m_nCurrentMaskID == 0 ) {
  648. m_nCurrentMaskID = m_pDBC->addLRFMask(m_sNodeData.aux_id);
  649. } else {
  650. // Clear Existed Masks
  651. m_pShadow->clear();
  652. m_pDBC->cleanLRFMask( m_nCurrentMaskID );
  653. };
  654. #ifdef DEBUG
  655. char tB[64];
  656. sprintf( tB, "Mask Info: %d vertexes: %d", m_nCurrentMaskID, m_nDevicePolyCount );
  657. LOG(tB);
  658. #endif
  659. if( m_nDevicePolyCount ){
  660. m_pDBC->setLRFMaskStatus( m_nCurrentMaskID, ( SUB_CMD > 0 ) );
  661. } else {
  662. m_pDBC->setLRFMaskStatus( m_nCurrentMaskID, false );
  663. };
  664. if( m_nDevicePolyCount && m_nCurrentMaskID ){
  665. if( cWM == CWM_INITIAL_WAIT_MASKS_INFO ) {
  666. setWM( CWM_INITIAL_GET_VERTEX_DATA );
  667. } else {
  668. setWM( CWM_IDLE );
  669. };
  670. } else {
  671. setWM( CWM_IDLE );
  672. };
  673. };
  674. break;
  675. };
  676. case CMD_SET_HZ_POLY: {
  677. #ifdef DEBUG
  678. LOG("Finish Get HZ POLY");
  679. #endif
  680. uint8_t r = pReceivedPackage[6];
  681. uint8_t g = pReceivedPackage[7];
  682. uint8_t b = pReceivedPackage[8];
  683. uint32_t tLID = m_pDBC->addLRFMaskPolygone( m_nCurrentMaskID, r, g, b );
  684. //simplePolygone* tPolygone;
  685. //tPolygone = new simplePolygone();
  686. //if (tPolygone != NULL) {
  687. if( tLID ) {
  688. uint8_t pCount = pReceivedPackage[5];
  689. //tPolygone->count = pReceivedPackage[5];
  690. //tPolygone->color[0] = pReceivedPackage[6];
  691. //tPolygone->color[1] = pReceivedPackage[7];
  692. //tPolygone->color[2] = pReceivedPackage[8];
  693. #if defined( __linux__ ) && !defined( __GUI__ )
  694. //tPolygone->points = (simplePoint*)malloc(sizeof(simplePoint) * tPolygone->count);
  695. #else
  696. //tPolygone->points = (QPoint*)malloc(sizeof(QPoint) * tPolygone->count);
  697. #endif
  698. //if (tPolygone->count && tPolygone->points != NULL) {
  699. if( pCount ) {
  700. char tB[64];
  701. sprintf( tB, "Have Poly[%d] of %d Points", tLID, pCount );
  702. LOG( tB );
  703. uint16_t x, y;
  704. int index = 9;
  705. for (int i = 0; i < pCount; i++) {
  706. x = pReceivedPackage[index++];
  707. x = (x << 8) | pReceivedPackage[index++];
  708. y = pReceivedPackage[index++];
  709. y = (y << 8) | pReceivedPackage[index++];
  710. //tPolygone->points[i].setX(x);
  711. //tPolygone->points[i].setY(y);
  712. m_pDBC->addLRFMaskPolygonePoint( tLID, x, y );
  713. };
  714. //m_pShadow->addPoly(tPolygone);
  715. // } else {
  716. // free( tPolygone );
  717. };
  718. };
  719. m_nCurrentPolyID++;
  720. if ( m_nCurrentPolyID < m_nDevicePolyCount ) {
  721. setWM( CWM_INITIAL_GET_VERTEX_DATA );
  722. } else {
  723. //setWM( CWM_IDLE );
  724. setWM( CWM_GET_BORDER );
  725. m_nFullBorderKernelIndex = m_sDATA.draw_lrf_id;
  726. }
  727. break;
  728. };
  729. case CMD_SET_HZ_POLY_ACK: {
  730. m_nCurrentPolyID = SUB_CMD;
  731. if (m_nCurrentPolyID < m_pShadow->getPolyCount()) {
  732. simplePolygone* tPoly;
  733. tPoly = m_pShadow->getPoly(m_nCurrentPolyID);
  734. if (tPoly != NULL) {
  735. setShadowPoly(tPoly);
  736. } else {
  737. };
  738. } else {
  739. #ifdef DEBUG
  740. LOG("Send RayTraces");
  741. #endif
  742. vectorZoneMask VZ;
  743. if (m_pShadow->getZoneMask(VZ)) {
  744. if (setShadowTrace(&VZ)) {
  745. } else {
  746. };
  747. } else {
  748. };
  749. };
  750. break;
  751. };
  752. case CMD_GET_SERIAL_NUMBER:
  753. break;
  754. case CMD_SET_ADDRESS:
  755. break;
  756. case CMD_GET_SOFTWARE_VERSION:
  757. break;
  758. case CMD_UNSUPPORTED:
  759. break;
  760. case CMD_SET_COMM_SPEED:
  761. break;
  762. case CMD_FULL_RESET:
  763. #ifdef DEBUG
  764. LOG("Full Reset ACK");
  765. #endif
  766. break;
  767. case CMD_CAMERA:
  768. break;
  769. case CMD_SET_SERIAL_NUMBER:
  770. break;
  771. case CMD_BOARDDATA:
  772. break;
  773. case CMD_GET_CAMERA_PARAMETERS:
  774. break;
  775. case CMD_SET_CAMERA_PARAMETERS:
  776. break;
  777. case CMD_RELAY:
  778. break;
  779. case CMD_GET_RELAY_PARAMETERS:
  780. break;
  781. case CMD_SET_RELAY_PARAMETERS:
  782. break;
  783. case CMD_FS_SUB_PACKET:
  784. break;
  785. case CMD_STATE_2:{
  786. //if( cWM != CWM_GET_STATUS ) {
  787. // LOG("Wrong WM - No Status Requested!");
  788. //} else {
  789. LRF_WORK_MODE tStatus = ( LRF_WORK_MODE ) SUB_CMD;
  790. uint8_t evtCnt = pReceivedPackage[DATA_BEGIN_IDX];
  791. if ( tStatus > WM_NONE ) {
  792. if ( PKG_LEN > 10 ) {
  793. ALG_RUN_STATUS *ptr = (ALG_RUN_STATUS *)&pReceivedPackage[DATA_BEGIN_IDX + 1];
  794. m_pDBC->update3DRuntimeStatus( m_sNodeData.aux_id, ptr );
  795. };
  796. if ( evtCnt ) {
  797. char tB[64];
  798. sprintf( tB, "Have [%d] Events!", evtCnt );
  799. LOG(tB);
  800. setWM( CWM_GET_EVENTS );
  801. } else {
  802. LOG("No Events!");
  803. setWM( CWM_IDLE );
  804. };
  805. } else {
  806. LOG("STATUS WM_NONE!");
  807. setWM( CWM_IDLE );
  808. };
  809. /*
  810. if( m_sDeviceStatus.Status != tStatus ){
  811. LOG("Device Status Changed!");
  812. switch( tStatus ){
  813. case STG_STUDY: {
  814. //EVENT_CONTAINER * tEVT;
  815. tEVT = createBasicEvent( m_sNodeData.node_id, EVT_CLASS_SENSOR_3D, EVT_STUDY, NULL, "Device in STUDY mode");
  816. m_pEventHeap->broadcast(tEVT);
  817. break;
  818. };
  819. case STG_READY: {
  820. //basicEvent* tEVT;
  821. tEVT = createBasicEvent( m_sNodeData.node_id, EVT_CLASS_SENSOR_3D, EVT_READY, NULL, "Device in DETECTION mode");
  822. m_pEventHeap->broadcast(tEVT);
  823. break;
  824. };
  825. case STG_BUSY: {
  826. //basicEvent* tEVT;
  827. tEVT = createBasicEvent( m_sNodeData.node_id, EVT_CLASS_SENSOR_3D, EVT_ERROR, NULL, "Device in BUSY mode");
  828. m_pEventHeap->broadcast(tEVT);
  829. break;
  830. };
  831. };
  832. m_sDeviceStatus.Status = tStatus;
  833. };
  834. */
  835. //};
  836. //setWM( CWM_IDLE );
  837. break;
  838. };
  839. case CMD_EVT:{
  840. char tBuffer[256];
  841. uint8_t tCount = pReceivedPackage[SUB_CMD_IDX];//DATA_BEGIN_IDX];
  842. if( tCount > 0 ){
  843. CLIENT_EVENT *cEVT = (CLIENT_EVENT*)& pReceivedPackage[DATA_BEGIN_IDX];// + 1];
  844. for( uint8_t xindex = 0; xindex < tCount; xindex++ ) {
  845. switch (cEVT[xindex].type) {
  846. case EVT_TARGET_2D_WHITE:
  847. case EVT_TARGET_2D_BLACK:
  848. case EVT_TARGET_3D_WHITE:
  849. case EVT_TARGET_3D_BLACK:{
  850. TARGET_DATA *tData;
  851. tData = prepareTargetData(cEVT+xindex);
  852. if(tData != NULL){
  853. EVENT_CONTAINER * bEVT = NULL;
  854. sprintf(tBuffer, "%s[%d] %s target \tRange: %.2fm. \tAzimuth: %.2f \tElevation: %.2f \tTS: %d@%d W:%.2f",
  855. ((tData->lrf_id < 8) ? "M" : "S"),
  856. //bKernelID,
  857. tData->lrf_id,
  858. ((cEVT[xindex].type == EVT_TARGET_3D_WHITE) ? "White" : "Black"),
  859. tData->local_target.r,
  860. tData->local_target.fi,
  861. tData->local_target.tetta,
  862. cEVT[xindex].uts,
  863. cEVT[xindex].ums,
  864. tData->area
  865. );
  866. #ifdef DEBUG
  867. LOG( tBuffer );
  868. #endif
  869. bEVT = createBasicEvent( m_sNodeData.node_id, EVT_CLASS_SENSOR_3D, (basicEventTypeID) cEVT[xindex].type, NULL, tBuffer, tData );
  870. if (bEVT != NULL) {
  871. m_pEventHeap->broadcast(bEVT);
  872. } else {
  873. free(tData);
  874. };
  875. } else {
  876. LOG_ERROR("Error Preparing Target Data!");
  877. };
  878. break;
  879. };
  880. default:{
  881. sprintf( tBuffer, "Event[%d] Type:[%d]", xindex, cEVT[xindex].type );
  882. break;
  883. };
  884. };
  885. };
  886. //set new stage2
  887. m_nIdleTarget = m_nIdleTarget >> 1;
  888. if( m_nIdleTarget < LGS_MIN_IDLE_DELAY ) m_nIdleTarget = LGS_MIN_IDLE_DELAY;
  889. setWM( CWM_GET_EVENTS );
  890. } else {
  891. LOG("No Events in Package???");
  892. m_nIdleTarget = m_nIdleTarget << 1;
  893. if (m_nIdleTarget > LGS_MAX_IDLE_DELAY) m_nIdleTarget = LGS_MAX_IDLE_DELAY;
  894. setWM( CWM_IDLE );
  895. };
  896. break;
  897. };
  898. case CMD_GET_WIDTH_TABLE:
  899. break;
  900. case CMD_SET_WIDTH_TABLE:
  901. break;
  902. case CMD_GET_VOLTAGE:
  903. break;
  904. case CMD_GET_VOLTAGE_PARAMETERS:
  905. break;
  906. case CMD_SET_VOLTAGE_PARAMETERS:
  907. break;
  908. case CMD_GET_HZ_DATA:
  909. break;
  910. case CMD_SET_HZ_DATA:
  911. break;
  912. case CMD_GET_CALIBRATION_TABLE:
  913. break;
  914. case CMD_SET_CALIBRATION_TABLE:
  915. break;
  916. case CMD_GET_LRF_DISABLE:
  917. break;
  918. case CMD_SET_LRF_DISABLE:
  919. break;
  920. case CMD_GET_ZOOM_TABLE:
  921. break;
  922. case CMD_SET_ZOOM_TABLE:
  923. break;
  924. case CMD_GET_TILT_TABLE:
  925. break;
  926. case CMD_SET_TILT_TABLE:
  927. break;
  928. case CMD_GET_HZ_ENABLE:
  929. break;
  930. case CMD_SET_HZ_ENABLE:
  931. break;
  932. case CMD_FLIP_COMPLETE: {
  933. tEVT = createBasicEvent( m_sNodeData.node_id, EVT_CLASS_SENSOR_3D, EVT_FLIP, NULL, "Flip Complete");
  934. m_pEventHeap->broadcast(tEVT);
  935. break;
  936. };
  937. case CMD_SCAN_COMPLETE: {
  938. tEVT = createBasicEvent( m_sNodeData.node_id, EVT_CLASS_SENSOR_3D, EVT_SCAN, NULL, "Scan Complete");
  939. m_pEventHeap->broadcast(tEVT);
  940. break;
  941. };
  942. case CMD_TARGET: {
  943. tEVT = createBasicEvent( m_sNodeData.node_id, EVT_CLASS_SENSOR_3D, EVT_TARGET_3D_WHITE, NULL, "Target");
  944. m_pEventHeap->broadcast(tEVT);
  945. break;
  946. };
  947. case CMD_TARGET_WW: {
  948. tEVT = createBasicEvent( m_sNodeData.node_id, EVT_CLASS_SENSOR_3D, EVT_TARGET_3D_WHITE, NULL, "Target with Width");
  949. m_pEventHeap->broadcast(tEVT);
  950. break;
  951. };
  952. case CMD_TARGET_MW: {
  953. tData = (CLIENT_EVENT*)malloc(sizeof(CLIENT_EVENT));
  954. if (tData != NULL) {
  955. /*
  956. tData->mSampleID = (pReceivedPackage[5] << 24) + (pReceivedPackage[6] << 16) + (pReceivedPackage[7] << 8) + pReceivedPackage[8];
  957. tData->mInfo = pReceivedPackage[9];
  958. tData->mRange = (pReceivedPackage[12] << 8) + pReceivedPackage[11];
  959. tData->mWidth = (pReceivedPackage[12] << 24) + (pReceivedPackage[13] << 16) + (pReceivedPackage[14] << 8) + pReceivedPackage[15];
  960. //if( m_pMapWidget != NULL ) m_pMapWidget->addTarget(tData->mSampleID );
  961. //if( m_pRadarWidget != NULL ) m_pRadarWidget->addTarget(tData->mSampleID, tData->mRange, m_bIsMaster );
  962. tEVT = createBasicEvent( m_DbId, EVT_CLASS_SENSOR, EVT_TARGET_3D_WHITE, NULL, "White Intruder Detection", tData );
  963. m_pEventHeap->broadcast( tEVT );
  964. */
  965. };
  966. break;
  967. };
  968. case CMD_TARGET_MB: {
  969. tData = (CLIENT_EVENT*)malloc(sizeof(CLIENT_EVENT));
  970. if (tData != NULL) {
  971. /*
  972. tData->mSampleID = (pReceivedPackage[5] << 24) + (pReceivedPackage[6] << 16) + (pReceivedPackage[7] << 8) + pReceivedPackage[8];
  973. tData->mInfo = pReceivedPackage[9];
  974. tData->mRange = (pReceivedPackage[12] << 8) + pReceivedPackage[11];
  975. tData->mWidth = (pReceivedPackage[12] << 24) + (pReceivedPackage[13] << 16) + (pReceivedPackage[14] << 8) + pReceivedPackage[15];
  976. //if( m_pMapWidget != NULL ) m_pMapWidget->addTarget(tData->mSampleID, m_bIsMaster );
  977. //if( m_pRadarWidget != NULL ) m_pRadarWidget->addTarget(tData->mSampleID, tData->mRange, m_bIsMaster );
  978. tEVT = createBasicEvent( m_DbId, EVT_CLASS_SENSOR, EVT_TARGET_3D_BLACK, NULL, "Target Black", tData );
  979. m_pEventHeap->broadcast( tEVT );
  980. */
  981. };
  982. break;
  983. };
  984. case CMD_TARGET_ZZ: {
  985. uint8_t cWM = getWM();
  986. if( cWM == CWM_IDLE ){
  987. tData = (CLIENT_EVENT*)malloc(sizeof(CLIENT_EVENT));
  988. if (tData != NULL) {
  989. /*
  990. char tBuffer[256];
  991. float fRange, fAzimuth, fElevation;
  992. int sX, sY, eX, eY, cX, cY;
  993. for( int index = 0; index < sizeof(CLIENT_EVENT); index++ ){
  994. ((uint8_t*)tData)[index] = pReceivedPackage[5+index];
  995. };
  996. fRange = 300.0 / 4095.0 * tData->mRange;
  997. makeCds( tData->mStartID, sX, sY, ( tData->mInfo & 0xFF000000 ) > 0 );
  998. makeCds( tData->mEndID, eX, eY, ( tData->mInfo & 0xFF000000 ) > 0 );
  999. cX = ((sX+eX)>>1);
  1000. cY = ((sY+eY)>>1);
  1001. fAzimuth = (360.0 / TH_LINE_NODES )*cX;
  1002. fElevation = 90.0 - (90.0/Q_SCAN_COUNT )*cY;
  1003. //if( m_pMapWidget != NULL ) m_pMapWidget->addTarget( makeIds( cX, cY ), m_bIsMaster );
  1004. //if( m_pRadarWidget != NULL ) m_pRadarWidget->addTarget( makeIds( cX, cY ), tData->mRange, m_bIsMaster );
  1005. if( ( tData->mInfo & 0x00FF0000 ) > 0 ){
  1006. sprintf( tBuffer, "[%s]:%.2fm. Azimuth: %.2f Elevation: %.2f",
  1007. (m_bIsMaster?"M":"S"),
  1008. fRange,
  1009. fAzimuth,
  1010. fElevation
  1011. );
  1012. tEVT = createBasicEvent( m_DbId, EVT_CLASS_SENSOR, EVT_TARGET_3D_WHITE, NULL, tBuffer, tData );
  1013. } else {
  1014. sprintf( tBuffer, "[%s]:%.2fm. Azimuth: %.2f Elevation: %.2f",
  1015. (m_bIsMaster?"M":"S"),
  1016. fRange,
  1017. fAzimuth,
  1018. fElevation
  1019. );
  1020. tEVT = createBasicEvent( m_DbId, EVT_CLASS_SENSOR, EVT_TARGET_3D_BLACK, NULL, tBuffer, tData );
  1021. };
  1022. m_pEventHeap->broadcast( tEVT );
  1023. */
  1024. };
  1025. };
  1026. break;
  1027. };
  1028. case CMD_ACK_TWINK_LINK:{
  1029. char tB[64];
  1030. m_bSlaveConnectionStatus = SUB_CMD;
  1031. m_bTWINC =true;
  1032. sprintf( tB, "WINK SUPPORTED! [0x%02X]", SUB_CMD );
  1033. LOG( tB );
  1034. setWM( CWM_INITIAL_GET_PARAM );
  1035. break;
  1036. };
  1037. case CMD_ACK:{
  1038. setWM( CWM_IDLE );
  1039. break;
  1040. };
  1041. case CMD_NACK:{
  1042. if( SUB_CMD == CMD_QUERY_TWINK_LINK ) {
  1043. m_bTWINC =true;
  1044. LOG("TWINK SUPPORTED BUT TROUBLE!");
  1045. setWM( CWM_INITIAL_GET_PARAM );
  1046. };
  1047. break;
  1048. };
  1049. case CMD_IDONK:{
  1050. char tB[64];
  1051. sprintf( tB, "Not Recognized Command [0x%02X]", SUB_CMD );
  1052. LOG( tB );
  1053. break;
  1054. };
  1055. case CMD_NONE:
  1056. default:{
  1057. break;
  1058. };
  1059. }; // CMD SWITCH
  1060. return true;
  1061. };
  1062. #undef SIZE_IDX
  1063. #undef DEV_ID_IDX
  1064. #undef CMD_IDX
  1065. #undef SUB_CMD_IDX
  1066. #undef DATA_BEGIN_IDX
  1067. bool argus3DLIDAR::startGetScanData( uint8_t kID, uint32_t *th_zone ){
  1068. //if( getOperation() == CMD_FULL_BORDER ) return true;
  1069. m_mDataMutex.lock();
  1070. if( th_zone == NULL ){
  1071. if( m_pThresholdData != NULL ) free(m_pThresholdData);
  1072. m_pThresholdData = (thresholdNode *) malloc( sizeof(thresholdNode) * TH_SIZE_NODES );
  1073. } else {
  1074. m_pThresholdData = (thresholdNode *) th_zone;
  1075. };
  1076. m_nFullBorderDataIndex = 0;
  1077. m_nFullBorderKernelIndex = kID;
  1078. m_mDataMutex.unlock();
  1079. if( m_pThresholdData!= NULL ){
  1080. LOG("TH Assigned - OK");
  1081. return setOperation( CMD_FULL_BORDER );
  1082. } else {
  1083. LOG_ERROR("TH Assigned - NOK (mem issue)");
  1084. };
  1085. return false;
  1086. };
  1087. bool argus3DLIDAR::startGetParameters( uint32_t timeout ){
  1088. //if (CMD_GET_PARAMETERS == getOperation()) return true;
  1089. return setOperation( CMD_GET_PARAMETERS );
  1090. };
  1091. bool argus3DLIDAR::startDeviceReset( bool FullReset ){
  1092. if( FullReset ){
  1093. //if (CMD_FULL_RESET == getOperation()) return true;
  1094. return setOperation( CMD_FULL_RESET );
  1095. };
  1096. //if (CMD_RESET == getOperation()) return true;
  1097. return setOperation( CMD_RESET );
  1098. };
  1099. bool argus3DLIDAR::startUploadMask(void){
  1100. simplePolygone *tPoly;
  1101. m_nCurrentPolyID = 0;
  1102. #ifndef __linux__
  1103. m_pShadow->rasterizeVertexes();
  1104. #endif
  1105. if (setOperation(CMD_SET_HZ_POLY, true)){
  1106. return true;
  1107. };
  1108. return false;
  1109. };
  1110. bool argus3DLIDAR::startStopMotors( void ){
  1111. if (setOperation(CMD_DHWM_STOP_MOTORS, true)){
  1112. return true;
  1113. };
  1114. return false;
  1115. };
  1116. bool argus3DLIDAR::startStartMotors( void ){
  1117. if (setOperation(CMD_DHWM_START_MOTORS, true)){
  1118. return true;
  1119. };
  1120. return false;
  1121. };
  1122. bool argus3DLIDAR::startMasksStatus(bool Active){
  1123. if ( m_pShadow!=NULL ){
  1124. m_pShadow->setActive(Active);
  1125. if (setOperation(CMD_SET_HZ_STATUS, true)){
  1126. return true;
  1127. };
  1128. };
  1129. return false;
  1130. };
  1131. bool argus3DLIDAR::setOperation( E_LGS_CMD opCode, bool fNOW ){
  1132. m_mStatusMutex.lock();
  1133. /*
  1134. if( m_sDeviceStatus.CurrentOperation == CMD_NONE ){
  1135. m_sDeviceStatus.CurrentOperation = opCode;
  1136. } else if( fNOW ){
  1137. m_mStatusMutex.unlock();
  1138. return false;
  1139. } else if( m_sDeviceStatus.PendingOperation == CMD_NONE ){
  1140. m_sDeviceStatus.PendingOperation = opCode;
  1141. } else {
  1142. m_mStatusMutex.unlock();
  1143. return false;
  1144. };
  1145. */
  1146. m_mStatusMutex.unlock();
  1147. return true;
  1148. };
  1149. #define STB_IMAGE_IMPLEMENTATION
  1150. #include "../data/stbi/stb_image.h"
  1151. #define STB_IMAGE_WRITE_IMPLEMENTATION
  1152. #include "../data/stbi/stb_image_write.h"
  1153. #undef STB_IMAGE_WRITE_IMPLEMENTATION
  1154. #undef STB_IMAGE_IMPLEMENTATION
  1155. #define SKIP_LINE_PIXELS 28
  1156. #define HEIGHT 512
  1157. #define WIDTH 1024
  1158. // scale factor -> 4/9
  1159. bool argus3DLIDAR::saveTHPicture( thresholdNode *ptr, CSTR uid, bool master, bool byMinimum, bool monochrome ){
  1160. uint8_t *tMemory = NULL;
  1161. thresholdNode *cNode = NULL;
  1162. uint8_t *cPixel = NULL;
  1163. int cSpace;
  1164. int x, y;
  1165. tMemory = stbi_load( "data/assets/images/empty_3d.png", &x, &y, &cSpace, STBI_default );
  1166. if( tMemory == NULL ){
  1167. LOG_ERROR("Draw Memory Alloc Eror!");
  1168. return false;
  1169. };
  1170. if( x != 1024 || y != 512 ){
  1171. LOG("Wrong IMG Size!");
  1172. free( tMemory );
  1173. return NULL;
  1174. };
  1175. int pix = 0;
  1176. int mem = 0;
  1177. bool s_bit = false;
  1178. bool no_data = false;
  1179. float tVal;
  1180. for ( int idx = 0; idx < ( TH_SIZE_NODES << 2 ); idx++ ) { // tik-tak
  1181. if( ( idx % 4 ) == 0 ){
  1182. mem++;
  1183. if ( master ) {
  1184. cNode = ptr + TH_SIZE_NODES - mem - 1;
  1185. } else {
  1186. cNode = ptr + mem - 1;
  1187. };
  1188. if( !s_bit ) s_bit = cNode->values.sbit;
  1189. if( !no_data ) no_data = ( cNode->values.cnt == 0 );
  1190. if( !s_bit && !no_data ){
  1191. if( byMinimum ) {
  1192. tVal = ((float)cNode->values.min) / 4096.f;
  1193. } else {
  1194. tVal = ((float)cNode->values.max) / 4096.f;
  1195. };
  1196. };
  1197. };
  1198. if( ( idx % 9 ) == 0 ){
  1199. if( mem > ( SKIP_LINE_PIXELS * TH_LINE_NODES ) ) {
  1200. cPixel = tMemory + (pix << 2);
  1201. pix++;
  1202. if( s_bit ) { // Set Red
  1203. cPixel[0] = 0x00; cPixel[1] = 0xff; cPixel[2] = 0x00; cPixel[3] = 0xff;
  1204. } else {
  1205. if( no_data ) { // Set Blue
  1206. cPixel[0] = 0x00; cPixel[1] = 0x00; cPixel[2] = 0xff; cPixel[3] = 0xff;
  1207. } else {
  1208. uint32_t tColor;
  1209. tColor = interpolate( 0x000000ff, 0xffffffff, tVal );
  1210. cPixel[0] = ( tColor >> 24 ) & 0xFF;
  1211. cPixel[1] = ( tColor >> 16 ) & 0xFF;
  1212. cPixel[2] = ( tColor >> 8 ) & 0xFF;
  1213. cPixel[3] = tColor & 0xFF;
  1214. };
  1215. };
  1216. s_bit = false;
  1217. no_data = false;
  1218. }
  1219. };
  1220. };
  1221. char filename[256];
  1222. sprintf( filename, "data/device_draws/%s_100.png", uid );
  1223. int RetVal = stbi_write_png( filename, 1024, 512, cSpace, tMemory, 1024 * cSpace );
  1224. free( tMemory );
  1225. if( RetVal == 0 ) return false;
  1226. if( monochrome ) {
  1227. cSpace = 1;
  1228. } else {
  1229. cSpace = 4;
  1230. };
  1231. tMemory = (uint8_t *) malloc( TH_SIZE_NODES * cSpace );
  1232. if( tMemory == NULL ){
  1233. LOG_ERROR("Map Memory Alloc Eror!");
  1234. return false;
  1235. };
  1236. for (int j = 0; j < Q_SCAN_COUNT; j++) {
  1237. for ( int i = 0; i < TH_LINE_NODES; i++ ) {
  1238. //cNode = ptr+(j*i);
  1239. //cNode = &ptr[ ( TH_LINE_NODES * j ) + i ];
  1240. int ids = 0;
  1241. int idd = 0;
  1242. if ( master ) {
  1243. //cNode = ptr+makeIds( i, j , true );
  1244. //cPixel = tMemory+( cSpace * makeIds( i, j , false ));
  1245. ids = makeIds( i, j , true );
  1246. idd = cSpace * makeIds( i, j , false );
  1247. //cNode = ptr + ( i * j );
  1248. //cPixel = tMemory + ( cSpace * i * j );
  1249. } else {
  1250. //cNode = ptr+makeIds( i, j , false );
  1251. //cPixel = tMemory+( cSpace * makeIds( i, j , false ));
  1252. ids = makeIds( i, j , false );
  1253. idd = cSpace * makeIds( i, j , false );
  1254. };
  1255. cNode = ptr + ids ;
  1256. cPixel = tMemory + idd;
  1257. if( monochrome ) {
  1258. #ifdef CHECK_SUN_BIT
  1259. if( cNode->values.sbit ) {
  1260. cPixel[0] = 0;
  1261. } else {
  1262. cPixel[0] = (uint8_t)((((float) cNode->values.min ) * 255.f ) / 4096.f );
  1263. };
  1264. #else
  1265. cPixel[0] = (uint8_t)((((float) cNode->values.min ) * 255.f ) / 4096.f );
  1266. #endif
  1267. } else {
  1268. #ifdef CHECK_SUN_BIT
  1269. if( cNode->values.sbit ) {
  1270. // Set Red
  1271. cPixel[0] = 0x00;
  1272. cPixel[1] = 0xff;
  1273. cPixel[2] = 0x00;
  1274. cPixel[3] = 0xff;
  1275. } else {
  1276. if( cNode->values.cnt == 0 ) {
  1277. // Set Blue
  1278. cPixel[0] = 0x00;
  1279. cPixel[1] = 0x00;
  1280. cPixel[2] = 0xff;
  1281. cPixel[3] = 0xff;
  1282. } else {
  1283. uint32_t tColor;
  1284. float tVal = ((float) cNode->values.min ) / 4096.f;
  1285. tColor = interpolate( 0x000000ff, 0xffffffff, tVal );
  1286. cPixel[0] = ( tColor >> 24 ) & 0xFF;
  1287. cPixel[1] = ( tColor >> 16 ) & 0xFF;
  1288. cPixel[2] = ( tColor >> 8 ) & 0xFF;
  1289. cPixel[3] = tColor & 0xFF;
  1290. };
  1291. };
  1292. #else
  1293. if( cNode->values.cnt == 0 ) {
  1294. // Set Blue
  1295. cPixel[0] = 0x00;
  1296. cPixel[1] = 0x00;
  1297. cPixel[2] = 0xff;
  1298. cPixel[3] = 0xff;
  1299. } else {
  1300. uint32_t tColor;
  1301. float tVal = ((float) cNode->values.min ) / 4096.f;
  1302. tColor = interpolate( 0x000000ff, 0xffffffff, tVal );
  1303. cPixel[0] = ( tColor >> 24 ) & 0xFF;
  1304. cPixel[1] = ( tColor >> 16 ) & 0xFF;
  1305. cPixel[2] = ( tColor >> 8 ) & 0xFF;
  1306. cPixel[3] = tColor & 0xFF;
  1307. };
  1308. #endif
  1309. };
  1310. };
  1311. };
  1312. sprintf( filename, "data/device_draws/%s_map.png", uid );
  1313. RetVal = stbi_write_png( filename, TH_LINE_NODES, Q_SCAN_COUNT, cSpace, tMemory, TH_LINE_NODES * cSpace );
  1314. free( tMemory );
  1315. if( RetVal == 0 ) return false;
  1316. return true;
  1317. };
  1318. #undef LOG
  1319. #undef LOG_ERROR