pcan_sensor.cpp 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. #include <stdint.h>
  2. #include "../common/comm.h"
  3. #include "../common/iobuffer.h"
  4. #include "../common/sensor_socket.h"
  5. #include "protocol.pb.h"
  6. #include "pcan_sensor.h"
  7. #include "message_queue.h"
  8. #include "Rtk.h"
  9. uint8_t HIBYTE(int16_t value)
  10. {
  11. return (uint8_t)((value>>8)&0xFF);
  12. }
  13. uint8_t LOBYTE(int16_t value)
  14. {
  15. return (uint8_t)((value)&0xFF);
  16. }
  17. int16_t make_int16(int8_t h,int8_t l)
  18. {
  19. int16_t hi=(int16_t)(h&0x00FF);
  20. int16_t low= (int16_t)(l&0x00FF);
  21. return (hi<<8)|low;
  22. }
  23. float b2f(uint8_t m0, uint8_t m1, uint8_t m2, uint8_t m3)
  24. {
  25. //求符号位
  26. float sig = 1.;
  27. if (m0 >=128.)
  28. sig = -1.;
  29. //求阶码
  30. float jie = 0.;
  31. if (m0 >=128.)
  32. {
  33. jie = m0-128. ;
  34. }
  35. else
  36. {
  37. jie = m0;
  38. }
  39. jie = jie * 2.;
  40. if (m1 >=128.)
  41. jie += 1.;
  42. jie -= 127.;
  43. //求尾码
  44. float tail = 0.;
  45. if (m1 >=128.)
  46. m1 -= 128.;
  47. tail = m3 + (m2 + m1 * 256.) * 256.;
  48. tail = (tail)/8388608; // 8388608 = 2^23
  49. float f = sig * pow(2., jie) * (1+tail);
  50. return f;
  51. }
  52. CPcanSensor::CPcanSensor(CMessageQueue * q):_message(q)
  53. {
  54. for (int32_t i = 0; i < 4; i++)
  55. memset(&_msg[i], 0, sizeof(TPCANMsg));
  56. }
  57. void CPcanSensor::Start()
  58. {
  59. _message->btimeStopedCar = false;
  60. _run = true;
  61. }
  62. void CPcanSensor::Stop()
  63. {
  64. if (_run)
  65. {
  66. _run = false;
  67. }
  68. for (int32_t i = 0; i < 4; i++)
  69. memset(&_msg[i], 0, sizeof(TPCANMsg));
  70. }
  71. void CPcanSensor::SendStatusToMSG()
  72. {
  73. RemoNet::State req;
  74. req.set_engine_speed(_data.Engine_speed);
  75. req.set_travel_speed(_data.Travel_speed);
  76. req.set_fuel_level(_data.Fuel_level);
  77. req.set_engine_temperature(_data.Engine_temperature);
  78. req.set_hydraulic_oil_temperature(_data.Hydraulic_oil_temperature);
  79. req.set_main_pump_1_pressure(_data.Main_pump_1_pressure);
  80. req.set_main_pump_2_pressure(_data.Main_pump_2_pressure);
  81. req.set_hand_gear(_data.Hand_gear);
  82. req.set_actual_gear(_data.Actual_gear);
  83. req.set_gripper_height(_data.Gripper_height);
  84. req.set_amplitude(_data.amplitude);
  85. req.set_boom_angle(_data.Boom_angle);
  86. req.set_stick_angle(_data.Stick_angle);
  87. req.set_idle_protection(_data.Idle_protection);
  88. req.set_front_toggle(_data.Front_toggle);
  89. req.set_back_toggle(_data.Back_toggle);
  90. req.set_error_buff((char *)_data.Error_Buff,8);
  91. MessageHead Head;
  92. CIOBuffer pBuffer;
  93. Head.Command = RemoNet::CC_STATE;
  94. Head.Length = req.ByteSizeLong();
  95. Head.Serialize(pBuffer.Buffer);
  96. auto ptr = pBuffer.Buffer + MessageHead::Size();
  97. req.SerializeToArray(ptr, Head.Length);
  98. pBuffer.Length = MessageHead::Size() + Head.Length;
  99. _message->WritePacket(ChannelType::CHANNEL_CAR, pBuffer);
  100. }
  101. void CPcanSensor::Notify(TPCANMsg *buffer, int32_t size)
  102. {
  103. //std::cout << std::hex << Source_data->canid << std::endl;
  104. if(_run)
  105. {
  106. switch (buffer->ID)
  107. {
  108. case 0x283:
  109. _data.Main_pump_1_pressure = make_int16(buffer->DATA[1],buffer->DATA[0]);//主泵1压力
  110. _data.Main_pump_2_pressure = make_int16(buffer->DATA[3],buffer->DATA[2]);//主泵2压力
  111. break;
  112. case 0x285:
  113. _data.Stick_angle = make_int16(buffer->DATA[1],buffer->DATA[0]);//斗杆角度
  114. _data.Boom_angle = make_int16(buffer->DATA[3],buffer->DATA[2]);//动臂角度
  115. break;
  116. case 0x286:
  117. _data.Hydraulic_oil_temperature = make_int16(buffer->DATA[1],buffer->DATA[0]);//液压油油温
  118. _data.Fuel_level = (int32_t)buffer->DATA[6];//燃油油位
  119. break;
  120. case 0x28A:
  121. _data.Travel_speed = make_int16(buffer->DATA[1],buffer->DATA[0]);//行驶速度
  122. _data.Engine_temperature = make_int16(buffer->DATA[3],buffer->DATA[2]);//发动机水温
  123. _data.Hand_gear = make_int16(buffer->DATA[5],buffer->DATA[4]);//水箱水位
  124. _data.Actual_gear = make_int16(buffer->DATA[7],buffer->DATA[6]);//发动机机油压力
  125. break;
  126. case 0x28B:
  127. _data.Engine_speed = make_int16(buffer->DATA[1],buffer->DATA[0]);//发动机转速
  128. if(_data.Engine_speed > 700 && _message->bStopedCar)
  129. _message->bStopedCar = false;
  130. break;
  131. case 0x28D:
  132. _data.Gripper_height = b2f(buffer->DATA[3], buffer->DATA[2], buffer->DATA[1],
  133. buffer->DATA[0]);//抓具高度
  134. _data.amplitude = b2f(buffer->DATA[7], buffer->DATA[6], buffer->DATA[5],
  135. buffer->DATA[4]);//幅度
  136. break;
  137. case 0x28F:
  138. _data.Idle_protection = (buffer->DATA[3] & 0x80) == 0x80 ? 1 : 0;//怠速保护
  139. _data.Front_toggle = (buffer->DATA[1] & 0x08) == 0x08 ? 1 : 0;//前轮对中
  140. _data.Back_toggle = (buffer->DATA[1] & 0x10) == 0x10 ? 1 : 0;//后轮对中
  141. _data.interlock = (buffer->DATA[5] & 0x08) == 0x08 ? 1 : 0;//启动联锁
  142. _data.safety_switch = (buffer->DATA[6] & 0x80) == 0x80 ? 1 : 0;//安全开关阀异常
  143. _data.arm_lift_pilot = (buffer->DATA[7] & 0x01) == 0x01 ? 1 : 0;//大臂提升先导比例阀异常
  144. _data.arm_lowering_pilot = (buffer->DATA[7] & 0x02) == 0x02 ? 1 : 0;//大臂下降先导比例阀异常
  145. _data.lever_lifting_pilot = (buffer->DATA[7] & 0x04) == 0x04 ? 1 : 0;//斗杆提升先导比例阀异常
  146. _data.rod_lowering_pilot = (buffer->DATA[7] & 0x08) == 0x08 ? 1 : 0;//斗杆下降先导比例阀异常
  147. _data.left_rotary_pilot = (buffer->DATA[7] & 0x10) == 0x10 ? 1 : 0;//左回转先导比例阀异常
  148. _data.right_rotary_pilot = (buffer->DATA[7] & 0x20) == 0x20 ? 1 : 0;//右回转先导比例阀异常
  149. _data.grab_open_pilot = (buffer->DATA[7] & 0x40) == 0x40 ? 1 : 0;//抓斗开启先导比例阀异常
  150. _data.grab_close_pilot = (buffer->DATA[7] & 0x80) == 0x80 ? 1 : 0;//抓斗闭合先导比例阀异常
  151. _data.safety_valves = (buffer->DATA[4] & 0x10) == 0x10 ? 1 : 0;//安全开关阀
  152. break;
  153. case 0x280:
  154. memcpy(_data.Error_Buff,buffer->DATA,buffer->LEN);
  155. SendStatusToMSG();
  156. break;
  157. default:
  158. break;
  159. }
  160. }
  161. }
  162. void CPcanSensor::OnMessage(cannet_frame* frames, int32_t count)
  163. {
  164. for (int32_t i = 0; i < count; i++)
  165. {
  166. cannet_frame& frame = frames[i];
  167. int32_t canid = frame.canid;
  168. //std::cout << std::hex << canid << std::endl;
  169. switch (canid)
  170. {
  171. case 0x181:
  172. _msg[0].ID = 0x181;
  173. _msg[0].LEN = frame.dlc;
  174. _msg[0].MSGTYPE = PCAN_MESSAGE_STANDARD;
  175. Hand_Throttle = make_int16(frame.data[1],frame.data[0]);
  176. Foot_Throttle = make_int16(frame.data[3],frame.data[2]);
  177. Brake = make_int16(frame.data[5],frame.data[4]);
  178. memcpy(_msg[0].DATA, frame.data, frame.dlc);
  179. _socket->Write(&_msg[0]);
  180. //std::cout << "revice 181" << std::endl;
  181. break;
  182. case 0x182:
  183. _msg[1].ID = 0x182;
  184. _msg[1].LEN = frame.dlc;
  185. _msg[1].MSGTYPE = PCAN_MESSAGE_STANDARD;
  186. memcpy(_msg[1].DATA, frame.data, frame.dlc);
  187. _socket->Write(&_msg[1]);
  188. //if(_msg[1].DATA[0] != 0)
  189. // std::cout << "182" << _msg[i].DATA[0] << _msg[i].DATA[1] << std::endl;
  190. break;
  191. case 0x183:
  192. _msg[2].ID = 0x183;
  193. _msg[2].LEN = frame.dlc;
  194. _msg[2].MSGTYPE = PCAN_MESSAGE_STANDARD;
  195. Direction = make_int16(frame.data[3],frame.data[2]);
  196. // _message->SendVehicleStatus(Direction,Hand_Throttle,Foot_Throttle,Brake);
  197. memcpy(_msg[2].DATA, frame.data, frame.dlc);
  198. _socket->Write(&_msg[2]);
  199. //std::cout << "revice 183" << std::endl;
  200. break;
  201. case 0x184:
  202. _msg[3].ID = 0x184;
  203. _msg[3].LEN = frame.dlc;
  204. _msg[3].MSGTYPE = PCAN_MESSAGE_STANDARD;
  205. //模式
  206. model = (frame.data[3] & 0x04) == 0x04 ? 1 : 0;
  207. //切换前后摄像头
  208. _front_view = (frame.data[4] & 0x04) == 0x04 ? false: true;
  209. _message->SwitchCamera(_front_view);
  210. if((frame.data[4] & 0x10) == 0x10 ? 1 : 0)//已就绪 抓钢机就绪状态,待无人车入场
  211. {
  212. _message->SendZGJStatus(1);
  213. }
  214. if((frame.data[4] & 0x20) == 0x20 ? 1 : 0)//已完成 抓钢机抓钢结束,待无人车离场
  215. {
  216. _message->SendZGJStatus(2);
  217. }
  218. if(!_message->btimeStopedCar)
  219. {
  220. _socket->Write(&_msg[3]);
  221. }
  222. else
  223. {
  224. long long tick=std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now().time_since_epoch()).count();
  225. /*
  226. if(tick-_message->_curStopTick > 500)
  227. {
  228. Emergency();
  229. _message->btimeStopedCar = false;
  230. }
  231. */
  232. if(tick-_message->_curStopTick < 500)
  233. Emergency();
  234. else
  235. _message->btimeStopedCar = false;
  236. }
  237. //std::cout << "revice 184" << std::endl;
  238. break;
  239. }
  240. }
  241. }
  242. bool CPcanSensor::Emergency()
  243. {
  244. if(model)
  245. {
  246. _msg[3].ID = 0x184;
  247. _msg[3].LEN = 8;
  248. _msg[3].MSGTYPE = PCAN_MESSAGE_STANDARD;
  249. _msg[3].DATA[3] &= 0xFD;
  250. _socket->Write(&_msg[3]);
  251. }
  252. return false;
  253. }
  254. void CPcanSensor::Run()
  255. {
  256. }
  257. void CPcanSensor::SetSensorSocket(SensorPeakCan<CPcanSensor>* can)
  258. {
  259. _socket=can;
  260. }