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.

1574 lines
39 KiB

​
​
​
​
Debug: log communication timing of sensors and the Ethernet2UART gateway DEBUG_COMMUNICATION is turned on in deviceNodeEthernet2Serial.cpp and deviceNodeSensor.cpp only. It isn't defined project-wide, which would also enable the per-byte hex dumps of protocol.cpp and the serial transports. To turn it off, comment out the #define at the top of the two files. Gateway I/O thread (tag E2UART): - "IO: request #n (x B): NO reply in N ms": nothing within the reply window (E2UART_REPLY_TIMEOUT_MS). - "IO: request #n: reply x B, first byte N ms, done N ms": replies slower than 500 ms (E2UART_SLOW_REPLY_MS), or an incomplete packet. - "IO: late data x B, N ms after request #n": a reply that came after the window (now given to the parser of that request). - "IO stats 60 s [host:port]": requests, replies, first-byte avg/max, slow, without reply, late. Sensor (tag NODSEN): - "COMM: reply/NACK WM STG reply N ms": replies slower than 500 ms (LGS_SLOW_REPLY_MS), NACKs and commands that needed retries, with the time since the first attempt. - "COMM: no reply - WM STG attempt k/17 timed out after N ms": each attempt that timed out. - "COMM: command failed - ...": details next to "Communication Wait State TO!". - "COMM stats 60 s": replies, avg/max reply time, slow, timed-out attempts (with the wait per attempt), failed commands. The gateway's per-chunk "Send/Read x bytes" debug lines are replaced by these per-request lines. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0139veDo3XyavMuVhdnHcN16
5 hours ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
Debug: log communication timing of sensors and the Ethernet2UART gateway DEBUG_COMMUNICATION is turned on in deviceNodeEthernet2Serial.cpp and deviceNodeSensor.cpp only. It isn't defined project-wide, which would also enable the per-byte hex dumps of protocol.cpp and the serial transports. To turn it off, comment out the #define at the top of the two files. Gateway I/O thread (tag E2UART): - "IO: request #n (x B): NO reply in N ms": nothing within the reply window (E2UART_REPLY_TIMEOUT_MS). - "IO: request #n: reply x B, first byte N ms, done N ms": replies slower than 500 ms (E2UART_SLOW_REPLY_MS), or an incomplete packet. - "IO: late data x B, N ms after request #n": a reply that came after the window (now given to the parser of that request). - "IO stats 60 s [host:port]": requests, replies, first-byte avg/max, slow, without reply, late. Sensor (tag NODSEN): - "COMM: reply/NACK WM STG reply N ms": replies slower than 500 ms (LGS_SLOW_REPLY_MS), NACKs and commands that needed retries, with the time since the first attempt. - "COMM: no reply - WM STG attempt k/17 timed out after N ms": each attempt that timed out. - "COMM: command failed - ...": details next to "Communication Wait State TO!". - "COMM stats 60 s": replies, avg/max reply time, slow, timed-out attempts (with the wait per attempt), failed commands. The gateway's per-chunk "Send/Read x bytes" debug lines are replaced by these per-request lines. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0139veDo3XyavMuVhdnHcN16
5 hours ago
​
Debug: log communication timing of sensors and the Ethernet2UART gateway DEBUG_COMMUNICATION is turned on in deviceNodeEthernet2Serial.cpp and deviceNodeSensor.cpp only. It isn't defined project-wide, which would also enable the per-byte hex dumps of protocol.cpp and the serial transports. To turn it off, comment out the #define at the top of the two files. Gateway I/O thread (tag E2UART): - "IO: request #n (x B): NO reply in N ms": nothing within the reply window (E2UART_REPLY_TIMEOUT_MS). - "IO: request #n: reply x B, first byte N ms, done N ms": replies slower than 500 ms (E2UART_SLOW_REPLY_MS), or an incomplete packet. - "IO: late data x B, N ms after request #n": a reply that came after the window (now given to the parser of that request). - "IO stats 60 s [host:port]": requests, replies, first-byte avg/max, slow, without reply, late. Sensor (tag NODSEN): - "COMM: reply/NACK WM STG reply N ms": replies slower than 500 ms (LGS_SLOW_REPLY_MS), NACKs and commands that needed retries, with the time since the first attempt. - "COMM: no reply - WM STG attempt k/17 timed out after N ms": each attempt that timed out. - "COMM: command failed - ...": details next to "Communication Wait State TO!". - "COMM stats 60 s": replies, avg/max reply time, slow, timed-out attempts (with the wait per attempt), failed commands. The gateway's per-chunk "Send/Read x bytes" debug lines are replaced by these per-request lines. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0139veDo3XyavMuVhdnHcN16
5 hours ago
​
​
​
​
​
​
​
​
​
Debug: log communication timing of sensors and the Ethernet2UART gateway DEBUG_COMMUNICATION is turned on in deviceNodeEthernet2Serial.cpp and deviceNodeSensor.cpp only. It isn't defined project-wide, which would also enable the per-byte hex dumps of protocol.cpp and the serial transports. To turn it off, comment out the #define at the top of the two files. Gateway I/O thread (tag E2UART): - "IO: request #n (x B): NO reply in N ms": nothing within the reply window (E2UART_REPLY_TIMEOUT_MS). - "IO: request #n: reply x B, first byte N ms, done N ms": replies slower than 500 ms (E2UART_SLOW_REPLY_MS), or an incomplete packet. - "IO: late data x B, N ms after request #n": a reply that came after the window (now given to the parser of that request). - "IO stats 60 s [host:port]": requests, replies, first-byte avg/max, slow, without reply, late. Sensor (tag NODSEN): - "COMM: reply/NACK WM STG reply N ms": replies slower than 500 ms (LGS_SLOW_REPLY_MS), NACKs and commands that needed retries, with the time since the first attempt. - "COMM: no reply - WM STG attempt k/17 timed out after N ms": each attempt that timed out. - "COMM: command failed - ...": details next to "Communication Wait State TO!". - "COMM stats 60 s": replies, avg/max reply time, slow, timed-out attempts (with the wait per attempt), failed commands. The gateway's per-chunk "Send/Read x bytes" debug lines are replaced by these per-request lines. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0139veDo3XyavMuVhdnHcN16
5 hours ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
Debug: log communication timing of sensors and the Ethernet2UART gateway DEBUG_COMMUNICATION is turned on in deviceNodeEthernet2Serial.cpp and deviceNodeSensor.cpp only. It isn't defined project-wide, which would also enable the per-byte hex dumps of protocol.cpp and the serial transports. To turn it off, comment out the #define at the top of the two files. Gateway I/O thread (tag E2UART): - "IO: request #n (x B): NO reply in N ms": nothing within the reply window (E2UART_REPLY_TIMEOUT_MS). - "IO: request #n: reply x B, first byte N ms, done N ms": replies slower than 500 ms (E2UART_SLOW_REPLY_MS), or an incomplete packet. - "IO: late data x B, N ms after request #n": a reply that came after the window (now given to the parser of that request). - "IO stats 60 s [host:port]": requests, replies, first-byte avg/max, slow, without reply, late. Sensor (tag NODSEN): - "COMM: reply/NACK WM STG reply N ms": replies slower than 500 ms (LGS_SLOW_REPLY_MS), NACKs and commands that needed retries, with the time since the first attempt. - "COMM: no reply - WM STG attempt k/17 timed out after N ms": each attempt that timed out. - "COMM: command failed - ...": details next to "Communication Wait State TO!". - "COMM stats 60 s": replies, avg/max reply time, slow, timed-out attempts (with the wait per attempt), failed commands. The gateway's per-chunk "Send/Read x bytes" debug lines are replaced by these per-request lines. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0139veDo3XyavMuVhdnHcN16
5 hours ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
Debug: log communication timing of sensors and the Ethernet2UART gateway DEBUG_COMMUNICATION is turned on in deviceNodeEthernet2Serial.cpp and deviceNodeSensor.cpp only. It isn't defined project-wide, which would also enable the per-byte hex dumps of protocol.cpp and the serial transports. To turn it off, comment out the #define at the top of the two files. Gateway I/O thread (tag E2UART): - "IO: request #n (x B): NO reply in N ms": nothing within the reply window (E2UART_REPLY_TIMEOUT_MS). - "IO: request #n: reply x B, first byte N ms, done N ms": replies slower than 500 ms (E2UART_SLOW_REPLY_MS), or an incomplete packet. - "IO: late data x B, N ms after request #n": a reply that came after the window (now given to the parser of that request). - "IO stats 60 s [host:port]": requests, replies, first-byte avg/max, slow, without reply, late. Sensor (tag NODSEN): - "COMM: reply/NACK WM STG reply N ms": replies slower than 500 ms (LGS_SLOW_REPLY_MS), NACKs and commands that needed retries, with the time since the first attempt. - "COMM: no reply - WM STG attempt k/17 timed out after N ms": each attempt that timed out. - "COMM: command failed - ...": details next to "Communication Wait State TO!". - "COMM stats 60 s": replies, avg/max reply time, slow, timed-out attempts (with the wait per attempt), failed commands. The gateway's per-chunk "Send/Read x bytes" debug lines are replaced by these per-request lines. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0139veDo3XyavMuVhdnHcN16
5 hours ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
Debug: log communication timing of sensors and the Ethernet2UART gateway DEBUG_COMMUNICATION is turned on in deviceNodeEthernet2Serial.cpp and deviceNodeSensor.cpp only. It isn't defined project-wide, which would also enable the per-byte hex dumps of protocol.cpp and the serial transports. To turn it off, comment out the #define at the top of the two files. Gateway I/O thread (tag E2UART): - "IO: request #n (x B): NO reply in N ms": nothing within the reply window (E2UART_REPLY_TIMEOUT_MS). - "IO: request #n: reply x B, first byte N ms, done N ms": replies slower than 500 ms (E2UART_SLOW_REPLY_MS), or an incomplete packet. - "IO: late data x B, N ms after request #n": a reply that came after the window (now given to the parser of that request). - "IO stats 60 s [host:port]": requests, replies, first-byte avg/max, slow, without reply, late. Sensor (tag NODSEN): - "COMM: reply/NACK WM STG reply N ms": replies slower than 500 ms (LGS_SLOW_REPLY_MS), NACKs and commands that needed retries, with the time since the first attempt. - "COMM: no reply - WM STG attempt k/17 timed out after N ms": each attempt that timed out. - "COMM: command failed - ...": details next to "Communication Wait State TO!". - "COMM stats 60 s": replies, avg/max reply time, slow, timed-out attempts (with the wait per attempt), failed commands. The gateway's per-chunk "Send/Read x bytes" debug lines are replaced by these per-request lines. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0139veDo3XyavMuVhdnHcN16
5 hours ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
Debug: log communication timing of sensors and the Ethernet2UART gateway DEBUG_COMMUNICATION is turned on in deviceNodeEthernet2Serial.cpp and deviceNodeSensor.cpp only. It isn't defined project-wide, which would also enable the per-byte hex dumps of protocol.cpp and the serial transports. To turn it off, comment out the #define at the top of the two files. Gateway I/O thread (tag E2UART): - "IO: request #n (x B): NO reply in N ms": nothing within the reply window (E2UART_REPLY_TIMEOUT_MS). - "IO: request #n: reply x B, first byte N ms, done N ms": replies slower than 500 ms (E2UART_SLOW_REPLY_MS), or an incomplete packet. - "IO: late data x B, N ms after request #n": a reply that came after the window (now given to the parser of that request). - "IO stats 60 s [host:port]": requests, replies, first-byte avg/max, slow, without reply, late. Sensor (tag NODSEN): - "COMM: reply/NACK WM STG reply N ms": replies slower than 500 ms (LGS_SLOW_REPLY_MS), NACKs and commands that needed retries, with the time since the first attempt. - "COMM: no reply - WM STG attempt k/17 timed out after N ms": each attempt that timed out. - "COMM: command failed - ...": details next to "Communication Wait State TO!". - "COMM stats 60 s": replies, avg/max reply time, slow, timed-out attempts (with the wait per attempt), failed commands. The gateway's per-chunk "Send/Read x bytes" debug lines are replaced by these per-request lines. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0139veDo3XyavMuVhdnHcN16
5 hours ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
Debug: log communication timing of sensors and the Ethernet2UART gateway DEBUG_COMMUNICATION is turned on in deviceNodeEthernet2Serial.cpp and deviceNodeSensor.cpp only. It isn't defined project-wide, which would also enable the per-byte hex dumps of protocol.cpp and the serial transports. To turn it off, comment out the #define at the top of the two files. Gateway I/O thread (tag E2UART): - "IO: request #n (x B): NO reply in N ms": nothing within the reply window (E2UART_REPLY_TIMEOUT_MS). - "IO: request #n: reply x B, first byte N ms, done N ms": replies slower than 500 ms (E2UART_SLOW_REPLY_MS), or an incomplete packet. - "IO: late data x B, N ms after request #n": a reply that came after the window (now given to the parser of that request). - "IO stats 60 s [host:port]": requests, replies, first-byte avg/max, slow, without reply, late. Sensor (tag NODSEN): - "COMM: reply/NACK WM STG reply N ms": replies slower than 500 ms (LGS_SLOW_REPLY_MS), NACKs and commands that needed retries, with the time since the first attempt. - "COMM: no reply - WM STG attempt k/17 timed out after N ms": each attempt that timed out. - "COMM: command failed - ...": details next to "Communication Wait State TO!". - "COMM stats 60 s": replies, avg/max reply time, slow, timed-out attempts (with the wait per attempt), failed commands. The gateway's per-chunk "Send/Read x bytes" debug lines are replaced by these per-request lines. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0139veDo3XyavMuVhdnHcN16
5 hours ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. #include "deviceNodeSensor.h"
  2. #include "../../components/object/objectSystemAnchor.h"
  3. #include "kernelPrototype.h"
  4. // Communication timing: slow replies, timeouts and a one-minute summary (see txTimingReport)
  5. #define DEBUG_COMMUNICATION
  6. #ifndef LGS_SLOW_REPLY_MS
  7. #define LGS_SLOW_REPLY_MS 500 // replies slower than this are logged one by one
  8. #endif
  9. #define LGS_TIMING_REPORT_MS 60000
  10. #include <time.h>
  11. static uint64_t nsNowMs(void)
  12. {
  13. struct timespec ts;
  14. clock_gettime(CLOCK_MONOTONIC, &ts);
  15. return (uint64_t) ts.tv_sec * 1000 + ts.tv_nsec / 1000000;
  16. }
  17. #define LOG(X) deviceNode::m_pLog->Log("NODSEN", this, X)
  18. #define LOG_ERROR(X) deviceNode::m_pLog->Log("NODSEN", this, _LOG_ERROR, X)
  19. /*
  20. USER.INI
  21. AUTORESET_ENABLE=0
  22. AUTORESET_INTERLEAVE=2000
  23. AUTORESET_TIMEOUT=-5536
  24. BOARD_TEMPERATURE=20
  25. BOARD_ZEROSHIFT=0
  26. BOARD_ENCODERSTEPS=6
  27. BOARD_GEARRATIO=61
  28. SWITCHER_TABLE=27,55,82
  29. SWITCHER_REPLY_TIMEOUT=100
  30. SWITCHER_COMMAND_TIMEOUT=500
  31. SWITCHER_AUTO_MODE=0
  32. SWITCHER_AUTO_ENTER_TIMEOUT=60
  33. SWITCHER_AUTO_SWITCH_TIMEOUT=3
  34. */
  35. /*
  36. "ADDRESS",
  37. "CAMERA_TYPE",
  38. "CAMERA_SHIFT",
  39. "CAMERA_AUTO",
  40. "CAMERA_DELAY",
  41. "RELAY_NC",
  42. "RELAY_AUTO",
  43. "RELAY_DELAY",
  44. "VOLTAGE_CONTROL",
  45. "MASTER_DISABLED",
  46. "SLAVE_DISABLED",
  47. */
  48. /*
  49. typedef struct S_DBC_LRF_INI{
  50. uint16_t _AUTO_RESET_ENABLE;
  51. uint16_t _AUTO_RESET_INTERLEAVE;
  52. uint16_t _AUTO_RESET_TIMEOUT;
  53. uint16_t _BOARD_TEMPERATURE;
  54. uint16_t _BOARD_ZEROSHIFT;
  55. uint16_t _BOARD_SPEED;
  56. uint16_t _RUN_DIR;
  57. uint16_t _NO_BLACK_TARGETS;
  58. uint16_t _JUST_MOVING_TARGETS;
  59. uint16_t _JUST_MOVING_RADIAL;
  60. uint16_t _JUST_MOVING_DISTANCE;
  61. uint16_t _FREEZ_FIRST_TH;
  62. uint16_t _SAVE_FIRST_TH;
  63. }DBC_LRF_INI;
  64. */
  65. #define __INI_FILE_ENTRIES_CNT__ 13
  66. const CSTR settings_user_ini_fields[__INI_FILE_ENTRIES_CNT__] = {"AUTORESET_ENABLE", "AUTORESET_INTERLEAVE", "AUTORESET_TIMEOUT", "BOARD_TEMPERATURE", "BOARD_ZEROSHIFT", "BOARD_SPEED", "RUN_DIR", "NO_BLACK_TARGETS", "JUST_MOVING_TARGETS",
  67. "JUST_MOVING_RADIAL", "JUST_MOVING_DISTANCE", "FREEZ_FIRST_TH", "SAVE_FIRST_TH"};
  68. /*
  69. LPCTSTR m_pSetting[] =
  70. {
  71. { "SN" },
  72. { "ADDRESS" },
  73. { "CAMERA_TYPE" },
  74. { "CAMERA_SHIFT" },
  75. { "CAMERA_AUTO" },
  76. { "CAMERA_DELAY" },
  77. { "RELAY_NC" },
  78. { "RELAY_AUTO" },
  79. { "RELAY_DELAY" },
  80. { "VOLTAGE_CONTROL" },
  81. { "MASTER_DISABLED" },
  82. { "SLAVE_DISABLED" },
  83. { "AUTORESET_ENABLE" },
  84. { "AUTORESET_INTERLEAVE" },
  85. { "AUTORESET_TIMEOUT" },
  86. { "BOARD_TEMPERATURE" },
  87. { "BOARD_ZEROSHIFT" },
  88. { "BOARD_ENCODERSTEPS" },
  89. { "BOARD_GEARRATIO" },
  90. { "BOARD_MAXRANGE" },
  91. { "BOARD_SPEED" },
  92. { "RUN_DIR" },
  93. { "SWITCHER_TABLE" },
  94. { "SWITCHER_REPLY_TIMEOUT" },
  95. { "SWITCHER_COMMAND_TIMEOUT" },
  96. { "SWITCHER_AUTO_MODE" },
  97. { "SWITCHER_AUTO_ENTER_TIMEOUT" },
  98. { "SWITCHER_AUTO_SWITCH_TIMEOUT" },
  99. { "NO_BLACK_TARGETS" },
  100. { "JUST_MOVING_TARGETS" },
  101. { "JUST_MOVING_RADIAL" },
  102. { "JUST_MOVING_DISTANCE" },
  103. { "BLIND_ZONE_START_ANGLE" },
  104. { "BLIND_ZONE_END_ANGLE" },
  105. { "BLIND_ZONE_START_RANGE" },
  106. { "BLIND_ZONE_END_RANGE" },
  107. { "BLIND_ZONE_WIDTH" },
  108. { "BLIND_ZONE_DEPTH" },
  109. { "BLIND_ZONE_ANGLE" },
  110. { "BLIND_ZONE_RANGE" }
  111. #ifdef STORE_FIRST_SAF
  112. ,{ "FREEZ_FIRST_TH" }
  113. ,( "SAVE_FIRST_TH" )
  114. #endif
  115. };
  116. */
  117. LRF_WORK_STAGE operator++(LRF_WORK_STAGE & id, int)
  118. {
  119. LRF_WORK_STAGE currentID = id;
  120. if (LRF_WS_COUNT < id + 1)
  121. {
  122. id = LRF_WS_COUNT;
  123. }
  124. else
  125. {
  126. id = static_cast<LRF_WORK_STAGE>(id + 1);
  127. };
  128. return (currentID);
  129. }
  130. void deviceNodeSensor::transactionResetResult(void)
  131. {
  132. m_mPPModeProtector.lock();
  133. m_ePPMODE = STG_PUSH;
  134. m_mPPModeProtector.unlock();
  135. }
  136. void deviceNodeSensor::transactionSetResult(bool ACK)
  137. {
  138. m_mPPModeProtector.lock();
  139. bool tWasWaiting = (m_ePPMODE == STG_WAIT);
  140. m_ePPMODE = (ACK) ? STG_POOL : STG_PUSH;
  141. m_mPPModeProtector.unlock();
  142. if (tWasWaiting)
  143. {
  144. txTimingDone(ACK);
  145. };
  146. }
  147. void deviceNodeSensor::transactionWaitResult(void)
  148. {
  149. m_mPPModeProtector.lock();
  150. m_ePPMODE = STG_WAIT;
  151. m_mPPModeProtector.unlock();
  152. m_nTxStartMs = nsNowMs();
  153. if (m_nTxFirstMs == 0)
  154. {
  155. m_nTxFirstMs = m_nTxStartMs; // a retry keeps the time of the first attempt
  156. };
  157. }
  158. /*
  159. A reply ended the wait of the running request: reply time from the request, and from the first
  160. attempt when the command needed retries.
  161. */
  162. void deviceNodeSensor::txTimingDone(bool ok)
  163. {
  164. if (m_nTxStartMs == 0)
  165. {
  166. return;
  167. };
  168. uint64_t tNow = nsNowMs();
  169. uint64_t tReply = tNow - m_nTxStartMs;
  170. uint64_t tTotal = (m_nTxFirstMs != 0) ? tNow - m_nTxFirstMs : tReply;
  171. m_nTxCount++;
  172. m_nTxSumMs += tReply;
  173. if (tReply > m_nTxMaxMs)
  174. {
  175. m_nTxMaxMs = tReply;
  176. };
  177. #ifdef DEBUG_COMMUNICATION
  178. if (tReply > LGS_SLOW_REPLY_MS || tTotal > tReply || ! ok)
  179. {
  180. m_nTxSlow++;
  181. char tB[192];
  182. snprintf(tB, sizeof(tB), "COMM: %s WM 0x%02X STG 0x%02X reply %llu ms%s (wait %u ms per attempt)", ok ? "reply" : "NACK", (unsigned) getWM(), (unsigned) getStage(),
  183. (unsigned long long) tReply, (tTotal > tReply) ? ", command needed retries" : "", (unsigned) m_nTOCounterBase * 100);
  184. LOG(tB);
  185. if (tTotal > tReply)
  186. {
  187. snprintf(tB, sizeof(tB), "COMM: command done %llu ms after its first attempt", (unsigned long long) tTotal);
  188. LOG(tB);
  189. };
  190. };
  191. #endif
  192. m_nTxStartMs = 0;
  193. m_nTxFirstMs = 0;
  194. txTimingReport(false);
  195. }
  196. void deviceNodeSensor::txTimingReport(bool force)
  197. {
  198. #ifdef DEBUG_COMMUNICATION
  199. uint64_t tNow = nsNowMs();
  200. if (! force && tNow - m_nTxStatStartMs < LGS_TIMING_REPORT_MS)
  201. {
  202. return;
  203. };
  204. char tB[256];
  205. snprintf(tB, sizeof(tB), "COMM stats %llu s: %u replies, avg %llu ms, max %llu ms, %u slow (> %d ms) | %u attempts timed out (%u ms each), %u commands failed",
  206. (unsigned long long) ((tNow - m_nTxStatStartMs) / 1000), m_nTxCount, (unsigned long long) (m_nTxCount ? m_nTxSumMs / m_nTxCount : 0),
  207. (unsigned long long) m_nTxMaxMs, m_nTxSlow, LGS_SLOW_REPLY_MS, m_nTxAttemptTO, (unsigned) m_nTOCounterBase * 100, m_nTxFailed);
  208. LOG(tB);
  209. m_nTxCount = m_nTxSlow = m_nTxAttemptTO = m_nTxFailed = 0;
  210. m_nTxSumMs = m_nTxMaxMs = 0;
  211. m_nTxStatStartMs = tNow;
  212. #else
  213. if (force)
  214. {
  215. force = false;
  216. };
  217. #endif
  218. }
  219. STAGE_PP_CONDITION deviceNodeSensor::getPPStatus()
  220. {
  221. STAGE_PP_CONDITION rv;
  222. m_mPPModeProtector.lock();
  223. rv = m_ePPMODE;
  224. m_mPPModeProtector.unlock();
  225. return rv;
  226. }
  227. deviceNodeSensor::deviceNodeSensor(uint32_t id) :
  228. deviceNode(id),
  229. m_oPosition()
  230. {
  231. m_nCommErrorsCounter = 0;
  232. m_nTOCounterBase = LGS_DEFAULT_TO_COUNTER;
  233. m_nTxStartMs = m_nTxFirstMs = 0;
  234. m_nTxCount = m_nTxSlow = m_nTxAttemptTO = m_nTxFailed = 0;
  235. m_nTxSumMs = m_nTxMaxMs = 0;
  236. m_nTxStatStartMs = nsNowMs();
  237. m_ePPMODE = STG_INIT;
  238. m_bValidationSkipCounter = DAFAULT_VALIDATION_SKIP_COUNTER;
  239. m_nCommErrorsStatTO = COMMUNICATION_ERROR_STATISTIC_HARVEST_INTERVAL_IN_SECONDS;
  240. m_pTargetPipeLine = NULL;
  241. m_pEventHeap = basicEventHeap::getInstance();
  242. //m_bInitialSyncIsDone = false;
  243. switch (m_sNodeData.type)
  244. {
  245. case ARGUS_CLD_SENSOR : m_eEventClass = EVT_CLASS_SENSOR_CLD; break;
  246. case ARGUS_LGS_SENSOR : m_eEventClass = EVT_CLASS_SENSOR_LGS; break;
  247. case ARGUS_ALS_SENSOR : m_eEventClass = EVT_CLASS_SENSOR_ALS; break;
  248. case ARGUS_LFS_SENSOR : m_eEventClass = EVT_CLASS_SENSOR_LFS; break;
  249. case ARGUS_PLS_200_SENSOR: m_eEventClass = EVT_CLASS_SENSOR_PLS_210; break;
  250. case ARGUS_PLS_360_SENSOR: m_eEventClass = EVT_CLASS_SENSOR_PLS_360; break;
  251. case ARGUS_3D_SENSOR : m_eEventClass = EVT_CLASS_SENSOR_3D; break;
  252. default : m_eEventClass = EVT_CLASS_SYSTEM; break;
  253. };
  254. setWM(LRF_WM_INIT);
  255. memset(&m_sLRF_DATA, 0, sizeof(DBC_LRF_DATA));
  256. if (m_pDBC)
  257. {
  258. DBC_LRF_DATA * tDATA = m_pDBC->getLRFData(m_sNodeData.aux_id);
  259. if (tDATA != NULL)
  260. {
  261. memcpy(&m_sLRF_DATA, tDATA, sizeof(DBC_LRF_DATA));
  262. free(tDATA);
  263. m_pDBC->updateLRFUnfinishedUploads(m_sNodeData.aux_id);
  264. m_pTargetPipeLine = new targetPipeLine(m_sNodeData.node_id, m_sLRF_DATA.prefered_camera_id);
  265. #ifdef DEBUG
  266. }
  267. else
  268. {
  269. LOG_ERROR("No Sensor DATA !!");
  270. #endif
  271. };
  272. };
  273. // m_pTargetFilter = NULL;
  274. systemAnchor * SA = systemAnchor::getInstance();
  275. if (SA)
  276. {
  277. if (SA->beLocal())
  278. {
  279. LOG_ERROR("Local Coords!");
  280. m_oPosition.setPositionXYZ(m_sLRF_DATA.x, m_sLRF_DATA.y, m_sLRF_DATA.z);
  281. m_oPosition.setOrientationDeg(m_sLRF_DATA.x_rot, m_sLRF_DATA.y_rot, m_sLRF_DATA.z_rot);
  282. }
  283. else
  284. {
  285. LOG_ERROR("WGS Coords!");
  286. C3D_DECART LD = SA->getXYZ();
  287. char tZ[256];
  288. sprintf( tZ, "SX:%f SY:%f SZ:%f LX:%f LY:%f LZ:%f SH:%d Zone:%d", LD.x, LD.y, LD.z, m_sLRF_DATA.x, m_sLRF_DATA.y, m_sLRF_DATA.z, SA->getSH(), SA->getUTMZone() );
  289. LOG(tZ);
  290. m_oPosition.setPositionXYZ(LD.x + m_sLRF_DATA.x, LD.y + m_sLRF_DATA.y, LD.z + m_sLRF_DATA.z, SA->getSH(), SA->getUTMZone() );
  291. m_oPosition.setOrientationDeg(m_sLRF_DATA.x_rot, m_sLRF_DATA.y_rot, m_sLRF_DATA.z_rot);
  292. };
  293. }
  294. else
  295. {
  296. LOG_ERROR("No System Anchor Object - Using Local Binding!");
  297. m_oPosition.setPositionXYZ(m_sLRF_DATA.x, m_sLRF_DATA.y, m_sLRF_DATA.z);
  298. m_oPosition.setOrientationDeg(m_sLRF_DATA.x_rot, m_sLRF_DATA.y_rot, m_sLRF_DATA.z_rot);
  299. };
  300. m_oPosition.updateMatrix();
  301. #ifdef DEBUG
  302. char tB[128];
  303. sprintf(tB, "Finalized Sensor Location: Lat:%.3f Lon:%.3f Alt:%.3f", m_oPosition.getLat(), m_oPosition.getLon(), m_oPosition.getAlt());
  304. LOG(tB);
  305. sprintf(tB, "Finalized Sensor Location: x:%.3f y:%.3f z:%.3f", m_oPosition.getX(), m_oPosition.getY(), m_oPosition.getZ());
  306. LOG(tB);
  307. sprintf(tB, "Orientation: x:%.3f y:%.3f z:%.3f", m_oPosition.getRotX_deg(), m_oPosition.getRotY_deg(), m_oPosition.getRotZ_deg());
  308. LOG(tB);
  309. #endif
  310. LOG("Finish Construct!");
  311. }
  312. deviceNodeSensor::deviceNodeSensor(DBC_NODE_DATA * Data) :
  313. deviceNode(Data),
  314. m_oPosition()
  315. {
  316. m_nCommErrorsCounter = 0;
  317. m_nTOCounterBase = LGS_DEFAULT_TO_COUNTER;
  318. m_nTxStartMs = m_nTxFirstMs = 0;
  319. m_nTxCount = m_nTxSlow = m_nTxAttemptTO = m_nTxFailed = 0;
  320. m_nTxSumMs = m_nTxMaxMs = 0;
  321. m_nTxStatStartMs = nsNowMs();
  322. m_ePPMODE = STG_INIT;
  323. m_bValidationSkipCounter = DAFAULT_VALIDATION_SKIP_COUNTER;
  324. m_nCommErrorsStatTO = COMMUNICATION_ERROR_STATISTIC_HARVEST_INTERVAL_IN_SECONDS;
  325. m_pEventHeap = basicEventHeap::getInstance();
  326. //m_bInitialSyncIsDone = false;
  327. memset(&m_sLRF_DATA, 0, sizeof(DBC_LRF_DATA));
  328. m_pTargetPipeLine = NULL;
  329. if (m_pDBC)
  330. {
  331. reloadDeviceNodeFromDB();
  332. m_pDBC->updateLRFUnfinishedUploads(m_sLRF_DATA.id);
  333. m_pTargetPipeLine = new targetPipeLine(m_sNodeData.node_id, m_sLRF_DATA.prefered_camera_id);
  334. };
  335. setWM(LRF_WM_INIT);
  336. LOG("Finish Data Construct!");
  337. }
  338. deviceNodeSensor::~deviceNodeSensor()
  339. {
  340. // m_vTargetFilters.clear();
  341. if (m_pTargetPipeLine)
  342. {
  343. delete(m_pTargetPipeLine);
  344. }
  345. }
  346. /*
  347. bool deviceNodeSensor::filterTargetData( TARGET_DATA *ptr ){
  348. bool RetVal = false;
  349. if ( m_vTargetFilters.size() ) {
  350. for (vectorKernelEventFilters::iterator idx = m_vTargetFilters.begin(); idx != m_vTargetFilters.end(); idx++) {
  351. if( (*idx)->isActive() ) {
  352. if( (*idx)->validateTarget( m_sNodeData.node_id, ptr ) ) {
  353. RetVal = true;
  354. break;
  355. };
  356. };
  357. };
  358. };
  359. return RetVal;
  360. };
  361. */
  362. bool deviceNodeSensor::reloadDeviceNodeFromDB()
  363. {
  364. if (m_pDBC)
  365. {
  366. DBC_LRF_DATA * tDATA = m_pDBC->getLRFData(m_sNodeData.aux_id);
  367. if (tDATA != NULL)
  368. {
  369. memcpy(&m_sLRF_DATA, tDATA, sizeof(DBC_LRF_DATA));
  370. free(tDATA);
  371. };
  372. }
  373. else
  374. {
  375. return false;
  376. }
  377. systemAnchor * SA = systemAnchor::getInstance();
  378. if (SA)
  379. {
  380. if (SA->beLocal())
  381. {
  382. m_oPosition.setPositionXYZ(m_sLRF_DATA.x, m_sLRF_DATA.y, m_sLRF_DATA.z, 0); // SA->getUTMZone() );
  383. m_oPosition.setOrientationDeg(m_sLRF_DATA.x_rot, m_sLRF_DATA.y_rot, m_sLRF_DATA.z_rot);
  384. }
  385. else
  386. {
  387. C3D_DECART LD = SA->getXYZ();
  388. m_oPosition.setPositionXYZ(LD.x + m_sLRF_DATA.x, LD.y + m_sLRF_DATA.y, LD.z + m_sLRF_DATA.z, SA->getUTMZone());
  389. m_oPosition.setOrientationDeg(m_sLRF_DATA.x_rot, m_sLRF_DATA.y_rot, m_sLRF_DATA.z_rot);
  390. };
  391. }
  392. else
  393. {
  394. m_oPosition.setPositionXYZ(m_sLRF_DATA.x, m_sLRF_DATA.y, m_sLRF_DATA.z);
  395. m_oPosition.setOrientationDeg(m_sLRF_DATA.x_rot, m_sLRF_DATA.y_rot, m_sLRF_DATA.z_rot);
  396. };
  397. /// @todo Valgrind tells us something in this next call to updateMatrix() is leaked
  398. m_oPosition.updateMatrix();
  399. #ifdef DEBUG_EXTRA
  400. char tB[128];
  401. LOG("Reload Data From DB!");
  402. sprintf(tB, "Finalized Sensor Location: Lat:%.3f Lon:%.3f Alt:%.3f", m_oPosition.getLat(), m_oPosition.getLon(), m_oPosition.getAlt());
  403. LOG(tB);
  404. sprintf(tB, "Finalized Sensor Location: x:%.3f y:%.3f z:%.3f", m_oPosition.getX(), m_oPosition.getY(), m_oPosition.getZ());
  405. LOG(tB);
  406. sprintf(tB, "Orientation: x:%.3f y:%.3f z:%.3f", m_oPosition.getRotX_deg(), m_oPosition.getRotY_deg(), m_oPosition.getRotZ_deg());
  407. LOG(tB);
  408. #endif
  409. return true;
  410. }
  411. bool deviceNodeSensor::validate()
  412. {
  413. if (m_bValidationSkipCounter)
  414. {
  415. m_bValidationSkipCounter--;
  416. }
  417. else
  418. {
  419. if (m_pDBC == NULL)
  420. {
  421. return false;
  422. }
  423. bool RetVal = true;
  424. DBC_LRF_DATA * tDATA = m_pDBC->getLRFData(m_sNodeData.aux_id);
  425. if (tDATA != NULL)
  426. {
  427. if (RetVal && (tDATA->uart_id != m_sLRF_DATA.uart_id))
  428. {
  429. RetVal = false;
  430. }
  431. if (RetVal && (tDATA->device_id != m_sLRF_DATA.device_id))
  432. {
  433. RetVal = false;
  434. }
  435. if (RetVal && (tDATA->max_range != m_sLRF_DATA.max_range))
  436. {
  437. RetVal = false;
  438. }
  439. if (RetVal && (tDATA->base_shift != m_sLRF_DATA.base_shift))
  440. {
  441. RetVal = false;
  442. }
  443. if (RetVal && (tDATA->auto_draw != m_sLRF_DATA.auto_draw))
  444. {
  445. RetVal = false;
  446. }
  447. free(tDATA);
  448. }
  449. else
  450. {
  451. RetVal = false;
  452. }
  453. m_bValidationSkipCounter = DAFAULT_VALIDATION_SKIP_COUNTER;
  454. #ifdef DEBUG
  455. if (! RetVal)
  456. {
  457. LOG("Validation of Device Failure!");
  458. }
  459. #endif
  460. return RetVal;
  461. };
  462. return true;
  463. }
  464. #define SKIP_LINE_PIXELS 28
  465. #define HEIGHT 512
  466. #define WIDTH 1024
  467. #ifdef GEN_DRAW_IMAGE
  468. // scale factor -> 4/9
  469. 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)
  470. {
  471. uint8_t * RetVal = NULL;
  472. *out_size = 0;
  473. #ifdef DEBUG_EXTRA
  474. LOG("C2DD");
  475. #endif
  476. if (th_data != NULL && th_size != 0)
  477. {
  478. if (fileExist(tpl))
  479. {
  480. LOG(tpl);
  481. cv::Mat tImage = cv::imread(tpl, cv::IMREAD_UNCHANGED);
  482. int psize = th_size / (sizeof(uint32_t));
  483. float stp = 2.0f * (M_PI / (float) psize);
  484. // float MR = m_sLRF_DATA.max_range;
  485. float S = (M_PI / 3000.0f) * (float) radial_shift;
  486. int x0 = (tImage.cols >> 1);
  487. int y0 = (tImage.rows >> 1);
  488. float maxR = ((float) x0) * 0.98f;
  489. #ifdef DEBUG_EXTRA
  490. LOG("C2DD - Start");
  491. #endif
  492. cv::Point pts[(psize << 1)];
  493. cv::Point ptt[(psize + 2)];
  494. const cv::Point * ppt[1];
  495. int npp[1];
  496. const cv::Point * ppz[1];
  497. int npz[1];
  498. float rst;
  499. int rpt;
  500. uint16_t RANGE = 0;
  501. ptt[0].x = x0;
  502. ptt[0].y = y0;
  503. rst = maxR / ((float) (max_digital + 1));
  504. rpt = 1;
  505. for (int i = 0; i < psize; i++)
  506. {
  507. if (byMinimum)
  508. {
  509. RANGE = (th_data[i] >> 16) & 0xFFFF;
  510. }
  511. else
  512. {
  513. RANGE = th_data[i] & 0xFFFF;
  514. };
  515. float ca = 0.0 - (S + i * stp);
  516. if (RANGE > 0)
  517. {
  518. pts[psize + i].x = x0 + maxR * cosf(ca);
  519. pts[psize + i].y = y0 + maxR * sinf(ca);
  520. if (RANGE > max_digital)
  521. {
  522. pts[i].x = pts[psize + i].x;
  523. pts[i].y = pts[psize + i].y;
  524. }
  525. else
  526. {
  527. float cr = (rst * RANGE);
  528. pts[i].x = x0 + cr * cosf(ca);
  529. pts[i].y = y0 + cr * sinf(ca);
  530. };
  531. }
  532. else
  533. {
  534. ptt[rpt].x = pts[psize + i].x = pts[i].x = x0 + maxR * cosf(ca);
  535. ptt[rpt].y = pts[psize + i].y = pts[i].y = y0 + maxR * sinf(ca);
  536. rpt++;
  537. };
  538. };
  539. ppt[0] = pts;
  540. npp[0] = (psize << 1);
  541. ppz[0] = ptt;
  542. npz[0] = (rpt - 1);
  543. #ifdef DEBUG_EXTRA
  544. LOG("C2DD -> draw finish");
  545. #endif
  546. cv::Mat ttMOMO = cv::imread(tpl, cv::IMREAD_UNCHANGED);
  547. #ifdef DEBUG_EXTRA
  548. LOG("C2DD -> read tpl2 finish");
  549. #endif
  550. cv::fillPoly(ttMOMO, ppt, npp, 1, cv::Scalar(0, 128, 255, 50), cv::LINE_8);
  551. #ifdef DEBUG_EXTRA
  552. LOG("C2DD -> poly1 finish");
  553. #endif
  554. // try {
  555. // cv::fillPoly(ttMOMO, ppz, npz, 1, cv::Scalar(0, 0, 0, 100), cv::LINE_8);
  556. // LOG("C2DD -> poly2 finish");
  557. // } catch ( cv::Exception& e ) {
  558. // LOG("C2DD -> poly2 Error!");
  559. // LOG( e.what() );
  560. // };
  561. cv::addWeighted(tImage, 0.5, ttMOMO, 0.5, 0.0, tImage);
  562. #ifdef DEBUG_EXTRA
  563. LOG("C2DD -> add img finish");
  564. #endif
  565. ttMOMO.release();
  566. #ifdef DEBUG_EXTRA
  567. LOG("C2DD -> img release finish");
  568. #endif
  569. vector<int> compression_params;
  570. compression_params.push_back(cv::IMWRITE_PNG_COMPRESSION);
  571. compression_params.push_back(9);
  572. vector<uchar> buf;
  573. cv::imencode(".png", tImage, buf, compression_params);
  574. tImage.release();
  575. #ifdef DEBUG_EXTRA
  576. LOG("C2DD -> compress finish");
  577. #endif
  578. *out_size = buf.size();
  579. RetVal = (uint8_t *) malloc(*out_size);
  580. if (RetVal)
  581. {
  582. memcpy(RetVal, buf.data(), *out_size);
  583. }
  584. else
  585. {
  586. *out_size = 0;
  587. }
  588. buf.clear();
  589. };
  590. };
  591. return RetVal;
  592. }
  593. #endif // GEN_DRAW_IMAGE
  594. #ifndef LRF_PROXY_ONLY
  595. #include <opencv2/opencv.hpp>
  596. #endif
  597. bool deviceNodeSensor::handleDeviceDraw(uint8_t kID, void * th_data, size_t th_size, bool byMinimum)
  598. {
  599. if ( byMinimum ) byMinimum = true;
  600. if (th_data == NULL)
  601. {
  602. return false;
  603. }
  604. if (th_size == 0)
  605. {
  606. return false;
  607. }
  608. if (m_pDBC == NULL)
  609. {
  610. return false;
  611. }
  612. switch (getNodeType())
  613. {
  614. case ARGUS_ALS_SENSOR:
  615. case ARGUS_LFS_SENSOR:
  616. case ARGUS_PLS_200_SENSOR:
  617. case ARGUS_PLS_360_SENSOR:
  618. case ARGUS_NET_PLS_200:
  619. {
  620. // apply Base Shift to 2D draw
  621. if (m_sLRF_DATA.base_shift != 0)
  622. {
  623. int16_t cnt = (th_size >> 2);
  624. uint32_t * ttData = (uint32_t *) malloc(th_size);
  625. int16_t data_shift = (int16_t) (((float) m_sLRF_DATA.base_shift) * (((float) cnt) / 360.0f));
  626. for (uint16_t i = 0; i < cnt; i++)
  627. {
  628. ttData[i] = ((uint32_t *) th_data)[(cnt + i + data_shift) % cnt];
  629. };
  630. memcpy(th_data, ttData, th_size);
  631. };
  632. break;
  633. };
  634. default: break;
  635. };
  636. #ifdef DEBUG_EXTRA
  637. LOG("DATA DD TO DATABASE");
  638. #endif
  639. m_pDBC->addDeviceDraw(getNodeID(), DRAW_MODE_RAW_DATA, kID, th_data, th_size);
  640. /*
  641. //Clear Old Draw
  642. CSTR old = m_pDBC->getLRFDraw( getNodeID() );
  643. if ( old ) {
  644. // delete old draw
  645. char filename[256];
  646. sprintf( filename, "data/device_draws/%s_100.png", old );
  647. if( fileExist( filename ) ){
  648. unlink( filename );
  649. };
  650. sprintf( filename, "data/device_draws/%s_50.png", old );
  651. if( fileExist( filename ) ){
  652. unlink( filename );
  653. };
  654. sprintf( filename, "data/device_draws/%s_25.png", old );
  655. if( fileExist( filename ) ){
  656. unlink( filename );
  657. };
  658. sprintf( filename, "data/device_draws/%s_12.png", old );
  659. if( fileExist( filename ) ){
  660. unlink( filename );
  661. };
  662. sprintf( filename, "data/device_draws/%s_map.png", old );
  663. if( fileExist( filename ) ){
  664. unlink( filename );
  665. };
  666. free( old );
  667. };
  668. */
  669. #ifndef SAVE_DRAW_RAW_ONLY
  670. CSTR uid = getUniqID_MD5();
  671. if (uid == NULL)
  672. {
  673. return false;
  674. }
  675. switch (getNodeType())
  676. {
  677. case ARGUS_ALS_SENSOR:
  678. case ARGUS_LFS_SENSOR:
  679. case ARGUS_PLS_200_SENSOR:
  680. case ARGUS_PLS_360_SENSOR:
  681. case ARGUS_NET_PLS_200:
  682. {
  683. #ifdef GEN_DRAW_IMAGE // 2D Sensors does not need Image to be created
  684. #ifdef DEBUG_EXTRA
  685. LOG("DEV DRAW");
  686. #endif
  687. LOG( uid );
  688. uint32_t tSize;
  689. 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" );
  690. if ( tBuff ) {
  691. m_pDBC->addDeviceDraw( getNodeID(), DRAW_MODE_2D_100, kID, tBuff, tSize );
  692. free( tBuff );
  693. };
  694. tBuff = create2dDraw( &tSize, (uint32_t *) th_data, th_size, 512, m_sLRF_DATA.base_shift, "data/assets/images/lfs_draw_empty_50.png" );
  695. if ( tBuff ) {
  696. m_pDBC->addDeviceDraw( getNodeID(), DRAW_MODE_2D_50, kID, tBuff, tSize );
  697. free( tBuff );
  698. };
  699. tBuff = create2dDraw( &tSize, (uint32_t *) th_data, th_size, 256, m_sLRF_DATA.base_shift, "data/assets/images/lfs_draw_empty_25.png" );
  700. if ( tBuff ) {
  701. m_pDBC->addDeviceDraw( getNodeID(), DRAW_MODE_2D_25, kID, tBuff, tSize );
  702. free( tBuff );
  703. };
  704. tBuff = create2dDraw( &tSize, (uint32_t *) th_data, th_size, 128, m_sLRF_DATA.base_shift, "data/assets/images/lfs_draw_empty_12.png" );
  705. if ( tBuff ) {
  706. m_pDBC->addDeviceDraw( getNodeID(), DRAW_MODE_2D_12, kID, tBuff, tSize );
  707. free( tBuff );
  708. };
  709. tBuff = create2dDraw( &tSize, (uint32_t *) th_data, th_size, 1024, m_sLRF_DATA.base_shift, "data/assets/images/2d_MAP.png" );
  710. if ( tBuff ) {
  711. m_pDBC->addDeviceDraw( getNodeID(), DRAW_MODE_MAP, kID, tBuff, tSize );
  712. free( tBuff );
  713. };
  714. #ifdef DEBUG_EXTRA
  715. LOG(" DD FINISH!");
  716. #endif
  717. #endif // GEN_DRAW_IMAGE
  718. break;
  719. };
  720. case ARGUS_3D_SENSOR:
  721. {
  722. #ifndef LRF_PROXY_ONLY
  723. char * emptyBG = "data/assets/images/empty_3d.png";
  724. thresholdNode * ptr = (thresholdNode *) th_data;
  725. if (fileExist(emptyBG))
  726. {
  727. cv::Mat tImage = cv::imread(emptyBG, cv::IMREAD_UNCHANGED);
  728. if (tImage.rows != 512 || tImage.cols != 1024)
  729. {
  730. LOG_ERROR("Background Image wrong size (should be 1024x512)!");
  731. free(uid);
  732. return false;
  733. };
  734. uint8_t * tMemory = tImage.data;
  735. thresholdNode * cNode = NULL;
  736. uint8_t * cPixel = NULL;
  737. int pix = 0;
  738. int mem = 0;
  739. bool s_bit = false;
  740. bool no_data = false;
  741. float tVal;
  742. bool master = (kID < 16);
  743. for (int idx = 0; idx < (TH_SIZE_NODES << 2); idx++)
  744. { // tik-tak
  745. if ((idx % 4) == 0)
  746. {
  747. mem++;
  748. if (master)
  749. {
  750. cNode = ptr + TH_SIZE_NODES - mem - 1;
  751. }
  752. else
  753. {
  754. cNode = ptr + mem - 1;
  755. };
  756. if (! s_bit)
  757. {
  758. s_bit = cNode->values.sbit;
  759. }
  760. if (! no_data)
  761. {
  762. no_data = (cNode->values.cnt == 0);
  763. }
  764. if (! s_bit && ! no_data)
  765. {
  766. if (byMinimum)
  767. {
  768. tVal = ((float) cNode->values.min) / 4096.f;
  769. }
  770. else
  771. {
  772. tVal = ((float) cNode->values.max) / 4096.f;
  773. };
  774. };
  775. };
  776. if ((idx % 9) == 0)
  777. {
  778. if (mem > (SKIP_LINE_PIXELS * TH_LINE_NODES))
  779. {
  780. cPixel = tMemory + (pix << 2);
  781. pix++;
  782. if (s_bit)
  783. { // Set Red
  784. cPixel[0] = 0x00;
  785. cPixel[1] = 0x00;
  786. cPixel[2] = 0xff;
  787. cPixel[3] = 0xff;
  788. }
  789. else
  790. {
  791. if (no_data)
  792. { // Set Blue
  793. cPixel[0] = 0xff;
  794. cPixel[1] = 0x00;
  795. cPixel[2] = 0x00;
  796. cPixel[3] = 0xff;
  797. }
  798. else
  799. {
  800. uint32_t tColor;
  801. tColor = interpolate(0x000000ff, 0xffffffff, tVal);
  802. cPixel[0] = (tColor >> 24) & 0xFF;
  803. cPixel[1] = (tColor >> 16) & 0xFF;
  804. cPixel[2] = (tColor >> 8) & 0xFF;
  805. cPixel[3] = tColor & 0xFF;
  806. };
  807. };
  808. s_bit = false;
  809. no_data = false;
  810. };
  811. };
  812. };
  813. char filename[256];
  814. sprintf(filename, "data/device_draws/%s_100.png", uid);
  815. // int RetVal = stbi_write_png( filename, 1024, 512, cSpace, tMemory, 1024 * cSpace );
  816. vector<int> compression_params;
  817. compression_params.push_back(cv::IMWRITE_PNG_COMPRESSION);
  818. compression_params.push_back(9);
  819. if (! cv::imwrite(filename, tImage, compression_params))
  820. {
  821. tImage.release();
  822. free(uid);
  823. return false;
  824. };
  825. tImage.release();
  826. #ifdef DEBUG_EXTRA
  827. LOG("Draw Saved!");
  828. #endif
  829. cv::Mat tMap(Q_SCAN_COUNT, TH_LINE_NODES, CV_8UC4);
  830. #ifdef DEBUG_EXTRA
  831. LOG("Map Creating");
  832. #endif
  833. tMemory = tMap.data;
  834. for (int j = 0; j < Q_SCAN_COUNT; j++)
  835. {
  836. for (int i = 0; i < TH_LINE_NODES; i++)
  837. {
  838. // cNode = ptr+(j*i);
  839. // cNode = &ptr[ ( TH_LINE_NODES * j ) + i ];
  840. int ids = 0;
  841. int idd = 0;
  842. if (master)
  843. {
  844. // cNode = ptr+makeIds( i, j , true );
  845. // cPixel = tMemory+( cSpace * makeIds( i, j , false ));
  846. ids = makeIds(i, j, true);
  847. idd = 4 * makeIds(i, j, false);
  848. // cNode = ptr + ( i * j );
  849. // cPixel = tMemory + ( cSpace * i * j );
  850. }
  851. else
  852. {
  853. // cNode = ptr+makeIds( i, j , false );
  854. // cPixel = tMemory+( cSpace * makeIds( i, j , false ));
  855. ids = makeIds(i, j, false);
  856. idd = 4 * makeIds(i, j, false);
  857. };
  858. cNode = ptr + ids;
  859. cPixel = tMemory + idd;
  860. if (0)
  861. { // Monochrome
  862. #ifdef CHECK_SUN_BIT
  863. if (cNode->values.sbit)
  864. {
  865. cPixel[0] = 0;
  866. }
  867. else
  868. {
  869. cPixel[0] = (uint8_t) ((((float) cNode->values.min) * 255.f) / 4096.f);
  870. };
  871. #else
  872. cPixel[0] = (uint8_t) ((((float) cNode->values.min) * 255.f) / 4096.f);
  873. #endif
  874. }
  875. else
  876. {
  877. #ifdef CHECK_SUN_BIT
  878. if (cNode->values.sbit)
  879. {
  880. // Set Red
  881. cPixel[0] = 0x00;
  882. cPixel[1] = 0x00;
  883. cPixel[2] = 0xff;
  884. cPixel[3] = 0xff;
  885. }
  886. else
  887. {
  888. if (cNode->values.cnt == 0)
  889. {
  890. // Set Blue
  891. cPixel[0] = 0xff;
  892. cPixel[1] = 0x00;
  893. cPixel[2] = 0x00;
  894. cPixel[3] = 0xff;
  895. }
  896. else
  897. {
  898. uint32_t tColor;
  899. float tVal = ((float) cNode->values.min) / 4096.f;
  900. tColor = interpolate(0x000000ff, 0xffffffff, tVal);
  901. cPixel[0] = (tColor >> 24) & 0xFF;
  902. cPixel[1] = (tColor >> 16) & 0xFF;
  903. cPixel[2] = (tColor >> 8) & 0xFF;
  904. cPixel[3] = tColor & 0xFF;
  905. };
  906. };
  907. #else
  908. if (cNode->values.cnt == 0)
  909. {
  910. // Set Blue
  911. cPixel[0] = 0xff;
  912. cPixel[1] = 0x00;
  913. cPixel[2] = 0x00;
  914. cPixel[3] = 0xff;
  915. }
  916. else
  917. {
  918. uint32_t tColor;
  919. float tVal = ((float) cNode->values.min) / 4096.f;
  920. tColor = interpolate(0x000000ff, 0xffffffff, tVal);
  921. cPixel[0] = (tColor >> 24) & 0xFF;
  922. cPixel[1] = (tColor >> 16) & 0xFF;
  923. cPixel[2] = (tColor >> 8) & 0xFF;
  924. cPixel[3] = tColor & 0xFF;
  925. };
  926. #endif
  927. };
  928. };
  929. };
  930. sprintf(filename, "data/device_draws/%s_map.png", uid);
  931. LOG(filename);
  932. if (! cv::imwrite(filename, tMap, compression_params))
  933. {
  934. tMap.release();
  935. free(uid);
  936. return false;
  937. };
  938. tMap.release();
  939. m_pDBC->addDeviceDraw(getNodeID(), DRAW_MODE_MAP, kID, (void *) uid, 0);
  940. // m_pDBC->addLRFDraw( getNodeID(), TH_SIZE_NODES, uid, kID, 1, true );
  941. }
  942. else
  943. {
  944. LOG_ERROR("Background Image is Not Found!");
  945. free(uid);
  946. return false;
  947. };
  948. #endif //!LRF_PROXY_ONLY
  949. break;
  950. };
  951. default: break;
  952. };
  953. free(uid);
  954. #endif // !SAVE_DRAW_RAW_ONLY
  955. return true;
  956. }
  957. /*
  958. #define STB_IMAGE_IMPLEMENTATION
  959. #include "../data/stbi/stb_image.h"
  960. #define STB_IMAGE_WRITE_IMPLEMENTATION
  961. #include "../data/stbi/stb_image_write.h"
  962. #undef STB_IMAGE_WRITE_IMPLEMENTATION
  963. #undef STB_IMAGE_IMPLEMENTATION
  964. bool argus3DLIDAR::saveTHPicture( thresholdNode *ptr, CSTR uid, bool master, bool byMinimum, bool monochrome ){
  965. uint8_t *tMemory = NULL;
  966. thresholdNode *cNode = NULL;
  967. uint8_t *cPixel = NULL;
  968. int cSpace;
  969. int x, y;
  970. tMemory = stbi_load( "data/assets/images/empty_3d.png", &x, &y, &cSpace, STBI_default );
  971. if( tMemory == NULL ){
  972. LOG_ERROR("Draw Memory Alloc Eror!");
  973. return false;
  974. };
  975. if( x != 1024 || y != 512 ){
  976. LOG("Wrong IMG Size!");
  977. free( tMemory );
  978. return NULL;
  979. };
  980. int pix = 0;
  981. int mem = 0;
  982. bool s_bit = false;
  983. bool no_data = false;
  984. float tVal;
  985. for ( int idx = 0; idx < ( TH_SIZE_NODES << 2 ); idx++ ) { // tik-tak
  986. if( ( idx % 4 ) == 0 ){
  987. mem++;
  988. if ( master ) {
  989. cNode = ptr + TH_SIZE_NODES - mem - 1;
  990. } else {
  991. cNode = ptr + mem - 1;
  992. };
  993. if( !s_bit ) s_bit = cNode->values.sbit;
  994. if( !no_data ) no_data = ( cNode->values.cnt == 0 );
  995. if( !s_bit && !no_data ){
  996. if( byMinimum ) {
  997. tVal = ((float)cNode->values.min) / 4096.f;
  998. } else {
  999. tVal = ((float)cNode->values.max) / 4096.f;
  1000. };
  1001. };
  1002. };
  1003. if( ( idx % 9 ) == 0 ){
  1004. if( mem > ( SKIP_LINE_PIXELS * TH_LINE_NODES ) ) {
  1005. cPixel = tMemory + (pix << 2);
  1006. pix++;
  1007. if( s_bit ) { // Set Red
  1008. cPixel[0] = 0x00; cPixel[1] = 0xff; cPixel[2] = 0x00; cPixel[3] = 0xff;
  1009. } else {
  1010. if( no_data ) { // Set Blue
  1011. cPixel[0] = 0x00; cPixel[1] = 0x00; cPixel[2] = 0xff; cPixel[3] = 0xff;
  1012. } else {
  1013. uint32_t tColor;
  1014. tColor = interpolate( 0x000000ff, 0xffffffff, tVal );
  1015. cPixel[0] = ( tColor >> 24 ) & 0xFF;
  1016. cPixel[1] = ( tColor >> 16 ) & 0xFF;
  1017. cPixel[2] = ( tColor >> 8 ) & 0xFF;
  1018. cPixel[3] = tColor & 0xFF;
  1019. };
  1020. };
  1021. s_bit = false;
  1022. no_data = false;
  1023. }
  1024. };
  1025. };
  1026. char filename[256];
  1027. sprintf( filename, "data/device_draws/%s_100.png", uid );
  1028. int RetVal = stbi_write_png( filename, 1024, 512, cSpace, tMemory, 1024 * cSpace );
  1029. free( tMemory );
  1030. if( RetVal == 0 ) return false;
  1031. if( monochrome ) {
  1032. cSpace = 1;
  1033. } else {
  1034. cSpace = 4;
  1035. };
  1036. tMemory = (uint8_t *) malloc( TH_SIZE_NODES * cSpace );
  1037. if( tMemory == NULL ){
  1038. LOG_ERROR("Map Memory Alloc Eror!");
  1039. return false;
  1040. };
  1041. for (int j = 0; j < Q_SCAN_COUNT; j++) {
  1042. for ( int i = 0; i < TH_LINE_NODES; i++ ) {
  1043. //cNode = ptr+(j*i);
  1044. //cNode = &ptr[ ( TH_LINE_NODES * j ) + i ];
  1045. int ids = 0;
  1046. int idd = 0;
  1047. if ( master ) {
  1048. //cNode = ptr+makeIds( i, j , true );
  1049. //cPixel = tMemory+( cSpace * makeIds( i, j , false ));
  1050. ids = makeIds( i, j , true );
  1051. idd = cSpace * makeIds( i, j , false );
  1052. //cNode = ptr + ( i * j );
  1053. //cPixel = tMemory + ( cSpace * i * j );
  1054. } else {
  1055. //cNode = ptr+makeIds( i, j , false );
  1056. //cPixel = tMemory+( cSpace * makeIds( i, j , false ));
  1057. ids = makeIds( i, j , false );
  1058. idd = cSpace * makeIds( i, j , false );
  1059. };
  1060. cNode = ptr + ids ;
  1061. cPixel = tMemory + idd;
  1062. if( monochrome ) {
  1063. #ifdef CHECK_SUN_BIT
  1064. if( cNode->values.sbit ) {
  1065. cPixel[0] = 0;
  1066. } else {
  1067. cPixel[0] = (uint8_t)((((float) cNode->values.min ) * 255.f ) / 4096.f );
  1068. };
  1069. #else
  1070. cPixel[0] = (uint8_t)((((float) cNode->values.min ) * 255.f ) / 4096.f );
  1071. #endif
  1072. } else {
  1073. #ifdef CHECK_SUN_BIT
  1074. if( cNode->values.sbit ) {
  1075. // Set Red
  1076. cPixel[0] = 0x00;
  1077. cPixel[1] = 0xff;
  1078. cPixel[2] = 0x00;
  1079. cPixel[3] = 0xff;
  1080. } else {
  1081. if( cNode->values.cnt == 0 ) {
  1082. // Set Blue
  1083. cPixel[0] = 0x00;
  1084. cPixel[1] = 0x00;
  1085. cPixel[2] = 0xff;
  1086. cPixel[3] = 0xff;
  1087. } else {
  1088. uint32_t tColor;
  1089. float tVal = ((float) cNode->values.min ) / 4096.f;
  1090. tColor = interpolate( 0x000000ff, 0xffffffff, tVal );
  1091. cPixel[0] = ( tColor >> 24 ) & 0xFF;
  1092. cPixel[1] = ( tColor >> 16 ) & 0xFF;
  1093. cPixel[2] = ( tColor >> 8 ) & 0xFF;
  1094. cPixel[3] = tColor & 0xFF;
  1095. };
  1096. };
  1097. #else
  1098. if( cNode->values.cnt == 0 ) {
  1099. // Set Blue
  1100. cPixel[0] = 0x00;
  1101. cPixel[1] = 0x00;
  1102. cPixel[2] = 0xff;
  1103. cPixel[3] = 0xff;
  1104. } else {
  1105. uint32_t tColor;
  1106. float tVal = ((float) cNode->values.min ) / 4096.f;
  1107. tColor = interpolate( 0x000000ff, 0xffffffff, tVal );
  1108. cPixel[0] = ( tColor >> 24 ) & 0xFF;
  1109. cPixel[1] = ( tColor >> 16 ) & 0xFF;
  1110. cPixel[2] = ( tColor >> 8 ) & 0xFF;
  1111. cPixel[3] = tColor & 0xFF;
  1112. };
  1113. #endif
  1114. };
  1115. };
  1116. };
  1117. sprintf( filename, "data/device_draws/%s_map.png", uid );
  1118. RetVal = stbi_write_png( filename, TH_LINE_NODES, Q_SCAN_COUNT, cSpace, tMemory, TH_LINE_NODES * cSpace );
  1119. free( tMemory );
  1120. if( RetVal == 0 ) return false;
  1121. return true;
  1122. };
  1123. */
  1124. void deviceNodeSensor::broadcastFlip(uint16_t flipCount)
  1125. {
  1126. EVENT_CONTAINER * bEVT = NULL;
  1127. if( flipCount ) bEVT = NULL;
  1128. bEVT = createBasicEvent(m_sNodeData.node_id, m_eEventClass, EVT_FLIP, NULL, NULL, NULL);
  1129. if (bEVT != NULL)
  1130. {
  1131. m_pEventHeap->broadcast(bEVT);
  1132. }
  1133. }
  1134. void deviceNodeSensor::broadcastConnected(void)
  1135. {
  1136. EVENT_CONTAINER * bEVT = NULL;
  1137. bEVT = createBasicEvent(m_sNodeData.node_id, m_eEventClass, EVT_CONNECTED, NULL, NULL, NULL);
  1138. if (bEVT != NULL)
  1139. {
  1140. m_pEventHeap->broadcast(bEVT);
  1141. }
  1142. }
  1143. void deviceNodeSensor::broadcastDisconnected(void)
  1144. {
  1145. EVENT_CONTAINER * bEVT = NULL;
  1146. bEVT = createBasicEvent(m_sNodeData.node_id, m_eEventClass, EVT_DISCONNECTED, NULL, NULL, NULL);
  1147. if (bEVT != NULL)
  1148. {
  1149. m_pEventHeap->broadcast(bEVT);
  1150. }
  1151. }
  1152. void deviceNodeSensor::startWaitDeviceOnline(LRF_WORK_MODE wm, LRF_WORK_STAGE stg)
  1153. {
  1154. m_SP_WM = wm;
  1155. m_SP_STAGE = stg;
  1156. setWM(LRF_WM_WAIT_ONLINE_SPECIAL);
  1157. }
  1158. uint8_t deviceNodeSensor::getSensStatus()
  1159. {
  1160. return m_sLRF_DATA.data_status;
  1161. }
  1162. void deviceNodeSensor::updateDataStatus(uint8_t ds, uint8_t ns)
  1163. {
  1164. if (m_pDBC == NULL)
  1165. {
  1166. return;
  1167. }
  1168. if (m_sNodeData.node_id)
  1169. {
  1170. if (ds < ARGUS_DS_CONNECTING)
  1171. {
  1172. m_pDBC->setNodeOffline(m_sNodeData.node_id);
  1173. }
  1174. else
  1175. {
  1176. m_pDBC->setNodeOnline(m_sNodeData.node_id, ns);
  1177. };
  1178. };
  1179. if (m_sLRF_DATA.id)
  1180. {
  1181. m_sLRF_DATA.data_status = ds;
  1182. m_pDBC->setLRFDataStatus(m_sLRF_DATA.id, ds);
  1183. };
  1184. }
  1185. void deviceNodeSensor::waitStateFunction(LRF_WORK_STAGE boboStage)
  1186. {
  1187. #ifdef DEBUG_EXTRA
  1188. LOG("WaitState");
  1189. uint8_t cwm = getWM();
  1190. uint8_t cst = getStage();
  1191. char tB[128];
  1192. sprintf(tB, "WM: 0x%02X STG: 0x%02X", cwm, cst);
  1193. LOG(tB);
  1194. #endif
  1195. if (m_bTimeOutCounter)
  1196. {
  1197. m_bTimeOutCounter--;
  1198. DELAY_10MS(LGS_DEFAULT_WS_TIME);
  1199. }
  1200. else
  1201. {
  1202. if (m_bOpRetryCnt)
  1203. {
  1204. #ifdef DEBUG_EXTRA
  1205. LOG("ATTEMPT RECOVER");
  1206. #endif
  1207. m_nTxAttemptTO++;
  1208. #ifdef DEBUG_COMMUNICATION
  1209. {
  1210. char tB[160];
  1211. snprintf(tB, sizeof(tB), "COMM: no reply - WM 0x%02X STG 0x%02X attempt %d/%d timed out after %llu ms", (unsigned) getWM(), (unsigned) getStage(),
  1212. LGS_CMD_RETRY_COUNT - m_bOpRetryCnt + 1, LGS_CMD_RETRY_COUNT + 1, (unsigned long long) (m_nTxStartMs ? nsNowMs() - m_nTxStartMs : 0));
  1213. LOG(tB);
  1214. }
  1215. #endif
  1216. m_bOpRetryCnt--;
  1217. m_bTimeOutCounter = m_nTOCounterBase;
  1218. registerCommError(false);
  1219. LRF_WORK_STAGE pS = getPrevStage();
  1220. if (pS != LRF_WS_INIT)
  1221. {
  1222. #ifdef DEBUG_EXTRA
  1223. LOG("Set Prev Stage");
  1224. #endif
  1225. setStage(pS, false);
  1226. }
  1227. else
  1228. {
  1229. if (boboStage != LRF_WS_INIT)
  1230. {
  1231. #ifdef DEBUG_EXTRA
  1232. LOG("Set Desired Stage");
  1233. #endif
  1234. setStage(boboStage, false);
  1235. }
  1236. else
  1237. {
  1238. #ifdef DEBUG_EXTRA
  1239. LOG("Set Connection Stage");
  1240. #endif
  1241. setWM(LRF_WM_CONNECTION);
  1242. return;
  1243. };
  1244. };
  1245. }
  1246. else
  1247. {
  1248. LOG_ERROR("Communication Wait State TO!");
  1249. m_nTxFailed++;
  1250. #ifdef DEBUG_COMMUNICATION
  1251. {
  1252. char tB[192];
  1253. snprintf(tB, sizeof(tB), "COMM: command failed - WM 0x%02X STG 0x%02X, %d attempts of %u ms, %llu ms since the first attempt", (unsigned) getWM(), (unsigned) getStage(),
  1254. LGS_CMD_RETRY_COUNT + 1, (unsigned) m_nTOCounterBase * 100, (unsigned long long) (m_nTxFirstMs ? nsNowMs() - m_nTxFirstMs : 0));
  1255. LOG(tB);
  1256. }
  1257. txTimingReport(true);
  1258. #endif
  1259. m_nTxStartMs = 0;
  1260. m_nTxFirstMs = 0;
  1261. registerCommError(true);
  1262. // setStage( boboStage );
  1263. setWM(LRF_WM_CONNECTION);
  1264. return;
  1265. };
  1266. DELAY_10MS(LGS_DEFAULT_WS_TIME);
  1267. transactionResetResult();
  1268. };
  1269. }
  1270. void deviceNodeSensor::poolStateFunction()
  1271. {
  1272. goNextStage();
  1273. transactionResetResult();
  1274. }
  1275. bool deviceNodeSensor::initWModeFunction(uint8_t wm)
  1276. {
  1277. if (getOWM() != wm)
  1278. {
  1279. setWM(wm);
  1280. setWM(wm);
  1281. setStage(LRF_WS_INIT);
  1282. transactionResetResult();
  1283. m_nTxStartMs = 0; // a new work mode: no command running
  1284. m_nTxFirstMs = 0;
  1285. m_bOpRetryCnt = LGS_CMD_RETRY_COUNT;
  1286. return true;
  1287. };
  1288. return false;
  1289. }
  1290. void deviceNodeSensor::m_fThreadProc(void)
  1291. {
  1292. time_t tTimeT_Current;
  1293. time_t tTimeT_Old = time(NULL);
  1294. m_bThreadStatus = TH_WORKING;
  1295. setWM(LRF_WM_INIT);
  1296. pthread_setname_np( pthread_self(), "NodeSensor" );
  1297. while (m_bThreadStatus < TH_ENDED)
  1298. {
  1299. tTimeT_Current = time(NULL);
  1300. if (tTimeT_Old != tTimeT_Current)
  1301. {
  1302. tTimeT_Old = tTimeT_Current;
  1303. if (m_nCommErrorsStatTO)
  1304. {
  1305. m_nCommErrorsStatTO--;
  1306. }
  1307. else
  1308. {
  1309. /**
  1310. * Update Communication Status!
  1311. */
  1312. m_nCommErrorsStatTO = COMMUNICATION_ERROR_STATISTIC_HARVEST_INTERVAL_IN_SECONDS;
  1313. m_pDBC->updateLRFComStat(m_sNodeData.aux_id, m_nCommErrorsCounter);
  1314. m_nCommErrorsCounter = 0;
  1315. };
  1316. proceedDBCommands();
  1317. };
  1318. if (getWM() > LRF_WM_CONNECTION)
  1319. {
  1320. if (! validateCommunicationRessource())
  1321. {
  1322. setWM(LRF_WM_CONNECTION);
  1323. DELAY_10MS(200);
  1324. };
  1325. };
  1326. switch (getWM())
  1327. {
  1328. case LRF_WM_INIT:
  1329. reloadDeviceNodeFromDB();
  1330. setWM(LRF_WM_CONNECTION);
  1331. break;
  1332. case LRF_WM_CONNECTION : connectMode(); break;
  1333. case LRF_WM_HANDSHAKE : handshakeMode(); break;
  1334. case LRF_WM_SYNC_UP : readMode(); break;
  1335. case LRF_WM_SYNC_DOWN : syncMode(); break;
  1336. case LRF_WM_BORDER : drawMode(); break;
  1337. case LRF_WM_FULL_BORDER : fullDrawMode(); break;
  1338. case LRF_WM_FILE : getfileMode(); break;
  1339. case LRF_WM_FILE_TF : fileTransferMode(); break;
  1340. case LRF_WM_ALARM : alarmMode(); break;
  1341. case LRF_WM_IDLE : idleMode(); break;
  1342. case LRF_WM_STATUS_MASK : maskMode(); break;
  1343. case LRF_WM_SAVE_HW : save_ini(); break;
  1344. case LRF_WM_SAVE_DETECTION : save_detection(); break;
  1345. case LRF_WM_SAVE_EPROM : save_eprom(); break;
  1346. case LRF_WM_SAVE_MASKS : save_masks(); break;
  1347. case LRF_WM_WAIT_ONLINE_SPECIAL: wait_device(); break;
  1348. case LRF_WM_WAIT_CHANNEL : wait_channel(); break;
  1349. default : LOG("WM NOT REG"); setWM(LRF_WM_IDLE);
  1350. };
  1351. };
  1352. LOG("Stop Dev Thread");
  1353. }
  1354. void deviceNodeSensor::setStage(LRF_WORK_STAGE st, bool update_old_stage)
  1355. {
  1356. m_mStageProtector.lock();
  1357. if (update_old_stage)
  1358. {
  1359. m_eOldWorkingStage = m_eWorkingStage;
  1360. }
  1361. m_eWorkingStage = st;
  1362. m_mStageProtector.unlock();
  1363. }
  1364. void deviceNodeSensor::goNextStage(bool update_old_stage)
  1365. {
  1366. m_mStageProtector.lock();
  1367. if (m_eWorkingStage < LRF_WS_COUNT)
  1368. {
  1369. if (update_old_stage)
  1370. {
  1371. m_eOldWorkingStage = m_eWorkingStage;
  1372. }
  1373. m_eWorkingStage++;
  1374. };
  1375. m_mStageProtector.unlock();
  1376. }
  1377. LRF_WORK_STAGE deviceNodeSensor::getStage()
  1378. {
  1379. LRF_WORK_STAGE rV;
  1380. m_mStageProtector.lock();
  1381. rV = m_eWorkingStage;
  1382. m_mStageProtector.unlock();
  1383. return rV;
  1384. }
  1385. LRF_WORK_STAGE deviceNodeSensor::getPrevStage()
  1386. {
  1387. LRF_WORK_STAGE rV;
  1388. m_mStageProtector.lock();
  1389. rV = m_eOldWorkingStage;
  1390. m_mStageProtector.unlock();
  1391. return rV;
  1392. }
  1393. bool deviceNodeSensor::generateIniFile(uint8_t i)
  1394. {
  1395. if (m_pDBC == NULL)
  1396. {
  1397. return false;
  1398. }
  1399. DBC_LRF_INI * tData = m_pDBC->getINIData(m_sNodeData.aux_id);
  1400. if (tData)
  1401. {
  1402. }
  1403. else
  1404. {
  1405. LOG("No Data From DB! ---> Set Defaults!");
  1406. tData = (DBC_LRF_INI *) malloc(sizeof(DBC_LRF_INI));
  1407. if (tData)
  1408. {
  1409. memset(tData, 0, sizeof(DBC_LRF_INI));
  1410. tData->_AUTO_RESET_ENABLE = 0;
  1411. tData->_BOARD_SPEED = 4;
  1412. tData->_BOARD_TEMPERATURE = 1;
  1413. tData->_RUN_DIR = 2;
  1414. };
  1415. };
  1416. if (tData)
  1417. {
  1418. CSTR tMD5 = getUniqID_MD5();
  1419. if (tMD5)
  1420. {
  1421. sprintf(m_pTGENFILENAMES[i], "%s", tMD5);
  1422. free(tMD5);
  1423. char tB[256];
  1424. sprintf(tB, "data/uploads/%s", m_pTGENFILENAMES[i]);
  1425. FILE * fd = fopen(tB, "wb+");
  1426. if (fd)
  1427. {
  1428. uint16_t * tpP = (uint16_t *) tData;
  1429. for (int idx = 0; idx < __INI_FILE_ENTRIES_CNT__; idx++)
  1430. {
  1431. fprintf(fd, "%s=%d\r\n", settings_user_ini_fields[idx], tpP[idx]);
  1432. };
  1433. fclose(fd);
  1434. return true;
  1435. };
  1436. };
  1437. free(tData);
  1438. }
  1439. return false;
  1440. }
  1441. void deviceNodeSensor::registerCommError(bool withEvent)
  1442. {
  1443. m_nCommErrorsCounter++;
  1444. if (withEvent)
  1445. {
  1446. EVENT_CONTAINER * bEVT = createBasicEvent(m_sNodeData.node_id, m_eEventClass, EVT_COMMUNICATION_JAMED, NULL, NULL, NULL);
  1447. if (bEVT != NULL)
  1448. {
  1449. m_pEventHeap->broadcast(bEVT);
  1450. };
  1451. };
  1452. }
  1453. bool deviceNodeSensor::generateRestartFile(uint8_t i)
  1454. {
  1455. if (m_pDBC == NULL)
  1456. {
  1457. return false;
  1458. }
  1459. CSTR tMD5 = getUniqID_MD5();
  1460. if (tMD5)
  1461. {
  1462. sprintf(m_pTGENFILENAMES[i], "%s", tMD5);
  1463. free(tMD5);
  1464. char tB[256];
  1465. sprintf(tB, "data/uploads/%s", m_pTGENFILENAMES[i]);
  1466. FILE * fd = fopen(tB, "wb+");
  1467. if (fd)
  1468. {
  1469. fprintf(fd, "copy *.exe c:\r\ncopy *.ini c:\r\ncopy *.txt c:\r\n");
  1470. fclose(fd);
  1471. return true;
  1472. };
  1473. };
  1474. return false;
  1475. }
  1476. #undef LOG
  1477. #undef LOG_ERROR