main.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464
  1. #include <QCoreApplication>
  2. #include <QTime>
  3. #include <QMutex>
  4. #include "control/control_status.h"
  5. #include "control/controller.h"
  6. #include "xmlparam.h"
  7. #include "modulecomm.h"
  8. #include "ivbacktrace.h"
  9. #include "ivversion.h"
  10. #include "canmsg.pb.h"
  11. #include "decition.pb.h"
  12. #include "chassis.pb.h"
  13. #include "torquebrake.h"
  14. #include <thread>
  15. void * gpacansend;
  16. void * gpadecition;
  17. void * gpachassis;
  18. std::string gstrmemdecition;
  19. std::string gstrmemcansend;
  20. std::string gstrmemchassis;
  21. bool gbSendRun = true;
  22. bool gbChassisEPS = false;
  23. iv::brain::decition gdecition_def;
  24. iv::brain::decition gdecition;
  25. QTime gTime;
  26. int gnLastSendTime = 0;
  27. int gnLastRecvDecTime = -1000;
  28. int gnDecitionNum = 0; //when is zero,send default;
  29. const int gnDecitionNumMax = 100;
  30. int gnIndex = 0;
  31. boost::shared_ptr<iv::control::Controller> gcontroller; //实际车辆控制器
  32. static QMutex gMutex;
  33. // signal: @ACC_LatAngReq
  34. #define ECU_144_ACC_LatAngReq_CovFactor (0.1)
  35. // conversion value to CAN signal
  36. #define ECU_144_ACC_LatAngReq_toS(x) ((int16_t)((x) / 0.1 + 7200))
  37. // signal: @ACC_MotorTorqueMaxLimitRequest
  38. #define ECU_144_ACC_MotorTorqueMaxLimitRequest_CovFactor (0.02)
  39. // conversion value to CAN signal
  40. #define ECU_144_ACC_MotorTorqueMaxLimitRequest_toS(x) ((int16_t)((x) / 0.02 + 1024))
  41. // signal: @ACC_MotorTorqueMinLimitRequest
  42. #define ECU_144_ACC_MotorTorqueMinLimitRequest_CovFactor (0.02)
  43. // conversion value to CAN signal
  44. #define ECU_144_ACC_MotorTorqueMinLimitRequest_toS(x) ((int16_t)((x) / 0.02 + 1024))
  45. typedef struct
  46. {
  47. int16_t ACC_LatAngReq;
  48. uint8_t ADS_Reqmode;
  49. int16_t ACC_MotorTorqueMaxLimitRequest;
  50. uint8_t ACC_LatAngReqActive;
  51. int16_t ACC_MotorTorqueMinLimitRequest;
  52. uint8_t ACC_ADCReqType;
  53. } ECU_144_t;
  54. // signal: @ACC_AccTrqReq
  55. #define ECU_24B_ACC_AccTrqReq_CovFactor (1)
  56. // conversion value to CAN signal
  57. #define ECU_24B_ACC_AccTrqReq_toS(x) ((int16_t)((x) + 16384))
  58. // signal: @ACC_ACCTargetAcceleration
  59. #define ECU_24B_ACC_ACCTargetAcceleration_CovFactor (0.05)
  60. // conversion value to CAN signal
  61. #define ECU_24B_ACC_ACCTargetAcceleration_toS(x) ((int16_t)((x) / 0.05 + 200))
  62. // signal: @ACC_AEBTargetDeceleration
  63. #define ECU_24B_ACC_AEBTargetDeceleration_CovFactor (0.0005)
  64. // conversion value to CAN signal
  65. #define ECU_24B_ACC_AEBTargetDeceleration_toS(x) ((int32_t)((x) / 0.0005 + 32000))
  66. typedef struct
  67. {
  68. int16_t ACC_AccTrqReq;
  69. uint8_t ACC_AccTrqReqActive;
  70. int16_t ACC_ACCTargetAcceleration;
  71. int32_t ACC_AEBTargetDeceleration;
  72. uint8_t ACC_AEBVehilceHoldReq;
  73. uint8_t ADCReqMode;
  74. uint8_t ACC_AEBActive;
  75. uint8_t ACC_Driveoff_Request;
  76. uint8_t ACC_DecToStop;
  77. uint8_t ACC_CDDActive;
  78. uint8_t ACC_ACCMode;
  79. } ECU_24B_t;
  80. typedef struct
  81. {
  82. uint8_t ADS_UDLCTurnLightReq;
  83. } ECU_36E_t;
  84. unsigned char byte_144[8];
  85. unsigned char byte_24B[8];
  86. unsigned char byte_36E[8];
  87. ECU_144_t* _m144;
  88. ECU_24B_t* _m24B;
  89. ECU_36E_t* _m36E;
  90. void executeDecition(const iv::brain::decition decition)
  91. {
  92. // std::cout<<"acc is "<<decition.torque()<<" ang is "<<decition.wheelangle()<<std::endl;
  93. _m144->ACC_LatAngReq = ECU_144_ACC_LatAngReq_toS(decition.wheelangle());
  94. _m144->ADS_Reqmode = 2;
  95. _m144->ACC_MotorTorqueMaxLimitRequest = ECU_144_ACC_MotorTorqueMaxLimitRequest_toS(10);
  96. _m144->ACC_LatAngReqActive = 1;
  97. _m144->ACC_MotorTorqueMinLimitRequest = ECU_144_ACC_MotorTorqueMinLimitRequest_toS(-10);
  98. _m144->ACC_ADCReqType = decition.brake_type();//ADC请求类型(制动时为1,其余情况为0)
  99. byte_144[0] |= ((_m144->ACC_LatAngReq >> 9) & (0x1FU)) | ((_m144->ADS_Reqmode & (0x07U)) << 5);
  100. byte_144[1] |= ((_m144->ACC_LatAngReq >> 1) & (0xFFU));
  101. byte_144[2] |= ((_m144->ACC_LatAngReq & (0x01U)) << 7) | ((_m144->ACC_MotorTorqueMaxLimitRequest >> 5) & (0x3FU)) | ((_m144->ACC_LatAngReqActive & (0x01U)) << 6);
  102. byte_144[3] |= ((_m144->ACC_MotorTorqueMaxLimitRequest & (0x1FU)) << 3) | ((_m144->ACC_MotorTorqueMinLimitRequest >> 8) & (0x07U));
  103. byte_144[4] |= (_m144->ACC_MotorTorqueMinLimitRequest & (0xFFU));
  104. byte_144[5] |= ((_m144->ACC_ADCReqType & (0x03U)) << 6);
  105. /*制动过程用的减速度,加速用扭矩*/
  106. _m24B->ACC_AccTrqReq = decition.niuju_y();
  107. _m24B->ACC_AccTrqReqActive = decition.acc_active();
  108. _m24B->ACC_ACCTargetAcceleration = decition.accelerator();
  109. // _m24B->ACC_AEBTargetDeceleration = 0;
  110. // _m24B->ACC_AEBVehilceHoldReq = 0;
  111. // _m24B->ADCReqMode = 0;
  112. // _m24B->ACC_AEBActive = 0;
  113. // _m24B->ACC_Driveoff_Request = 0;
  114. // _m24B->ACC_DecToStop = 0;
  115. _m24B->ACC_CDDActive = decition.brake_active();
  116. _m24B->ACC_ACCMode = 3;
  117. byte_24B[0] |= ((_m24B->ACC_AccTrqReq >> 7) & (0xFFU));
  118. byte_24B[1] |= ((_m24B->ACC_AccTrqReq & (0x7FU)) << 1) | (_m24B->ACC_AccTrqReqActive & (0x01U));
  119. byte_24B[2] |= ((_m24B->ACC_ACCTargetAcceleration >> 3) & (0x1FU));
  120. byte_24B[3] |= ((_m24B->ACC_ACCTargetAcceleration & (0x07U)) << 5) | ((_m24B->ACC_AEBTargetDeceleration >> 15) & (0x01U));
  121. byte_24B[4] |= ((_m24B->ACC_AEBTargetDeceleration >> 7) & (0xFFU));
  122. byte_24B[5] |= ((_m24B->ACC_AEBTargetDeceleration & (0x7FU)) << 1) | (_m24B->ACC_AEBVehilceHoldReq & (0x01U));
  123. byte_24B[6] |= ((_m24B->ADCReqMode & (0x03U)) << 1) | ((_m24B->ACC_AEBActive & (0x01U)) << 3) | ((_m24B->ACC_Driveoff_Request & (0x01U)) << 5) | ((_m24B->ACC_DecToStop & (0x01U)) << 6) | ((_m24B->ACC_CDDActive & (0x01U)) << 7);
  124. byte_24B[7] |= (_m24B->ACC_ACCMode & (0x07U));
  125. if(decition.leftlamp() == true && decition.rightlamp() == false)
  126. _m36E->ADS_UDLCTurnLightReq = 3;
  127. else if(decition.leftlamp() == false && decition.rightlamp() == true)
  128. _m36E->ADS_UDLCTurnLightReq = 4;
  129. else
  130. _m36E->ADS_UDLCTurnLightReq = 0;
  131. byte_36E[0] |= ((_m36E->ADS_UDLCTurnLightReq & (0x07U)) << 5);
  132. #if 0
  133. // std::cout<<"acc is "<<decition.torque()<<" ang is "<<decition.wheelangle()<<std::endl;
  134. gcontroller->control_acc_en(true);
  135. gcontroller->control_angle_enable(true);
  136. gcontroller->control_gear_en(true);
  137. gcontroller->control_speed_limit(30);
  138. gcontroller->control_wheel(decition.wheelangle());
  139. gcontroller->control_angle_speed(decition.wheelspeed());
  140. gcontroller->control_torque(decition.torque());
  141. gcontroller->control_brake(decition.brake());
  142. gcontroller->control_gear(decition.gear());
  143. gcontroller->control_handBrake(decition.handbrake());
  144. gcontroller->control_mode(decition.mode());
  145. // gcontroller->control_mode(1);
  146. gcontroller->control_near_light(decition.dippedlight());
  147. //gcontroller->control_air_enable(decition.air_enable());
  148. //gcontroller->control_air_on(decition.air_on());
  149. //gcontroller->control_air_temp(decition.air_temp());
  150. //gcontroller->control_air_mode(decition.air_mode());
  151. //gcontroller->control_wind_level(decition.wind_level());
  152. gcontroller->control_roof_light(decition.roof_light());
  153. gcontroller->control_home_light(decition.home_light());
  154. //gcontroller->control_air_worktime(decition.air_worktime());
  155. //gcontroller->control_air_offtime(decition.air_offtime());
  156. gcontroller->control_turnsignals(decition.leftlamp(),decition.rightlamp());
  157. gcontroller->control_door(decition.door());
  158. #ifdef TORQUEBRAKETEST
  159. if(gnothavetb < 10)
  160. {
  161. iv::controller::torquebrake xtb;
  162. gMutextb.lock();
  163. xtb.CopyFrom(gtb);
  164. gMutextb.unlock();
  165. if(xtb.enable())
  166. {
  167. gcontroller->control_torque(xtb.torque());
  168. gcontroller->control_brake(xtb.brake());
  169. qDebug("use tb value torque is %f brake is %f",xtb.torque(),xtb.brake());
  170. }
  171. else
  172. {
  173. // qDebug("torquebrake not enable.");
  174. }
  175. gnothavetb++;
  176. }
  177. #endif
  178. //qDebug("door is %d",decition.door());
  179. gcontroller->cmd_checksum(0x10);
  180. gcontroller->cmd_checksum(0x11);
  181. gcontroller->cmd_checksum(0x12);
  182. // qDebug("dangwei is %d mode is %d",decition.gear(),decition.mode());
  183. #endif
  184. }
  185. void UpdateChassis(const char *strdata, const unsigned int nSize, const unsigned int index, const QDateTime *dt, const char *strmemname)
  186. {
  187. (void)index;
  188. (void)dt;
  189. (void)strmemname;
  190. iv::chassis xchassis;
  191. // static int ncount = 0;
  192. if(!xchassis.ParseFromArray(strdata,nSize))
  193. {
  194. std::cout<<"iv::decition::BrainDecition::UpdateChassis ParseFrom Array Error."<<std::endl;
  195. return;
  196. }
  197. if(xchassis.epsmode() == 0)
  198. {
  199. gbChassisEPS = true;
  200. }
  201. }
  202. void ListenDeciton(const char * strdata,const unsigned int nSize,const unsigned int index,const QDateTime * dt,const char * strmemname)
  203. {
  204. (void)index;
  205. (void)dt;
  206. (void)strmemname;
  207. static qint64 oldtime = QDateTime::currentMSecsSinceEpoch();
  208. iv::brain::decition xdecition;
  209. if(!xdecition.ParseFromArray(strdata,nSize))
  210. {
  211. std::cout<<"ListenDecition parse error."<<std::endl;
  212. return;
  213. }
  214. if(xdecition.gear() != 4)
  215. {
  216. qDebug("not D");
  217. }
  218. if((oldtime - QDateTime::currentMSecsSinceEpoch())<-100)qDebug("dection time is %lld diff is %lld ",QDateTime::currentMSecsSinceEpoch(),oldtime - QDateTime::currentMSecsSinceEpoch());
  219. oldtime = QDateTime::currentMSecsSinceEpoch();
  220. gMutex.lock();
  221. gdecition.CopyFrom(xdecition);
  222. gMutex.unlock();
  223. gnDecitionNum = gnDecitionNumMax;
  224. gbChassisEPS = false;
  225. }
  226. void ExecSend()
  227. {
  228. iv::can::canmsg xmsg;
  229. iv::can::canraw xraw;
  230. // unsigned char * strp = (unsigned char *)&(ServiceControlStatus.command10.byte[0]);
  231. // qDebug("%02x %02x %02x %02x %02x %02x %02x %02x",strp[0]
  232. // ,strp[1],strp[2],strp[3],strp[4],strp[5],strp[6],strp[7]);
  233. xraw.set_id(0x144);
  234. xraw.set_data(byte_144,8);
  235. xraw.set_bext(false);
  236. xraw.set_bremote(false);
  237. xraw.set_len(8);
  238. iv::can::canraw * pxraw144 = xmsg.add_rawmsg();
  239. pxraw144->CopyFrom(xraw);
  240. xmsg.set_channel(0);
  241. xmsg.set_index(gnIndex);
  242. xraw.set_id(0x24B);
  243. xraw.set_data(byte_144,8);
  244. xraw.set_bext(false);
  245. xraw.set_bremote(false);
  246. xraw.set_len(8);
  247. iv::can::canraw * pxraw24B = xmsg.add_rawmsg();
  248. pxraw24B->CopyFrom(xraw);
  249. xmsg.set_channel(0);
  250. xmsg.set_index(gnIndex);
  251. xraw.set_id(0x36E);
  252. xraw.set_data(byte_144,8);
  253. xraw.set_bext(false);
  254. xraw.set_bremote(false);
  255. xraw.set_len(8);
  256. iv::can::canraw * pxraw36E = xmsg.add_rawmsg();
  257. pxraw36E->CopyFrom(xraw);
  258. xmsg.set_channel(0);
  259. xmsg.set_index(gnIndex);
  260. // xraw.set_id(ServiceControlStatus.command_ID10);
  261. // xraw.set_data(ServiceControlStatus.command10.byte,8);
  262. // xraw.set_bext(false);
  263. // xraw.set_bremote(false);
  264. // xraw.set_len(8);
  265. // iv::can::canraw * pxraw = xmsg.add_rawmsg();
  266. // pxraw->CopyFrom(xraw);
  267. // xmsg.set_channel(0);
  268. // xmsg.set_index(gnIndex);
  269. // xraw.set_id(ServiceControlStatus.command_ID11);
  270. // xraw.set_data(ServiceControlStatus.command11.byte,8);
  271. // int a = ServiceControlStatus.command11.byte[5]&0x0f;
  272. // if(a != 0x04)
  273. // {
  274. // qDebug("not D.");
  275. // }
  276. // xraw.set_bext(false);
  277. // xraw.set_bremote(false);
  278. // xraw.set_len(8);
  279. // iv::can::canraw * pxraw1 = xmsg.add_rawmsg();
  280. // pxraw1->CopyFrom(xraw);
  281. // xmsg.set_channel(0);
  282. // xmsg.set_index(gnIndex);
  283. // xraw.set_id(ServiceControlStatus.command_ID12);
  284. // xraw.set_data(ServiceControlStatus.command12.byte,8);
  285. // xraw.set_bext(false);
  286. // xraw.set_bremote(false);
  287. // xraw.set_len(8);
  288. // iv::can::canraw * pxraw2 = xmsg.add_rawmsg();
  289. // pxraw2->CopyFrom(xraw);
  290. // xmsg.set_channel(0);
  291. // xmsg.set_index(gnIndex);
  292. gnIndex++;
  293. xmsg.set_mstime(QDateTime::currentMSecsSinceEpoch());
  294. int ndatasize = xmsg.ByteSize();
  295. char * strser = new char[ndatasize];
  296. std::shared_ptr<char> pstrser;
  297. pstrser.reset(strser);
  298. if(xmsg.SerializeToArray(strser,ndatasize))
  299. {
  300. iv::modulecomm::ModuleSendMsg(gpacansend,strser,ndatasize);
  301. }
  302. else
  303. {
  304. std::cout<<"MainWindow::onTimer serialize error."<<std::endl;
  305. }
  306. }
  307. void initial()
  308. {
  309. // ServiceControlStatus.command.byte[0] = 0;
  310. // ServiceControlStatus.command.byte[1] = 0;
  311. // ServiceControlStatus.command.byte[2] = 0;
  312. // ServiceControlStatus.command.byte[3] = 0;
  313. // ServiceControlStatus.command.byte[4] = 0;
  314. // ServiceControlStatus.command.byte[5] = 0;
  315. // ServiceControlStatus.command.byte[6] = 0;
  316. // ServiceControlStatus.command.byte[7] = 0;
  317. for (uint8_t i = 0; i < 8; i++)
  318. {
  319. byte_144[i] = 0;
  320. byte_24B[i] = 0;
  321. byte_36E[i] = 0;
  322. }
  323. }
  324. void sendthread()
  325. {
  326. initial();
  327. iv::brain::decition xdecition;
  328. while(gbSendRun)
  329. {
  330. if(gnDecitionNum <= 0)
  331. {
  332. xdecition.CopyFrom(gdecition_def);
  333. }
  334. else
  335. {
  336. gMutex.lock();
  337. xdecition.CopyFrom(gdecition);
  338. gMutex.unlock();
  339. gnDecitionNum--;
  340. }
  341. executeDecition(xdecition);
  342. if(gbChassisEPS == false) ExecSend();
  343. std::this_thread::sleep_for(std::chrono::milliseconds(10));
  344. }
  345. }
  346. int main(int argc, char *argv[])
  347. {
  348. RegisterIVBackTrace();
  349. showversion("controller_changan_shenlan");
  350. QCoreApplication a(argc, argv);
  351. QString strpath = QCoreApplication::applicationDirPath();
  352. if(argc < 2)
  353. strpath = strpath + "/controller_changan_shenlan.xml";
  354. else
  355. strpath = argv[1];
  356. std::cout<<strpath.toStdString()<<std::endl;
  357. gdecition_def.set_accelerator(-0.5);
  358. // gdecition_def.set_brake(20);
  359. // gdecition_def.set_torque(0);
  360. gdecition_def.set_rightlamp(true);
  361. // gdecition_def.set_doublespark(true);
  362. gdecition_def.set_wheelangle(0);
  363. // gdecition_def.set_mode(1);
  364. // gdecition_def.set_grade(1);
  365. // gdecition_def.set_gear(1);
  366. // gdecition_def.set_engine(0);
  367. // gdecition_def.set_gear(0);
  368. gTime.start();
  369. gcontroller = boost::shared_ptr<iv::control::Controller>(new iv::control::Controller());
  370. iv::xmlparam::Xmlparam xp(strpath.toStdString());
  371. gstrmemcansend = xp.GetParam("cansend","cansend0");
  372. gstrmemdecition = xp.GetParam("dection","deciton");
  373. gstrmemchassis = xp.GetParam("chassismsgname","chassis");
  374. gpacansend = iv::modulecomm::RegisterSend(gstrmemcansend.data(),10000,1);
  375. gpadecition = iv::modulecomm::RegisterRecv(gstrmemdecition.data(),ListenDeciton);
  376. gpachassis = iv::modulecomm::RegisterRecv(gstrmemchassis.data(),UpdateChassis);
  377. #ifdef TORQUEBRAKETEST
  378. EnableTorqueBrakeTest();
  379. #endif
  380. std::thread xthread(sendthread);
  381. return a.exec();
  382. }