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.

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