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.

1212 lines
44 KiB

4 years ago
​
12 months ago
4 years ago
​
​
​
​
​
4 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
4 years ago
4 years ago
​
​
4 years ago
​
4 years ago
​
​
​
​
​
​
​
3 years ago
4 years ago
​
4 years ago
4 years ago
​
4 years ago
4 years ago
​
​
​
​
​
4 years ago
​
4 years ago
12 months ago
4 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
4 years ago
​
4 years ago
​
4 years ago
12 months ago
4 years ago
4 years ago
​
​
4 years ago
​
​
​
12 months ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
4 years ago
​
​
​
4 years ago
​
​
​
​
4 years ago
4 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
4 years ago
4 years ago
​
​
​
​
​
​
4 years ago
4 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
4 years ago
​
​
​
​
​
​
4 years ago
​
​
​
​
​
​
​
2 years ago
4 years ago
2 years ago
4 years ago
​
​
​
​
​
​
​
​
​
​
2 years ago
4 years ago
2 years ago
4 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
2 years ago
4 years ago
2 years ago
4 years ago
2 years ago
4 years ago
2 years ago
4 years ago
2 years ago
4 years ago
2 years ago
4 years ago
​
​
​
​
​
​
​
2 years ago
4 years ago
2 years ago
4 years ago
2 years ago
4 years ago
2 years ago
4 years ago
​
​
​
​
​
​
​
2 years ago
4 years ago
2 years ago
4 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
4 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
2 years ago
​
​
4 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
4 years ago
​
​
​
​
​
​
2 years ago
​
​
​
​
​
4 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
2 years ago
​
​
​
4 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
4 years ago
​
​
​
​
​
​
​
​
4 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
4 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
3 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
2 years ago
​
​
​
​
​
​
​
2 years ago
​
​
​
2 years ago
​
​
2 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
4 years ago
​
​
4 years ago
​
​
​
4 years ago
​
​
​
​
​
​
​
4 years ago
​
​
​
4 years ago
​
​
​
​
​
4 years ago
​
​
​
​
4 years ago
4 years ago
2 years ago
4 years ago
4 years ago
​
4 years ago
​
​
​
​
3 years ago
4 years ago
​
4 years ago
​
4 years ago
4 years ago
​
​
​
​
​
​
​
​
​
​
​
4 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
4 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
4 years ago
​
  1. #include "deviceNodeSensor.h"
  2. #include "../../components/object/objectSystemAnchor.h"
  3. #include "kernelPrototype.h"
  4. #include <opencv2/opencv.hpp>
  5. #define LOG( X ) deviceNode::m_pLog->Log( "NODSEN", this, X )
  6. #define LOG_ERROR( X ) deviceNode::m_pLog->Log( "NODSEN", this, _LOG_ERROR, X )
  7. /*
  8. USER.INI
  9. AUTORESET_ENABLE=0
  10. AUTORESET_INTERLEAVE=2000
  11. AUTORESET_TIMEOUT=-5536
  12. BOARD_TEMPERATURE=20
  13. BOARD_ZEROSHIFT=0
  14. BOARD_ENCODERSTEPS=6
  15. BOARD_GEARRATIO=61
  16. SWITCHER_TABLE=27,55,82
  17. SWITCHER_REPLY_TIMEOUT=100
  18. SWITCHER_COMMAND_TIMEOUT=500
  19. SWITCHER_AUTO_MODE=0
  20. SWITCHER_AUTO_ENTER_TIMEOUT=60
  21. SWITCHER_AUTO_SWITCH_TIMEOUT=3
  22. */
  23. /*
  24. "ADDRESS",
  25. "CAMERA_TYPE",
  26. "CAMERA_SHIFT",
  27. "CAMERA_AUTO",
  28. "CAMERA_DELAY",
  29. "RELAY_NC",
  30. "RELAY_AUTO",
  31. "RELAY_DELAY",
  32. "VOLTAGE_CONTROL",
  33. "MASTER_DISABLED",
  34. "SLAVE_DISABLED",
  35. */
  36. /*
  37. typedef struct S_DBC_LRF_INI{
  38. uint16_t _AUTO_RESET_ENABLE;
  39. uint16_t _AUTO_RESET_INTERLEAVE;
  40. uint16_t _AUTO_RESET_TIMEOUT;
  41. uint16_t _BOARD_TEMPERATURE;
  42. uint16_t _BOARD_ZEROSHIFT;
  43. uint16_t _BOARD_SPEED;
  44. uint16_t _RUN_DIR;
  45. uint16_t _NO_BLACK_TARGETS;
  46. uint16_t _JUST_MOVING_TARGETS;
  47. uint16_t _JUST_MOVING_RADIAL;
  48. uint16_t _JUST_MOVING_DISTANCE;
  49. uint16_t _FREEZ_FIRST_TH;
  50. uint16_t _SAVE_FIRST_TH;
  51. }DBC_LRF_INI;
  52. */
  53. #define __INI_FILE_ENTRIES_CNT__ 13
  54. const CSTR settings_user_ini_fields[ __INI_FILE_ENTRIES_CNT__ ] = {
  55. "AUTORESET_ENABLE",
  56. "AUTORESET_INTERLEAVE",
  57. "AUTORESET_TIMEOUT",
  58. "BOARD_TEMPERATURE",
  59. "BOARD_ZEROSHIFT",
  60. "BOARD_SPEED",
  61. "RUN_DIR",
  62. "NO_BLACK_TARGETS",
  63. "JUST_MOVING_TARGETS",
  64. "JUST_MOVING_RADIAL",
  65. "JUST_MOVING_DISTANCE",
  66. "FREEZ_FIRST_TH",
  67. "SAVE_FIRST_TH"
  68. };
  69. /*
  70. LPCTSTR m_pSetting[] =
  71. {
  72. { "SN" },
  73. { "ADDRESS" },
  74. { "CAMERA_TYPE" },
  75. { "CAMERA_SHIFT" },
  76. { "CAMERA_AUTO" },
  77. { "CAMERA_DELAY" },
  78. { "RELAY_NC" },
  79. { "RELAY_AUTO" },
  80. { "RELAY_DELAY" },
  81. { "VOLTAGE_CONTROL" },
  82. { "MASTER_DISABLED" },
  83. { "SLAVE_DISABLED" },
  84. { "AUTORESET_ENABLE" },
  85. { "AUTORESET_INTERLEAVE" },
  86. { "AUTORESET_TIMEOUT" },
  87. { "BOARD_TEMPERATURE" },
  88. { "BOARD_ZEROSHIFT" },
  89. { "BOARD_ENCODERSTEPS" },
  90. { "BOARD_GEARRATIO" },
  91. { "BOARD_MAXRANGE" },
  92. { "BOARD_SPEED" },
  93. { "RUN_DIR" },
  94. { "SWITCHER_TABLE" },
  95. { "SWITCHER_REPLY_TIMEOUT" },
  96. { "SWITCHER_COMMAND_TIMEOUT" },
  97. { "SWITCHER_AUTO_MODE" },
  98. { "SWITCHER_AUTO_ENTER_TIMEOUT" },
  99. { "SWITCHER_AUTO_SWITCH_TIMEOUT" },
  100. { "NO_BLACK_TARGETS" },
  101. { "JUST_MOVING_TARGETS" },
  102. { "JUST_MOVING_RADIAL" },
  103. { "JUST_MOVING_DISTANCE" },
  104. { "BLIND_ZONE_START_ANGLE" },
  105. { "BLIND_ZONE_END_ANGLE" },
  106. { "BLIND_ZONE_START_RANGE" },
  107. { "BLIND_ZONE_END_RANGE" },
  108. { "BLIND_ZONE_WIDTH" },
  109. { "BLIND_ZONE_DEPTH" },
  110. { "BLIND_ZONE_ANGLE" },
  111. { "BLIND_ZONE_RANGE" }
  112. #ifdef STORE_FIRST_SAF
  113. ,{ "FREEZ_FIRST_TH" }
  114. ,( "SAVE_FIRST_TH" )
  115. #endif
  116. };
  117. */
  118. LRF_WORK_STAGE operator ++( LRF_WORK_STAGE &id, int ){
  119. LRF_WORK_STAGE currentID = id;
  120. if( LRF_WS_COUNT < id + 1 ){
  121. id = LRF_WS_COUNT;
  122. } else {
  123. id = static_cast<LRF_WORK_STAGE>( id + 1 );
  124. };
  125. return ( currentID );
  126. };
  127. void deviceNodeSensor::transactionResetResult( void ){
  128. m_mPPModeProtector.lock();
  129. m_ePPMODE = STG_PUSH;
  130. m_mPPModeProtector.unlock();
  131. };
  132. void deviceNodeSensor::transactionSetResult( bool ACK ){
  133. m_mPPModeProtector.lock();
  134. m_ePPMODE = (ACK)?STG_POOL:STG_PUSH;
  135. m_mPPModeProtector.unlock();
  136. };
  137. void deviceNodeSensor::transactionWaitResult( void ){
  138. m_mPPModeProtector.lock();
  139. m_ePPMODE = STG_WAIT;
  140. m_mPPModeProtector.unlock();
  141. };
  142. STAGE_PP_CONDITION deviceNodeSensor::getPPStatus(){
  143. STAGE_PP_CONDITION rv;
  144. m_mPPModeProtector.lock();
  145. rv = m_ePPMODE;
  146. m_mPPModeProtector.unlock();
  147. return rv;
  148. };
  149. deviceNodeSensor::deviceNodeSensor( uint32_t id ):deviceNode( id ),m_oPosition(){
  150. m_nCommErrorsCounter = 0;
  151. m_ePPMODE = STG_INIT;
  152. m_bValidationSkipCounter = DAFAULT_VALIDATION_SKIP_COUNTER;
  153. m_nCommErrorsStatTO = COMMUNICATION_ERROR_STATISTIC_HARVEST_INTERVAL_IN_SECONDS;
  154. m_pEventHeap = basicEventHeap::getInstance();
  155. switch( m_sNodeData.type ){
  156. case ARGUS_CLD_SENSOR: m_eEventClass = EVT_CLASS_SENSOR_CLD; break;
  157. case ARGUS_LGS_SENSOR: m_eEventClass = EVT_CLASS_SENSOR_LGS; break;
  158. case ARGUS_ALS_SENSOR: m_eEventClass = EVT_CLASS_SENSOR_ALS; break;
  159. case ARGUS_LFS_SENSOR: m_eEventClass = EVT_CLASS_SENSOR_LFS; break;
  160. case ARGUS_PLS_200_SENSOR: m_eEventClass = EVT_CLASS_SENSOR_PLS_210; break;
  161. case ARGUS_PLS_360_SENSOR: m_eEventClass = EVT_CLASS_SENSOR_PLS_360; break;
  162. case ARGUS_3D_SENSOR: m_eEventClass = EVT_CLASS_SENSOR_3D; break;
  163. default: m_eEventClass = EVT_CLASS_SYSTEM; break;
  164. };
  165. setWM( LRF_WM_INIT );
  166. memset( &m_sLRF_DATA, 0, sizeof(DBC_LRF_DATA));
  167. if( m_pDBC ){
  168. DBC_LRF_DATA *tDATA = m_pDBC->getLRFData( m_sNodeData.aux_id );
  169. if( tDATA != NULL ) {
  170. memcpy( &m_sLRF_DATA, tDATA, sizeof(DBC_LRF_DATA));
  171. free( tDATA );
  172. m_pDBC->updateLRFUnfinishedUploads( m_sNodeData.aux_id );
  173. #ifdef DEBUG
  174. } else {
  175. LOG_ERROR("No Sensor DATA !!");
  176. #endif
  177. };
  178. };
  179. //m_pTargetFilter = NULL;
  180. systemAnchor *SA = systemAnchor::getInstance();
  181. if( SA ){
  182. if ( SA->beLocal() ) {
  183. m_oPosition.setPositionXYZ( m_sLRF_DATA.x, m_sLRF_DATA.y, m_sLRF_DATA.z, 0 ); //SA->getUTMZone() );
  184. m_oPosition.setOrientationDeg( m_sLRF_DATA.x_rot , m_sLRF_DATA.y_rot, m_sLRF_DATA.z_rot );
  185. } else {
  186. C3D_DECART LD = SA->getXYZ();
  187. m_oPosition.setPositionXYZ( LD.x + m_sLRF_DATA.x, LD.y + m_sLRF_DATA.y, LD.z + m_sLRF_DATA.z, SA->getUTMZone() );
  188. m_oPosition.setOrientationDeg( m_sLRF_DATA.x_rot , m_sLRF_DATA.y_rot, m_sLRF_DATA.z_rot );
  189. };
  190. } else {
  191. m_oPosition.setPositionXYZ( m_sLRF_DATA.x , m_sLRF_DATA.y, m_sLRF_DATA.z );
  192. m_oPosition.setOrientationDeg( m_sLRF_DATA.x_rot , m_sLRF_DATA.y_rot, m_sLRF_DATA.z_rot );
  193. };
  194. m_oPosition.updateMatrix();
  195. #ifdef DEBUG
  196. char tB[128];
  197. sprintf( tB, "Finalized Sensor Location: Lat:%.3f Lon:%.3f Alt:%.3f", m_oPosition.getLat(), m_oPosition.getLon(), m_oPosition.getAlt() );
  198. LOG( tB );
  199. sprintf( tB, "Finalized Sensor Location: x:%.3f y:%.3f z:%.3f", m_oPosition.getX(), m_oPosition.getY(), m_oPosition.getZ() );
  200. LOG( tB );
  201. sprintf( tB, "Orientation: x:%.3f y:%.3f z:%.3f", m_oPosition.getRotX_deg(), m_oPosition.getRotY_deg(), m_oPosition.getRotZ_deg() );
  202. LOG( tB );
  203. #endif
  204. LOG("Finish Construct!");
  205. };
  206. deviceNodeSensor::deviceNodeSensor( DBC_NODE_DATA *Data ):deviceNode( Data ),m_oPosition(){
  207. m_nCommErrorsCounter = 0;
  208. m_ePPMODE = STG_INIT;
  209. m_bValidationSkipCounter = DAFAULT_VALIDATION_SKIP_COUNTER;
  210. m_nCommErrorsStatTO = COMMUNICATION_ERROR_STATISTIC_HARVEST_INTERVAL_IN_SECONDS;
  211. m_pEventHeap = basicEventHeap::getInstance();
  212. memset( &m_sLRF_DATA, 0, sizeof(DBC_LRF_DATA));
  213. //m_pTargetFilter = NULL;
  214. if( m_pDBC ){
  215. reloadDeviceNodeFromDB();
  216. m_pDBC->updateLRFUnfinishedUploads( m_sLRF_DATA.id );
  217. };
  218. setWM( LRF_WM_INIT );
  219. LOG("Finish Data Construct!");
  220. };
  221. deviceNodeSensor::~deviceNodeSensor(){
  222. m_vTargetFilters.clear();
  223. };
  224. bool deviceNodeSensor::filterTargetData( TARGET_DATA *ptr ){
  225. bool RetVal = false;
  226. if ( m_vTargetFilters.size() ) {
  227. for (vectorKernelEventFilters::iterator idx = m_vTargetFilters.begin(); idx != m_vTargetFilters.end(); idx++) {
  228. if( (*idx)->isActive() ) {
  229. if( (*idx)->validateTarget( m_sNodeData.node_id, ptr ) ) {
  230. RetVal = true;
  231. break;
  232. };
  233. };
  234. };
  235. };
  236. return RetVal;
  237. };
  238. bool deviceNodeSensor::loadFilters(){
  239. if( m_pDBC ){
  240. kernelPrototype *AK = kernelPrototype::getInstance();
  241. if( AK==NULL ){
  242. LOG_ERROR("KERNEL <<< GRAB >>> Failure!");
  243. return false;
  244. };
  245. DBC_INT_LIST *IDS = m_pDBC->listSensorFilters(m_sNodeData.aux_id);
  246. if ( IDS ) {
  247. if ( IDS->size() ) {
  248. for ( DBC_INT_LIST::iterator idx = IDS->begin(); idx != IDS->end(); idx++ ) {
  249. kernelEventFilter * tFilter = AK->getFilter( *idx );
  250. if( tFilter ) m_vTargetFilters.push_back( tFilter );
  251. };
  252. };
  253. delete(IDS);
  254. return true;
  255. };
  256. };
  257. return false;
  258. };
  259. bool deviceNodeSensor::reloadDeviceNodeFromDB(){
  260. if( m_pDBC ){
  261. DBC_LRF_DATA *tDATA = m_pDBC->getLRFData(m_sNodeData.aux_id);
  262. if( tDATA != NULL ) {
  263. memcpy( &m_sLRF_DATA, tDATA, sizeof(DBC_LRF_DATA));
  264. free( tDATA );
  265. };
  266. } else return false;
  267. systemAnchor *SA = systemAnchor::getInstance();
  268. if( SA ){
  269. if ( SA->beLocal() ) {
  270. m_oPosition.setPositionXYZ( m_sLRF_DATA.x, m_sLRF_DATA.y, m_sLRF_DATA.z, 0 ); //SA->getUTMZone() );
  271. m_oPosition.setOrientationDeg( m_sLRF_DATA.x_rot , m_sLRF_DATA.y_rot, m_sLRF_DATA.z_rot );
  272. } else {
  273. C3D_DECART LD = SA->getXYZ();
  274. m_oPosition.setPositionXYZ( LD.x + m_sLRF_DATA.x, LD.y + m_sLRF_DATA.y, LD.z + m_sLRF_DATA.z, SA->getUTMZone() );
  275. m_oPosition.setOrientationDeg( m_sLRF_DATA.x_rot , m_sLRF_DATA.y_rot, m_sLRF_DATA.z_rot );
  276. };
  277. } else {
  278. m_oPosition.setPositionXYZ( m_sLRF_DATA.x , m_sLRF_DATA.y, m_sLRF_DATA.z );
  279. m_oPosition.setOrientationDeg( m_sLRF_DATA.x_rot , m_sLRF_DATA.y_rot, m_sLRF_DATA.z_rot );
  280. };
  281. m_oPosition.updateMatrix();
  282. #ifdef DEBUG_EXTRA
  283. char tB[128];
  284. LOG("Reload Data From DB!");
  285. sprintf( tB, "Finalized Sensor Location: Lat:%.3f Lon:%.3f Alt:%.3f", m_oPosition.getLat(), m_oPosition.getLon(), m_oPosition.getAlt() );
  286. LOG( tB );
  287. sprintf( tB, "Finalized Sensor Location: x:%.3f y:%.3f z:%.3f", m_oPosition.getX(), m_oPosition.getY(), m_oPosition.getZ() );
  288. LOG( tB );
  289. sprintf( tB, "Orientation: x:%.3f y:%.3f z:%.3f", m_oPosition.getRotX_deg(), m_oPosition.getRotY_deg(), m_oPosition.getRotZ_deg() );
  290. LOG( tB );
  291. #endif
  292. return true;
  293. };
  294. bool deviceNodeSensor::validate(){
  295. if ( m_bValidationSkipCounter ) {
  296. m_bValidationSkipCounter --;
  297. } else {
  298. if ( m_pDBC == NULL ) return false;
  299. bool RetVal = true;
  300. DBC_LRF_DATA *tDATA = m_pDBC->getLRFData(m_sNodeData.aux_id);
  301. if( tDATA != NULL ) {
  302. if( RetVal && ( tDATA->uart_id != m_sLRF_DATA.uart_id ) ) RetVal = false;
  303. if( RetVal && ( tDATA->device_id != m_sLRF_DATA.device_id ) ) RetVal = false;
  304. if( RetVal && ( tDATA->max_range != m_sLRF_DATA.max_range ) ) RetVal = false;
  305. if( RetVal && ( tDATA->base_shift != m_sLRF_DATA.base_shift ) ) RetVal = false;
  306. if( RetVal && ( tDATA->auto_draw != m_sLRF_DATA.auto_draw ) ) RetVal = false;
  307. free(tDATA);
  308. } else RetVal = false;
  309. m_bValidationSkipCounter = DAFAULT_VALIDATION_SKIP_COUNTER;
  310. #ifdef DEBUG
  311. if (!RetVal) LOG("Validation of Device Failure!");
  312. #endif
  313. return RetVal;
  314. };
  315. return true;
  316. };
  317. #define SKIP_LINE_PIXELS 28
  318. #define HEIGHT 512
  319. #define WIDTH 1024
  320. // scale factor -> 4/9
  321. uint8_t *deviceNodeSensor::create2dDraw( uint32_t *out_size, uint32_t *th_data, size_t th_size, uint16_t max_digital, int radial_shift, CSTR tpl, bool byMinimum ){
  322. uint8_t *RetVal = NULL;
  323. *out_size = 0;
  324. #ifdef DEBUG_EXTRA
  325. LOG("C2DD");
  326. #endif
  327. if( th_data != NULL && th_size != 0 ) {
  328. if ( fileExist( tpl ) ) {
  329. LOG(tpl);
  330. cv::Mat tImage = cv::imread( tpl, cv::IMREAD_UNCHANGED );
  331. int psize = th_size / ( sizeof( uint32_t ));
  332. float stp = 2.0f * ( M_PI / (float) psize );
  333. //float MR = m_sLRF_DATA.max_range;
  334. float S = ( M_PI / 3000.0f) * (float) radial_shift;
  335. int x0 = (tImage.cols >> 1);
  336. int y0 = (tImage.rows >> 1);
  337. float maxR = ((float) x0 ) * 0.98f;
  338. #ifdef DEBUG_EXTRA
  339. LOG("C2DD - Start");
  340. #endif
  341. cv::Point pts[(psize << 1)];
  342. cv::Point ptt[(psize+2)];
  343. const cv::Point * ppt[1];
  344. int npp[1];
  345. const cv::Point * ppz[1];
  346. int npz[1];
  347. float rst;
  348. int rpt;
  349. uint16_t RANGE = 0;
  350. ptt[0].x = x0;
  351. ptt[0].y = y0;
  352. rst = maxR / ((float)(max_digital+1));
  353. rpt = 1;
  354. for( int i=0; i < psize; i++ ){
  355. if( byMinimum ) {
  356. RANGE = ( th_data[i] >> 16 ) & 0xFFFF;
  357. } else {
  358. RANGE = th_data[i] & 0xFFFF;
  359. };
  360. float ca = 0.0 - ( S + i * stp );
  361. if( RANGE > 0 ){
  362. pts[psize+i].x = x0 + maxR * cosf( ca );
  363. pts[psize+i].y = y0 + maxR * sinf( ca );
  364. if( RANGE > max_digital ){
  365. pts[i].x = pts[psize+i].x;
  366. pts[i].y = pts[psize+i].y;
  367. } else {
  368. float cr = (rst * RANGE);
  369. pts[i].x = x0 + cr * cosf( ca );
  370. pts[i].y = y0 + cr * sinf( ca );
  371. };
  372. } else {
  373. ptt[rpt].x = pts[psize+i].x = pts[i].x = x0 + maxR * cosf( ca );
  374. ptt[rpt].y = pts[psize+i].y = pts[i].y = y0 + maxR * sinf( ca );
  375. rpt++;
  376. };
  377. };
  378. ppt[0] = pts;
  379. npp[0] = ( psize<<1 );
  380. ppz[0] = ptt;
  381. npz[0] = ( rpt - 1);
  382. #ifdef DEBUG_EXTRA
  383. LOG("C2DD -> draw finish");
  384. #endif
  385. cv::Mat ttMOMO = cv::imread( tpl, cv::IMREAD_UNCHANGED );
  386. #ifdef DEBUG_EXTRA
  387. LOG("C2DD -> read tpl2 finish");
  388. #endif
  389. cv::fillPoly( ttMOMO, ppt, npp, 1, cv::Scalar( 0, 128, 255, 50 ), cv::LINE_8 );
  390. #ifdef DEBUG_EXTRA
  391. LOG("C2DD -> poly1 finish");
  392. #endif
  393. //try {
  394. // cv::fillPoly(ttMOMO, ppz, npz, 1, cv::Scalar(0, 0, 0, 100), cv::LINE_8);
  395. // LOG("C2DD -> poly2 finish");
  396. //} catch ( cv::Exception& e ) {
  397. // LOG("C2DD -> poly2 Error!");
  398. // LOG( e.what() );
  399. //};
  400. cv::addWeighted( tImage, 0.5, ttMOMO, 0.5, 0.0, tImage );
  401. #ifdef DEBUG_EXTRA
  402. LOG("C2DD -> add img finish");
  403. #endif
  404. ttMOMO.release();
  405. #ifdef DEBUG_EXTRA
  406. LOG("C2DD -> img release finish");
  407. #endif
  408. vector<int> compression_params;
  409. compression_params.push_back( cv::IMWRITE_PNG_COMPRESSION );
  410. compression_params.push_back(9);
  411. vector<uchar> buf;
  412. cv::imencode( ".png", tImage, buf, compression_params );
  413. tImage.release();
  414. #ifdef DEBUG_EXTRA
  415. LOG("C2DD -> compress finish");
  416. #endif
  417. *out_size = buf.size();
  418. RetVal = (uint8_t *) malloc( *out_size );
  419. if( RetVal ) {
  420. memcpy( RetVal, buf.data(), *out_size );
  421. } else *out_size =0;
  422. buf.clear();
  423. };
  424. };
  425. return RetVal;
  426. };
  427. bool deviceNodeSensor::handleDeviceDraw( uint8_t kID, void *th_data, size_t th_size, bool byMinimum ){
  428. if( th_data == NULL ) return false;
  429. if( th_size == 0 ) return false;
  430. if( m_pDBC == NULL ) return false;
  431. switch( getNodeType() ){
  432. case ARGUS_ALS_SENSOR:
  433. case ARGUS_LFS_SENSOR:
  434. case ARGUS_PLS_200_SENSOR:
  435. case ARGUS_PLS_360_SENSOR:
  436. case ARGUS_NET_PLS_200:{
  437. //apply Base Shift to 2D draw
  438. if( m_sLRF_DATA.base_shift != 0 ){
  439. int16_t cnt = ( th_size >> 2 );
  440. uint32_t *ttData = (uint32_t *)malloc( th_size );
  441. int16_t data_shift = ( int16_t ) (((float)m_sLRF_DATA.base_shift ) * (((float)cnt) / 360.0f));
  442. for ( uint16_t i = 0; i<cnt; i++ ) {
  443. ttData[i] = ((uint32_t *)th_data)[(cnt+i+data_shift)%cnt];
  444. };
  445. memcpy( th_data, ttData, th_size );
  446. };
  447. break;
  448. };
  449. };
  450. #ifdef DEBUG_EXTRA
  451. LOG("DATA DD TO DATABASE");
  452. #endif
  453. m_pDBC->addDeviceDraw( getNodeID(), DRAW_MODE_RAW_DATA, kID, th_data, th_size );
  454. /*
  455. //Clear Old Draw
  456. CSTR old = m_pDBC->getLRFDraw( getNodeID() );
  457. if ( old ) {
  458. // delete old draw
  459. char filename[256];
  460. sprintf( filename, "data/device_draws/%s_100.png", old );
  461. if( fileExist( filename ) ){
  462. unlink( filename );
  463. };
  464. sprintf( filename, "data/device_draws/%s_50.png", old );
  465. if( fileExist( filename ) ){
  466. unlink( filename );
  467. };
  468. sprintf( filename, "data/device_draws/%s_25.png", old );
  469. if( fileExist( filename ) ){
  470. unlink( filename );
  471. };
  472. sprintf( filename, "data/device_draws/%s_12.png", old );
  473. if( fileExist( filename ) ){
  474. unlink( filename );
  475. };
  476. sprintf( filename, "data/device_draws/%s_map.png", old );
  477. if( fileExist( filename ) ){
  478. unlink( filename );
  479. };
  480. free( old );
  481. };
  482. */
  483. #ifndef SAVE_DRAW_RAW_ONLY
  484. CSTR uid = getUniqID_MD5();
  485. if( uid == NULL ) return false;
  486. switch( getNodeType() ){
  487. case ARGUS_ALS_SENSOR:
  488. case ARGUS_LFS_SENSOR:
  489. case ARGUS_PLS_200_SENSOR:
  490. case ARGUS_PLS_360_SENSOR:
  491. case ARGUS_NET_PLS_200:{
  492. /*
  493. 2D Sensors does not need Image to be created
  494. #ifdef DEBUG_EXTRA
  495. LOG("DEV DRAW");
  496. #endif
  497. LOG( uid );
  498. uint32_t tSize;
  499. uint8_t *tBuff = create2dDraw( &tSize, (uint32_t *) th_data, th_size, 1024, m_sLRF_DATA.base_shift, "data/assets/images/lfs_draw_empty_100.png" );
  500. if ( tBuff ) {
  501. m_pDBC->addDeviceDraw( getNodeID(), DRAW_MODE_2D_100, kID, tBuff, tSize );
  502. free( tBuff );
  503. };
  504. tBuff = create2dDraw( &tSize, (uint32_t *) th_data, th_size, 512, m_sLRF_DATA.base_shift, "data/assets/images/lfs_draw_empty_50.png" );
  505. if ( tBuff ) {
  506. m_pDBC->addDeviceDraw( getNodeID(), DRAW_MODE_2D_50, kID, tBuff, tSize );
  507. free( tBuff );
  508. };
  509. tBuff = create2dDraw( &tSize, (uint32_t *) th_data, th_size, 256, m_sLRF_DATA.base_shift, "data/assets/images/lfs_draw_empty_25.png" );
  510. if ( tBuff ) {
  511. m_pDBC->addDeviceDraw( getNodeID(), DRAW_MODE_2D_25, kID, tBuff, tSize );
  512. free( tBuff );
  513. };
  514. tBuff = create2dDraw( &tSize, (uint32_t *) th_data, th_size, 128, m_sLRF_DATA.base_shift, "data/assets/images/lfs_draw_empty_12.png" );
  515. if ( tBuff ) {
  516. m_pDBC->addDeviceDraw( getNodeID(), DRAW_MODE_2D_12, kID, tBuff, tSize );
  517. free( tBuff );
  518. };
  519. tBuff = create2dDraw( &tSize, (uint32_t *) th_data, th_size, 1024, m_sLRF_DATA.base_shift, "data/assets/images/2d_MAP.png" );
  520. if ( tBuff ) {
  521. m_pDBC->addDeviceDraw( getNodeID(), DRAW_MODE_MAP, kID, tBuff, tSize );
  522. free( tBuff );
  523. };
  524. #ifdef DEBUG_EXTRA
  525. LOG(" DD FINISH!");
  526. #endif
  527. */
  528. break;
  529. };
  530. case ARGUS_3D_SENSOR:{
  531. char * emptyBG = "data/assets/images/empty_3d.png";
  532. thresholdNode * ptr = (thresholdNode *) th_data;
  533. if ( fileExist( emptyBG ) ) {
  534. cv::Mat tImage = cv::imread( emptyBG, cv::IMREAD_UNCHANGED );
  535. if( tImage.rows != 512 || tImage.cols != 1024 ){
  536. LOG_ERROR( "Background Image wrong size (should be 1024x512)!" );
  537. free( uid );
  538. return false;
  539. };
  540. uint8_t * tMemory = tImage.data;
  541. thresholdNode * cNode = NULL;
  542. uint8_t * cPixel = NULL;
  543. int pix = 0;
  544. int mem = 0;
  545. bool s_bit = false;
  546. bool no_data = false;
  547. float tVal;
  548. bool master = ( kID < 16 );
  549. for( int idx = 0; idx < ( TH_SIZE_NODES << 2 ); idx++ ) { // tik-tak
  550. if( ( idx % 4 ) == 0 ){
  551. mem++;
  552. if ( master ) {
  553. cNode = ptr + TH_SIZE_NODES - mem - 1;
  554. } else {
  555. cNode = ptr + mem - 1;
  556. };
  557. if( !s_bit ) s_bit = cNode->values.sbit;
  558. if( !no_data ) no_data = ( cNode->values.cnt == 0 );
  559. if( !s_bit && !no_data ){
  560. if( byMinimum ) {
  561. tVal = ((float)cNode->values.min) / 4096.f;
  562. } else {
  563. tVal = ((float)cNode->values.max) / 4096.f;
  564. };
  565. };
  566. };
  567. if( ( idx % 9 ) == 0 ){
  568. if( mem > ( SKIP_LINE_PIXELS * TH_LINE_NODES ) ) {
  569. cPixel = tMemory + (pix << 2);
  570. pix++;
  571. if( s_bit ) { // Set Red
  572. cPixel[0] = 0x00; cPixel[1] = 0x00; cPixel[2] = 0xff; cPixel[3] = 0xff;
  573. } else {
  574. if( no_data ) { // Set Blue
  575. cPixel[0] = 0xff; cPixel[1] = 0x00; cPixel[2] = 0x00; cPixel[3] = 0xff;
  576. } else {
  577. uint32_t tColor;
  578. tColor = interpolate( 0x000000ff, 0xffffffff, tVal );
  579. cPixel[0] = ( tColor >> 24 ) & 0xFF;
  580. cPixel[1] = ( tColor >> 16 ) & 0xFF;
  581. cPixel[2] = ( tColor >> 8 ) & 0xFF;
  582. cPixel[3] = tColor & 0xFF;
  583. };
  584. };
  585. s_bit = false;
  586. no_data = false;
  587. };
  588. };
  589. };
  590. char filename[256];
  591. sprintf( filename, "data/device_draws/%s_100.png", uid );
  592. //int RetVal = stbi_write_png( filename, 1024, 512, cSpace, tMemory, 1024 * cSpace );
  593. vector<int> compression_params;
  594. compression_params.push_back( cv::IMWRITE_PNG_COMPRESSION );
  595. compression_params.push_back(9);
  596. if(! cv::imwrite( filename, tImage, compression_params ) ){
  597. tImage.release();
  598. free( uid );
  599. return false;
  600. };
  601. tImage.release();
  602. #ifdef DEBUG_EXTRA
  603. LOG("Draw Saved!");
  604. #endif
  605. cv::Mat tMap( Q_SCAN_COUNT, TH_LINE_NODES, CV_8UC4 );
  606. #ifdef DEBUG_EXTRA
  607. LOG("Map Creating");
  608. #endif
  609. tMemory = tMap.data;
  610. for( int j = 0; j < Q_SCAN_COUNT; j++ ){
  611. for( int i = 0; i < TH_LINE_NODES; i++ ){
  612. //cNode = ptr+(j*i);
  613. //cNode = &ptr[ ( TH_LINE_NODES * j ) + i ];
  614. int ids = 0;
  615. int idd = 0;
  616. if ( master ) {
  617. //cNode = ptr+makeIds( i, j , true );
  618. //cPixel = tMemory+( cSpace * makeIds( i, j , false ));
  619. ids = makeIds( i, j , true );
  620. idd = 4 * makeIds( i, j , false );
  621. //cNode = ptr + ( i * j );
  622. //cPixel = tMemory + ( cSpace * i * j );
  623. } else {
  624. //cNode = ptr+makeIds( i, j , false );
  625. //cPixel = tMemory+( cSpace * makeIds( i, j , false ));
  626. ids = makeIds( i, j , false );
  627. idd = 4 * makeIds( i, j , false );
  628. };
  629. cNode = ptr + ids ;
  630. cPixel = tMemory + idd;
  631. if( 0 ) { // Monochrome
  632. #ifdef CHECK_SUN_BIT
  633. if( cNode->values.sbit ) {
  634. cPixel[0] = 0;
  635. } else {
  636. cPixel[0] = (uint8_t)((((float) cNode->values.min ) * 255.f ) / 4096.f );
  637. };
  638. #else
  639. cPixel[0] = (uint8_t)((((float) cNode->values.min ) * 255.f ) / 4096.f );
  640. #endif
  641. } else {
  642. #ifdef CHECK_SUN_BIT
  643. if( cNode->values.sbit ) {
  644. // Set Red
  645. cPixel[0] = 0x00;
  646. cPixel[1] = 0x00;
  647. cPixel[2] = 0xff;
  648. cPixel[3] = 0xff;
  649. } else {
  650. if( cNode->values.cnt == 0 ) {
  651. // Set Blue
  652. cPixel[0] = 0xff;
  653. cPixel[1] = 0x00;
  654. cPixel[2] = 0x00;
  655. cPixel[3] = 0xff;
  656. } else {
  657. uint32_t tColor;
  658. float tVal = ((float) cNode->values.min ) / 4096.f;
  659. tColor = interpolate( 0x000000ff, 0xffffffff, tVal );
  660. cPixel[0] = ( tColor >> 24 ) & 0xFF;
  661. cPixel[1] = ( tColor >> 16 ) & 0xFF;
  662. cPixel[2] = ( tColor >> 8 ) & 0xFF;
  663. cPixel[3] = tColor & 0xFF;
  664. };
  665. };
  666. #else
  667. if( cNode->values.cnt == 0 ) {
  668. // Set Blue
  669. cPixel[0] = 0xff;
  670. cPixel[1] = 0x00;
  671. cPixel[2] = 0x00;
  672. cPixel[3] = 0xff;
  673. } else {
  674. uint32_t tColor;
  675. float tVal = ((float) cNode->values.min ) / 4096.f;
  676. tColor = interpolate( 0x000000ff, 0xffffffff, tVal );
  677. cPixel[0] = ( tColor >> 24 ) & 0xFF;
  678. cPixel[1] = ( tColor >> 16 ) & 0xFF;
  679. cPixel[2] = ( tColor >> 8 ) & 0xFF;
  680. cPixel[3] = tColor & 0xFF;
  681. };
  682. #endif
  683. };
  684. };
  685. };
  686. sprintf( filename, "data/device_draws/%s_map.png", uid );
  687. LOG(filename);
  688. if(! cv::imwrite( filename, tMap, compression_params ) ){
  689. tMap.release();
  690. free( uid );
  691. return false;
  692. };
  693. tMap.release();
  694. m_pDBC->addDeviceDraw( getNodeID(), DRAW_MODE_MAP, kID, (void *) uid, 0 );
  695. //m_pDBC->addLRFDraw( getNodeID(), TH_SIZE_NODES, uid, kID, 1, true );
  696. } else {
  697. LOG_ERROR( "Background Image is Not Found!" );
  698. free( uid );
  699. return false;
  700. };
  701. break;
  702. };
  703. };
  704. free( uid );
  705. #endif // !SAVE_DRAW_RAW_ONLY
  706. return true;
  707. };
  708. /*
  709. #define STB_IMAGE_IMPLEMENTATION
  710. #include "../data/stbi/stb_image.h"
  711. #define STB_IMAGE_WRITE_IMPLEMENTATION
  712. #include "../data/stbi/stb_image_write.h"
  713. #undef STB_IMAGE_WRITE_IMPLEMENTATION
  714. #undef STB_IMAGE_IMPLEMENTATION
  715. bool argus3DLIDAR::saveTHPicture( thresholdNode *ptr, CSTR uid, bool master, bool byMinimum, bool monochrome ){
  716. uint8_t *tMemory = NULL;
  717. thresholdNode *cNode = NULL;
  718. uint8_t *cPixel = NULL;
  719. int cSpace;
  720. int x, y;
  721. tMemory = stbi_load( "data/assets/images/empty_3d.png", &x, &y, &cSpace, STBI_default );
  722. if( tMemory == NULL ){
  723. LOG_ERROR("Draw Memory Alloc Eror!");
  724. return false;
  725. };
  726. if( x != 1024 || y != 512 ){
  727. LOG("Wrong IMG Size!");
  728. free( tMemory );
  729. return NULL;
  730. };
  731. int pix = 0;
  732. int mem = 0;
  733. bool s_bit = false;
  734. bool no_data = false;
  735. float tVal;
  736. for ( int idx = 0; idx < ( TH_SIZE_NODES << 2 ); idx++ ) { // tik-tak
  737. if( ( idx % 4 ) == 0 ){
  738. mem++;
  739. if ( master ) {
  740. cNode = ptr + TH_SIZE_NODES - mem - 1;
  741. } else {
  742. cNode = ptr + mem - 1;
  743. };
  744. if( !s_bit ) s_bit = cNode->values.sbit;
  745. if( !no_data ) no_data = ( cNode->values.cnt == 0 );
  746. if( !s_bit && !no_data ){
  747. if( byMinimum ) {
  748. tVal = ((float)cNode->values.min) / 4096.f;
  749. } else {
  750. tVal = ((float)cNode->values.max) / 4096.f;
  751. };
  752. };
  753. };
  754. if( ( idx % 9 ) == 0 ){
  755. if( mem > ( SKIP_LINE_PIXELS * TH_LINE_NODES ) ) {
  756. cPixel = tMemory + (pix << 2);
  757. pix++;
  758. if( s_bit ) { // Set Red
  759. cPixel[0] = 0x00; cPixel[1] = 0xff; cPixel[2] = 0x00; cPixel[3] = 0xff;
  760. } else {
  761. if( no_data ) { // Set Blue
  762. cPixel[0] = 0x00; cPixel[1] = 0x00; cPixel[2] = 0xff; cPixel[3] = 0xff;
  763. } else {
  764. uint32_t tColor;
  765. tColor = interpolate( 0x000000ff, 0xffffffff, tVal );
  766. cPixel[0] = ( tColor >> 24 ) & 0xFF;
  767. cPixel[1] = ( tColor >> 16 ) & 0xFF;
  768. cPixel[2] = ( tColor >> 8 ) & 0xFF;
  769. cPixel[3] = tColor & 0xFF;
  770. };
  771. };
  772. s_bit = false;
  773. no_data = false;
  774. }
  775. };
  776. };
  777. char filename[256];
  778. sprintf( filename, "data/device_draws/%s_100.png", uid );
  779. int RetVal = stbi_write_png( filename, 1024, 512, cSpace, tMemory, 1024 * cSpace );
  780. free( tMemory );
  781. if( RetVal == 0 ) return false;
  782. if( monochrome ) {
  783. cSpace = 1;
  784. } else {
  785. cSpace = 4;
  786. };
  787. tMemory = (uint8_t *) malloc( TH_SIZE_NODES * cSpace );
  788. if( tMemory == NULL ){
  789. LOG_ERROR("Map Memory Alloc Eror!");
  790. return false;
  791. };
  792. for (int j = 0; j < Q_SCAN_COUNT; j++) {
  793. for ( int i = 0; i < TH_LINE_NODES; i++ ) {
  794. //cNode = ptr+(j*i);
  795. //cNode = &ptr[ ( TH_LINE_NODES * j ) + i ];
  796. int ids = 0;
  797. int idd = 0;
  798. if ( master ) {
  799. //cNode = ptr+makeIds( i, j , true );
  800. //cPixel = tMemory+( cSpace * makeIds( i, j , false ));
  801. ids = makeIds( i, j , true );
  802. idd = cSpace * makeIds( i, j , false );
  803. //cNode = ptr + ( i * j );
  804. //cPixel = tMemory + ( cSpace * i * j );
  805. } else {
  806. //cNode = ptr+makeIds( i, j , false );
  807. //cPixel = tMemory+( cSpace * makeIds( i, j , false ));
  808. ids = makeIds( i, j , false );
  809. idd = cSpace * makeIds( i, j , false );
  810. };
  811. cNode = ptr + ids ;
  812. cPixel = tMemory + idd;
  813. if( monochrome ) {
  814. #ifdef CHECK_SUN_BIT
  815. if( cNode->values.sbit ) {
  816. cPixel[0] = 0;
  817. } else {
  818. cPixel[0] = (uint8_t)((((float) cNode->values.min ) * 255.f ) / 4096.f );
  819. };
  820. #else
  821. cPixel[0] = (uint8_t)((((float) cNode->values.min ) * 255.f ) / 4096.f );
  822. #endif
  823. } else {
  824. #ifdef CHECK_SUN_BIT
  825. if( cNode->values.sbit ) {
  826. // Set Red
  827. cPixel[0] = 0x00;
  828. cPixel[1] = 0xff;
  829. cPixel[2] = 0x00;
  830. cPixel[3] = 0xff;
  831. } else {
  832. if( cNode->values.cnt == 0 ) {
  833. // Set Blue
  834. cPixel[0] = 0x00;
  835. cPixel[1] = 0x00;
  836. cPixel[2] = 0xff;
  837. cPixel[3] = 0xff;
  838. } else {
  839. uint32_t tColor;
  840. float tVal = ((float) cNode->values.min ) / 4096.f;
  841. tColor = interpolate( 0x000000ff, 0xffffffff, tVal );
  842. cPixel[0] = ( tColor >> 24 ) & 0xFF;
  843. cPixel[1] = ( tColor >> 16 ) & 0xFF;
  844. cPixel[2] = ( tColor >> 8 ) & 0xFF;
  845. cPixel[3] = tColor & 0xFF;
  846. };
  847. };
  848. #else
  849. if( cNode->values.cnt == 0 ) {
  850. // Set Blue
  851. cPixel[0] = 0x00;
  852. cPixel[1] = 0x00;
  853. cPixel[2] = 0xff;
  854. cPixel[3] = 0xff;
  855. } else {
  856. uint32_t tColor;
  857. float tVal = ((float) cNode->values.min ) / 4096.f;
  858. tColor = interpolate( 0x000000ff, 0xffffffff, tVal );
  859. cPixel[0] = ( tColor >> 24 ) & 0xFF;
  860. cPixel[1] = ( tColor >> 16 ) & 0xFF;
  861. cPixel[2] = ( tColor >> 8 ) & 0xFF;
  862. cPixel[3] = tColor & 0xFF;
  863. };
  864. #endif
  865. };
  866. };
  867. };
  868. sprintf( filename, "data/device_draws/%s_map.png", uid );
  869. RetVal = stbi_write_png( filename, TH_LINE_NODES, Q_SCAN_COUNT, cSpace, tMemory, TH_LINE_NODES * cSpace );
  870. free( tMemory );
  871. if( RetVal == 0 ) return false;
  872. return true;
  873. };
  874. */
  875. void deviceNodeSensor::broadcastFlip( uint16_t flipCount ){
  876. EVENT_CONTAINER *bEVT = NULL;
  877. bEVT = createBasicEvent( m_sNodeData.node_id, m_eEventClass, EVT_FLIP, NULL, NULL, NULL );
  878. if( bEVT != NULL ) m_pEventHeap->broadcast( bEVT );
  879. };
  880. void deviceNodeSensor::broadcastConnected( void ){
  881. EVENT_CONTAINER *bEVT = NULL;
  882. bEVT = createBasicEvent( m_sNodeData.node_id, m_eEventClass, EVT_CONNECTED, NULL, NULL, NULL );
  883. if( bEVT != NULL ) m_pEventHeap->broadcast( bEVT );
  884. };
  885. void deviceNodeSensor::broadcastDisconnected( void ){
  886. EVENT_CONTAINER *bEVT = NULL;
  887. bEVT = createBasicEvent( m_sNodeData.node_id, m_eEventClass, EVT_DISCONNECTED, NULL, NULL, NULL );
  888. if( bEVT != NULL ) m_pEventHeap->broadcast( bEVT );
  889. };
  890. void deviceNodeSensor::startWaitDeviceOnline( LRF_WORK_MODE wm, LRF_WORK_STAGE stg ){
  891. m_SP_WM = wm;
  892. m_SP_STAGE = stg;
  893. setWM( LRF_WM_WAIT_ONLINE_SPECIAL );
  894. };
  895. uint8_t deviceNodeSensor::getSensStatus(){
  896. return m_sLRF_DATA.data_status;
  897. };
  898. void deviceNodeSensor::updateDataStatus( uint8_t ds, uint8_t ns ){
  899. if ( m_pDBC == NULL ) return;
  900. if ( m_sNodeData.node_id ) {
  901. if( ds < ARGUS_DS_CONNECTING ) {
  902. m_pDBC->setNodeOffline( m_sNodeData.node_id );
  903. } else {
  904. m_pDBC->setNodeOnline( m_sNodeData.node_id, ns );
  905. };
  906. };
  907. if ( m_sLRF_DATA.id ) {
  908. m_sLRF_DATA.data_status = ds;
  909. m_pDBC->setLRFDataStatus( m_sLRF_DATA.id, ds );
  910. };
  911. };
  912. void deviceNodeSensor::waitStateFunction( LRF_WORK_STAGE boboStage ){
  913. #ifdef DEBUG_EXTRA
  914. LOG("WaitState");
  915. uint8_t cwm = getWM();
  916. uint8_t cst = getStage();
  917. char tB[128];
  918. sprintf( tB, "WM: 0x%02X STG: 0x%02X", cwm, cst );
  919. LOG( tB );
  920. #endif
  921. if( m_bTimeOutCounter ){
  922. m_bTimeOutCounter--;
  923. DELAY_10MS( LGS_DEFAULT_WS_TIME );
  924. } else {
  925. if( m_bOpRetryCnt ){
  926. #ifdef DEBUG_EXTRA
  927. LOG("ATTEMPT RECOVER");
  928. #endif
  929. m_bOpRetryCnt--;
  930. m_bTimeOutCounter = LGS_DEFAULT_TO_COUNTER;
  931. registerCommError( false );
  932. LRF_WORK_STAGE pS = getPrevStage();
  933. if ( pS != LRF_WS_INIT ) {
  934. #ifdef DEBUG_EXTRA
  935. LOG("Set Prev Stage");
  936. #endif
  937. setStage( pS, false );
  938. } else {
  939. if ( boboStage != LRF_WS_INIT ) {
  940. #ifdef DEBUG_EXTRA
  941. LOG("Set Desired Stage");
  942. #endif
  943. setStage( boboStage, false );
  944. } else {
  945. #ifdef DEBUG_EXTRA
  946. LOG("Set Connection Stage");
  947. #endif
  948. setWM( LRF_WM_CONNECTION );
  949. return;
  950. };
  951. };
  952. } else {
  953. LOG_ERROR("Communication Wait State TO!");
  954. registerCommError( true );
  955. //setStage( boboStage );
  956. setWM( LRF_WM_CONNECTION );
  957. return;
  958. };
  959. DELAY_10MS( LGS_DEFAULT_WS_TIME );
  960. transactionResetResult();
  961. };
  962. };
  963. void deviceNodeSensor::poolStateFunction(){
  964. goNextStage();
  965. transactionResetResult();
  966. };
  967. bool deviceNodeSensor::initWModeFunction( uint8_t wm ){
  968. if( getOWM() != wm ) {
  969. setWM( wm );
  970. setWM( wm );
  971. setStage( LRF_WS_INIT );
  972. transactionResetResult();
  973. m_bOpRetryCnt = LGS_CMD_RETRY_COUNT;
  974. return true;
  975. };
  976. return false;
  977. };
  978. void deviceNodeSensor::m_fThreadProc( void ){
  979. time_t tTimeT_Current;
  980. time_t tTimeT_Old = time( NULL );
  981. m_bThreadStatus = TH_WORKING;
  982. setWM( LRF_WM_INIT );
  983. LOG("Start Dev Thread");
  984. while( m_bThreadStatus < TH_ENDED ){
  985. tTimeT_Current = time( NULL );
  986. if ( tTimeT_Old != tTimeT_Current ) {
  987. tTimeT_Old = tTimeT_Current;
  988. if ( m_nCommErrorsStatTO ) {
  989. m_nCommErrorsStatTO --;
  990. } else {
  991. /**
  992. * Update Communication Status!
  993. */
  994. m_nCommErrorsStatTO = COMMUNICATION_ERROR_STATISTIC_HARVEST_INTERVAL_IN_SECONDS;
  995. m_pDBC->updateLRFComStat( m_sNodeData.aux_id, m_nCommErrorsCounter );
  996. m_nCommErrorsCounter = 0;
  997. };
  998. proceedDBCommands();
  999. };
  1000. if( getWM() > LRF_WM_CONNECTION ){
  1001. if( !validateCommunicationRessource() ){
  1002. setWM( LRF_WM_CONNECTION );
  1003. DELAY_10MS( 200 );
  1004. };
  1005. };
  1006. switch( getWM() ) {
  1007. case LRF_WM_INIT: reloadDeviceNodeFromDB(); setWM( LRF_WM_CONNECTION ); break;
  1008. case LRF_WM_CONNECTION: connectMode(); break;
  1009. case LRF_WM_HANDSHAKE: handshakeMode(); break;
  1010. case LRF_WM_SYNC_UP: readMode(); break;
  1011. case LRF_WM_SYNC_DOWN: syncMode(); break;
  1012. case LRF_WM_BORDER: drawMode(); break;
  1013. case LRF_WM_FULL_BORDER: fullDrawMode(); break;
  1014. case LRF_WM_FILE: getfileMode(); break;
  1015. case LRF_WM_FILE_TF: fileTransferMode(); break;
  1016. case LRF_WM_ALARM: alarmMode(); break;
  1017. case LRF_WM_IDLE: idleMode(); break;
  1018. case LRF_WM_STATUS_MASK: maskMode(); break;
  1019. case LRF_WM_SAVE_HW: save_ini(); break;
  1020. case LRF_WM_SAVE_DETECTION: save_detection(); break;
  1021. case LRF_WM_SAVE_EPROM: save_eprom(); break;
  1022. case LRF_WM_SAVE_MASKS: save_masks(); break;
  1023. case LRF_WM_WAIT_ONLINE_SPECIAL: wait_device(); break;
  1024. default: LOG("WM NOT REG"); setWM( LRF_WM_IDLE );
  1025. };
  1026. };
  1027. LOG("Stop Dev Thread");
  1028. };
  1029. void deviceNodeSensor::setStage( LRF_WORK_STAGE st, bool update_old_stage ){
  1030. m_mStageProtector.lock();
  1031. if( update_old_stage ) m_eOldWorkingStage = m_eWorkingStage;
  1032. m_eWorkingStage = st;
  1033. m_mStageProtector.unlock();
  1034. };
  1035. void deviceNodeSensor::goNextStage( bool update_old_stage ){
  1036. m_mStageProtector.lock();
  1037. if( m_eWorkingStage < LRF_WS_COUNT ){
  1038. if( update_old_stage ) m_eOldWorkingStage = m_eWorkingStage;
  1039. m_eWorkingStage++;
  1040. };
  1041. m_mStageProtector.unlock();
  1042. };
  1043. LRF_WORK_STAGE deviceNodeSensor::getStage(){
  1044. LRF_WORK_STAGE rV;
  1045. m_mStageProtector.lock();
  1046. rV = m_eWorkingStage;
  1047. m_mStageProtector.unlock();
  1048. return rV;
  1049. };
  1050. LRF_WORK_STAGE deviceNodeSensor::getPrevStage(){
  1051. LRF_WORK_STAGE rV;
  1052. m_mStageProtector.lock();
  1053. rV = m_eOldWorkingStage;
  1054. m_mStageProtector.unlock();
  1055. return rV;
  1056. };
  1057. bool deviceNodeSensor::generateIniFile( uint8_t i ){
  1058. if ( m_pDBC == NULL ) return false;
  1059. DBC_LRF_INI *tData = m_pDBC->getINIData( m_sNodeData.aux_id );
  1060. if ( tData ) {
  1061. } else {
  1062. LOG("No Data From DB! ---> Set Defaults!");
  1063. tData = (DBC_LRF_INI *) malloc( sizeof(DBC_LRF_INI));
  1064. if ( tData ) {
  1065. memset( tData, 0 , sizeof(DBC_LRF_INI));
  1066. tData->_AUTO_RESET_ENABLE = 0;
  1067. tData->_BOARD_SPEED = 4;
  1068. tData->_BOARD_TEMPERATURE = 1;
  1069. tData->_RUN_DIR = 2;
  1070. };
  1071. };
  1072. if ( tData ) {
  1073. CSTR tMD5 = getUniqID_MD5();
  1074. if ( tMD5 ) {
  1075. sprintf( m_pTGENFILENAMES[i], "%s", tMD5 );
  1076. free( tMD5 );
  1077. char tB[256];
  1078. sprintf( tB,"data/uploads/%s", m_pTGENFILENAMES[i] );
  1079. FILE *fd = fopen( tB, "wb+" );
  1080. if ( fd ) {
  1081. uint16_t *tpP = (uint16_t *)tData;
  1082. for( int idx=0; idx < __INI_FILE_ENTRIES_CNT__; idx++ ){
  1083. fprintf( fd, "%s=%d\r\n", settings_user_ini_fields[idx], tpP[idx] );
  1084. };
  1085. fclose( fd );
  1086. return true;
  1087. };
  1088. };
  1089. free( tData );
  1090. }
  1091. return false;
  1092. };
  1093. void deviceNodeSensor::registerCommError( bool withEvent ){
  1094. m_nCommErrorsCounter++;
  1095. if ( withEvent ) {
  1096. EVENT_CONTAINER *bEVT = createBasicEvent(m_sNodeData.node_id, m_eEventClass, EVT_COMMUNICATION_JAMED, NULL, NULL, NULL);
  1097. if( bEVT != NULL ) {
  1098. m_pEventHeap->broadcast(bEVT);
  1099. };
  1100. };
  1101. };
  1102. bool deviceNodeSensor::generateRestartFile( uint8_t i ){
  1103. if ( m_pDBC == NULL ) return false;
  1104. CSTR tMD5 = getUniqID_MD5();
  1105. if ( tMD5 ) {
  1106. sprintf( m_pTGENFILENAMES[i], "%s", tMD5 );
  1107. free( tMD5 );
  1108. char tB[256];
  1109. sprintf( tB,"data/uploads/%s", m_pTGENFILENAMES[i] );
  1110. FILE *fd = fopen( tB, "wb+" );
  1111. if ( fd ) {
  1112. fprintf( fd, "copy *.exe c:\r\ncopy *.ini c:\r\ncopy *.txt c:\r\n" );
  1113. fclose( fd );
  1114. return true;
  1115. };
  1116. };
  1117. return false;
  1118. };
  1119. #undef LOG
  1120. #undef LOG_ERROR