main.cpp 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065
  1. #include <QCoreApplication>
  2. #include <QTime>
  3. #include <QMutex>
  4. #include "xmlparam.h"
  5. #include "modulecomm.h"
  6. #include "ivbacktrace.h"
  7. #include "ivversion.h"
  8. #include "canmsg.pb.h"
  9. #include "decition.pb.h"
  10. #include "chassis.pb.h"
  11. #include "torquebrake.h"
  12. #include <thread>
  13. #include <math.h>
  14. #ifdef Q_OS_LINUX
  15. #include <signal.h>
  16. #endif
  17. #include "candbc.h"
  18. #include "sterraes.h"
  19. std::string gstrdbcpath;
  20. bool gbUseOutDBC;
  21. sterraes * gpsterraes;
  22. static void * gpacansend;
  23. static void * gpadecition;
  24. static void * gpachassis;
  25. static std::string gstrmemdecition;
  26. static std::string gstrmemcansend;
  27. static std::string gstrmemchassis;
  28. static bool gbSendRun = true;
  29. static bool gbChassisEPS = false;
  30. static iv::brain::decition gdecition_def;
  31. static iv::brain::decition gdecition;
  32. static QTime gTime;
  33. static int gnLastSendTime = 0;
  34. static int gnLastRecvDecTime = -1000;
  35. static int gnDecitionNum = 0; //when is zero,send default;
  36. const int gnDecitionNumMax = 100;
  37. static int gnIndex = 0;
  38. static bool gbHaveVehSpd = false;
  39. static double gfVehSpd = 0.0;
  40. static bool gbHaveWheelAngle = false;
  41. static double gfWheelAngle = 0.0;
  42. static double gfWheelSpeedLim = 300; //300 degrees per second
  43. static QMutex gMutex;
  44. static std::thread * gpsendthread = NULL;
  45. unsigned char ADS_EPS_1[24];// 0x195/405
  46. unsigned char ADS_EPS_3[24]; // 0x1BC/444
  47. unsigned char ADS_ONEBOX_1[24];// 0x159/345
  48. unsigned char ADS_VCU_1[24]; // 0x167/359
  49. unsigned char ADS_ONEBOX_2[24];
  50. unsigned char ADS_ONEBOX_3[24];
  51. unsigned char ADS_COM2[8];
  52. unsigned char ADS_COM3[24];
  53. static int gnState = 0; //0 not act 1 act
  54. CANPacker * gpPacker;
  55. std::vector<SignalPackValue> mvectorADSEPS1;
  56. std::vector<SignalPackValue> mvectorADSEPS3;
  57. std::vector<SignalPackValue> mvectorADSONEBOX1;
  58. std::vector<SignalPackValue> mvectorADSVCU1;
  59. double ADS_1_DrvCtrlReq = 0.0;
  60. double ADS_1_CtrlReqMod = 0.0;
  61. double ADS_1_DrvTarTqVld = 0.0;
  62. double ADS_1_DrvTarTqEnable = 0.0;
  63. double ADS_1_DrvTarTq = 0.0;
  64. double ADS_1_TarGearReq = 0.0;
  65. double ADS_1_TarGearReqVld = 0.0;
  66. double ADS_1_GearCtrlEnable = 1.0;
  67. double ADS_1_SteerAgReq = 0.0;
  68. double ADS_1_SteerAgVld = 0.0;
  69. double ADS_1_SteerPilotAgEna = 0.0;
  70. double gfWheelReq = 0.0;
  71. double gfTorqueReq = 0.0;
  72. double gfBrakeReq = 0.0;
  73. double ADS_1_PilotCtrlRepSta = 0.0;
  74. double ADS_1_PilotParkCtrlType = 0.0;
  75. double ADS_1_PilotParkBrkDecTar = 0.0;
  76. double ADS_1_PilotParkCtrlRepMod = 0.0;
  77. double ADS_1_PilotParkBrkDecTarVld = 0.0;
  78. double ADS_1_PilotParkBrkDecTarEnable = 0.0;
  79. double ADS_1_PilotParkDec2StpReq = 0.0;
  80. double ADS_1_PilotParkDriOffReq = 0.0;
  81. double ADS_1_ParkCtrlMod = 0.0;
  82. double ADS_1_ADCCAvl = 0.0;
  83. double ADS_1_StopDist = 0.0;
  84. double FCM_2_AebReqTyp = 0;
  85. double FCM_2_AebTarDec = 0;
  86. double FCM_2_AebTarDecVld = 1.0;
  87. double FCM_2_AwbReq = 0;
  88. double FCM_2_AwbLvl = 0;
  89. double FCM_2_BrkPreFillReq = 0;
  90. double FCM_2_AbaReq = 0;
  91. double FCM_2_AbaLvl = 0;
  92. double FCM_2_Avl = 1;
  93. double FCM_3_PilotParkCtrlRepSta = 0;
  94. double FCM_3_PilotCtrlType = 0;
  95. double FCM_3_PilotkBrkDecTar = 0;
  96. double FCM_3_PilotBrkDecTarVld = 1;
  97. double FCM_3_PilotBrkDecTarReq = 0;
  98. double FCM_3_PilotDec2StpReq = 0;
  99. double FCM_3_PilotDriOffReq = 0;
  100. double CutOutFreshvalues_2CB_S = 0.0;
  101. double CutOutMAC_2CB_S = 0.0;
  102. double FCM_2_SysStatus = 1;
  103. double ADS_2_TTC = 5.11;
  104. double ADS_2_AEBStatus = 0;
  105. double ADS_2_ClosingSpeed = 255;
  106. double ADS_3_ACCSts = 0;
  107. static bool gbSendBrake = false;
  108. static double gfVehAcc = 0.0;
  109. void set_EPS1_signal(std::string strsigname,double value){
  110. gpsterraes->set_EPS1_signal(strsigname,value);
  111. }
  112. void set_EPS3_signal(std::string strsigname,double value){
  113. gpsterraes->set_EPS3_signal(strsigname,value);
  114. }
  115. void set_ONEBOX1_signal(std::string strsigname,double value){
  116. gpsterraes->set_ONEBOX1_signal(strsigname,value);
  117. }
  118. void set_VCU1_signal(std::string strsigname,double value){
  119. gpsterraes->set_VCU1_signal(strsigname,value);
  120. }
  121. void set_ONEBOX2_signal(std::string strsigname,double value){
  122. gpsterraes->set_ONEBOX2_signal(strsigname,value);
  123. }
  124. void set_ONEBOX3_signal(std::string strsigname,double value){
  125. gpsterraes->set_ONEBOX3_signal(strsigname,value);
  126. }
  127. void set_ADSCOM3_Signal(std::string strsigname,double value){
  128. gpsterraes->set_ADSCOM3_signal(strsigname,value);
  129. }
  130. void set_ADSCOM2_Signal(std::string strsigname,double value){
  131. gpsterraes->set_ADSCOM2_signal(strsigname,value);
  132. }
  133. void ExecSend();
  134. int testnum = 0;
  135. int testwheel = 0;
  136. void executeDecition(const iv::brain::decition &decition)
  137. {
  138. double fwheel = decition.wheelangle()*0.9;
  139. if(fwheel<-430)fwheel = 430;
  140. if(fwheel>380)fwheel = 380;
  141. ADS_1_SteerAgReq =fwheel;
  142. // if(testwheel<1000)ADS_1_SteerAgReq = 180;
  143. // else ADS_1_SteerAgReq = -180;
  144. // testwheel++;
  145. // if(testwheel > 2000)testwheel = 0;
  146. // std::cout<<"brake: "<<decition.brake()<<std::endl;
  147. // if((testnum < 1000) || (testnum > 1500))
  148. // {
  149. if(decition.brake()<(-0.0001))
  150. {
  151. ADS_1_DrvTarTq = 0.0;
  152. FCM_3_PilotDriOffReq = 0.0;
  153. FCM_3_PilotDec2StpReq = 1.0;
  154. FCM_3_PilotkBrkDecTar = decition.brake();// 0.0;//decition.brake();
  155. FCM_2_AebTarDec = 0;
  156. FCM_3_PilotBrkDecTarVld = 1.0;
  157. FCM_3_PilotCtrlType = 0.0;
  158. FCM_3_PilotBrkDecTarReq = 1.0;
  159. FCM_2_AebTarDecVld = 1.0;
  160. FCM_3_PilotParkCtrlRepSta = 1.0;
  161. if(gbSendBrake == false)
  162. {
  163. FCM_3_PilotDec2StpReq = 0.0;
  164. FCM_3_PilotBrkDecTarReq = 0.0;
  165. FCM_3_PilotBrkDecTarVld = 1.0;
  166. FCM_3_PilotkBrkDecTar = 0.0;
  167. FCM_2_AebTarDec = 1.0;
  168. FCM_3_PilotParkCtrlRepSta = 0.0;
  169. FCM_2_AebTarDecVld = 1.0;
  170. if(fabs(gfVehSpd)>0.1)
  171. {
  172. std::cout<<std::chrono::system_clock::now().time_since_epoch().count()<<" Warning: can't brake."<<std::endl;
  173. }
  174. }
  175. // ADS_1_PilotParkCtrlRepMod = 1.0;
  176. // std::cout<<" send brake "<<std::endl;
  177. }
  178. else
  179. {
  180. if(fabs(gfVehSpd) < 0.1)
  181. {
  182. FCM_3_PilotDriOffReq = 1.0;
  183. FCM_3_PilotDec2StpReq = 0.0;
  184. FCM_3_PilotBrkDecTarReq = 1.0;
  185. FCM_3_PilotBrkDecTarVld = 1.0;
  186. FCM_3_PilotkBrkDecTar = 1.0;
  187. FCM_2_AebTarDec = 1.0;
  188. FCM_3_PilotParkCtrlRepSta = 1.0;
  189. // ADS_1_PilotParkCtrlRepMod = 1.0;
  190. }
  191. else
  192. {
  193. FCM_3_PilotDec2StpReq = 0.0;
  194. FCM_3_PilotBrkDecTarReq = 0.0;
  195. FCM_3_PilotBrkDecTarVld = 1.0;
  196. FCM_3_PilotkBrkDecTar = 0.0;
  197. FCM_2_AebTarDec = 1.0;
  198. FCM_3_PilotParkCtrlRepSta = 0.0;
  199. }
  200. FCM_2_AebTarDecVld = 1.0;
  201. ADS_1_DrvTarTq = decition.torque();
  202. }
  203. // }
  204. // else
  205. // {
  206. // if(fabs(gfVehSpd) < 0.1)
  207. // {
  208. // FCM_3_PilotDriOffReq = 1.0;
  209. // FCM_3_PilotDec2StpReq = 0.0;
  210. // FCM_3_PilotBrkDecTarReq = 1.0;
  211. // FCM_3_PilotBrkDecTarVld = 1.0;
  212. // FCM_3_PilotkBrkDecTar = 1.0;
  213. // FCM_2_AebTarDec = 1.0;
  214. // FCM_3_PilotParkCtrlRepSta = 1.0;
  215. // }
  216. // else
  217. // {
  218. // FCM_3_PilotDec2StpReq = 0.0;
  219. // FCM_3_PilotBrkDecTarReq = 0.0;
  220. // FCM_3_PilotBrkDecTarVld = 1.0;
  221. // FCM_3_PilotkBrkDecTar = 0.0;
  222. // FCM_2_AebTarDec = 1.0;
  223. // FCM_3_PilotParkCtrlRepSta = 0.0;
  224. // }
  225. // ADS_1_DrvTarTq = 300;
  226. // FCM_2_AebTarDecVld = 1.0;
  227. // }
  228. // testnum++;
  229. // if(testnum > 1500)testnum = 0;
  230. // std::cout<<" send dec. "<<std::endl;
  231. // ADS_1_SteerAgReq = 90.0;//decition.wheelangle();
  232. }
  233. void Activate()
  234. {
  235. // for(int j=0;j<100000;j++)
  236. // {
  237. std::cout<<" activate "<<std::endl;
  238. for(int i = 0; i < 3; i++){
  239. ADS_1_DrvCtrlReq = 1.0;
  240. ADS_1_CtrlReqMod = 1.0; //1 Pilot 2 Park
  241. ADS_1_DrvTarTqVld = 1.0;
  242. ADS_1_DrvTarTqEnable = 1.0;
  243. ADS_1_DrvTarTq = 0.0;
  244. ADS_1_TarGearReq = 4.0; //1 P 4 D
  245. ADS_1_TarGearReqVld = 1.0;
  246. ADS_1_GearCtrlEnable = 0.0;
  247. ADS_1_SteerAgReq = gfWheelReq;
  248. ADS_1_SteerAgVld = 1.0;
  249. ADS_1_SteerPilotAgEna = 2.0;
  250. ADS_1_PilotParkDec2StpReq = 1.0;
  251. ADS_1_ParkCtrlMod = 1.0;
  252. ADS_1_ADCCAvl = 1.0;
  253. FCM_2_Avl = 1.0;
  254. ADS_3_ACCSts = 3.0;
  255. FCM_2_SysStatus = 0;
  256. ADS_2_AEBStatus = 1;
  257. // std::cout<<"activate."<<std::endl;
  258. ExecSend();
  259. std::this_thread::sleep_for(std::chrono::milliseconds(10));
  260. }
  261. // }
  262. }
  263. void UnAcitvate()
  264. {
  265. if(fabs(gfVehSpd)<0.1)
  266. {
  267. for(int i = 0; i < 3; i++){
  268. ADS_1_DrvCtrlReq = 1.0;
  269. ADS_1_CtrlReqMod = 2.0;
  270. ADS_1_DrvTarTqVld = 1.0;
  271. ADS_1_DrvTarTqEnable = 1.0;
  272. ADS_1_DrvTarTq = 0.0;
  273. ADS_1_TarGearReq = 1.0;
  274. ADS_1_TarGearReqVld = 1.0;
  275. ADS_1_GearCtrlEnable = 0.0;
  276. ADS_1_SteerAgReq = 0.0;
  277. ADS_1_SteerAgVld = 1.0;
  278. ADS_1_SteerPilotAgEna = 1.0;
  279. ExecSend();
  280. std::this_thread::sleep_for(std::chrono::milliseconds(10));
  281. }
  282. }
  283. for(int i = 0; i < 3; i++){
  284. ADS_1_DrvCtrlReq = 0.0;
  285. ADS_1_CtrlReqMod = 0.0;
  286. ADS_1_DrvTarTqVld = 0.0;
  287. ADS_1_DrvTarTqEnable = 0.0;
  288. ADS_1_DrvTarTq = 0.0;
  289. // ADS_1_TarGearReq = 1.0;
  290. ADS_1_TarGearReqVld = 0.0;
  291. ADS_1_GearCtrlEnable = 1.0;
  292. ADS_1_SteerAgReq = 0.0;
  293. ADS_1_SteerAgVld = 0.0;
  294. ADS_1_SteerPilotAgEna = 0.0;
  295. ADS_1_PilotParkDec2StpReq = 0.0;
  296. ADS_1_ParkCtrlMod = 0.0;
  297. ADS_1_ADCCAvl = 0.0;
  298. ADS_3_ACCSts = 0;
  299. FCM_2_Avl = 1.0;
  300. FCM_2_SysStatus = 1;
  301. ADS_2_AEBStatus = 0;
  302. ExecSend();
  303. std::this_thread::sleep_for(std::chrono::milliseconds(10));
  304. }
  305. }
  306. void UpdateChassis(const char *strdata, const unsigned int nSize, const unsigned int index, const QDateTime *dt, const char *strmemname)
  307. {
  308. (void)index;
  309. (void)dt;
  310. (void)strmemname;
  311. iv::chassis xchassis;
  312. // static int ncount = 0;
  313. if(!xchassis.ParseFromArray(strdata,nSize))
  314. {
  315. std::cout<<"iv::decition::BrainDecition::UpdateChassis ParseFrom Array Error."<<std::endl;
  316. return;
  317. }
  318. if(xchassis.has_epsmode())
  319. {
  320. if(xchassis.epsmode() == 0)
  321. {
  322. gbChassisEPS = true;
  323. }
  324. }
  325. if(xchassis.has_vel())
  326. {
  327. gfVehSpd = xchassis.vel();
  328. gbHaveVehSpd = true;
  329. // std::cout<<" gf Veh speed : "<<gfVehSpd<<std::endl;
  330. }
  331. if(xchassis.has_angle_feedback())
  332. {
  333. gfWheelAngle = xchassis.angle_feedback();
  334. gbHaveWheelAngle = true;
  335. }
  336. }
  337. void ListenDeciton(const char * strdata,const unsigned int nSize,const unsigned int index,const QDateTime * dt,const char * strmemname)
  338. {
  339. (void)index;
  340. (void)dt;
  341. (void)strmemname;
  342. static qint64 oldtime = QDateTime::currentMSecsSinceEpoch();
  343. iv::brain::decition xdecition;
  344. if(!xdecition.ParseFromArray(strdata,nSize))
  345. {
  346. std::cout<<"ListenDecition parse error."<<std::endl;
  347. return;
  348. }
  349. // if(xdecition.gear() != 4)
  350. // {
  351. // qDebug("not D");
  352. // }
  353. xdecition.set_angle_mode(1);
  354. xdecition.set_angle_active(1);
  355. xdecition.set_acc_active(1);
  356. xdecition.set_brake_active(1);
  357. // xdecition.set_brake_type(1);
  358. xdecition.set_auto_mode(3);
  359. // std::cout<<"decition acc: "<<xdecition.accelerator()<<std::endl;
  360. // xdecition.set_wheelangle(45.0);
  361. if((oldtime - QDateTime::currentMSecsSinceEpoch())<-100)qDebug("dection time is %lld diff is %lld ",QDateTime::currentMSecsSinceEpoch(),oldtime - QDateTime::currentMSecsSinceEpoch());
  362. oldtime = QDateTime::currentMSecsSinceEpoch();
  363. gMutex.lock();
  364. gdecition.CopyFrom(xdecition);
  365. gMutex.unlock();
  366. gnDecitionNum = gnDecitionNumMax;
  367. gbChassisEPS = false;
  368. }
  369. void PrepareMsg()
  370. {
  371. static int rollcouter = 0;
  372. // std::cout<<" roll count:: "<<rollcouter<<std::endl;
  373. set_EPS1_signal("ADS_1_RollgCntr1",rollcouter);
  374. set_EPS1_signal("ADS_1_Resd1",0.0);
  375. set_EPS1_signal("ADS_1_SteerAgReq",ADS_1_SteerAgReq);
  376. set_EPS1_signal("ADS_1_SteerAgVld",ADS_1_SteerAgVld);
  377. set_EPS1_signal("ADS_1_SteerPilotAgEna",ADS_1_SteerPilotAgEna);
  378. set_EPS1_signal("ADS_1_RollgCntr2",rollcouter);
  379. set_EPS1_signal("ADS_1_Resd2",0.0);
  380. set_EPS1_signal("ADS_1_SteerTqEna",1.0);
  381. set_EPS1_signal("ADS_1_LdwWarningCmd",0.0);
  382. set_EPS1_signal("ADS_1_LdwWarningCmdVld",2.0);
  383. set_EPS1_signal("ADS_1_SteerMaxTqReq",60.0);
  384. set_EPS1_signal("ADS_1_SteerMinTqReq",-60.0);
  385. set_EPS1_signal("ADS_1_ADSHealthSts",1.0);
  386. set_EPS1_signal("CutOutFreshvalues_2CB_S",1.0);
  387. set_EPS1_signal("CutOutMAC_2CB_S",1.0);
  388. gpsterraes->GetEPS1Data(ADS_EPS_1);
  389. set_VCU1_signal("ADS_1_RollgCntr1",rollcouter);
  390. set_VCU1_signal("ADS_1_Resd1",0);
  391. set_VCU1_signal("ADS_1_DrvTarTq",ADS_1_DrvTarTq);
  392. set_VCU1_signal("ADS_1_DrvTarTqVld",ADS_1_DrvTarTqVld);
  393. set_VCU1_signal("ADS_1_DrvCtrlReq",ADS_1_DrvCtrlReq);
  394. set_VCU1_signal("ADS_1_CtrlReqMod",ADS_1_CtrlReqMod);
  395. set_VCU1_signal("ADS_1_DrvTarTqEnable",ADS_1_DrvTarTqEnable);
  396. set_VCU1_signal("ADS_1_RollgCntr2",rollcouter);
  397. set_VCU1_signal("ADS_1_Resd2",0);
  398. // set_VCU1_signal("ADS_1_TarGearReq",ADS_1_TarGearReq);
  399. // set_VCU1_signal("ADS_1_TarGearReqVld",ADS_1_TarGearReqVld);
  400. // set_VCU1_signal("ADS_1_GearCtrlEnable",ADS_1_GearCtrlEnable);
  401. set_VCU1_signal("ADS_1_ADCCAvl",ADS_1_ADCCAvl);
  402. gpsterraes->GetVCU1Data(ADS_VCU_1);
  403. // set_ONEBOX1_signal("ADS_1_RollgCntr1",rollcouter);
  404. // set_ONEBOX1_signal("ADS_1_Resd1",0);
  405. // set_ONEBOX1_signal("ADS_1_PilotCtrlRepSta",ADS_1_PilotCtrlRepSta);
  406. // set_ONEBOX1_signal("ADS_1_PilotParkCtrlType",ADS_1_PilotParkCtrlType);
  407. // set_ONEBOX1_signal("ADS_1_PilotParkBrkDecTar",ADS_1_PilotParkBrkDecTar);
  408. // set_ONEBOX1_signal("ADS_1_PilotParkCtrlRepMod",ADS_1_PilotParkCtrlRepMod);
  409. // set_ONEBOX1_signal("ADS_1_PilotParkBrkDecTarVld",ADS_1_PilotParkBrkDecTarVld);
  410. // set_ONEBOX1_signal("ADS_1_PilotParkBrkDecTarEnable",ADS_1_PilotParkBrkDecTarEnable);
  411. // set_ONEBOX1_signal("ADS_1_PilotParkDec2StpReq",ADS_1_PilotParkDec2StpReq);
  412. // set_ONEBOX1_signal("ADS_1_PilotParkDriOffReq",ADS_1_PilotParkDriOffReq);
  413. // set_ONEBOX1_signal("ADS_1_ParkCtrlMod",ADS_1_ParkCtrlMod);
  414. // set_ONEBOX1_signal("ADS_1_PilotCtrlRepSta",ADS_1_PilotCtrlRepSta);
  415. // set_ONEBOX1_signal("ADS_1_StopDist",ADS_1_StopDist);
  416. // set_ONEBOX1_signal("ADS_1_PilotParkCtrlRepMod",ADS_1_PilotParkCtrlRepMod);
  417. // gpsterraes->GetONEBOX1Data(ADS_ONEBOX_1);
  418. set_ONEBOX2_signal("FCM_2_RollgCntr1",rollcouter);
  419. set_ONEBOX2_signal("FCM_2_Resd1",rollcouter);
  420. set_ONEBOX2_signal("FCM_2_AebReqTyp",FCM_2_AebReqTyp);
  421. set_ONEBOX2_signal("FCM_2_AebTarDec",FCM_2_AebTarDec);
  422. set_ONEBOX2_signal("FCM_2_AebTarDecVld",FCM_2_AebTarDecVld);
  423. set_ONEBOX2_signal("FCM_2_AwbReq",FCM_2_AwbReq);
  424. set_ONEBOX2_signal("FCM_2_AwbLvl",FCM_2_AwbLvl);
  425. set_ONEBOX2_signal("FCM_2_BrkPreFillReq",FCM_2_BrkPreFillReq);
  426. set_ONEBOX2_signal("FCM_2_AbaReq",FCM_2_AbaReq);
  427. set_ONEBOX2_signal("FCM_2_AbaLvl",FCM_2_AbaLvl);
  428. set_ONEBOX2_signal("FCM_2_Avl",FCM_2_Avl);
  429. gpsterraes->GetONEBOX2Data(ADS_ONEBOX_2);
  430. set_ONEBOX3_signal("FCM_3_RollgCntr1",rollcouter);
  431. set_ONEBOX3_signal("FCM_3_Resd1",rollcouter);
  432. set_ONEBOX3_signal("FCM_3_PilotParkCtrlRepSta",FCM_3_PilotParkCtrlRepSta);
  433. set_ONEBOX3_signal("FCM_3_PilotCtrlType",FCM_3_PilotCtrlType);
  434. set_ONEBOX3_signal("FCM_3_PilotkBrkDecTar",FCM_3_PilotkBrkDecTar);
  435. set_ONEBOX3_signal("FCM_3_PilotBrkDecTarVld",FCM_3_PilotBrkDecTarVld);
  436. set_ONEBOX3_signal("FCM_3_PilotBrkDecTarReq",FCM_3_PilotBrkDecTarReq);
  437. set_ONEBOX3_signal("FCM_3_PilotDec2StpReq",FCM_3_PilotDec2StpReq);
  438. set_ONEBOX3_signal("FCM_3_PilotDriOffReq",FCM_3_PilotDriOffReq);
  439. set_ONEBOX3_signal("CutOutFreshvalues_2CB_S",CutOutFreshvalues_2CB_S);
  440. set_ONEBOX3_signal("CutOutMAC_2CB_S",CutOutMAC_2CB_S);
  441. gpsterraes->GetONEBOX3Data(ADS_ONEBOX_3);
  442. set_ADSCOM3_Signal("ADS_3_RollgCntr1",rollcouter);
  443. set_ADSCOM3_Signal("ADS_3_ACCSts",ADS_3_ACCSts);
  444. gpsterraes->GetADSCOM3Data(ADS_COM3);
  445. set_ADSCOM2_Signal("ADS_2_RollgCntr1",rollcouter);
  446. set_ADSCOM2_Signal("ADS_2_AEBStatus",ADS_2_AEBStatus);
  447. set_ADSCOM2_Signal("ADS_2_TTC",ADS_2_TTC);
  448. set_ADSCOM2_Signal("FCM_2_SysStatus",FCM_2_SysStatus);
  449. gpsterraes->GetADSCOM2Data(ADS_COM2);
  450. rollcouter++;
  451. if(rollcouter>=14)rollcouter = 0;
  452. }
  453. void ExecSend()
  454. {
  455. static int nrd = 0;
  456. PrepareMsg();
  457. // gpsterraes->GetEPS1Data(ADS_EPS_1);
  458. static int nCount = 0;
  459. nCount++;
  460. iv::can::canmsg xmsg;
  461. iv::can::canraw xraw;
  462. // unsigned char * strp = (unsigned char *)&(ServiceControlStatus.command10.byte[0]);
  463. // qDebug("%02x %02x %02x %02x %02x %02x %02x %02x",strp[0],strp[1],strp[2],strp[3],strp[4],strp[5],strp[6],strp[7]);
  464. xraw.set_id(0x195);
  465. xraw.set_data(ADS_EPS_1,24);
  466. xraw.set_bext(false);
  467. xraw.set_bremote(false);
  468. xraw.set_len(24);
  469. iv::can::canraw * pxraw195 = xmsg.add_rawmsg();
  470. pxraw195->CopyFrom(xraw);
  471. // qDebug(" 0x144: %02X %02X %02X %02X %02X %02X %02X %02X",byte_144[0],byte_144[1],byte_144[2],byte_144[3],
  472. // byte_144[4],byte_144[5],byte_144[6],byte_144[7]);
  473. xmsg.set_channel(0);
  474. xmsg.set_index(gnIndex);
  475. xraw.set_id(0x1BC);
  476. xraw.set_data(ADS_EPS_3,24);
  477. xraw.set_bext(false);
  478. xraw.set_bremote(false);
  479. xraw.set_len(24);
  480. xmsg.set_channel(0);
  481. // iv::can::canraw * pxraw1BC = xmsg.add_rawmsg();
  482. // pxraw1BC->CopyFrom(xraw);
  483. xraw.set_id(0x159);
  484. xraw.set_data(ADS_ONEBOX_1,24);
  485. xraw.set_bext(false);
  486. xraw.set_bremote(false);
  487. xraw.set_len(24);
  488. // iv::can::canraw * pxraw159 = xmsg.add_rawmsg();
  489. // pxraw159->CopyFrom(xraw);
  490. xraw.set_id(0x145);
  491. xraw.set_data(ADS_ONEBOX_2,24);
  492. xraw.set_bext(false);
  493. xraw.set_bremote(false);
  494. xraw.set_len(24);
  495. iv::can::canraw * pxraw145 = xmsg.add_rawmsg();
  496. pxraw145->CopyFrom(xraw);
  497. if(nrd == 0)
  498. {
  499. xraw.set_id(0x31A);
  500. xraw.set_data(ADS_COM3,24);
  501. xraw.set_bext(false);
  502. xraw.set_bremote(false);
  503. xraw.set_len(24);
  504. iv::can::canraw * pxraw31A = xmsg.add_rawmsg();
  505. pxraw31A->CopyFrom(xraw);
  506. xraw.set_id(0x314);
  507. xraw.set_data(ADS_COM2,8);
  508. xraw.set_bext(false);
  509. xraw.set_bremote(false);
  510. xraw.set_len(8);
  511. iv::can::canraw * pxraw314 = xmsg.add_rawmsg();
  512. pxraw314->CopyFrom(xraw);
  513. }
  514. xraw.set_id(0x14A);
  515. xraw.set_data(ADS_ONEBOX_3,24);
  516. xraw.set_bext(false);
  517. xraw.set_bremote(false);
  518. xraw.set_len(24);
  519. iv::can::canraw * pxraw14A = xmsg.add_rawmsg();
  520. pxraw14A->CopyFrom(xraw);
  521. xraw.set_id(0x167);
  522. xraw.set_data(ADS_VCU_1,24);
  523. xraw.set_bext(false);
  524. xraw.set_bremote(false);
  525. xraw.set_len(24);
  526. iv::can::canraw * pxraw167 = xmsg.add_rawmsg();
  527. pxraw167->CopyFrom(xraw);
  528. xmsg.set_channel(0);
  529. xmsg.set_index(gnIndex);
  530. gnIndex++;
  531. xmsg.set_mstime(QDateTime::currentMSecsSinceEpoch());
  532. int ndatasize = xmsg.ByteSize();
  533. char * strser = new char[ndatasize];
  534. std::shared_ptr<char> pstrser;
  535. pstrser.reset(strser);
  536. if(xmsg.SerializeToArray(strser,ndatasize))
  537. {
  538. iv::modulecomm::ModuleSendMsg(gpacansend,strser,ndatasize);
  539. }
  540. else
  541. {
  542. std::cout<<"MainWindow::onTimer serialize error."<<std::endl;
  543. }
  544. nrd++;
  545. if(nrd == 5)nrd = 0;
  546. }
  547. void initial()
  548. {
  549. for (uint8_t i = 0; i < 24; i++) //CAN to canfd
  550. {
  551. //byte_36E[i] = 0;
  552. }
  553. }
  554. void SendEPS3()
  555. {
  556. static int rollcouter = 0;
  557. gpsterraes->GetEPS3Data(ADS_EPS_3);
  558. ExecSend();
  559. rollcouter++;
  560. if(rollcouter>14)rollcouter = 0;
  561. }
  562. void SendEPS1()
  563. {
  564. static int rollcouter = 0;
  565. // std::cout<<" roll count:: "<<rollcouter<<std::endl;
  566. set_EPS1_signal("ADS_1_RollgCntr1",rollcouter);
  567. set_EPS1_signal("ADS_1_Resd1",0.0);
  568. gpsterraes->GetEPS1Data(ADS_EPS_1);
  569. ExecSend();
  570. rollcouter++;
  571. if(rollcouter>14)rollcouter = 0;
  572. }
  573. void testes()
  574. {
  575. int i = 0;
  576. int rollcouter = 0;
  577. double fwheelang = 90.0;
  578. set_EPS1_signal("ADS_1_RollgCntr1",rollcouter);
  579. set_EPS1_signal("ADS_1_Resd1",0.0);
  580. set_EPS1_signal("ADS_1_SteerAgReq",fwheelang);
  581. set_EPS1_signal("ADS_1_SteerAgVld",1.0);
  582. set_EPS1_signal("ADS_1_SteerPilotAgEna",0.0);
  583. set_EPS1_signal("ADS_1_RollgCntr2",rollcouter);
  584. set_EPS1_signal("ADS_1_Resd2",0.0);
  585. set_EPS1_signal("ADS_1_SteerTqEna",1.0);
  586. set_EPS1_signal("ADS_1_LdwWarningCmd",0.0);
  587. set_EPS1_signal("ADS_1_LdwWarningCmdVld",2.0);
  588. set_EPS1_signal("ADS_1_SteerMaxTqReq",10.0);
  589. set_EPS1_signal("ADS_1_SteerMinTqReq",1.0);
  590. set_EPS1_signal("ADS_1_ADSHealthSts",1.0);
  591. set_EPS1_signal("CutOutFreshvalues_2CB_S",1.0);
  592. set_EPS1_signal("CutOutMAC_2CB_S",1.0);
  593. set_EPS3_signal("ADS_3_RollgCntr1",rollcouter);
  594. set_EPS3_signal("ADS_3_Resd1",0.0);
  595. set_EPS3_signal("ADS_3_SteerParkAgReq",0.0);
  596. set_EPS3_signal("ADS_3_SteerParkAgVld",0.0);
  597. set_EPS3_signal("ADS_3_SteerParkAgEna",0.0);
  598. set_EPS3_signal("ADS_3_RollgCntr2",rollcouter);
  599. set_EPS3_signal("ADS_3_Resd2",0.0);
  600. set_EPS3_signal("ADS_3_ParkFcnMode",0.0);
  601. set_EPS3_signal("ADS_3_ADSParkHealthSts",0.0);
  602. for(i=0;i<10;i++)
  603. {
  604. set_EPS1_signal("ADS_1_SteerPilotAgEna",0.0);
  605. SendEPS1();
  606. std::this_thread::sleep_for(std::chrono::milliseconds(10));
  607. }
  608. // for(i=0;i<10;i++)
  609. // {
  610. // set_EPS3_signal("ADS_3_SteerParkAgEna",1.0);
  611. // set_EPS3_signal("ADS_3_SteerParkAgReq",0.0);
  612. // set_EPS3_signal("ADS_3_SteerParkAgVld",0.0);
  613. // SendEPS3();
  614. // std::this_thread::sleep_for(std::chrono::milliseconds(10));
  615. // }
  616. for(i=0;i<3000;i++)
  617. {
  618. set_EPS1_signal("ADS_1_SteerPilotAgEna",2.0);
  619. set_EPS1_signal("ADS_1_SteerAgReq",fwheelang);
  620. set_EPS1_signal("ADS_1_SteerAgVld",1.0);
  621. SendEPS1();
  622. std::this_thread::sleep_for(std::chrono::milliseconds(10));
  623. }
  624. for(i=0;i<10;i++)
  625. {
  626. set_EPS1_signal("ADS_1_SteerPilotAgEna",0.0);
  627. SendEPS1();
  628. std::this_thread::sleep_for(std::chrono::milliseconds(10));
  629. }
  630. }
  631. void sendthread()
  632. {
  633. initial();
  634. iv::brain::decition xdecition;
  635. UnAcitvate();
  636. // UnAcitvate();
  637. int nstate = 0; //0 Un 1 Activate
  638. // Activate();
  639. while(gbSendRun)
  640. {
  641. if(gnDecitionNum <= 0)
  642. {
  643. if(nstate == 1)
  644. {
  645. UnAcitvate();
  646. nstate = 0;
  647. }
  648. xdecition.CopyFrom(gdecition_def);
  649. }
  650. else
  651. {
  652. if(nstate == 0)
  653. {
  654. Activate();
  655. nstate = 1;
  656. }
  657. gMutex.lock();
  658. xdecition.CopyFrom(gdecition);
  659. gMutex.unlock();
  660. gnDecitionNum--;
  661. }
  662. #ifdef TORQUEBRAKETEST
  663. if(gnothavetb < 10)
  664. {
  665. iv::controller::torquebrake xtb;
  666. gMutextb.lock();
  667. xtb.CopyFrom(gtb);
  668. gMutextb.unlock();
  669. if(xtb.enable())
  670. {
  671. xdecition.set_torque(xtb.torque());
  672. xdecition.set_brake(xtb.brake());
  673. std::cout<<" use xtb. torque: "<<xtb.torque()<<" brake: "<<xtb.brake()<<std::endl;
  674. // gcontroller->control_torque(xtb.torque());
  675. // gcontroller->control_brake(xtb.brake());
  676. // qDebug("use tb value torque is %f brake is %f",xtb.torque(),xtb.brake());
  677. }
  678. else
  679. {
  680. // qDebug("torquebrake not enable.");
  681. }
  682. gnothavetb++;
  683. }
  684. #endif
  685. executeDecition(xdecition);
  686. if(gbChassisEPS == false) ExecSend();
  687. std::this_thread::sleep_for(std::chrono::milliseconds(10));
  688. }
  689. UnAcitvate();
  690. }
  691. #ifdef Q_OS_LINUX
  692. void sig_int(int signo)
  693. {
  694. gbSendRun = false;
  695. gpsendthread->join();
  696. iv::modulecomm::Unregister(gpacansend);
  697. iv::modulecomm::Unregister(gpachassis);
  698. iv::modulecomm::Unregister(gpadecition);
  699. std::cout<<" controller exit."<<std::endl;
  700. exit(0);
  701. }
  702. #endif
  703. // void processStream(std::istream& stream) {
  704. // std::string line;
  705. // while (std::getline(stream, line)) {
  706. // std::cout << "Read line: " << line << std::endl;
  707. // }
  708. // }
  709. void LoadXML(std::string strxmlpath){
  710. iv::xmlparam::Xmlparam xp(strxmlpath);
  711. gbUseOutDBC = xp.GetParam("UseOutDBC",false);
  712. gstrdbcpath = xp.GetParam("dbcpath","./ADCC_CH.dbc");
  713. }
  714. void Listencanrecv0()
  715. {
  716. void * pblambda = iv::modulecomm::RegisterRecv("canrecv0",[](const char * strdata,const unsigned int nSize,const unsigned int index,const QDateTime * dt,const char * strmemname){
  717. (void)strdata;
  718. (void)nSize;
  719. (void)index;
  720. (void)dt;
  721. (void)strmemname;
  722. iv::can::canmsg xmsg;
  723. if(false == xmsg.ParseFromArray(strdata,nSize))
  724. {
  725. std::cout<<"controller Listencan0 fail."<<std::endl;
  726. return;
  727. }
  728. int i;
  729. for(i=0;i<xmsg.rawmsg_size();i++)
  730. {
  731. iv::can::canraw * praw = xmsg.mutable_rawmsg(i);
  732. if(praw->id() == 0x14f)
  733. {
  734. unsigned char byte[24];
  735. if(praw->len() == 24)
  736. {
  737. memcpy(byte,praw->data().data(),24);
  738. unsigned int value;
  739. value = byte[2]&0x01;
  740. if(value == 0)
  741. {
  742. gbSendBrake = true;//std::cout<<" brake available."<<std::endl;
  743. }
  744. else
  745. {
  746. gbSendBrake = false;//std::cout<<" warning: no brake unavailable."<<std::endl;
  747. }
  748. }
  749. }
  750. if(praw->id() == 0x21)
  751. {
  752. unsigned char xdata[8];
  753. if(praw->len() == 8)
  754. {
  755. memcpy(xdata,praw->data().data(),8);
  756. unsigned int value;
  757. value = xdata[2]&0xff;
  758. value = value<<8;
  759. value =value + (xdata[3]&0xff);
  760. double facc = value;
  761. facc = facc * 0.001 - 2.0;
  762. gfVehAcc = facc*9.8;
  763. // std::cout<<" acc : "<<gfVehAcc<<std::endl;
  764. }
  765. }
  766. }
  767. });
  768. (void)pblambda;
  769. }
  770. #include <QFile>
  771. #include <QTextStream>
  772. #include <strings.h>
  773. #include <streambuf>
  774. #include <sstream>
  775. #include <iostream>
  776. #include <istream>
  777. #include <fstream>
  778. int main(int argc, char *argv[])
  779. {
  780. // std::istringstream iss("First line.\nSecond line.\n");
  781. // // 将 iss 传递给 processStream,这是合法的
  782. // processStream(iss);
  783. RegisterIVBackTrace();
  784. showversion("controller_changan_shenlan");
  785. QCoreApplication a(argc, argv);
  786. QString strpath = QCoreApplication::applicationDirPath();
  787. if(argc < 2)
  788. strpath = strpath + "/controller_chery_sterra_es_fcm.xml";
  789. else
  790. strpath = argv[1];
  791. std::cout<<strpath.toStdString()<<std::endl;
  792. LoadXML(strpath.toStdString());
  793. if(gbUseOutDBC == false)
  794. {
  795. QFile xFile;
  796. xFile.setFileName(":/FCM.dbc");
  797. if(xFile.open(QIODevice::ReadOnly))
  798. {
  799. std::cout<<" open qrc dbc file success. "<<std::endl;
  800. QTextStream in(&xFile);
  801. QString content = in.readAll(); // 读取文件全部内容到QString
  802. xFile.close();
  803. // 将QString转换为std::string,然后传递给std::istringstream
  804. std::istringstream iss(content.toStdString());
  805. gpsterraes = new sterraes(std::string(":/ADCC_CH.dbc"),iss);
  806. }
  807. else
  808. std::cout<<" open qrc dbc file fail. "<<std::endl;
  809. }
  810. else
  811. {
  812. gpsterraes = new sterraes(gstrdbcpath);
  813. }
  814. // gdecition_def.set_accelerator(-0.5);
  815. gdecition_def.set_brake(0);
  816. gdecition_def.set_rightlamp(false);
  817. gdecition_def.set_leftlamp(false);
  818. gdecition_def.set_wheelangle(0);
  819. gdecition_def.set_angle_mode(0);
  820. gdecition_def.set_angle_active(0);
  821. gdecition_def.set_acc_active(0);
  822. // gdecition_def.set_brake_active(1);
  823. gdecition_def.set_brake_type(0);
  824. gdecition_def.set_auto_mode(0);
  825. // gdecition_def.set_angle_mode(0);
  826. // gdecition_def.set_angle_active(0);
  827. // gdecition_def.set_acc_active(0);
  828. // gdecition_def.set_brake_active(0);
  829. // gdecition_def.set_brake_type(0);
  830. // gdecition_def.set_auto_mode(0);
  831. // gTime.start();
  832. iv::xmlparam::Xmlparam xp(strpath.toStdString());
  833. gstrmemcansend = xp.GetParam("cansend","cansend0");
  834. gstrmemdecition = xp.GetParam("dection","deciton");
  835. gstrmemchassis = xp.GetParam("chassismsgname","chassis");
  836. gpacansend = iv::modulecomm::RegisterSend(gstrmemcansend.data(),10000,1);
  837. gpadecition = iv::modulecomm::RegisterRecv(gstrmemdecition.data(),ListenDeciton);
  838. gpachassis = iv::modulecomm::RegisterRecv(gstrmemchassis.data(),UpdateChassis);
  839. #ifdef TORQUEBRAKETEST
  840. EnableTorqueBrakeTest();
  841. #endif
  842. // testes();
  843. // return 0;
  844. Listencanrecv0();
  845. std::thread xthread(sendthread);
  846. gpsendthread = &xthread;
  847. #ifdef Q_OS_LINUX
  848. signal(SIGINT, sig_int);
  849. signal(SIGTERM,sig_int);
  850. #endif
  851. return a.exec();
  852. }