main.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458
  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. #include "remotectrl.pb.h"
  16. #include "platform_feedback.pb.h"
  17. void * gpacansend;
  18. void * gpadecition;
  19. void * gpachassis;
  20. void * gparemote;
  21. void * gpaPaltformFeedback;
  22. std::string gstrmemdecition;
  23. std::string gstrmemcansend;
  24. std::string gstrmemchassis;
  25. std::string gstrmemremote; //Remote Ctrl
  26. std::string gstrmemPaltformFeedback;
  27. bool gbSendRun = true;
  28. bool gbChassisEPS = false;
  29. iv::brain::decition gdecition_def;
  30. iv::brain::decition gdecition;
  31. bool gbAllowRemote = false; //Default, Not Allow Remote
  32. bool gbAutoDriving = true; //if true, Auto Driving, false, remote controll
  33. iv::brain::decition gdecition_remote;
  34. iv::platformFeedback gPlatformFeedback;
  35. qint64 gLastRemoteTime = 0;
  36. QTime gTime;
  37. int gnLastSendTime = 0;
  38. int gnLastRecvDecTime = -1000;
  39. int gnDecitionNum = 0; //when is zero,send default;
  40. const int gnDecitionNumMax = 100;
  41. int gnIndex = 0;
  42. boost::shared_ptr<iv::control::Controller> gcontroller; //实际车辆控制器
  43. static QMutex gMutex;
  44. //void executeDecition(const iv::decition::Decition decition)
  45. //{
  46. // std::cout<<"acc is"<<decition->accelerator<<" ang is "<<decition->wheel_angle<<std::endl;
  47. // controller_->control_wheel(decition->wheel_angle);
  48. // controller_->control_accelerate(decition->accelerator);
  49. // if(!decition->leftlamp && !decition->rightlamp){
  50. // ServiceControlStatus.set_turnsignals_control(0,0);
  51. // }else if(decition->leftlamp){
  52. // ServiceControlStatus.set_turnsignals_control(decition->leftlamp,0);
  53. // }else if(decition->rightlamp){
  54. // ServiceControlStatus.set_turnsignals_control(0,decition->rightlamp);
  55. // }
  56. //}
  57. // controller_->control_accelerate(decition->accelerator);
  58. void executeDecition(const iv::brain::decition decition)
  59. {
  60. // std::cout<<"acc is "<<decition.torque()<<" ang is "<<decition.wheelangle()<<std::endl;
  61. gcontroller->control_acc_en(true);
  62. gcontroller->control_angle_enable(true);
  63. gcontroller->control_gear_en(true);
  64. gcontroller->control_speed_limit(30);
  65. gcontroller->control_wheel(decition.wheelangle());
  66. gcontroller->control_angle_speed(decition.wheelspeed());
  67. gcontroller->control_torque(decition.torque());
  68. gcontroller->control_brake(decition.brake());
  69. gcontroller->control_gear(decition.gear());
  70. gcontroller->control_handBrake(decition.handbrake());
  71. gcontroller->control_mode(decition.mode());
  72. // gcontroller->control_mode(1);
  73. gcontroller->control_near_light(decition.dippedlight());
  74. //gcontroller->control_air_enable(decition.air_enable());
  75. //gcontroller->control_air_on(decition.air_on());
  76. //gcontroller->control_air_temp(decition.air_temp());
  77. //gcontroller->control_air_mode(decition.air_mode());
  78. //gcontroller->control_wind_level(decition.wind_level());
  79. gcontroller->control_roof_light(decition.roof_light());
  80. gcontroller->control_home_light(decition.home_light());
  81. //gcontroller->control_air_worktime(decition.air_worktime());
  82. //gcontroller->control_air_offtime(decition.air_offtime());
  83. gcontroller->control_turnsignals(decition.leftlamp(),decition.rightlamp());
  84. gcontroller->control_door(decition.door());
  85. #ifdef TORQUEBRAKETEST
  86. if(gnothavetb < 10)
  87. {
  88. iv::controller::torquebrake xtb;
  89. gMutextb.lock();
  90. xtb.CopyFrom(gtb);
  91. gMutextb.unlock();
  92. if(xtb.enable())
  93. {
  94. gcontroller->control_torque(xtb.torque());
  95. gcontroller->control_brake(xtb.brake());
  96. qDebug("use tb value torque is %f brake is %f",xtb.torque(),xtb.brake());
  97. }
  98. else
  99. {
  100. // qDebug("torquebrake not enable.");
  101. }
  102. gnothavetb++;
  103. }
  104. #endif
  105. /*if(decition.has_door())
  106. {
  107. if(decition.door())
  108. {
  109. gcontroller->control_door(2);
  110. }else{
  111. gcontroller->control_door(3);
  112. }
  113. }*/
  114. //qDebug("door is %d",decition.door());
  115. gcontroller->cmd_checksum(0x10);
  116. gcontroller->cmd_checksum(0x11);
  117. gcontroller->cmd_checksum(0x12);
  118. switch (decition.gear()) {
  119. case 0:
  120. gPlatformFeedback.set_shift(3); //0N 1D 2R 3P
  121. break;
  122. case 1:
  123. gPlatformFeedback.set_shift(1); //0N 1D 2R 3P
  124. break;
  125. case 2:
  126. gPlatformFeedback.set_shift(2); //0N 1D 2R 3P
  127. break;
  128. case 3:
  129. gPlatformFeedback.set_shift(0); //0N 1D 2R 3P
  130. break;
  131. default:
  132. break;
  133. }
  134. gPlatformFeedback.set_throttle(decition.torque());
  135. gPlatformFeedback.set_brake(decition.brake());
  136. gPlatformFeedback.set_steeringwheelangle(decition.wheelangle());
  137. // qDebug("dangwei is %d mode is %d",decition.gear(),decition.mode());
  138. }
  139. void ListenRemotectrl(const char * strdata,const unsigned int nSize,const unsigned int index,const QDateTime * dt,const char * strmemname)
  140. {
  141. // qint64 oldtime;
  142. iv::brain::decition xdecition;
  143. iv::remotectrl xrc;
  144. if(!xrc.ParseFromArray(strdata,nSize))
  145. {
  146. std::cout<<"ListenRemotectrl parse error."<<std::endl;
  147. return;
  148. }
  149. if(xrc.ntype() == iv::remotectrl_CtrlType_AUTO)
  150. {
  151. gbAutoDriving = true;
  152. gPlatformFeedback.set_typefeedback(iv::platformFeedback::ctrlType::platformFeedback_ctrlType_AUTO);
  153. }
  154. else if(xrc.ntype() == iv::remotectrl_CtrlType_STOP)
  155. {
  156. gbAutoDriving = false;
  157. gPlatformFeedback.set_typefeedback(iv::platformFeedback::ctrlType::platformFeedback_ctrlType_STOP);
  158. xdecition.set_torque(0.0);
  159. xdecition.set_brake(100.0);
  160. xdecition.set_wheelangle(0.0);
  161. xdecition.set_gear(1); //0P 1D 2R 3N
  162. xdecition.set_handbrake(0);
  163. xdecition.set_mode(1);
  164. gMutex.lock();
  165. gdecition_remote.CopyFrom(xdecition);
  166. gMutex.unlock();
  167. gLastRemoteTime = QDateTime::currentMSecsSinceEpoch();
  168. }
  169. else
  170. {
  171. std::cout<<" shift "<<xrc.shift()<<" acc "<<xrc.acc()<<" brake: "<<xrc.brake()<<" wheel: "<<xrc.wheel()<<std::endl;
  172. gbAutoDriving = false;
  173. gPlatformFeedback.set_typefeedback(iv::platformFeedback::ctrlType::platformFeedback_ctrlType_REMOTE);
  174. xdecition.set_torque(xrc.acc()*0.08);
  175. xdecition.set_brake(xrc.brake());
  176. xdecition.set_wheelangle(xrc.wheel()*5.5f);
  177. if(xrc.shift() > 0) //D shift
  178. xdecition.set_gear(4); //0P 1D 2R 3N
  179. else if(xrc.shift() < 0)
  180. xdecition.set_gear(2);
  181. // else
  182. // xdecition.set_gear(3);
  183. xdecition.set_handbrake(0);
  184. xdecition.set_mode(1);
  185. gMutex.lock();
  186. gdecition_remote.CopyFrom(xdecition);
  187. gMutex.unlock();
  188. gLastRemoteTime = QDateTime::currentMSecsSinceEpoch();
  189. }
  190. }
  191. void UpdateChassis(const char *strdata, const unsigned int nSize, const unsigned int index, const QDateTime *dt, const char *strmemname)
  192. {
  193. iv::chassis xchassis;
  194. static int ncount = 0;
  195. if(!xchassis.ParseFromArray(strdata,nSize))
  196. {
  197. std::cout<<"iv::decition::BrainDecition::UpdateChassis ParseFrom Array Error."<<std::endl;
  198. return;
  199. }
  200. if(xchassis.epsmode() == 0)
  201. {
  202. gbChassisEPS = true;
  203. }
  204. }
  205. void ListenDeciton(const char * strdata,const unsigned int nSize,const unsigned int index,const QDateTime * dt,const char * strmemname)
  206. {
  207. qint64 oldtime;
  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 %ld diff is %ld ",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(ServiceControlStatus.command_ID10);
  234. xraw.set_data(ServiceControlStatus.command10.byte,8);
  235. xraw.set_bext(false);
  236. xraw.set_bremote(false);
  237. xraw.set_len(8);
  238. iv::can::canraw * pxraw = xmsg.add_rawmsg();
  239. pxraw->CopyFrom(xraw);
  240. xmsg.set_channel(0);
  241. xmsg.set_index(gnIndex);
  242. xraw.set_id(ServiceControlStatus.command_ID11);
  243. xraw.set_data(ServiceControlStatus.command11.byte,8);
  244. int a = ServiceControlStatus.command11.byte[5]&0x0f;
  245. if(a != 0x04)
  246. {
  247. qDebug("not D.");
  248. }
  249. xraw.set_bext(false);
  250. xraw.set_bremote(false);
  251. xraw.set_len(8);
  252. iv::can::canraw * pxraw1 = xmsg.add_rawmsg();
  253. pxraw1->CopyFrom(xraw);
  254. xmsg.set_channel(0);
  255. xmsg.set_index(gnIndex);
  256. xraw.set_id(ServiceControlStatus.command_ID12);
  257. xraw.set_data(ServiceControlStatus.command12.byte,8);
  258. xraw.set_bext(false);
  259. xraw.set_bremote(false);
  260. xraw.set_len(8);
  261. iv::can::canraw * pxraw2 = xmsg.add_rawmsg();
  262. pxraw2->CopyFrom(xraw);
  263. xmsg.set_channel(0);
  264. xmsg.set_index(gnIndex);
  265. gnIndex++;
  266. xmsg.set_mstime(QDateTime::currentMSecsSinceEpoch());
  267. int ndatasize = xmsg.ByteSize();
  268. char * strser = new char[ndatasize];
  269. std::shared_ptr<char> pstrser;
  270. pstrser.reset(strser);
  271. if(xmsg.SerializeToArray(strser,ndatasize))
  272. {
  273. iv::modulecomm::ModuleSendMsg(gpacansend,strser,ndatasize);
  274. }
  275. else
  276. {
  277. std::cout<<"MainWindow::onTimer serialize error."<<std::endl;
  278. }
  279. int ndatasize_pf = gPlatformFeedback.ByteSize();
  280. char * str_pf = new char[ndatasize_pf];
  281. std::shared_ptr<char> pstr;pstr.reset(str_pf);
  282. if(!gPlatformFeedback.SerializeToArray(str_pf,ndatasize_pf))
  283. {
  284. std::cout<<"MainWindow::on_horizontalSlider_valueChanged serialize error."<<std::endl;
  285. return;
  286. }
  287. iv::modulecomm::ModuleSendMsg(gpaPaltformFeedback,str_pf,ndatasize_pf);
  288. }
  289. void initial()
  290. {
  291. // ServiceControlStatus.command.byte[0] = 0;
  292. // ServiceControlStatus.command.byte[1] = 0;
  293. // ServiceControlStatus.command.byte[2] = 0;
  294. // ServiceControlStatus.command.byte[3] = 0;
  295. // ServiceControlStatus.command.byte[4] = 0;
  296. // ServiceControlStatus.command.byte[5] = 0;
  297. // ServiceControlStatus.command.byte[6] = 0;
  298. // ServiceControlStatus.command.byte[7] = 0;
  299. }
  300. void sendthread()
  301. {
  302. initial();
  303. iv::brain::decition xdecition;
  304. while(gbSendRun)
  305. {
  306. if(gbAutoDriving)
  307. {
  308. if(gnDecitionNum <= 0)
  309. {
  310. xdecition.CopyFrom(gdecition_def);
  311. }
  312. else
  313. {
  314. gMutex.lock();
  315. xdecition.CopyFrom(gdecition);
  316. gMutex.unlock();
  317. gnDecitionNum--;
  318. }
  319. }
  320. else
  321. {
  322. if((QDateTime::currentMSecsSinceEpoch() - gLastRemoteTime)> 1000)
  323. {
  324. xdecition.CopyFrom(gdecition_def);
  325. }
  326. else
  327. {
  328. gMutex.lock();
  329. xdecition.CopyFrom(gdecition_remote);
  330. gMutex.unlock();
  331. }
  332. }
  333. executeDecition(xdecition);
  334. if(gbChassisEPS == false) ExecSend();
  335. std::this_thread::sleep_for(std::chrono::milliseconds(10));
  336. }
  337. }
  338. int main(int argc, char *argv[])
  339. {
  340. RegisterIVBackTrace();
  341. showversion("controller_hapo");
  342. QCoreApplication a(argc, argv);
  343. QString strpath = QCoreApplication::applicationDirPath();
  344. if(argc < 2)
  345. strpath = strpath + "/controller_vv7.xml";
  346. else
  347. strpath = argv[1];
  348. std::cout<<strpath.toStdString()<<std::endl;
  349. gdecition_def.set_accelerator(-0.5);
  350. // gdecition_def.set_brake(1.0);
  351. gdecition_def.set_brake(20); //apollo_fu 20200417
  352. gdecition_def.set_torque(0);
  353. // gdecition_def.set_rightlamp(true);
  354. // gdecition_def.set_doublespark(true);
  355. gdecition_def.set_wheelangle(0);
  356. gdecition_def.set_mode(1);
  357. gdecition_def.set_grade(1);
  358. gdecition_def.set_gear(1);
  359. gdecition_def.set_engine(0);
  360. // gdecition_def.set_gear(0);
  361. gTime.start();
  362. gcontroller = boost::shared_ptr<iv::control::Controller>(new iv::control::Controller());
  363. iv::xmlparam::Xmlparam xp(strpath.toStdString());
  364. gstrmemcansend = xp.GetParam("cansend","cansend0");
  365. gstrmemdecition = xp.GetParam("dection","deciton");
  366. gstrmemchassis = xp.GetParam("chassismsgname","chassis");
  367. gstrmemremote = xp.GetParam("remotectrl","remotectrl");
  368. gstrmemPaltformFeedback = xp.GetParam("paltformFeedback","platformFeedback");
  369. std::string strremote = xp.GetParam("allowremote","true");
  370. if(strremote == "true")
  371. {
  372. gbAllowRemote = true;
  373. }
  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. if(gbAllowRemote)
  378. {
  379. gpaPaltformFeedback = iv::modulecomm::RegisterSend(gstrmemPaltformFeedback.data(),10000,1);
  380. gparemote = iv::modulecomm::RegisterRecv(gstrmemremote.data(),ListenRemotectrl);
  381. }
  382. #ifdef TORQUEBRAKETEST
  383. EnableTorqueBrakeTest();
  384. #endif
  385. std::thread xthread(sendthread);
  386. return a.exec();
  387. }