encoder_sensor.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. #include "../common/iobuffer.h"
  2. #include "../common/sensor_socket.h"
  3. #include "PCANBasic.h"
  4. #include "message_queue.h"
  5. #include "encoder_sensor.h"
  6. CEncoderSensor::CEncoderSensor(CMessageQueue *q):_message(q)
  7. {
  8. }
  9. void CEncoderSensor::Start()
  10. {
  11. _channelReady=false;
  12. CIOBuffer pBuffer;
  13. cannet_frame* frame=reinterpret_cast<cannet_frame *>(pBuffer.Buffer);
  14. memset(frame->data,0,sizeof(frame->data));
  15. frame->dlc=2;
  16. frame->canid=0x0000;
  17. frame->data[0]=0x01;
  18. frame->data[1]=0x00;
  19. _socket->Write(pBuffer.Buffer,sizeof(cannet_frame));
  20. }
  21. void CEncoderSensor::SetSensorSocket(SensorSocket<CEncoderSensor>* can)
  22. {
  23. _socket=can;
  24. }
  25. void CEncoderSensor::Stop()
  26. {
  27. _channelReady=false;
  28. }
  29. extern bool g_collbrate;
  30. void CEncoderSensor::Notify(int8_t * buffer,int32_t size)
  31. {
  32. int32_t receivedCnt = size/CAN_MSG_LEN;
  33. cannet_frame* p = (cannet_frame*)buffer;
  34. for(int32_t i=0;i<receivedCnt;i++)
  35. {
  36. int32_t id_normal=ntohl(p[i].canid);
  37. switch(id_normal)
  38. {
  39. case 0x181:
  40. {
  41. //int32_t l0=p[i].data[0]&0x000000ff;
  42. //int32_t l1=p[i].data[1]&0x000000ff;
  43. //int16_t last_angle= (l1<<8)|l0;
  44. //_data.left_angle=last_angle;
  45. int16_t hi=(int16_t)(p[i].data[1] & 0x00FF);
  46. int16_t low= (int16_t)(p[i].data[2] & 0x00FF);
  47. int16_t last_angle = ((hi<<8)|low) / 100;
  48. _message->SetLeftAngle(last_angle);
  49. //if(g_collbrate)
  50. //std::cout<<"1b1 "<<","<<last_angle<<std::endl;
  51. }
  52. break;
  53. case 0x182:
  54. {
  55. //int32_t l0=p[i].data[0]&0x000000ff;
  56. //int32_t l1=p[i].data[1]&0x000000ff;
  57. //int16_t last_angle= (l1<<8)|l0;
  58. int16_t hi=(int16_t)(p[i].data[1] & 0x00FF);
  59. int16_t low= (int16_t)(p[i].data[2] & 0x00FF);
  60. int16_t last_angle = ((hi<<8)|low) / 100;
  61. //_data.right_angle=last_angle;
  62. _message->SetRightAngle(last_angle);
  63. //if(g_collbrate)
  64. //std::cout<<"1b2 "<<","<<last_angle<<std::endl;
  65. }
  66. break;
  67. }
  68. }
  69. CIOBuffer Buffer;
  70. static auto tick= std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now().time_since_epoch()).count();
  71. auto now= std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now().time_since_epoch()).count();
  72. if(now-tick>1000)
  73. {
  74. cannet_frame* frame=reinterpret_cast<cannet_frame *>(Buffer.Buffer);
  75. memset(frame->data,0,sizeof(frame->data));
  76. frame->dlc=2;
  77. frame->canid=0x0000;
  78. frame->data[0]=0x01;
  79. frame->data[1]=0x00;
  80. _socket->Write(Buffer.Buffer,sizeof(cannet_frame));
  81. tick=std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now().time_since_epoch()).count();
  82. }
  83. std::this_thread::sleep_for(std::chrono::milliseconds(100));
  84. }