car_sensor.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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 <fstream>
  38. #include "../common/notifier.h"
  39. #include "event_log.h"
  40. class CCanSensor;
  41. class CMessageQueue;
  42. class CCarSensor;
  43. class CCarSensor
  44. {
  45. public:
  46. CCarSensor(CMessageQueue * q);
  47. ~CCarSensor();
  48. void Notify(TPCANMsg& canmsg);
  49. void SetSensorSocket(SensorSocket<CCarSensor>* can);
  50. bool Start();
  51. void Stop();
  52. void Notify(int8_t * buffer,int32_t size);
  53. void OnMessage(cannet_frame* frames,int32_t count=5);
  54. void SetChannelReady(bool bRet);
  55. void Write(TPCANMsg& msg);
  56. void SetAutomous(bool value,int32_t delay);
  57. int64_t GetLastEncodeTime();
  58. void StartParking(bool b);
  59. bool IsFrontView();
  60. int16_t GetRiseDownValue() const;
  61. int16_t GetTipValue() const;
  62. //void OnSignal(RemoNet::CCRobotSignal& req);
  63. private:
  64. void Run();
  65. void CanSendProc();
  66. void DelayFn();
  67. // bool Throttle(int32_t value);
  68. // bool Brake(int32_t value);
  69. // bool Steer(int32_t value);
  70. // bool Bucket(int32_t value);
  71. // bool DriverAnalog(int32_t steer,int32_t throttle,int32_t brake,bool onoff);
  72. // bool EmbraceAnalog(int32_t tip,int32_t zoomL,int32_t zoomR,bool onoff);
  73. // bool JobAnalog(int16_t n1,int16_t n2,int16_t n3,int16_t n4);
  74. // bool EmergencyAnalog(int16_t n1,int16_t n2,int16_t n3,int16_t n4);
  75. private:
  76. CMessageQueue * _message;
  77. std::thread _thread;
  78. std::thread _can_thread;
  79. bool _can_run;
  80. bool _run;
  81. bool _tip;
  82. bool _rise_down;
  83. int16_t _rise_down_value=0;
  84. int16_t _tip_value=0;
  85. bool _can_ready=false;
  86. std::unique_ptr<CCanSensor> _can;
  87. std::unique_ptr<CEventLog> _log;
  88. //cannet_frame frames[21];
  89. bool _channelReady;
  90. bool _front_view=true;
  91. bool _automous=false;
  92. bool _delayset;
  93. // bool _finished=false;
  94. SensorSocket<CCarSensor> * _socket;
  95. FeedData _data;
  96. int32_t _low_rpm_count=0;
  97. std::thread _delay;
  98. int32_t _delayseconds;
  99. int16_t _brake=0;
  100. volatile int64_t _last_encode_uptime=0;
  101. volatile int64_t _last_can_uptime=0;
  102. std::mutex _last_can_lock;
  103. TPCANMsg _msg[5];
  104. int32_t _range_count=0;
  105. int16_t _steer=0;
  106. private:
  107. };