car_sim.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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 "car_sim.h"
  7. #include <random>
  8. #include "control_sensor.h"
  9. #include <chrono>
  10. #include <iomanip>
  11. #include <windows.h>
  12. #include <iomanip>
  13. #include <sstream>
  14. CCarSim::CCarSim(CEgoClient* car) :_car(car)
  15. {
  16. }
  17. void CCarSim::Start()
  18. {
  19. }
  20. void CCarSim::Stop()
  21. {
  22. }
  23. void CCarSim::OnPeerMessage(int16_t cmd, int16_t length, const void* data)
  24. {
  25. switch (cmd)
  26. {
  27. case RemoNet::CC_Ping:
  28. {
  29. RemoNet::CCPing Req;
  30. Req.ParseFromArray(data, length);
  31. int64_t tick = GetTickCount64();
  32. int32_t diff = tick - Req.tick();
  33. double temp = Req.temp();
  34. testPing= Req.tick();
  35. //增加打印日志文件内容
  36. /*
  37. auto now = std::chrono::system_clock::now();
  38. auto time = std::chrono::system_clock::to_time_t(now);
  39. auto localtime = std::localtime(&time);
  40. // 格式化当前系统时间作为文件名
  41. std::ostringstream filenameStream;
  42. filenameStream << "E:\\DY\\EgoDY\\Run\\log_" << std::put_time(localtime, "%Y%m%d_%H%M%S") << ".txt";
  43. filename = filenameStream.str();
  44. // 添加日志记录
  45. std::ofstream logFile("E:\\DY\\EgoDY\\Run\\log.txt", std::ios::app);
  46. //std::ofstream logFile(filename, std::ios::app);
  47. if (logFile.is_open()) {
  48. logFile << "System Time: " << std::put_time(localtime, "%Y-%m-%d %H:%M:%S") << std::endl;
  49. logFile << "Diff: " << diff << " Temp: " << temp << std::endl;
  50. //logFile.close();
  51. }
  52. */
  53. _car->OnPingValue(diff, temp);
  54. }
  55. break;
  56. case RemoNet::CC_StopACK:
  57. {
  58. _car->OnStopAck();
  59. }
  60. case RemoNet::CC_STATE:
  61. {
  62. //创建一个RemoNet::State
  63. RemoNet::State msg;
  64. //用ParseFromArray(data, length)将从data中解析的数据填充到msg对象中
  65. msg.ParseFromArray(data, length);
  66. //_control_sensor->_automous = (msg.left_lock()!=0) ? true:false;//left_lock为1时,切换为遥操,发送报文给下位机
  67. //创建了FeedData类型的数据结构,用于存储从msg中提取的不同数据字段
  68. FeedData data;
  69. data.brake_pressure = msg.brake_pressure();//制动气压
  70. data.engine_rpm = msg.engine_rpm();//发动机转速
  71. data.engine_pressure = msg.engine_pressure();//发动机压力
  72. data.gearbox_oil_pressure = msg.gearbox_oil_pressure();//变速箱油压
  73. data.gearbox_oil_temp = msg.gearbox_oil_temp();//变速箱油温
  74. data.speed = msg.speed();//行驶速度
  75. data.cold_water = msg.cold_water();//发动机水温
  76. data.steer_angle = msg.steer_angle();//车辆姿态???原来是轮子现在定义为铰链
  77. data.left_lock_status = msg.left_lock();
  78. data.right_lock_status = msg.right_lock();
  79. data.work_pressure = msg.work_pressure();
  80. //data.gear = msg.gear();
  81. //发送给qt的信息以下是大冶增加的:
  82. data.engine_time = msg.engine_time();//发动机工作小时
  83. data.wire_sensor_l = msg.wire_sensor_l();//左支腿高度
  84. data.wire_sensor_r = msg.wire_sensor_r();//右支腿高度
  85. //OutputDebugStringA(("===================打印左支腿高度 " + std::to_string(data.wire_sensor_l) + "============\n").c_str());
  86. //OutputDebugStringA(("===================打印右支腿高度 " + std::to_string(data.wire_sensor_r) + "============\n").c_str());
  87. data.fuel_level = msg.fuel_level();//燃油油位油量
  88. //data.lock_switch_l = msg.lock_switch_l();//锁钩到位开关L
  89. //data.lock_switch_r = msg.lock_switch_r();//锁钩到位开关R
  90. //data.tip_limit = msg.tip_limit();//倾翻限位
  91. data.lock_switch_l = msg.outgrigger_l();//左支腿伸到位开关
  92. data.lock_switch_r = msg.outgrigger_r();//右支腿伸到位开关
  93. data.tip_limit = msg.tip_pressure();//倾翻压力开关
  94. data.turn_left = msg.turn_left();//左转
  95. data.turn_right = msg.turn_right();//右转
  96. data.hight_beam = msg.hight_beam();//远光灯
  97. data.low_beam = msg.low_beam();//近光灯
  98. data.parking = msg.parking();//驻车
  99. data.brake = msg.brake();//刹车
  100. data.back_car = msg.back_car();//倒车
  101. data.front_work_lamp = msg.front_work_lamp();//前工作灯
  102. data.rear_work_lamp = msg.rear_work_lamp();//后工作灯
  103. data.cargo_weight = msg.cargo_weight();//渣包重量
  104. data.system_vol = msg.system_vol();//系统电压
  105. data.f_gear = msg.f_gear();//档位信息1=F 2=R 3=N
  106. data.gear_1 = msg.gear_1();//档位1234
  107. //发送信息给PCAN(下位机传的0x280透传)
  108. TPCANMsg* CANMsg = (TPCANMsg*)malloc(sizeof(TPCANMsg));
  109. CANMsg->ID = 0x280;
  110. CANMsg->MSGTYPE = PCAN_MESSAGE_STANDARD;
  111. CANMsg->LEN = msg.error_buff().size();
  112. memcpy(CANMsg->DATA, data.warningData, CANMsg->LEN);
  113. //SendCANMessage(CANMsg);
  114. _car->SendCanDate(CANMsg);
  115. free(CANMsg);
  116. for (int i = 0; i < msg.error_buff().size(); ++i)//报警信息
  117. {
  118. data.warningData[i] = msg.error_buff()[i];
  119. }
  120. //将报警信息解读出来赋值给data中的变量
  121. data.low_oil_pressure_alarm = (data.warningData[0] & 0x1) == 0x1 ? 1 : 0;//机油压力低报警
  122. data.engine_water_temperature_alarm = (data.warningData[0] & 0x2) == 0x2 ? 1 : 0;//发动机水温高报警
  123. data.high_engine_oil_temperature_alarm = (data.warningData[0] & 0x4) == 0x4 ? 1 : 0;//发动机油温高报警
  124. data.engine_water_level_low_alarm = (data.warningData[0] & 0x8) == 0x8 ? 1 : 0;//发动机水位低报警
  125. data.low_fuel_oil_alarm = (data.warningData[0] & 0x10) == 0x10 ? 1 : 0;//燃油油位低报警
  126. data.return_oil_filter_blocked_alarm = (data.warningData[0] & 0x20) == 0x20 ? 1 : 0;//回油滤网堵塞报警
  127. data.oil_filter_blocked_alarm = (data.warningData[0] & 0x40) == 0x40 ? 1 : 0;//吸油滤网堵塞报警
  128. data.control_screen_blockage_alarm = (data.warningData[0] & 0x80) == 0x80 ? 1 : 0;//控制滤网堵塞报警
  129. data.emergency_filter_blocking_alarm = (data.warningData[1] & 0x1) == 0x1 ? 1 : 0;//应急滤网堵塞报警
  130. data.cooling_filter_blocked_alarm = (data.warningData[1] & 0x2) == 0x2 ? 1 : 0;//冷却滤网堵塞报警
  131. data.electric_pump_screen_blocked_alarm = (data.warningData[1] & 0x4) == 0x4 ? 1 : 0;//电泵滤网堵塞报警
  132. data.brake_filter_blocked_alarm = (data.warningData[1] & 0x8) == 0x8 ? 1 : 0;//制动滤网堵塞报警
  133. data.variable_speed_filter_blocked_alarm = (data.warningData[1] & 0x10) == 0x10 ? 1 : 0;//变速滤网堵塞报警
  134. data.low_voltage_alarm = (data.warningData[1] & 0x20) == 0x20 ? 1 : 0;//电压低报警
  135. data.high_transmission_oil_temperature = (data.warningData[2] & 0x1) == 0x1 ? 1 : 0;//变速箱油温高
  136. data.transmission_oil_pressure_is_low = (data.warningData[2] & 0x2) == 0x2 ? 1 : 0;//变速箱油压低
  137. data.oil_temperature_sensor_1_is_faulty = (data.warningData[2] & 0x4) == 0x4 ? 1 : 0; //油温传感器1故障
  138. data.oil_temperature_sensor_2_is_faulty = (data.warningData[2] & 0x8) == 0x8 ? 1 : 0;//油温传感器2故障
  139. data.oil_level_sensor_is_faulty = (data.warningData[2] & 0x10) == 0x10 ? 1 : 0;//油位传感器故障
  140. data.front_throttle_fault = (data.warningData[2] & 0x20) == 0x20 ? 1 : 0;//前油门故障
  141. data.rear_throttle_fault = (data.warningData[2] & 0x40) == 0x40 ? 1 : 0;//后油门故障
  142. data.handle_of_the_upper_arm_is_faulty = (data.warningData[2] & 0x80) == 0x80 ? 1 : 0;//大臂手柄故障
  143. data.left_leg_handle_is_faulty = (data.warningData[3] & 0x8) == 0x8 ? 1 : 0;//左支腿手柄故障
  144. data.right_leg_handle_is_faulty = (data.warningData[3] & 0x10) == 0x10 ? 1 : 0;//右支腿手柄故障
  145. data.engine_bus_fault = (data.warningData[3] & 0x20) == 0x20 ? 1 : 0;//发动机总线故障
  146. data.transmission_bus_fault = (data.warningData[3] & 0x40) == 0x40 ? 1 : 0;//变速箱总线故障
  147. data.remote_control_bus_fault = (data.warningData[3] & 0x80) == 0x80 ? 1 : 0;//遥控器总线故
  148. data.rod_chamber_pressure_sensor_is_faulty = (data.warningData[4] & 0x1) == 0x1 ? 1 : 0;//有杆腔压力传感器故障
  149. data.no_rod_chamber_pressure_sensor_is_faulty = (data.warningData[4] & 0x2) == 0x2 ? 1 : 0;//无杆腔压力传感器故障
  150. data.brake_pressure_sensor_is_faulty = (data.warningData[4] & 0x4) == 0x4 ? 1 : 0;//制动压力传感器故障
  151. data.system_pressure_sensor_is_faulty = (data.warningData[4] & 0x8) == 0x8 ? 1 : 0;//系统压力传感器故障
  152. data.control_pressure_sensor_is_faulty = (data.warningData[4] & 0x10) == 0x10 ? 1 : 0;//控制压力传感器故障
  153. data.steering_pressure_sensor_is_faulty = (data.warningData[4] & 0x20) == 0x20 ? 1 : 0;//转向压力传感器故障
  154. data.pressure_sensor_of_the_lower_arm_is_faulty = (data.warningData[4] & 0x40) == 0x40 ? 1 : 0;//小臂压力传感器故障
  155. data.locking_pin_displacement_sensor_L_is_faulty = (data.warningData[5] & 0x1) == 0x1 ? 1 : 0;//锁销位移传感器L故障
  156. data.locking_pin_displacement_sensor_R_is_faulty = (data.warningData[5] & 0x2) == 0x2 ? 1 : 0;//锁销位移传感器R故障
  157. data.leg_displacement_sensor_L_is_faulty = (data.warningData[5] & 0x4) == 0x4 ? 1 : 0;//支腿位移传感器L故障
  158. data.leg_displacement_sensor_R_is_faulty = (data.warningData[5] & 0x8) == 0x8 ? 1 : 0;//支腿位移传感器R故障
  159. data.steering_encoder_failure = (data.warningData[5] & 0x10) == 0x10 ? 1 : 0;//转向编码器故障
  160. //qqq 发送给QT
  161. _car->OnFeedPage(data);
  162. }
  163. break;
  164. case RemoNet::CC_NDTPOS:
  165. {
  166. RemoNet::NDTPos Req;
  167. Req.ParseFromArray(data, length);
  168. Position pos;
  169. pos.x = Req.x();
  170. pos.y = Req.y();
  171. pos.z = Req.z();
  172. pos.rx = Req.rx();
  173. pos.ry = Req.ry();
  174. pos.rz = Req.rz();
  175. pos.rw = Req.rw();
  176. _car->OnNDTPos(&pos);
  177. }
  178. break;
  179. }
  180. }