car_sim.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  1. #include <stdint.h>
  2. #include "../common/comm.h"
  3. #include "Protocol.pb.h"
  4. #include "car_sim.h"
  5. #include "EgoClient.h"
  6. #include <random>
  7. #include "control_sensor.h"
  8. #include <stdio.h>
  9. #include <stdarg.h>
  10. CCarSim::CCarSim(CEgoClient* car) :_car(car)
  11. {
  12. }
  13. void CCarSim::Start()
  14. {
  15. }
  16. void CCarSim::Stop()
  17. {
  18. }
  19. //接收下位机数据
  20. void CCarSim::OnPeerMessage(int16_t cmd, int16_t length, const void* data)
  21. {
  22. switch (cmd)
  23. {
  24. case RemoNet::CC_Ping:
  25. {
  26. RemoNet::CCPing Req;
  27. Req.ParseFromArray(data, length);
  28. int64_t tick = GetTickCount64();
  29. int32_t diff = tick - Req.tick();
  30. double temp = Req.temp();
  31. _car->OnPingValue(diff,temp);
  32. }
  33. break;
  34. case RemoNet::CC_StopACK:
  35. {
  36. _car->OnStopAck();
  37. }
  38. //接收下位机传上来的数据
  39. case RemoNet::CC_STATE:
  40. {
  41. RemoNet::State msg;
  42. msg.ParseFromArray(data, length);
  43. /*
  44. RemoNet::CCCanMsg msg;
  45. msg.ParseFromArray(data, length);
  46. int32_t count = msg.frams_size();
  47. std::vector<cannet_frame> vec;
  48. for (int32_t i = 0; i < count; i++)
  49. {
  50. const RemoNet::can_net_frame& frame = msg.frams(i);
  51. if (frame.canid() == 0x189)
  52. {
  53. cannet_frame node;
  54. node.canid = frame.canid();
  55. node.dlc = frame.dlc();
  56. memcpy(node.data, frame.data().data(), node.dlc);
  57. }
  58. switch (_car->GetCurrentPage())
  59. {
  60. case PageProp::WorkPage1:
  61. {
  62. switch (frame.canid())
  63. {
  64. case 0x1F0:case 0x2F0:case 0x3F0:case 0x4F0:case 0x1F1:case 0x1F2:case 0x2F1:
  65. case 0x2F2:
  66. {
  67. cannet_frame node;
  68. node.canid = frame.canid();
  69. node.dlc = frame.dlc();
  70. memcpy(node.data, frame.data().data(), node.dlc);
  71. vec.push_back(node);
  72. }
  73. break;
  74. }
  75. }
  76. break;
  77. case PageProp::WorkPage2:
  78. {
  79. switch (frame.canid())
  80. {
  81. case 0x3F2:
  82. case 0x4F2:
  83. {
  84. cannet_frame node;
  85. node.canid = frame.canid();
  86. node.dlc = frame.dlc();
  87. memcpy(node.data, frame.data().data(), node.dlc);
  88. vec.push_back(node);
  89. }
  90. break;
  91. }
  92. }
  93. break;
  94. case PageProp::CalibrationPage:
  95. {
  96. switch (frame.canid())
  97. {
  98. case 0x2F6:case 0x3F6:case 0x4F6:
  99. case 0x1F9:case 0x2F9:case 0x4F9:
  100. case 0x380 + 0x81: //0x401
  101. {
  102. cannet_frame node;
  103. node.canid = frame.canid();
  104. node.dlc = frame.dlc();
  105. memcpy(node.data, frame.data().data(), node.dlc);
  106. vec.push_back(node);
  107. }
  108. break;
  109. }
  110. }
  111. break;
  112. case PageProp::EngineParameterPage:
  113. {
  114. switch (frame.canid())
  115. {
  116. case 0x1F3: case 0x2F3:case 0x3F3:case 0x4F3:
  117. case 0x1F4:case 0x2F4:case 0x3F4: case 0x4F4:
  118. case 0x1F5:
  119. {
  120. cannet_frame node;
  121. node.canid = frame.canid();
  122. node.dlc = frame.dlc();
  123. memcpy(node.data, frame.data().data(), node.dlc);
  124. vec.push_back(node);
  125. }
  126. break;
  127. }
  128. }
  129. break;
  130. case PageProp::WarningPage:
  131. {
  132. switch (frame.canid())
  133. {
  134. case 0x1F6:
  135. {
  136. cannet_frame node;
  137. node.canid = frame.canid();
  138. node.dlc = frame.dlc();
  139. memcpy(node.data, frame.data().data(), node.dlc);
  140. vec.push_back(node);
  141. }
  142. break;
  143. }
  144. }
  145. break;
  146. }
  147. }
  148. */
  149. /*
  150. FeedData data;
  151. data.brake_pressure = msg.brake_pressure();
  152. data.engine_rpm = msg.engine_rpm();
  153. data.engine_pressure = msg.engine_pressure();
  154. //data.gear = msg.gear();
  155. data.gearbox_oil_pressure = msg.gearbox_oil_pressure();
  156. data.gearbox_oil_temp = msg.gearbox_oil_temp();
  157. data.speed = msg.speed();
  158. data.work_pressure = msg.work_pressure();
  159. data.cold_water = msg.cold_water();
  160. data.steer_angle = msg.steer_angle();
  161. data.left_lock_status = msg.left_lock();
  162. data.right_lock_status = msg.right_lock();
  163. */
  164. //定义临时变量用于接收
  165. FeedData data;
  166. data.engine_speed = msg.engine_speed();
  167. data.travel_speed = msg.travel_speed();
  168. data.fuel_level = msg.fuel_level();
  169. data.engine_temperature = msg.engine_temperature();
  170. data.hydraulic_oil_temperature = msg.hydraulic_oil_temperature();
  171. data.main_pump_1_pressure = msg.main_pump_1_pressure();
  172. data.main_pump_2_pressure = msg.main_pump_2_pressure();
  173. data.hand_gear = msg.hand_gear();
  174. data.actual_gear = msg.actual_gear();
  175. data.gripper_height = msg.gripper_height();
  176. data.amplitude = msg.amplitude();
  177. data.boom_angle = msg.boom_angle();
  178. data.stick_angle = msg.stick_angle();
  179. data.idle_protection = msg.idle_protection();
  180. data.front_toggle = msg.front_toggle();
  181. data.back_toggle = msg.back_toggle();
  182. data.interlock = msg.interlock();
  183. data.safety_switch = msg.safety_switch();
  184. data.arm_lift_pilot = msg.arm_lift_pilot();
  185. data.arm_lowering_pilot = msg.arm_lowering_pilot();
  186. data.lever_lifting_pilot = msg.lever_lifting_pilot();
  187. data.rod_lowering_pilot = msg.rod_lowering_pilot();
  188. data.left_rotary_pilot = msg.left_rotary_pilot();
  189. data.right_rotary_pilot = msg.right_rotary_pilot();
  190. data.grab_open_pilot = msg.grab_open_pilot();
  191. data.grab_close_pilot = msg.grab_close_pilot();
  192. data.safety_valves = msg.safety_valves();
  193. int jjj = msg.error_buff().size();
  194. for (int i = 0; i < msg.error_buff().size(); ++i)
  195. {
  196. data.warningData[i] = msg.error_buff()[i];
  197. }
  198. //发送信息给PCAN(下位机传的0x280透传)
  199. //动态分配TPCANMsg结构体大小的内存,将其地址赋给 CANMsg 指针
  200. TPCANMsg* CANMsg = (TPCANMsg*)malloc(sizeof(TPCANMsg));
  201. CANMsg->ID = 0x280;
  202. CANMsg->MSGTYPE = PCAN_MESSAGE_STANDARD;
  203. CANMsg->LEN = msg.error_buff().size();
  204. //将 data.warningData 中的数据复制到 CANMsg->DATA 中
  205. memcpy(CANMsg->DATA, data.warningData, CANMsg->LEN);
  206. //SendCANMessage(CANMsg);
  207. _car->SendCanDate(CANMsg);
  208. //释放之前通过 malloc 分配的 CANMsg 内存空间,确保不会发生内存泄漏
  209. free(CANMsg);
  210. //将报警信息解读出来赋值给data中的变量
  211. data.liftpilotpressuresensor_f = (data.warningData[0] & 0x1) == 0x1 ? 1 : 0;
  212. data.loweringpilotpressuresensor_f = (data.warningData[0] & 0x2) == 0x2 ? 1 : 0;
  213. data.leverliftpilotpressuresensor_f = (data.warningData[0] & 0x4) == 0x4 ? 1 : 0;
  214. data.rodloweringpilotpressurepensor_f = (data.warningData[0] & 0x8) == 0x8 ? 1 : 0;
  215. data.leftturnpilotpressuresensor_f = (data.warningData[0] & 0x10) == 0x10 ? 1 : 0;
  216. data.rightturnpilotpressuresensor_f = (data.warningData[0] & 0x20) == 0x20 ? 1 : 0;
  217. data.legextensionpilotpressuresensor_f = (data.warningData[0] & 0x40) == 0x40 ? 1 : 0;
  218. data.legretractpilotpressuresensor_f = (data.warningData[0] & 0x80) == 0x80 ? 1 : 0;
  219. data.grabopenpilotpressuresensor_f = (data.warningData[1] & 0x1) == 0x1 ? 1 : 0;
  220. data.grabclosurepilotpressuresensor_f = (data.warningData[1] & 0x2) == 0x2 ? 1 : 0;
  221. data.mainpump1pressuresensor_f = (data.warningData[1] & 0x4) == 0x4 ? 1 : 0;
  222. data.mainpump2pressuresensor_f = (data.warningData[1] & 0x8) == 0x8 ? 1 : 0;
  223. data.chamberpressuresensor_f = (data.warningData[1] & 0x10) == 0x10 ? 1 : 0;
  224. data.bucketrodpressuresensor_f = (data.warningData[1] & 0x20) == 0x20 ? 1 : 0;
  225. data.parkingpressuresensor_f = (data.warningData[1] & 0x40) == 0x40 ? 1 : 0;
  226. data.accumulatorpressuresensor_f = (data.warningData[1] & 0x80) == 0x80 ? 1 : 0;
  227. data.gaspedal_f = (data.warningData[2] & 0x1) == 0x1 ? 1 : 0;
  228. data.throttleknob_f = (data.warningData[2] & 0x2) == 0x2 ? 1 : 0;
  229. data.leghandle_f = (data.warningData[2] & 0x4) == 0x4 ? 1 : 0;
  230. data.gripknob_f = (data.warningData[2] & 0x8) == 0x8 ? 1 : 0;
  231. data.hydraulicoiltemperaturesensor_f = (data.warningData[2] & 0x10) == 0x10 ? 1 : 0;
  232. data.fuellevelsensor_f = (data.warningData[2] & 0x20) == 0x20 ? 1 : 0;
  233. data.boomanglesensor_f = (data.warningData[2] & 0x40) == 0x40 ? 1 : 0;
  234. data.bucketlevertiltsensor_f = (data.warningData[2] & 0x80) == 0x80 ? 1 : 0;
  235. data.expansionmodule_f = (data.warningData[3] & 0x1) == 0x1 ? 1 : 0;
  236. data.workingdistanceoverrun_f = (data.warningData[3] & 0x2) == 0x2 ? 1 : 0;
  237. data.lowoillevel_f = (data.warningData[3] & 0x4) == 0x4 ? 1 : 0;
  238. data.batterylowvoltage_f = (data.warningData[3] & 0x8) == 0x8 ? 1 : 0;
  239. data.batteryhighvoltage_f = (data.warningData[3] & 0x10) == 0x10 ? 1 : 0;
  240. data.accumulatorpressure_f = (data.warningData[3] & 0x20) == 0x20 ? 1 : 0;
  241. data.highhydraulicoiltemperature_f = (data.warningData[3] & 0x40) == 0x40 ? 1 : 0;
  242. data.lowoilpressure_f = (data.warningData[3] & 0x80) == 0x80 ? 1 : 0;
  243. data.coolantlevellow_f = (data.warningData[4] & 0x1) == 0x1 ? 1 : 0;
  244. data.coolanttemperaturehigh_f = (data.warningData[4] & 0x2) == 0x2 ? 1 : 0;
  245. data.systemoverload_f = (data.warningData[4] & 0x4) == 0x4 ? 1 : 0;
  246. data.safebypasspressdown_f = (data.warningData[4] & 0x8) == 0x8 ? 1 : 0;
  247. data.enginespn_f = (data.warningData[4] & 0x10) == 0x10 ? 1 : 0;
  248. data.enginewarmup_f = (data.warningData[4] & 0x20) == 0x20 ? 1 : 0;
  249. data.enginewarmuptimeout_f = (data.warningData[4] & 0x40) == 0x40 ? 1 : 0;
  250. data.emergencystoppress_f = (data.warningData[4] & 0x80) == 0x80 ? 1 : 0;
  251. data.leftfrontlegchosed = (data.warningData[6] & 0x1) == 0x1 ? 1 : 0;
  252. data.rightfrontlegchosed = (data.warningData[6] & 0x2) == 0x2 ? 1 : 0;
  253. data.leftrearlegchosed = (data.warningData[6] & 0x4) == 0x4 ? 1 : 0;
  254. data.rightrearlegchosed = (data.warningData[6] & 0x8) == 0x8 ? 1 : 0;
  255. //发送信息给PCAN(下位机传的0x384透传)
  256. TPCANMsg* CANMsg384 = (TPCANMsg*)malloc(sizeof(TPCANMsg));
  257. CANMsg384->ID = 0x384;
  258. CANMsg384->MSGTYPE = PCAN_MESSAGE_STANDARD;
  259. CANMsg384->LEN = msg.error_buff().size();
  260. //将 新增数据添加到 CANMsg384->DATA 中
  261. CANMsg384->DATA[0] = static_cast<BYTE>((msg.function_code() >> 8) & 0xFF);
  262. CANMsg384->DATA[1] = static_cast<BYTE>(msg.function_code() & 0xFF);
  263. CANMsg384->DATA[2] = static_cast<BYTE>((msg.main_add() >> 8) & 0xFF);
  264. CANMsg384->DATA[3] = static_cast<BYTE>(msg.main_add() & 0xFF);
  265. CANMsg384->DATA[4] = static_cast<BYTE>((msg.sub_add() >> 8) & 0xFF);
  266. CANMsg384->DATA[5] = static_cast<BYTE>(msg.sub_add() & 0xFF);
  267. CANMsg384->DATA[6] = static_cast<BYTE>((msg.paramter() >> 8) & 0xFF);
  268. CANMsg384->DATA[7] = static_cast<BYTE>(msg.paramter() & 0xFF);
  269. _car->SendCanDate(CANMsg384);
  270. //释放之前通过 malloc 分配的 CANMsg384 内存空间,确保不会发生内存泄漏
  271. free(CANMsg384);
  272. //发送信息给PCAN(下位机传的0x28B透传)
  273. TPCANMsg* CANMsg28B = (TPCANMsg*)malloc(sizeof(TPCANMsg));
  274. CANMsg28B->ID = 0x28B;
  275. CANMsg28B->MSGTYPE = PCAN_MESSAGE_STANDARD;
  276. CANMsg28B->LEN = msg.error_buff().size();
  277. //将 新增数据添加到 CANMsg28B->DATA 中
  278. CANMsg28B->DATA[0] = static_cast<BYTE>(msg.engine_speed() & 0xFF);
  279. CANMsg28B->DATA[1] = static_cast<BYTE>((msg.engine_speed() >> 8) & 0xFF);
  280. CANMsg28B->DATA[2] = static_cast<BYTE>(msg.engine_spn() & 0xFF);
  281. CANMsg28B->DATA[3] = static_cast<BYTE>((msg.engine_spn() >> 8) & 0xFF);
  282. CANMsg28B->DATA[4] = static_cast<BYTE>(msg.engine_fmi() & 0xFF);
  283. CANMsg28B->DATA[5] = static_cast<BYTE>((msg.engine_fmi() >> 8) & 0xFF);
  284. CANMsg28B->DATA[6] = static_cast<BYTE>(msg.engine_num() & 0xFF);
  285. CANMsg28B->DATA[7] = static_cast<BYTE>((msg.engine_num() >> 8) & 0xFF);
  286. _car->SendCanDate(CANMsg28B);
  287. //释放之前通过 malloc 分配的 CANMsg28B 内存空间,确保不会发生内存泄漏
  288. free(CANMsg28B);
  289. //发送信息给PCAN(下位机传的0x28F透传)
  290. TPCANMsg* CANMsg28F = (TPCANMsg*)malloc(sizeof(TPCANMsg));
  291. CANMsg28F->ID = 0x28F;
  292. CANMsg28F->MSGTYPE = PCAN_MESSAGE_STANDARD;
  293. CANMsg28F->LEN = msg.error_buff().size();
  294. //将 新增数据添加到 CANMsg28F->DATA 中
  295. int8_t data28F[8];
  296. for (int i = 0; i < msg.all_buff().size();i++)
  297. {
  298. data28F[i] = msg.error_buff()[i];
  299. }
  300. memcpy(CANMsg28F->DATA, data28F, CANMsg28F->LEN);
  301. _car->SendCanDate(CANMsg28F);
  302. //释放之前通过 malloc 分配的 CANMsg28F 内存空间,确保不会发生内存泄漏
  303. free(CANMsg28F);
  304. ///**报警信息打印数据*/
  305. char buffer1[64];
  306. std::string ret1;
  307. for (int i = 0; i < 1; i++)
  308. {
  309. int16_t t = int(data.boomanglesensor_f);
  310. sprintf_s(buffer1, "%x 动臂倾角传感器故障", t);
  311. ret1 += buffer1;
  312. ret1 += "\n";
  313. }
  314. OutputDebugStringA(ret1.c_str());
  315. ///**报警信息打印数据*/
  316. char buffer2[64];
  317. std::string ret2;
  318. for (int i = 0; i < 1; i++)
  319. {
  320. int16_t t = int(data.bucketlevertiltsensor_f);
  321. sprintf_s(buffer2, "%x 铲斗倾角传感器故障", t);
  322. ret2 += buffer2;
  323. ret2 += "\n";
  324. }
  325. OutputDebugStringA(ret2.c_str());
  326. //发送信息给qt界面
  327. _car->OnFeedPage(data);
  328. }
  329. break;
  330. case RemoNet::CC_NDTPOS:
  331. {
  332. RemoNet::NDTPos Req;
  333. Req.ParseFromArray(data, length);
  334. Position pos;
  335. pos.x = Req.x();
  336. pos.y = Req.y();
  337. pos.z = Req.z();
  338. pos.rx = Req.rx();
  339. pos.ry = Req.ry();
  340. pos.rz = Req.rz();
  341. pos.rw = Req.rw();
  342. _car->OnNDTPos(&pos);
  343. }
  344. break;
  345. }
  346. }