main.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. #include <QCoreApplication>
  2. #include <QTime>
  3. #include "control/control_status.h"
  4. #include "control/controller.h"
  5. #include "xmlparam.h"
  6. #include "modulecomm.h"
  7. #include "ivversion.h"
  8. #include "ivbacktrace.h"
  9. #include "canmsg.pb.h"
  10. #include "decition.pb.h"
  11. #include <thread>
  12. void * gpacansend;
  13. void * gpadecition;
  14. std::string gstrmemdecition;
  15. std::string gstrmemcansend;
  16. bool gbSendRun = true;
  17. iv::brain::decition gdecition_def;
  18. iv::brain::decition gdecition;
  19. QTime gTime;
  20. int gnLastSendTime = 0;
  21. int gnLastRecvDecTime = -1000;
  22. int gnDecitionNum = 0; //when is zero,send default;
  23. const int gnDecitionNumMax = 100;
  24. int gnIndex = 0;
  25. boost::shared_ptr<iv::control::Controller> gcontroller; //实际车辆控制器
  26. void executeDecition(const iv::brain::decition decition)
  27. {
  28. std::cout<<"***************************"<<std::endl;
  29. std::cout<<"acc is "<<decition.accelerator()<<std::endl;
  30. std::cout<<"ang is "<<decition.wheelangle()<<std::endl;
  31. std::cout<<"brake is "<<decition.brake()<<std::endl;
  32. std::cout<<"limitspeed is "<<decition.speed()<<std::endl;
  33. std::cout<<"dangwei is "<<decition.gear()<<std::endl;
  34. std::cout<<std::endl;
  35. gcontroller->inialize();
  36. gcontroller->control_torque(decition.accelerator());
  37. gcontroller->control_wheel(decition.wheelangle());
  38. gcontroller->control_braking(decition.brake());
  39. gcontroller->control_speed_limit(decition.speed());
  40. gcontroller->control_dangWei(decition.gear());
  41. //gcontroller->control_zhuche(int P_zhuche); //++
  42. gcontroller->control_drivemode(true);
  43. gcontroller->control_brakemode(true);
  44. gcontroller->control_torquemode(true);
  45. gcontroller->control_dangweimode(true); //++
  46. gcontroller->control_turnmode(true);
  47. gcontroller->control_turnLight(decition.leftlamp(), decition.rightlamp()); //++
  48. gcontroller->control_nearLight(0); //++
  49. gcontroller->control_beep(decition.speak()); //++
  50. gcontroller->control_brakeLight(decition.brakelamp());//++
  51. }
  52. void ListenDeciton(const char * strdata,const unsigned int nSize,const unsigned int index,const QDateTime * dt,const char * strmemname)
  53. {
  54. (void)index;
  55. (void)dt;
  56. (void)strmemname;
  57. iv::brain::decition xdecition;
  58. if(!xdecition.ParseFromArray(strdata,nSize))
  59. {
  60. std::cout<<"ListenDecition parse error."<<std::endl;
  61. return;
  62. }
  63. gdecition.CopyFrom(xdecition);
  64. gnDecitionNum = gnDecitionNumMax;
  65. }
  66. void ExecSend()
  67. {
  68. iv::can::canmsg xmsg;
  69. iv::can::canraw xraw;
  70. xraw.set_id(ServiceControlStatus.command_ID);
  71. xraw.set_data(ServiceControlStatus.command.byte,8);
  72. // qDebug("%02x %02x",ServiceControlStatus.command.byte[2],ServiceControlStatus.command.byte[3]);
  73. xraw.set_bext(false);
  74. xraw.set_bremote(false);
  75. xraw.set_len(8);
  76. iv::can::canraw * pxraw = xmsg.add_rawmsg();
  77. pxraw->CopyFrom(xraw);
  78. xmsg.set_channel(0);
  79. xmsg.set_index(gnIndex);
  80. gnIndex++;
  81. xmsg.set_mstime(QDateTime::currentMSecsSinceEpoch());
  82. int ndatasize = xmsg.ByteSize();
  83. char * strser = new char[ndatasize];
  84. std::shared_ptr<char> pstrser;
  85. pstrser.reset(strser);
  86. if(xmsg.SerializePartialToArray(strser,ndatasize))
  87. {
  88. iv::modulecomm::ModuleSendMsg(gpacansend,strser,ndatasize);
  89. }
  90. else
  91. {
  92. std::cout<<"MainWindow::onTimer serialize error."<<std::endl;
  93. }
  94. }
  95. void sendthread()
  96. {
  97. iv::brain::decition xdecition;
  98. while(gbSendRun)
  99. {
  100. if(gnDecitionNum <= 0)
  101. {
  102. xdecition.CopyFrom(gdecition_def);
  103. }
  104. else
  105. {
  106. xdecition.CopyFrom(gdecition);
  107. gnDecitionNum--;
  108. }
  109. executeDecition(xdecition);
  110. ExecSend();
  111. std::this_thread::sleep_for(std::chrono::milliseconds(10));
  112. }
  113. }
  114. int main(int argc, char *argv[])
  115. {
  116. RegisterIVBackTrace();
  117. showversion("controller_jinlong_peisong");
  118. QCoreApplication a(argc, argv);
  119. QString strpath = QCoreApplication::applicationDirPath();
  120. if(argc < 2)
  121. strpath = strpath + "/controller_jinlong_peisong.xml";
  122. else
  123. strpath = argv[1];
  124. std::cout<<strpath.toStdString()<<std::endl;
  125. gdecition_def.set_accelerator(-0.5);
  126. gdecition_def.set_brake(1.0);
  127. gdecition_def.set_torque(0);
  128. gdecition_def.set_rightlamp(true);
  129. gdecition_def.set_doublespark(true);
  130. gTime.start();
  131. gcontroller = boost::shared_ptr<iv::control::Controller>(new iv::control::Controller());
  132. iv::xmlparam::Xmlparam xp(strpath.toStdString());
  133. gstrmemcansend = xp.GetParam("cansend","cansend0");
  134. gstrmemdecition = xp.GetParam("dection","deciton");
  135. const char * strx = gstrmemcansend.data();
  136. gpacansend = iv::modulecomm::RegisterSend(gstrmemcansend.data(),10000,3);
  137. gpadecition = iv::modulecomm::RegisterRecv(gstrmemdecition.data(),ListenDeciton);
  138. std::thread xthread(sendthread);
  139. return a.exec();
  140. }