control_sensor.cpp 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  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. switch (canmsg.ID)
  40. {
  41. case 0x181:
  42. {
  43. /*
  44. std::string str;
  45. char text[32];
  46. for (int i = 0; i < canmsg.LEN; i++)
  47. {
  48. int16_t t = canmsg.DATA[i];
  49. sprintf_s(text, "%x ", t);
  50. str += text;
  51. }
  52. str += "\n";
  53. OutputDebugString(str.c_str());
  54. */
  55. isFront = (canmsg.DATA[1] & 0x1) == 1;
  56. frames[0].canid = canmsg.ID;
  57. frames[0].dlc = canmsg.LEN;
  58. memcpy(&frames[0].data, canmsg.DATA, sizeof(canmsg.DATA));
  59. if (_remote_active)
  60. {
  61. frames[0].data[0] = 0x10;
  62. _remote_active = false;
  63. _active_tick = GetTickCount64();
  64. }
  65. _collection = (Sensor_Collection)(_collection | Sensor_Collection::Sensor_ER);
  66. /*bool enbling = p->data[0] & 0x1;
  67. bool motoring = p->data[0] & 0x2;
  68. bool gear = p->data[0] & 0x4;
  69. bool turnL = p->data[0] & 0x8;
  70. bool turnR = p->data[0] & 0x10;
  71. _window->GetEgoClient()->OnEmergency(enbling, motoring, gear, turnL, turnR);*/
  72. }
  73. break;
  74. case 0x281:
  75. {
  76. /* if (canmsg.DATA[2] == 0x03)
  77. {
  78. if (_rpm >= 300)
  79. {
  80. canmsg.DATA[2] = 0x01;
  81. }
  82. if (_startup == 0)
  83. {
  84. _startup= std::chrono::duration_cast<std::chrono::seconds>(std::chrono::system_clock::now().time_since_epoch()).count();
  85. }
  86. else
  87. {
  88. auto tick= std::chrono::duration_cast<std::chrono::seconds>(std::chrono::system_clock::now().time_since_epoch()).count();
  89. if (tick - _startup > 15)
  90. {
  91. canmsg.DATA[2] = 0x01;
  92. }
  93. }
  94. }
  95. else
  96. {
  97. _startup = 0;
  98. _rpm = 0;
  99. }
  100. */
  101. frames[2].canid = canmsg.ID;
  102. frames[2].dlc = canmsg.LEN;
  103. memcpy(&frames[2].data, canmsg.DATA, sizeof(canmsg.DATA));
  104. _collection = (Sensor_Collection)(_collection | Sensor_Collection::Sensor_Signal);
  105. }
  106. break;
  107. case 0x381:
  108. {
  109. //if (!_automous)
  110. {
  111. frames[3].canid = canmsg.ID;
  112. frames[3].dlc = canmsg.LEN;
  113. /* if (isSliping)
  114. {
  115. canmsg.DATA[0] = 100;
  116. canmsg.DATA[1] = 100;
  117. }
  118. */
  119. memcpy(&frames[3].data, canmsg.DATA, sizeof(canmsg.DATA));
  120. }
  121. /*else
  122. {
  123. frames[3].canid = 0;
  124. }
  125. */
  126. _collection = (Sensor_Collection)(_collection | Sensor_Collection::Sensor_Drive);
  127. /* int16_t accel = p->data[0];
  128. bool lock_on_off = p->data[6] != 0;
  129. _window->GetEgoClient()->OnAccel(accel, lock_on_off);*/
  130. }
  131. break;
  132. case 0x185:
  133. {
  134. // if (!_bflash)
  135. // {
  136. // _bflash = true;
  137. {
  138. //
  139. //if (!_automous)
  140. {
  141. int32_t steer = make_int16(canmsg.DATA[1], canmsg.DATA[0]);
  142. float period = steer - _old_steer;
  143. _old_steer = steer;
  144. float diff = timestamp - _old_timestamp;
  145. diff /= 1000.f;
  146. diff /= 1000.f;
  147. _old_timestamp = timestamp;
  148. //float diff = tick - _cur_tick;
  149. frames[1].canid = canmsg.ID;
  150. frames[1].dlc = 8;
  151. int32_t speed = (period / (diff * 2.0f));
  152. if (speed == 0)
  153. {
  154. auto tick = GetTickCount64();
  155. if (_remote_active == false && tick - _active_tick > 500)
  156. {
  157. _remote_active = true;
  158. }
  159. }
  160. if (_window->GetSteerAngle() < -MAX_DGREE)
  161. {
  162. if (isFront)
  163. {
  164. if (speed < 0)
  165. speed = 0;
  166. }
  167. else
  168. {
  169. if (speed > 0)
  170. speed = 0;
  171. }
  172. }
  173. else if (_window->GetSteerAngle() > MAX_DGREE)
  174. {
  175. if (isFront)
  176. {
  177. if (speed > 0)
  178. speed = 0;
  179. }
  180. else {
  181. if (speed < 0)
  182. speed = 0;
  183. }
  184. }
  185. else
  186. {
  187. if (_last_speed != 0xF000)
  188. {
  189. if ((_last_speed * speed) < 0)
  190. {
  191. _last_speed = speed;
  192. speed = 0;
  193. }
  194. }
  195. else
  196. {
  197. _last_speed = speed;
  198. }
  199. }
  200. int16_t m = abs(speed);
  201. if (m < 40) m = 0;
  202. if (m > 40 && m < 600) m = 600;
  203. //m=std::min(m, 3000);
  204. //_window->GetEgoClient()->OnSteer(m);
  205. if (m > 2500) m = 2500;
  206. //m /= 4; 5haoche shiyong
  207. frames[1].data[0] = 0xFF;
  208. frames[1].data[1] = 0x00;
  209. frames[1].data[2] = hi_byte(m);
  210. frames[1].data[3] = lo_byte(m);
  211. if(isFront)
  212. frames[1].data[4] = speed < 0 ? 1 : 0;
  213. else
  214. frames[1].data[4] = speed < 0 ? 0 : 1;
  215. frames[1].data[4] |= 0x02;
  216. frames[1].data[5] = 0;
  217. frames[1].data[6] = 0;
  218. frames[1].data[7] = 0;
  219. }
  220. /*else
  221. {
  222. frames[1].canid = 0;
  223. }*/
  224. _collection = (Sensor_Collection)(_collection | Sensor_Collection::Sensor_Steer);
  225. }
  226. // }
  227. // memcpy(&frames[1], p, sizeof(cannet_frame));
  228. }
  229. break;
  230. case 0x481:
  231. {
  232. _collection = (Sensor_Collection)(_collection | Sensor_Collection::Sensor_Arm);
  233. frames[4].canid = canmsg.ID;
  234. frames[4].dlc = canmsg.LEN;
  235. int16_t d = canmsg.DATA[0];
  236. int16_t l = canmsg.DATA[2];
  237. int16_t k = canmsg.DATA[4];
  238. isSliping = std::abs(d) > 10 || std::abs(l) >10 ||std::abs(k) > 10;
  239. memcpy(&frames[4].data, canmsg.DATA, sizeof(canmsg.DATA));
  240. //OutputDebugString(ret.c_str());
  241. /*int16_t flip = p->data[0];
  242. int16_t armL = p->data[2];
  243. int16_t armR = p->data[4];
  244. bool jar_on_off = (p->data[6]&0x80) != 0;
  245. _window->GetEgoClient()->OnArm(flip, armL, armR, jar_on_off);
  246. */
  247. }
  248. break;
  249. case 0x701:
  250. //_automous = canmsg.DATA[5] != 0;
  251. frames[5].canid = canmsg.ID;
  252. frames[5].dlc = canmsg.LEN;
  253. memcpy(&frames[5].data, canmsg.DATA, sizeof(canmsg.DATA));
  254. //_collection = (Sensor_Collection)(_collection | Sensor_Collection::Sensor_Switch);
  255. break;
  256. }
  257. if (_window->GetControlState() == ControlState::Process && _collection == Sensor_Collection::Sensor_All)
  258. {
  259. static DWORD tick = GetTickCount();
  260. RemoNet::CCCanMsg req;
  261. for (int32_t i = 0; i < _countof(frames); i++)
  262. {
  263. if (frames[i].canid != 0)
  264. {
  265. RemoNet::can_net_frame* frame = req.add_frams();
  266. frame->set_canid(frames[i].canid);
  267. frame->set_dlc(frames[i].dlc);
  268. frame->set_data(frames[i].data, frames[i].dlc);
  269. }
  270. }
  271. MessageHead Head;
  272. CIOBuffer pBuffer;
  273. Head.Command = RemoNet::CC_CANMSG;
  274. Head.Length = req.ByteSizeLong();
  275. Head.Serialize(pBuffer.Buffer);
  276. auto ptr = pBuffer.Buffer + MessageHead::Size();
  277. req.SerializeToArray(ptr, Head.Length);
  278. pBuffer.Length = MessageHead::Size() + Head.Length;
  279. _window->SendData(pBuffer);
  280. _collection = Sensor_Collection::Sensor_None;
  281. /*DWORD diff = GetTickCount() - tick;
  282. tick = GetTickCount();
  283. std::string str = std::to_string(diff);
  284. str += "\n";
  285. OutputDebugStringA(str.c_str());
  286. */
  287. }
  288. }
  289. ControlStatus CControlSensor::CheckStatus()
  290. {
  291. return ControlStatus::Ok;
  292. }
  293. bool CControlSensor::Start()
  294. {
  295. int iBuffer;
  296. TPCANStatus stsResult;
  297. TPCANHandle _HandlesArray[16];
  298. _HandlesArray[0] = PCAN_USBBUS1;
  299. _HandlesArray[1] = PCAN_USBBUS2;
  300. _HandlesArray[2] = PCAN_USBBUS3;
  301. _HandlesArray[3] = PCAN_USBBUS4;
  302. _HandlesArray[4] = PCAN_USBBUS5;
  303. _HandlesArray[5] = PCAN_USBBUS6;
  304. _HandlesArray[6] = PCAN_USBBUS7;
  305. _HandlesArray[7] = PCAN_USBBUS8;
  306. _HandlesArray[8] = PCAN_USBBUS9;
  307. _HandlesArray[9] = PCAN_USBBUS10;
  308. _HandlesArray[10] = PCAN_USBBUS11;
  309. _HandlesArray[11] = PCAN_USBBUS12;
  310. _HandlesArray[12] = PCAN_USBBUS13;
  311. _HandlesArray[13] = PCAN_USBBUS14;
  312. _HandlesArray[14] = PCAN_USBBUS15;
  313. _HandlesArray[15] = PCAN_USBBUS16;
  314. for (int i = 0; i < (sizeof(_HandlesArray) / sizeof(TPCANHandle)); i++)
  315. {
  316. stsResult = CAN_GetValue(_HandlesArray[i], PCAN_CHANNEL_CONDITION, &iBuffer, sizeof(iBuffer));
  317. if (((stsResult) == PCAN_ERROR_OK) && ((iBuffer & PCAN_CHANNEL_AVAILABLE) == PCAN_CHANNEL_AVAILABLE))
  318. {
  319. stsResult = CAN_GetValue((TPCANHandle)_HandlesArray[i], PCAN_CHANNEL_FEATURES, (void*)&iBuffer, sizeof(iBuffer));
  320. _isFD = (stsResult == PCAN_ERROR_OK) && (iBuffer & FEATURE_FD_CAPABLE);
  321. _handle = _HandlesArray[i];
  322. break;
  323. }
  324. }
  325. if (_handle != -1)
  326. {
  327. TPCANStatus stsResult = CAN_Initialize(_handle, PCAN_BAUD_250K);
  328. if (stsResult != PCAN_ERROR_OK)
  329. {
  330. return false;
  331. }
  332. _thread = std::thread(&CControlSensor::Run, this);
  333. }
  334. return _handle != -1;
  335. }
  336. void CControlSensor::Run()
  337. {
  338. _run = true;
  339. while (_run)
  340. {
  341. for (int32_t i = 0; i < sizeof(frames) / sizeof(cannet_frame); i++)
  342. {
  343. frames[i].canid = 0;
  344. }
  345. Sleep(10);
  346. ReadMessages();
  347. }
  348. CAN_Uninitialize(_handle);
  349. }
  350. void CControlSensor::ReadMessages()
  351. {
  352. TPCANStatus stsResult;
  353. // We read at least one time the queue looking for messages. If a message is found, we look again trying to
  354. // find more. If the queue is empty or an error occurr, we get out from the dowhile statement.
  355. do
  356. {
  357. ReadMessage();
  358. if (stsResult != PCAN_ERROR_OK && stsResult != PCAN_ERROR_QRCVEMPTY)
  359. {
  360. //ShowStatus(stsResult);
  361. return;
  362. }
  363. } while (!(stsResult & PCAN_ERROR_QRCVEMPTY));
  364. }
  365. TPCANStatus CControlSensor::ReadMessage()
  366. {
  367. TPCANMsg CANMsg;
  368. TPCANTimestamp CANTimeStamp;
  369. // We execute the "Read" function of the PCANBasic
  370. TPCANStatus stsResult = CAN_Read(_handle, &CANMsg, &CANTimeStamp);
  371. if (stsResult != PCAN_ERROR_QRCVEMPTY)
  372. { // We process the received message
  373. uint64_t newTimestamp = (CANTimeStamp.micros + 1000 * CANTimeStamp.millis + 0x100000000 * 1000 * CANTimeStamp.millis_overflow);
  374. Notify(CANMsg, newTimestamp);
  375. }
  376. return stsResult;
  377. }
  378. void CControlSensor::Stop()
  379. {
  380. _run = false;
  381. _thread.join();
  382. }
  383. void CControlSensor::SetEngineRPM(int32_t value)
  384. {
  385. _rpm = value;
  386. }