imu_sensor.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. #include <stdint.h>
  2. #include "../common/comm.h"
  3. #include "../common/iobuffer.h"
  4. #include "../common/sensor_socket.h"
  5. #include "imu_sensor.h"
  6. #include "message_queue.h"
  7. CImuSensor::CImuSensor(CMessageQueue* q):_message(q)
  8. {
  9. }
  10. void CImuSensor::SetSensorSocket(SensorSocket<CImuSensor>* can)
  11. {
  12. _can=can;
  13. }
  14. void CImuSensor::Notify(int8_t * buffer,int32_t size)
  15. {
  16. //struct cannet_fram frame;
  17. int32_t receivedCnt = size/CAN_MSG_LEN;
  18. cannet_frame* p = (cannet_frame*)buffer;
  19. for(int i=0; i<receivedCnt; i++)
  20. {
  21. p->canid=htonl(p->canid);
  22. if((p->canid&0xFF0)==0x580)
  23. {
  24. ImuProcess(*p);
  25. }
  26. p++;
  27. }
  28. if(receivedCnt>0)
  29. {
  30. //std::cout<<"rot :"<<_imu.rx<<","<<_imu.ry<<","<<std::endl;
  31. _message->WriteIMUData(&_imu);
  32. }
  33. }
  34. int32_t CImuSensor::bcd2dec(const int8_t *data)
  35. {
  36. int32_t h0=(data[0]>>4)&0xF;
  37. int32_t l0=data[0]&0xF;
  38. int32_t h1=(data[1]>>4)&0xF;
  39. int32_t l1=data[1]&0xF;
  40. int32_t ret=((h0*10+l0)*10+h1)*10+l1;
  41. return ret;
  42. }
  43. void CImuSensor::ImuProcess(const cannet_frame& frame)
  44. {
  45. const int8_t* p=frame.data;
  46. if(p[0]==0x50&&p[1]==0x00) //角速度
  47. {
  48. /*
  49. p+=2;
  50. float temp=bcd2dec(p);// (((((p[0]>>4)&0xF*10)+(p[0]&0xF))*10)+((p[1]>>4)&0xF)) 00/16+p[1]*10/16;
  51. float val=temp-5000;
  52. _imu.AngVel.X=val/10.f;
  53. p+=2;
  54. temp=bcd2dec(p);
  55. val=temp-5000;
  56. _imu.AngVel.Y=val/10.f;
  57. p+=2;
  58. temp=bcd2dec(p);
  59. val=temp-5000;
  60. _imu.AngVel.Z=val/10.f;
  61. */
  62. }
  63. else if(p[0]==0x54&&p[1]==0x00) //加速度
  64. {
  65. /*
  66. p+=2;
  67. float temp=bcd2dec(p);
  68. float val=temp-5000;
  69. _imu.Accel.X=val/2500.f;
  70. p+=2;
  71. temp=bcd2dec(p);
  72. val=temp-5000;
  73. _imu.Accel.Y=val/2500.f;
  74. p+=2;
  75. temp=bcd2dec(p);
  76. val=temp-5000;
  77. _imu.Accel.Z=val/2500.f;
  78. */
  79. }
  80. else
  81. {
  82. float sign=((p[0]>>4)&0xF) !=0?-1.f:1.f;
  83. int32_t h2=p[0]&0xF;
  84. int32_t h1=(p[1]>>4)&0xF;
  85. int32_t h0=p[1]&0xf;
  86. int32_t l1=((p[2]>>4)&0xF)*10+(p[2]&0xF);
  87. float dec=h2*100+h1*10+h0+(l1/100.f);
  88. // float fraction=((p[2]>>4)&0xF)*10+(p[2]&0xF);
  89. _imu.rx=dec*sign;//(dec+fraction/100.f)*sign;
  90. //p+=3;
  91. sign=((p[3]>>4)&0xF) !=0?1.f:-1.f;
  92. h2=p[3]&0xF;
  93. h1=(p[4]>>4)&0xF;
  94. h0=p[4]&0xf;
  95. l1=((p[5]>>4)&0xF)*10+(p[5]&0xF);
  96. dec=h2*100+h1*10+h0+(l1/100.f);
  97. _imu.ry=dec*sign;
  98. /*
  99. p+=3;
  100. h2=(p[0]>>4)&0xF;
  101. h1=(p[0])&0xF;
  102. h0=(p[1]>>4)&0xF;
  103. int32_t l0=(p[1])&0xF;
  104. dec=h2*100+h1*10+h0+l0/10.f;
  105. _imu.rz=dec;
  106. */
  107. }
  108. }
  109. void CImuSensor::Start()
  110. {
  111. CIOBuffer buffer;
  112. cannet_frame* frame=(cannet_frame *)buffer.Buffer;
  113. frame->canid=htons(0x600+0x05);
  114. frame->dlc=8;
  115. frame->data[0]=0x40;
  116. frame->data[1]=0x53;
  117. frame->data[2]=0x10;
  118. frame->data[3]=0;
  119. frame->data[4]=0;
  120. frame->data[5]=0;
  121. frame->data[6]=0;
  122. frame->data[7]=0;
  123. buffer.Length=sizeof(cannet_frame);
  124. _can->Write(&buffer);
  125. _can->Read(&buffer);
  126. }
  127. void CImuSensor::Stop()
  128. {
  129. }
  130. void CImuSensor::PreProcess()
  131. {
  132. }