control_sensor.cpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. #define WIN32_LEAN_AND_MEAN
  2. #include <WinSock2.h>
  3. #include "PCANBasic.h"
  4. #include "./include/EgoInterface.h"
  5. #include "EgoClient.h"
  6. #include "EgoWindow.h"
  7. #include "protocol.pb.h"
  8. #include "../common/iobuffer.h"
  9. #include "protocol.pb.h"
  10. #include "control_sensor.h"
  11. #include <algorithm>
  12. #include <string>
  13. #define MAX_DGREE (int32_t)(4096/6)
  14. inline int16_t make_int16(int8_t h, int8_t l)
  15. {
  16. int16_t hi = (int16_t)(h & 0x00FF);
  17. int16_t low = (int16_t)(l & 0x00FF);
  18. return (hi << 8) | low;
  19. }
  20. inline int8_t hi_byte(int16_t value)
  21. {
  22. int8_t hi =(int8_t)((value & 0xFF00) >> 8);
  23. return hi;
  24. }
  25. inline int8_t lo_byte(int16_t value)
  26. {
  27. int8_t lo = (int8_t)(value & 0xff);
  28. return lo;
  29. }
  30. CControlSensor::CControlSensor(CMessageQueue* q)
  31. {
  32. _window = static_cast<CEgoWindow*>(q);
  33. _collection = Sensor_Collection::Sensor_None;
  34. }
  35. void CControlSensor::Notify(TPCANMsg& canmsg,uint64_t timestamp)
  36. {
  37. //OutputDebugString(str);
  38. //OutputDebugString(ret.c_str());
  39. //发送座椅信号给下位机
  40. switch (canmsg.ID)
  41. {
  42. case 0x181:
  43. {
  44. //接收181msg
  45. frames[0].canid = canmsg.ID;
  46. frames[0].dlc = canmsg.LEN;
  47. memcpy(&frames[0].data, canmsg.DATA, sizeof(canmsg.DATA));
  48. //flag收到181
  49. _collection = (Sensor_Collection)(_collection | Sensor_Collection::Sensor_181);
  50. }
  51. break;
  52. case 0x182:
  53. {
  54. //接收182msg
  55. frames[1].canid = canmsg.ID;
  56. frames[1].dlc = canmsg.LEN;
  57. memcpy(&frames[1].data, canmsg.DATA, sizeof(canmsg.DATA));
  58. //flag收到182
  59. _collection = (Sensor_Collection)(_collection | Sensor_Collection::Sensor_182);
  60. }
  61. break;
  62. case 0x183:
  63. { //接收183msg
  64. frames[2].canid = canmsg.ID;
  65. frames[2].dlc = canmsg.LEN;
  66. memcpy(&frames[2].data, canmsg.DATA, sizeof(canmsg.DATA));
  67. //flag收到183
  68. _collection = (Sensor_Collection)(_collection | Sensor_Collection::Sensor_183);
  69. }
  70. break;
  71. case 0x184:
  72. //case 0x611:
  73. {
  74. //接收184msg
  75. frames[3].canid = canmsg.ID;
  76. frames[3].dlc = canmsg.LEN;
  77. memcpy(&frames[3].data, canmsg.DATA, sizeof(canmsg.DATA));
  78. //flag收到184
  79. _collection = (Sensor_Collection)(_collection | Sensor_Collection::Sensor_184);
  80. //
  81. //解读184信号,发送到Qt显示(灯光与档位状态和声音的改变)
  82. bool cautionLight = frames[3].data[0] & 0x4;
  83. bool lightL = frames[3].data[0] & 0x8;
  84. bool lightR = frames[3].data[0] & 0x10;
  85. bool gearF = frames[3].data[0] & 0x20;
  86. bool gearR = frames[3].data[0] & 0x40;
  87. bool carPark = frames[3].data[1] & 0x1;
  88. bool buzzerPlay = frames[3].data[1] & 0x2;
  89. bool lightFront = frames[3].data[2] & 0x10;
  90. bool lightWork = frames[3].data[2] & 0x20;
  91. bool emergency = frames[3].data[3] & 0x2;
  92. _window->GetEgoClient()->OnCautionLight(cautionLight);
  93. _window->GetEgoClient()->OnLightL(lightL);
  94. _window->GetEgoClient()->OnLightR(lightR);
  95. _window->GetEgoClient()->OnGearF(gearF);
  96. _window->GetEgoClient()->OnGearR(gearR);
  97. _window->GetEgoClient()->OnCarPark(carPark);
  98. _window->GetEgoClient()->OnBuzzerPlay(buzzerPlay);
  99. _window->GetEgoClient()->OnLightFront(lightFront);
  100. _window->GetEgoClient()->OnLightWork(lightWork);
  101. _window->GetEgoClient()->OnEmergency(emergency);
  102. }
  103. break;
  104. case 0x185:
  105. {
  106. //接收185msg
  107. frames[4].canid = canmsg.ID;
  108. frames[4].dlc = canmsg.LEN;
  109. memcpy(&frames[4].data, canmsg.DATA, sizeof(canmsg.DATA));
  110. //flag收到185
  111. _collection = (Sensor_Collection)(_collection | Sensor_Collection::Sensor_185);
  112. }
  113. break;
  114. }
  115. if (_window->GetControlState() == ControlState::Process && _collection == Sensor_Collection::Sensor_All)
  116. {
  117. static DWORD tick = GetTickCount();
  118. RemoNet::CCCanMsg req;
  119. for (int32_t i = 0; i < _countof(frames); i++)
  120. {
  121. if (frames[i].canid != 0)
  122. {
  123. RemoNet::can_net_frame* frame = req.add_frams();
  124. frame->set_canid(frames[i].canid);
  125. frame->set_dlc(frames[i].dlc);
  126. frame->set_data(frames[i].data, frames[i].dlc);
  127. }
  128. }
  129. MessageHead Head;
  130. CIOBuffer pBuffer;
  131. Head.Command = RemoNet::CC_CANMSG;
  132. Head.Length = req.ByteSizeLong();
  133. Head.Serialize(pBuffer.Buffer);
  134. auto ptr = pBuffer.Buffer + MessageHead::Size();
  135. req.SerializeToArray(ptr, Head.Length);
  136. pBuffer.Length = MessageHead::Size() + Head.Length;
  137. _window->SendData(pBuffer);
  138. _collection = Sensor_Collection::Sensor_None;
  139. /*DWORD diff = GetTickCount() - tick;
  140. tick = GetTickCount();
  141. std::string str = std::to_string(diff);
  142. str += "\n";
  143. OutputDebugStringA(str.c_str());
  144. */
  145. }
  146. }
  147. ControlStatus CControlSensor::CheckStatus()
  148. {
  149. return ControlStatus::Ok;
  150. }
  151. bool CControlSensor::Start()
  152. {
  153. int iBuffer;
  154. TPCANStatus stsResult;
  155. TPCANHandle _HandlesArray[16];
  156. _HandlesArray[0] = PCAN_USBBUS1;
  157. _HandlesArray[1] = PCAN_USBBUS2;
  158. _HandlesArray[2] = PCAN_USBBUS3;
  159. _HandlesArray[3] = PCAN_USBBUS4;
  160. _HandlesArray[4] = PCAN_USBBUS5;
  161. _HandlesArray[5] = PCAN_USBBUS6;
  162. _HandlesArray[6] = PCAN_USBBUS7;
  163. _HandlesArray[7] = PCAN_USBBUS8;
  164. _HandlesArray[8] = PCAN_USBBUS9;
  165. _HandlesArray[9] = PCAN_USBBUS10;
  166. _HandlesArray[10] = PCAN_USBBUS11;
  167. _HandlesArray[11] = PCAN_USBBUS12;
  168. _HandlesArray[12] = PCAN_USBBUS13;
  169. _HandlesArray[13] = PCAN_USBBUS14;
  170. _HandlesArray[14] = PCAN_USBBUS15;
  171. _HandlesArray[15] = PCAN_USBBUS16;
  172. for (int i = 0; i < (sizeof(_HandlesArray) / sizeof(TPCANHandle)); i++)
  173. {
  174. stsResult = CAN_GetValue(_HandlesArray[i], PCAN_CHANNEL_CONDITION, &iBuffer, sizeof(iBuffer));
  175. if (((stsResult) == PCAN_ERROR_OK) && ((iBuffer & PCAN_CHANNEL_AVAILABLE) == PCAN_CHANNEL_AVAILABLE))
  176. {
  177. stsResult = CAN_GetValue((TPCANHandle)_HandlesArray[i], PCAN_CHANNEL_FEATURES, (void*)&iBuffer, sizeof(iBuffer));
  178. _isFD = (stsResult == PCAN_ERROR_OK) && (iBuffer & FEATURE_FD_CAPABLE);
  179. _handle = _HandlesArray[i];
  180. break;
  181. }
  182. }
  183. if (_handle != -1)
  184. {
  185. TPCANStatus stsResult = CAN_Initialize(_handle, PCAN_BAUD_250K);
  186. //使用雷达盒子测试用500k
  187. //TPCANStatus stsResult = CAN_Initialize(_handle, PCAN_BAUD_500K);
  188. if (stsResult != PCAN_ERROR_OK)
  189. {
  190. return false;
  191. }
  192. _thread = std::thread(&CControlSensor::Run, this);
  193. }
  194. return _handle != -1;
  195. }
  196. void CControlSensor::Run()
  197. {
  198. _run = true;
  199. while (_run)
  200. {
  201. for (int32_t i = 0; i < sizeof(frames) / sizeof(cannet_frame); i++)
  202. {
  203. frames[i].canid = 0;
  204. }
  205. Sleep(10);
  206. ReadMessages();
  207. }
  208. CAN_Uninitialize(_handle);
  209. }
  210. void CControlSensor::ReadMessages()
  211. {
  212. TPCANStatus stsResult;
  213. // We read at least one time the queue looking for messages. If a message is found, we look again trying to
  214. // find more. If the queue is empty or an error occurr, we get out from the dowhile statement.
  215. do
  216. {
  217. ReadMessage();
  218. if (stsResult != PCAN_ERROR_OK && stsResult != PCAN_ERROR_QRCVEMPTY)
  219. {
  220. //ShowStatus(stsResult);
  221. return;
  222. }
  223. } while (!(stsResult & PCAN_ERROR_QRCVEMPTY));
  224. }
  225. TPCANStatus CControlSensor::ReadMessage()
  226. {
  227. TPCANMsg CANMsg;
  228. TPCANTimestamp CANTimeStamp;
  229. // We execute the "Read" function of the PCANBasic
  230. TPCANStatus stsResult = CAN_Read(_handle, &CANMsg, &CANTimeStamp);
  231. if (stsResult != PCAN_ERROR_QRCVEMPTY)
  232. { // We process the received message
  233. uint64_t newTimestamp = (CANTimeStamp.micros + 1000 * CANTimeStamp.millis + 0x100000000 * 1000 * CANTimeStamp.millis_overflow);
  234. Notify(CANMsg, newTimestamp);
  235. }
  236. return stsResult;
  237. }
  238. //将PCAN信号传回座椅PLC中
  239. TPCANStatus CControlSensor::SendCANMessage(TPCANMsg* CANMsg)
  240. {
  241. // 执行PCANBasic的“写入”功能
  242. TPCANStatus stsResult = CAN_Write(_handle, CANMsg);
  243. return stsResult;
  244. }
  245. void CControlSensor::Stop()
  246. {
  247. _run = false;
  248. _thread.join();
  249. }
  250. void CControlSensor::SetEngineRPM(int32_t value)
  251. {
  252. _rpm = value;
  253. }