encoder_sensor.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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 0x1B1:
  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. _message->SetLeftAngle(last_angle);
  46. if(g_collbrate)
  47. std::cout<<"1b1 "<<","<<last_angle<<std::endl;
  48. }
  49. break;
  50. case 0x1B2:
  51. {
  52. int32_t l0=p[i].data[0]&0x000000ff;
  53. int32_t l1=p[i].data[1]&0x000000ff;
  54. int16_t last_angle= (l1<<8)|l0;
  55. //_data.right_angle=last_angle;
  56. _message->SetRightAngle(last_angle);
  57. if(g_collbrate)
  58. std::cout<<"1b2 "<<","<<last_angle<<std::endl;
  59. }
  60. break;
  61. }
  62. }
  63. CIOBuffer Buffer;
  64. static auto tick= std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now().time_since_epoch()).count();
  65. auto now= std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now().time_since_epoch()).count();
  66. if(now-tick>1000)
  67. {
  68. cannet_frame* frame=reinterpret_cast<cannet_frame *>(Buffer.Buffer);
  69. memset(frame->data,0,sizeof(frame->data));
  70. frame->dlc=2;
  71. frame->canid=0x0000;
  72. frame->data[0]=0x01;
  73. frame->data[1]=0x00;
  74. _socket->Write(Buffer.Buffer,sizeof(cannet_frame));
  75. tick=std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now().time_since_epoch()).count();
  76. }
  77. std::this_thread::sleep_for(std::chrono::milliseconds(100));
  78. }