socket_lidar.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. #include "socket_lidar.h"
  2. #include "../common/iobuffer.h"
  3. #include <stdio.h>
  4. #include <iostream>
  5. #include<string>
  6. #include <cstring>
  7. #include "message_queue.h"
  8. SocketLidar::SocketLidar(CMessageQueue* n):_canNotify(n)
  9. {
  10. }
  11. bool SocketLidar::Start(int32_t port)
  12. {
  13. #ifdef WIN32
  14. WSAData data;
  15. WSAStartup(MAKEWORD(2, 2), &data);
  16. #endif
  17. _fd = socket(AF_INET, SOCK_DGRAM, 0);
  18. sockaddr_in sin;
  19. sin.sin_family = AF_INET;
  20. sin.sin_port = htons(port);
  21. sin.sin_addr.s_addr = htonl(INADDR_ANY);
  22. if (::bind(_fd, (sockaddr*)&sin, sizeof(sin)) == -1)
  23. return false;
  24. _thread = std::thread(&SocketLidar::Run, this);
  25. return true;
  26. }
  27. void SocketLidar::Run()
  28. {
  29. _run = true;
  30. int8_t stopCnt;
  31. long long k = 0;
  32. long long tick = std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now().time_since_epoch()).count();
  33. while (_run)
  34. {
  35. CIOBuffer pBuffer;
  36. sockaddr_in from;
  37. socklen_t fromlen=sizeof(sockaddr_in);
  38. int32_t ret = recvfrom(_fd,(char *)pBuffer.Buffer, CIOBuffer::IO_BUFFER_SIZE,0,(sockaddr*)&from,&fromlen);
  39. if (ret <= 0)
  40. {
  41. continue;
  42. }
  43. int32_t receivedCnt = ret/CAN_MSG_LEN;
  44. cannet_frame* p = (cannet_frame*)pBuffer.Buffer;
  45. memset(&CANThread::can_receive_struct, 0, sizeof(CANThread::can_receive_struct));
  46. for(int i=0; i<receivedCnt; i++)
  47. {
  48. _message[p->canid] = *p;
  49. uint32_t tmp = ntohl(_message[p->canid].canid);
  50. //std::cout << i << "###" <<ntohl(_message[p->canid].canid) << std::endl;
  51. uint32_t radarMsgID = tmp & 0xFFFFFF0F;
  52. uint32_t radarID = (tmp & 0xF0) / 0x10;
  53. // if(radarID == 3 || radarID == 4 || radarID == 5)
  54. {
  55. CANThread::can_receive_struct[i].DataLen = _message[p->canid].dlc;
  56. CANThread::can_receive_struct[i].ID = radarMsgID;
  57. CANThread::can_receive_struct[i].NodeId = radarID;
  58. for (uint32_t j = 0; j < 8; j++) {
  59. CANThread::can_receive_struct[i].Data[j] = _message[p->canid].data[j];
  60. }
  61. radarDeal.receiveCanData();
  62. bool stop = false;
  63. for(uint32_t i=0; i<radarDeal.radar_object_counter; i++)
  64. {
  65. float dist = 0;
  66. //list_of_externally_accessible_object为1时,radar_object_list1[]可用
  67. //list_of_externally_accessible_object为2时,radar_object_list2[]可用
  68. if(CANThread::list_of_externally_accessible_object == 1)
  69. {
  70. dist = CANThread::radar_object_list1[i].Object_DistLong;
  71. }
  72. else
  73. {
  74. dist = CANThread::radar_object_list2[i].Object_DistLong;
  75. }
  76. // if(stopCnt >= 3)
  77. // {
  78. // _canNotify->StopCar();
  79. // stop=true;
  80. // break;
  81. // }
  82. // if(dist < 3.0)
  83. // {
  84. // stopCnt++;
  85. // }
  86. // else
  87. // {
  88. // if(stop)
  89. // _canNotify->StartCar();
  90. // stopCnt = 0;
  91. // }
  92. }
  93. }
  94. p++;
  95. }
  96. long long diff = std::chrono::duration_cast<std::chrono::microseconds>(std::chrono::system_clock::now().time_since_epoch()).count();
  97. diff -= tick;
  98. if (diff > 300)
  99. {
  100. _canNotify->WriteCanMessage(_message,true);
  101. _message.clear();
  102. tick = std::chrono::duration_cast<std::chrono::microseconds>(std::chrono::system_clock::now().time_since_epoch()).count();
  103. }
  104. }
  105. }