robot_sensor.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. #pragma once
  2. #ifdef WIN32
  3. #ifndef WIN32_LEAN_AND_MEAN
  4. #define WIN32_LEAN_AND_MEAN
  5. #endif
  6. #include <WS2tcpip.h>
  7. #include <WinSock2.h>
  8. #define socketerrno WSAGetLastError()
  9. #define SOCKET_EAGAIN_EINPROGRESS WSAEINPROGRESS
  10. #define SOCKET_EWOULDBLOCK WSAEWOULDBLOCK
  11. #ifndef _SSIZE_T_DEFINED
  12. typedef int ssize_t;
  13. #define _SSIZE_T_DEFINED
  14. #endif
  15. #ifndef _SOCKET_T_DEFINED
  16. typedef SOCKET socket_t;
  17. #define _SOCKET_T_DEFINED
  18. #endif
  19. #else
  20. #include <unistd.h>
  21. #include <arpa/inet.h>
  22. #include <sys/types.h>
  23. #include <sys/socket.h>
  24. #include <netinet/in.h>
  25. #include <netinet/tcp.h>
  26. #include <fcntl.h>
  27. #define socketerrno errno
  28. #define SOCKET_EAGAIN_EINPROGRESS EAGAIN
  29. #define SOCKET_EWOULDBLOCK EWOULDBLOCK
  30. #define INVALID_SOCKET -1
  31. #define SOCKET_ERROR -1
  32. #ifndef _SOCKET_T_DEFINED
  33. typedef int socket_t;
  34. #define _SOCKET_T_DEFINED
  35. #endif
  36. #endif
  37. #include "../common/notifier.h"
  38. class CMessageQueue;
  39. #pragma pack(1)
  40. struct OpState{
  41. uint16_t func=htons(10);
  42. uint16_t boot_addr=htons(101);
  43. uint16_t boot_value=0;
  44. uint16_t start_addr=htons(102);
  45. uint16_t start_value=0;
  46. uint16_t stop_addr=htons(103);
  47. uint16_t stop_value=0;
  48. uint16_t emergency_addr=htons(104);
  49. uint16_t emergency_value=0;
  50. uint16_t gear_addr=htons(135);
  51. uint16_t gear_value=0;
  52. uint16_t wip_addr=htons(132);
  53. uint16_t wip_value=0;
  54. uint16_t flight_addr=htons(133);
  55. uint16_t flight_value=0;
  56. uint16_t blight_addr=htons(134);
  57. uint16_t blight_value=0;
  58. uint16_t park_addr=htons(135);
  59. uint16_t park_value=0;
  60. uint16_t ldlight_addr=htons(136);
  61. uint16_t ldlight_value=0;
  62. uint16_t rdlight_addr=htons(137);
  63. uint16_t rdlight_value=0;
  64. uint16_t gear_d_addr=htons(138);
  65. uint16_t gear_d_value=0;
  66. uint16_t buz_addr=htons(139);
  67. uint16_t buz_value=0;
  68. };
  69. struct Analog_rtu {
  70. int16_t func=htons(10);
  71. int16_t steer_addr=htons(113);
  72. int16_t steer_value=0;
  73. int16_t arm_addr=htons(117);
  74. int16_t arm_value=0;
  75. int16_t bucket_addr=htons(119);
  76. int16_t bucket_value=0;
  77. int16_t throttle_addr=htons(111);
  78. int16_t throttle_value=0;
  79. int16_t brake_addr=htons(115);
  80. int16_t brake_value=0;
  81. };
  82. struct ReadCmd
  83. {
  84. int16_t func=htons(03);
  85. int16_t boot_addr=htons(201);
  86. int16_t start_addr=htons(203);
  87. int16_t stop_addr=htons(205);
  88. int16_t emergenc_addr=htons(207);
  89. };
  90. struct ReadRet
  91. {
  92. int16_t func=3;
  93. int16_t boot_addr;
  94. int16_t boot_value;
  95. int16_t start_addr;
  96. int16_t start_value;
  97. int16_t stop_addr;
  98. int16_t stop_value;
  99. int16_t emergenc_addr;
  100. int16_t emergenc_value;
  101. };
  102. #pragma pack()
  103. class CRobotSensor
  104. {
  105. public:
  106. CRobotSensor(CMessageQueue * q);
  107. void Notify(TPCANMsg *buffer, int32_t size);
  108. void SetSensorSocket(SensorPeakCan<CRobotSensor>* can);
  109. void OnMessage(cannet_frame* frames, int32_t count = 4);
  110. void Start();
  111. void Stop();
  112. bool Emergency(); //急停 只局限于程序 和网络
  113. void Run();
  114. private:
  115. unsigned int CRC16(uint8_t *buf, int len);
  116. void OnPark(bool on);
  117. bool Resume();
  118. void CanSendProc();
  119. void SendStatusToMSG();
  120. private:
  121. CMessageQueue * _message;
  122. SensorPeakCan<CRobotSensor> * _socket;
  123. bool _resume=false;
  124. bool _emergency=false;
  125. bool _autorise=false;
  126. RemoNet::Gears _gears=RemoNet::Gears::N;
  127. RemoNet::DirectionLight _light=RemoNet::DirectionLight::OffLight;
  128. RemoNet::Gears_D _gears_d=RemoNet::Gears_D::None_d;
  129. bool _frontLight=false;
  130. bool _backLight=false;
  131. bool _wipe=false;
  132. bool _buzzer=false;
  133. bool _park=false;
  134. bool _run;
  135. int8_t value_g=1;
  136. OpState state;
  137. std::mutex _last_can_lock;
  138. std::thread _can_thread;
  139. bool _front_view = true;
  140. volatile int64_t _last_can_uptime = 0;
  141. TPCANMsg _msg[4];
  142. FeedData _data;
  143. int b_ready;
  144. int b_over;
  145. int model;//0本地,1远程
  146. };