control_sensor.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  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. //0x181 0x281 0x381 0x481 0x185 0x701
  40. //打印
  41. //OutputDebugStringA(("===================打印ID " + std::to_string(canmsg.ID) + "============\n").c_str());
  42. switch (canmsg.ID)
  43. {
  44. case 0x181:
  45. {
  46. //此处需要改成QT上位机按钮控制_automous
  47. if ((frames[2].data[7] & 0x1) != 1)
  48. {
  49. //取按钮的值
  50. isFront = (canmsg.DATA[1] & 0x1) == 1;
  51. frames[0].canid = canmsg.ID;
  52. frames[0].dlc = canmsg.LEN;
  53. memcpy(&frames[0].data, canmsg.DATA, sizeof(canmsg.DATA));
  54. if (_remote_active)
  55. {
  56. frames[0].data[0] = 0x10;
  57. _remote_active = false;
  58. _active_tick = GetTickCount64();
  59. }
  60. }
  61. else
  62. {
  63. frames[0].canid = 0;
  64. }
  65. _collection = (Sensor_Collection)(_collection | Sensor_Collection::Sensor_ER);
  66. }
  67. break;
  68. case 0x281:
  69. {
  70. frames[2].canid = canmsg.ID;
  71. frames[2].dlc = canmsg.LEN;
  72. memcpy(&frames[2].data, canmsg.DATA, sizeof(canmsg.DATA));
  73. _collection = (Sensor_Collection)(_collection | Sensor_Collection::Sensor_Signal);
  74. // 添加打印语句
  75. /*std::string receivedData = "Received data: ";
  76. for (int i = 0; i < canmsg.LEN; i++)
  77. {
  78. receivedData += std::to_string(canmsg.DATA[i]) + " ";
  79. }
  80. receivedData += "\n";
  81. OutputDebugStringA(receivedData.c_str());*/
  82. }
  83. break;
  84. case 0x381:
  85. {
  86. if ((frames[2].data[7] & 0x1) != 1)
  87. //QT按钮增加判断是遥操还是监控
  88. //if (_automous == true)
  89. {
  90. frames[3].canid = canmsg.ID;
  91. frames[3].dlc = canmsg.LEN;
  92. /* if (isSliping)
  93. {
  94. canmsg.DATA[0] = 100;
  95. canmsg.DATA[1] = 100;
  96. }
  97. */
  98. memcpy(&frames[3].data, canmsg.DATA, sizeof(canmsg.DATA));
  99. }
  100. else
  101. {
  102. frames[3].canid = 0;
  103. }
  104. _collection = (Sensor_Collection)(_collection | Sensor_Collection::Sensor_Drive);
  105. }
  106. break;
  107. case 0x1A0:
  108. {
  109. if ((frames[2].data[7] & 0x1) != 1)
  110. {
  111. int32_t steer = make_int16(canmsg.DATA[1], canmsg.DATA[0]);
  112. //OutputDebugStringA(("=====" + std::to_string(steer) + "=====\n").c_str());
  113. float period = steer - _old_steer;
  114. _old_steer = steer;
  115. float diff = timestamp - _old_timestamp;
  116. diff /= 1000.f;
  117. diff /= 1000.f;
  118. _old_timestamp = timestamp;
  119. //float diff = tick - _cur_tick;
  120. frames[1].canid = canmsg.ID;
  121. frames[1].dlc = 8;
  122. //原来的脉冲值是每500秒
  123. //int32_t speed = (period / (diff * 2.0f));
  124. //改成每50毫秒的脉冲值
  125. int32_t speed = (period / (diff*40.0f));
  126. if (speed == 0)
  127. {
  128. auto tick = GetTickCount64();
  129. if (_remote_active == false && tick - _active_tick > 500)
  130. {
  131. _remote_active = true;
  132. }
  133. }
  134. if (_window->GetSteerAngle() < -MAX_DGREE)
  135. {
  136. if (isFront)
  137. {
  138. if (speed < 0)
  139. speed = 0;
  140. }
  141. else
  142. {
  143. if (speed > 0)
  144. speed = 0;
  145. }
  146. }
  147. else if (_window->GetSteerAngle() > MAX_DGREE)
  148. {
  149. if (isFront)
  150. {
  151. if (speed > 0)
  152. speed = 0;
  153. }
  154. else {
  155. if (speed < 0)
  156. speed = 0;
  157. }
  158. }
  159. else
  160. {
  161. if (_last_speed != 0xF000)
  162. {
  163. if ((_last_speed * speed) < 0)
  164. {
  165. _last_speed = speed;
  166. speed = 0;
  167. }
  168. }
  169. else
  170. {
  171. _last_speed = speed;
  172. }
  173. }
  174. frames[1].data[0] = 0xFF;
  175. frames[1].data[1] = 0x00;
  176. if (!isFront)
  177. speed = (speed * -1);
  178. //OutputDebugStringA(("========速度" + std::to_string(speed) + "脉冲每50毫秒============\n").c_str());
  179. frames[1].data[2] = hi_byte(speed);
  180. frames[1].data[3] = lo_byte(speed);
  181. if (isFront)
  182. frames[1].data[4] = speed < 0 ? 1 : 0;
  183. else
  184. frames[1].data[4] = speed < 0 ? 0 : 1;
  185. frames[1].data[4] |= 0x02;
  186. frames[1].data[5] = 0;
  187. frames[1].data[6] = 0;
  188. frames[1].data[7] = 0;
  189. }
  190. else
  191. {
  192. frames[1].canid = 0;
  193. }
  194. _collection = (Sensor_Collection)(_collection | Sensor_Collection::Sensor_Steer);
  195. }
  196. break;
  197. case 0x481:
  198. {
  199. if ((frames[2].data[7] & 0x1) != 1)
  200. {
  201. frames[4].canid = canmsg.ID;
  202. frames[4].dlc = canmsg.LEN;
  203. int16_t d = canmsg.DATA[0];
  204. int16_t l = canmsg.DATA[2];
  205. int16_t k = canmsg.DATA[4];
  206. isSliping = std::abs(d) > 10 || std::abs(l) > 10 || std::abs(k) > 10;
  207. memcpy(&frames[4].data, canmsg.DATA, sizeof(canmsg.DATA));
  208. _collection = (Sensor_Collection)(_collection | Sensor_Collection::Sensor_Arm);
  209. }
  210. else
  211. {
  212. frames[4].canid = 0;
  213. }
  214. }
  215. break;
  216. }
  217. if (_window->GetControlState() == ControlState::Process && _collection == Sensor_Collection::Sensor_All)
  218. {
  219. static DWORD tick = GetTickCount();
  220. RemoNet::CCCanMsg req;
  221. for (int32_t i = 0; i < _countof(frames); i++)
  222. {
  223. if (frames[i].canid != 0)
  224. {
  225. RemoNet::can_net_frame* frame = req.add_frams();
  226. frame->set_canid(frames[i].canid);
  227. frame->set_dlc(frames[i].dlc);
  228. frame->set_data(frames[i].data, frames[i].dlc);
  229. }
  230. }
  231. MessageHead Head;
  232. CIOBuffer pBuffer;
  233. Head.Command = RemoNet::CC_CANMSG;
  234. Head.Length = req.ByteSizeLong();
  235. Head.Serialize(pBuffer.Buffer);
  236. auto ptr = pBuffer.Buffer + MessageHead::Size();
  237. req.SerializeToArray(ptr, Head.Length);
  238. pBuffer.Length = MessageHead::Size() + Head.Length;
  239. //使用pcan发送给下位机
  240. //_window->SendData(pBuffer);
  241. //使用udp发送给下位机
  242. _window->WriteCan(&pBuffer);
  243. //OutputDebugStringA(("=================" + std::to_string((frames[2].data[7] & 0x1) != 1) + "=======\n").c_str());
  244. _collection = Sensor_Collection::Sensor_None;
  245. }
  246. }
  247. ControlStatus CControlSensor::CheckStatus()
  248. {
  249. return ControlStatus::Ok;
  250. }
  251. bool CControlSensor::Start()
  252. {
  253. int iBuffer;
  254. TPCANStatus stsResult;
  255. TPCANHandle _HandlesArray[16];
  256. _HandlesArray[0] = PCAN_USBBUS1;
  257. _HandlesArray[1] = PCAN_USBBUS2;
  258. _HandlesArray[2] = PCAN_USBBUS3;
  259. _HandlesArray[3] = PCAN_USBBUS4;
  260. _HandlesArray[4] = PCAN_USBBUS5;
  261. _HandlesArray[5] = PCAN_USBBUS6;
  262. _HandlesArray[6] = PCAN_USBBUS7;
  263. _HandlesArray[7] = PCAN_USBBUS8;
  264. _HandlesArray[8] = PCAN_USBBUS9;
  265. _HandlesArray[9] = PCAN_USBBUS10;
  266. _HandlesArray[10] = PCAN_USBBUS11;
  267. _HandlesArray[11] = PCAN_USBBUS12;
  268. _HandlesArray[12] = PCAN_USBBUS13;
  269. _HandlesArray[13] = PCAN_USBBUS14;
  270. _HandlesArray[14] = PCAN_USBBUS15;
  271. _HandlesArray[15] = PCAN_USBBUS16;
  272. for (int i = 0; i < (sizeof(_HandlesArray) / sizeof(TPCANHandle)); i++)
  273. {
  274. stsResult = CAN_GetValue(_HandlesArray[i], PCAN_CHANNEL_CONDITION, &iBuffer, sizeof(iBuffer));
  275. if (((stsResult) == PCAN_ERROR_OK) && ((iBuffer & PCAN_CHANNEL_AVAILABLE) == PCAN_CHANNEL_AVAILABLE))
  276. {
  277. stsResult = CAN_GetValue((TPCANHandle)_HandlesArray[i], PCAN_CHANNEL_FEATURES, (void*)&iBuffer, sizeof(iBuffer));
  278. _isFD = (stsResult == PCAN_ERROR_OK) && (iBuffer & FEATURE_FD_CAPABLE);
  279. _handle = _HandlesArray[i];
  280. break;
  281. }
  282. }
  283. if (_handle != -1)
  284. {
  285. //波特率设置位250k
  286. TPCANStatus stsResult = CAN_Initialize(_handle, PCAN_BAUD_250K);
  287. if (stsResult != PCAN_ERROR_OK)
  288. {
  289. return false;
  290. }
  291. _thread = std::thread(&CControlSensor::Run, this);
  292. }
  293. return _handle != -1;
  294. }
  295. void CControlSensor::Run()
  296. {
  297. _run = true;
  298. while (_run)
  299. {
  300. for (int32_t i = 0; i < sizeof(frames) / sizeof(cannet_frame); i++)
  301. {
  302. frames[i].canid = 0;
  303. }
  304. Sleep(10);
  305. ReadMessages();
  306. }
  307. CAN_Uninitialize(_handle);
  308. }
  309. void CControlSensor::ReadMessages()
  310. {
  311. TPCANStatus stsResult;
  312. // We read at least one time the queue looking for messages. If a message is found, we look again trying to
  313. // find more. If the queue is empty or an error occurr, we get out from the dowhile statement.
  314. do
  315. {
  316. ReadMessage();
  317. if (stsResult != PCAN_ERROR_OK && stsResult != PCAN_ERROR_QRCVEMPTY)
  318. {
  319. //ShowStatus(stsResult);
  320. return;
  321. }
  322. } while (!(stsResult & PCAN_ERROR_QRCVEMPTY));
  323. }
  324. //读取can消息并处理
  325. TPCANStatus CControlSensor::ReadMessage()
  326. {
  327. TPCANMsg CANMsg; //存放接收到的can消息
  328. TPCANTimestamp CANTimeStamp; //存放can消息时间戳
  329. // 读取can消息并存放在CANMsg和CANTimeStamp中,函数返回一个TPCANStatus类型状态值
  330. TPCANStatus stsResult = CAN_Read(_handle, &CANMsg, &CANTimeStamp);
  331. //检查是否成功接受到can信息,成功则计算新时间戳并调用Notify函数
  332. if (stsResult != PCAN_ERROR_QRCVEMPTY)
  333. { // We process the received message
  334. uint64_t newTimestamp = (CANTimeStamp.micros + 1000 * CANTimeStamp.millis + 0x100000000 * 1000 * CANTimeStamp.millis_overflow);
  335. Notify(CANMsg, newTimestamp);
  336. }
  337. return stsResult;
  338. }
  339. //将PCAN信号传回座椅PLC中
  340. TPCANStatus CControlSensor::SendCANMessage(TPCANMsg* CANMsg)
  341. {
  342. // 执行PCANBasic的“写入”功能
  343. TPCANStatus stsResult = CAN_Write(_handle, CANMsg);
  344. return stsResult;
  345. }
  346. void CControlSensor::Stop()
  347. {
  348. _run = false;
  349. _thread.join();
  350. }
  351. void CControlSensor::SetEngineRPM(int32_t value)
  352. {
  353. _rpm = value;
  354. }