main.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560
  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. #ifdef Q_OS_LINUX
  14. #include <signal.h>
  15. #endif
  16. #include "candbc.h"
  17. #include "sterraes.h"
  18. sterraes * gpsterraes;
  19. static void * gpacansend;
  20. static void * gpadecition;
  21. static void * gpachassis;
  22. static std::string gstrmemdecition;
  23. static std::string gstrmemcansend;
  24. static std::string gstrmemchassis;
  25. static bool gbSendRun = true;
  26. static bool gbChassisEPS = false;
  27. static iv::brain::decition gdecition_def;
  28. static iv::brain::decition gdecition;
  29. static QTime gTime;
  30. static int gnLastSendTime = 0;
  31. static int gnLastRecvDecTime = -1000;
  32. static int gnDecitionNum = 0; //when is zero,send default;
  33. const int gnDecitionNumMax = 100;
  34. static int gnIndex = 0;
  35. static bool gbHaveVehSpd = false;
  36. static double gfVehSpd = 0.0;
  37. static bool gbHaveWheelAngle = false;
  38. static double gfWheelAngle = 0.0;
  39. static double gfWheelSpeedLim = 300; //300 degrees per second
  40. static QMutex gMutex;
  41. static std::thread * gpsendthread = NULL;
  42. unsigned char ADS_EPS_1[24];// 0x195/405
  43. unsigned char ADS_EPS_3[24]; // 0x1BC/444
  44. unsigned char ADS_ONEBOX_1[24];// 0x159/345
  45. unsigned char ADS_VCU_1[24]; // 0x167/359
  46. static int gnState = 0; //0 not act 1 act
  47. CANPacker * gpPacker;
  48. std::vector<SignalPackValue> mvectorADSEPS1;
  49. std::vector<SignalPackValue> mvectorADSEPS3;
  50. std::vector<SignalPackValue> mvectorADSONEBOX1;
  51. std::vector<SignalPackValue> mvectorADSVCU1;
  52. void set_EPS1_signal(std::string strsigname,double value){
  53. gpsterraes->set_EPS1_signal(strsigname,value);
  54. }
  55. void set_EPS3_signal(std::string strsigname,double value){
  56. gpsterraes->set_EPS3_signal(strsigname,value);
  57. }
  58. void set_ONEBOX1_signal(std::string strsigname,double value){
  59. gpsterraes->set_ONEBOX1_signal(strsigname,value);
  60. }
  61. void set_VCU1_signal(std::string strsigname,double value){
  62. gpsterraes->set_VCU1_signal(strsigname,value);
  63. }
  64. void ExecSend();
  65. void executeDecition(const iv::brain::decition &decition)
  66. {
  67. }
  68. void Activate()
  69. {
  70. std::this_thread::sleep_for(std::chrono::milliseconds(100));
  71. iv::brain::decition xdecition;
  72. xdecition.set_brake(0.0);
  73. xdecition.set_torque(0.0);
  74. // for(int j=0;j<100000;j++)
  75. // {
  76. std::cout<<" run "<<std::endl;
  77. for(int i = 0; i < 3; i++){
  78. xdecition.set_wheelangle(0.0);
  79. xdecition.set_angle_mode(0);
  80. xdecition.set_angle_active(0);
  81. xdecition.set_acc_active(0);
  82. xdecition.set_brake_active(0);
  83. // xdecition.set_brake_type(0);
  84. xdecition.set_auto_mode(0);
  85. gnState = 0;
  86. executeDecition(xdecition);
  87. ExecSend();
  88. std::this_thread::sleep_for(std::chrono::milliseconds(10));
  89. }
  90. for(int i = 0; i < 3; i++){
  91. xdecition.set_wheelangle(0.0);
  92. xdecition.set_angle_mode(1);
  93. xdecition.set_angle_active(1);
  94. xdecition.set_acc_active(1);
  95. xdecition.set_brake_active(1);
  96. // xdecition.set_brake_type(1);
  97. xdecition.set_auto_mode(3);
  98. gnState = 1;
  99. executeDecition(xdecition);
  100. ExecSend();
  101. std::this_thread::sleep_for(std::chrono::milliseconds(10));
  102. }
  103. // }
  104. }
  105. void UnAcitvate()
  106. {
  107. iv::brain::decition xdecition;
  108. xdecition.set_brake(0.0);
  109. xdecition.set_torque(0.0);
  110. for(int i = 0; i < 3; i++){
  111. xdecition.set_wheelangle(0);
  112. xdecition.set_angle_mode(1);
  113. xdecition.set_angle_active(1);
  114. xdecition.set_acc_active(1);
  115. xdecition.set_brake_active(1);
  116. // xdecition.set_brake_type(1);
  117. xdecition.set_auto_mode(3);
  118. gnState = 1;
  119. executeDecition(xdecition);
  120. ExecSend();
  121. std::this_thread::sleep_for(std::chrono::milliseconds(10));
  122. }
  123. for(int i = 0; i < 3; i++){
  124. xdecition.set_wheelangle(0);
  125. xdecition.set_angle_mode(0);
  126. xdecition.set_angle_active(0);
  127. xdecition.set_acc_active(0);
  128. xdecition.set_brake_active(0);
  129. // xdecition.set_brake_type(0);
  130. gnState = 0;
  131. xdecition.set_auto_mode(0);
  132. executeDecition(xdecition);
  133. ExecSend();
  134. std::this_thread::sleep_for(std::chrono::milliseconds(10));
  135. }
  136. }
  137. void UpdateChassis(const char *strdata, const unsigned int nSize, const unsigned int index, const QDateTime *dt, const char *strmemname)
  138. {
  139. (void)index;
  140. (void)dt;
  141. (void)strmemname;
  142. iv::chassis xchassis;
  143. // static int ncount = 0;
  144. if(!xchassis.ParseFromArray(strdata,nSize))
  145. {
  146. std::cout<<"iv::decition::BrainDecition::UpdateChassis ParseFrom Array Error."<<std::endl;
  147. return;
  148. }
  149. if(xchassis.has_epsmode())
  150. {
  151. if(xchassis.epsmode() == 0)
  152. {
  153. gbChassisEPS = true;
  154. }
  155. }
  156. if(xchassis.has_vel())
  157. {
  158. gfVehSpd = xchassis.vel();
  159. gbHaveVehSpd = true;
  160. // std::cout<<" gf Veh speed : "<<gfVehSpd<<std::endl;
  161. }
  162. if(xchassis.has_angle_feedback())
  163. {
  164. gfWheelAngle = xchassis.angle_feedback();
  165. gbHaveWheelAngle = true;
  166. }
  167. }
  168. void ListenDeciton(const char * strdata,const unsigned int nSize,const unsigned int index,const QDateTime * dt,const char * strmemname)
  169. {
  170. (void)index;
  171. (void)dt;
  172. (void)strmemname;
  173. static qint64 oldtime = QDateTime::currentMSecsSinceEpoch();
  174. iv::brain::decition xdecition;
  175. if(!xdecition.ParseFromArray(strdata,nSize))
  176. {
  177. std::cout<<"ListenDecition parse error."<<std::endl;
  178. return;
  179. }
  180. // if(xdecition.gear() != 4)
  181. // {
  182. // qDebug("not D");
  183. // }
  184. xdecition.set_angle_mode(1);
  185. xdecition.set_angle_active(1);
  186. xdecition.set_acc_active(1);
  187. xdecition.set_brake_active(1);
  188. // xdecition.set_brake_type(1);
  189. xdecition.set_auto_mode(3);
  190. // xdecition.set_wheelangle(45.0);
  191. if((oldtime - QDateTime::currentMSecsSinceEpoch())<-100)qDebug("dection time is %lld diff is %lld ",QDateTime::currentMSecsSinceEpoch(),oldtime - QDateTime::currentMSecsSinceEpoch());
  192. oldtime = QDateTime::currentMSecsSinceEpoch();
  193. gMutex.lock();
  194. gdecition.CopyFrom(xdecition);
  195. gMutex.unlock();
  196. gnDecitionNum = gnDecitionNumMax;
  197. gbChassisEPS = false;
  198. }
  199. void ExecSend()
  200. {
  201. static int nCount = 0;
  202. nCount++;
  203. iv::can::canmsg xmsg;
  204. iv::can::canraw xraw;
  205. // unsigned char * strp = (unsigned char *)&(ServiceControlStatus.command10.byte[0]);
  206. // 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]);
  207. xraw.set_id(0x195);
  208. xraw.set_data(ADS_EPS_1,24);
  209. xraw.set_bext(false);
  210. xraw.set_bremote(false);
  211. xraw.set_len(24);
  212. iv::can::canraw * pxraw195 = xmsg.add_rawmsg();
  213. pxraw195->CopyFrom(xraw);
  214. // qDebug(" 0x144: %02X %02X %02X %02X %02X %02X %02X %02X",byte_144[0],byte_144[1],byte_144[2],byte_144[3],
  215. // byte_144[4],byte_144[5],byte_144[6],byte_144[7]);
  216. xmsg.set_channel(0);
  217. xmsg.set_index(gnIndex);
  218. xraw.set_id(0x1BC);
  219. xraw.set_data(ADS_EPS_3,24);
  220. xraw.set_bext(false);
  221. xraw.set_bremote(false);
  222. xraw.set_len(24);
  223. xmsg.set_channel(0);
  224. // iv::can::canraw * pxraw1BC = xmsg.add_rawmsg();
  225. // pxraw1BC->CopyFrom(xraw);
  226. xraw.set_id(0x159);
  227. xraw.set_data(ADS_ONEBOX_1,24);
  228. xraw.set_bext(false);
  229. xraw.set_bremote(false);
  230. xraw.set_len(24);
  231. // iv::can::canraw * pxraw159 = xmsg.add_rawmsg();
  232. // pxraw159->CopyFrom(xraw);
  233. xraw.set_id(0x167);
  234. xraw.set_data(ADS_VCU_1,24);
  235. xraw.set_bext(false);
  236. xraw.set_bremote(false);
  237. xraw.set_len(24);
  238. // iv::can::canraw * pxraw167 = xmsg.add_rawmsg();
  239. // pxraw167->CopyFrom(xraw);
  240. xmsg.set_channel(0);
  241. xmsg.set_index(gnIndex);
  242. gnIndex++;
  243. xmsg.set_mstime(QDateTime::currentMSecsSinceEpoch());
  244. int ndatasize = xmsg.ByteSize();
  245. char * strser = new char[ndatasize];
  246. std::shared_ptr<char> pstrser;
  247. pstrser.reset(strser);
  248. if(xmsg.SerializeToArray(strser,ndatasize))
  249. {
  250. iv::modulecomm::ModuleSendMsg(gpacansend,strser,ndatasize);
  251. }
  252. else
  253. {
  254. std::cout<<"MainWindow::onTimer serialize error."<<std::endl;
  255. }
  256. }
  257. void initial()
  258. {
  259. for (uint8_t i = 0; i < 24; i++) //CAN to canfd
  260. {
  261. //byte_36E[i] = 0;
  262. }
  263. }
  264. void SendEPS3()
  265. {
  266. static int rollcouter = 0;
  267. gpsterraes->GetEPS3Data(ADS_EPS_3);
  268. ExecSend();
  269. rollcouter++;
  270. if(rollcouter>14)rollcouter = 0;
  271. }
  272. void SendEPS1()
  273. {
  274. static int rollcouter = 0;
  275. // std::cout<<" roll count:: "<<rollcouter<<std::endl;
  276. set_EPS1_signal("ADS_1_RollgCntr1",rollcouter);
  277. set_EPS1_signal("ADS_1_Resd1",0.0);
  278. gpsterraes->GetEPS1Data(ADS_EPS_1);
  279. ExecSend();
  280. rollcouter++;
  281. if(rollcouter>14)rollcouter = 0;
  282. }
  283. void testes()
  284. {
  285. int i = 0;
  286. int rollcouter = 0;
  287. double fwheelang = 90.0;
  288. set_EPS1_signal("ADS_1_RollgCntr1",rollcouter);
  289. set_EPS1_signal("ADS_1_Resd1",0.0);
  290. set_EPS1_signal("ADS_1_SteerAgReq",fwheelang);
  291. set_EPS1_signal("ADS_1_SteerAgVld",1.0);
  292. set_EPS1_signal("ADS_1_SteerPilotAgEna",0.0);
  293. set_EPS1_signal("ADS_1_RollgCntr2",rollcouter);
  294. set_EPS1_signal("ADS_1_Resd2",0.0);
  295. set_EPS1_signal("ADS_1_SteerTqEna",1.0);
  296. set_EPS1_signal("ADS_1_LdwWarningCmd",0.0);
  297. set_EPS1_signal("ADS_1_LdwWarningCmdVld",2.0);
  298. set_EPS1_signal("ADS_1_SteerMaxTqReq",10.0);
  299. set_EPS1_signal("ADS_1_SteerMinTqReq",1.0);
  300. set_EPS1_signal("ADS_1_ADSHealthSts",1.0);
  301. set_EPS1_signal("CutOutFreshvalues_2CB_S",1.0);
  302. set_EPS1_signal("CutOutMAC_2CB_S",1.0);
  303. set_EPS3_signal("ADS_3_RollgCntr1",rollcouter);
  304. set_EPS3_signal("ADS_3_Resd1",0.0);
  305. set_EPS3_signal("ADS_3_SteerParkAgReq",0.0);
  306. set_EPS3_signal("ADS_3_SteerParkAgVld",0.0);
  307. set_EPS3_signal("ADS_3_SteerParkAgEna",0.0);
  308. set_EPS3_signal("ADS_3_RollgCntr2",rollcouter);
  309. set_EPS3_signal("ADS_3_Resd2",0.0);
  310. set_EPS3_signal("ADS_3_ParkFcnMode",0.0);
  311. set_EPS3_signal("ADS_3_ADSParkHealthSts",0.0);
  312. for(i=0;i<10;i++)
  313. {
  314. set_EPS1_signal("ADS_1_SteerPilotAgEna",0.0);
  315. SendEPS1();
  316. std::this_thread::sleep_for(std::chrono::milliseconds(10));
  317. }
  318. // for(i=0;i<10;i++)
  319. // {
  320. // set_EPS3_signal("ADS_3_SteerParkAgEna",1.0);
  321. // set_EPS3_signal("ADS_3_SteerParkAgReq",0.0);
  322. // set_EPS3_signal("ADS_3_SteerParkAgVld",0.0);
  323. // SendEPS3();
  324. // std::this_thread::sleep_for(std::chrono::milliseconds(10));
  325. // }
  326. for(i=0;i<3000;i++)
  327. {
  328. set_EPS1_signal("ADS_1_SteerPilotAgEna",2.0);
  329. set_EPS1_signal("ADS_1_SteerAgReq",fwheelang);
  330. set_EPS1_signal("ADS_1_SteerAgVld",1.0);
  331. SendEPS1();
  332. std::this_thread::sleep_for(std::chrono::milliseconds(10));
  333. }
  334. for(i=0;i<10;i++)
  335. {
  336. set_EPS1_signal("ADS_1_SteerPilotAgEna",0.0);
  337. SendEPS1();
  338. std::this_thread::sleep_for(std::chrono::milliseconds(10));
  339. }
  340. }
  341. void sendthread()
  342. {
  343. initial();
  344. iv::brain::decition xdecition;
  345. UnAcitvate();
  346. // UnAcitvate();
  347. int nstate = 0; //0 Un 1 Activate
  348. // Activate();
  349. while(gbSendRun)
  350. {
  351. if(gnDecitionNum <= 0)
  352. {
  353. if(nstate == 1)
  354. {
  355. UnAcitvate();
  356. nstate = 0;
  357. }
  358. xdecition.CopyFrom(gdecition_def);
  359. }
  360. else
  361. {
  362. if(nstate == 0)
  363. {
  364. Activate();
  365. nstate = 1;
  366. }
  367. gMutex.lock();
  368. xdecition.CopyFrom(gdecition);
  369. gMutex.unlock();
  370. gnDecitionNum--;
  371. }
  372. #ifdef TORQUEBRAKETEST
  373. if(gnothavetb < 10)
  374. {
  375. iv::controller::torquebrake xtb;
  376. gMutextb.lock();
  377. xtb.CopyFrom(gtb);
  378. gMutextb.unlock();
  379. if(xtb.enable())
  380. {
  381. xdecition.set_torque(xtb.torque());
  382. xdecition.set_brake(xtb.brake());
  383. std::cout<<" use xtb. torque: "<<xtb.torque()<<" brake: "<<xtb.brake()<<std::endl;
  384. // gcontroller->control_torque(xtb.torque());
  385. // gcontroller->control_brake(xtb.brake());
  386. // qDebug("use tb value torque is %f brake is %f",xtb.torque(),xtb.brake());
  387. }
  388. else
  389. {
  390. // qDebug("torquebrake not enable.");
  391. }
  392. gnothavetb++;
  393. }
  394. #endif
  395. executeDecition(xdecition);
  396. if(gbChassisEPS == false) ExecSend();
  397. std::this_thread::sleep_for(std::chrono::milliseconds(10));
  398. }
  399. UnAcitvate();
  400. }
  401. #ifdef Q_OS_LINUX
  402. void sig_int(int signo)
  403. {
  404. gbSendRun = false;
  405. gpsendthread->join();
  406. iv::modulecomm::Unregister(gpacansend);
  407. iv::modulecomm::Unregister(gpachassis);
  408. iv::modulecomm::Unregister(gpadecition);
  409. std::cout<<" controller exit."<<std::endl;
  410. exit(0);
  411. }
  412. #endif
  413. int main(int argc, char *argv[])
  414. {
  415. RegisterIVBackTrace();
  416. showversion("controller_changan_shenlan");
  417. QCoreApplication a(argc, argv);
  418. QString strpath = QCoreApplication::applicationDirPath();
  419. if(argc < 2)
  420. strpath = strpath + "/controller_changan_shenlan.xml";
  421. else
  422. strpath = argv[1];
  423. std::cout<<strpath.toStdString()<<std::endl;
  424. gpsterraes = new sterraes();
  425. // gdecition_def.set_accelerator(-0.5);
  426. gdecition_def.set_brake(0);
  427. gdecition_def.set_rightlamp(false);
  428. gdecition_def.set_leftlamp(false);
  429. gdecition_def.set_wheelangle(0);
  430. gdecition_def.set_angle_mode(0);
  431. gdecition_def.set_angle_active(0);
  432. gdecition_def.set_acc_active(0);
  433. // gdecition_def.set_brake_active(1);
  434. gdecition_def.set_brake_type(0);
  435. gdecition_def.set_auto_mode(0);
  436. // gdecition_def.set_angle_mode(0);
  437. // gdecition_def.set_angle_active(0);
  438. // gdecition_def.set_acc_active(0);
  439. // gdecition_def.set_brake_active(0);
  440. // gdecition_def.set_brake_type(0);
  441. // gdecition_def.set_auto_mode(0);
  442. // gTime.start();
  443. iv::xmlparam::Xmlparam xp(strpath.toStdString());
  444. gstrmemcansend = xp.GetParam("cansend","cansend0");
  445. gstrmemdecition = xp.GetParam("dection","deciton");
  446. gstrmemchassis = xp.GetParam("chassismsgname","chassis");
  447. gpacansend = iv::modulecomm::RegisterSend(gstrmemcansend.data(),10000,1);
  448. gpadecition = iv::modulecomm::RegisterRecv(gstrmemdecition.data(),ListenDeciton);
  449. gpachassis = iv::modulecomm::RegisterRecv(gstrmemchassis.data(),UpdateChassis);
  450. #ifdef TORQUEBRAKETEST
  451. EnableTorqueBrakeTest();
  452. #endif
  453. testes();
  454. return 0;
  455. std::thread xthread(sendthread);
  456. gpsendthread = &xthread;
  457. #ifdef Q_OS_LINUX
  458. signal(SIGINT, sig_int);
  459. signal(SIGTERM,sig_int);
  460. #endif
  461. return a.exec();
  462. }