control_sensor.cpp 10 KB

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