main.cpp 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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 "chassis.pb.h"
  12. #include <thread>
  13. void * gpacansend;
  14. void * gpadecition;
  15. void * gpachassis;
  16. std::string gstrmemdecition;
  17. std::string gstrmemcansend;
  18. std::string gstrmemchassis;
  19. bool gbSendRun = true;
  20. bool gbChassisEPS = false;
  21. iv::brain::decition gdecition_def;
  22. iv::brain::decition gdecition;
  23. QTime gTime;
  24. int gnLastSendTime = 0;
  25. int gnLastRecvDecTime = -1000;
  26. int gnDecitionNum = 0; //when is zero,send default;
  27. const int gnDecitionNumMax = 100;
  28. int gnIndex = 0;
  29. boost::shared_ptr<iv::control::Controller> gcontroller; //实际车辆控制器
  30. //void executeDecition(const iv::decition::Decition decition)
  31. //{
  32. // std::cout<<"acc is"<<decition->accelerator<<" ang is "<<decition->wheel_angle<<std::endl;
  33. // controller_->control_wheel(decition->wheel_angle);
  34. // controller_->control_accelerate(decition->accelerator);
  35. // if(!decition->leftlamp && !decition->rightlamp){
  36. // ServiceControlStatus.set_turnsignals_control(0,0);
  37. // }else if(decition->leftlamp){
  38. // ServiceControlStatus.set_turnsignals_control(decition->leftlamp,0);
  39. // }else if(decition->rightlamp){
  40. // ServiceControlStatus.set_turnsignals_control(0,decition->rightlamp);
  41. // }
  42. //}
  43. // controller_->control_accelerate(decition->accelerator);
  44. void executeDecition(const iv::brain::decition decition)
  45. {
  46. // std::cout<<"acc is"<<decition.accelerator()<<" ang is "<<decition.wheelangle()<<std::endl;
  47. ServiceControlStatus.set_brake(decition.brake());
  48. ServiceControlStatus.set_torque(decition.torque());
  49. ServiceControlStatus.set_angle_speed((char)decition.wheelspeed());
  50. ServiceControlStatus.set_wheel_angle(decition.wheelangle());
  51. ServiceControlStatus.set_dangwei(decition.gear());
  52. ServiceControlStatus.set_handBrake(decition.handbrake()); //add by fjk
  53. //ServiceControlStatus.set_brake_type(decition->brakeType); //?
  54. ServiceControlStatus.set_door(decition.door());
  55. ServiceControlStatus.set_driveMode(decition.mode());
  56. ServiceControlStatus.set_dangwei_enable(true);
  57. ServiceControlStatus.set_angle_enable(true);
  58. ServiceControlStatus.set_brake_enable(true); //add by fjk
  59. // ServiceControlStatus.set_turnsignals_control(decition->left, decition->right); //add by fjk
  60. ServiceControlStatus.set_near_light(0);
  61. ServiceControlStatus.set_far_light(0);
  62. ServiceControlStatus.set_speaker(0);
  63. if(decition.leftlamp()){
  64. ServiceControlStatus.set_turn_light(1);
  65. }else if(decition.rightlamp()){
  66. ServiceControlStatus.set_turn_light(2);
  67. }else {
  68. ServiceControlStatus.set_turn_light(0);
  69. }
  70. // gcontroller->control_wheel(decition.wheelangle());
  71. // // controller_->control_accelerate(decition->accelerator);
  72. // gcontroller->control_torque(decition.torque());
  73. // gcontroller->control_brake(decition.brake());
  74. // gcontroller->control_engine(decition.engine());
  75. // gcontroller->control_grade(decition.grade());
  76. // gcontroller->control_mode(decition.mode());
  77. // gcontroller->control_handBrake(decition.handbrake());
  78. // gcontroller->control_speaker(decition.speak());
  79. // gcontroller->control_door(decition.door());
  80. // gcontroller->control_engine(decition.engine());
  81. // // gcontroller->control_light(decition.bri);
  82. // gcontroller->control_leftlight(decition.leftlamp());
  83. // gcontroller->control_rightlight(decition.rightlamp());
  84. // gcontroller->control_dangWei(decition.gear());
  85. // qDebug("dangwei is %d mode is %d",decition.gear(),decition.mode());
  86. }
  87. void UpdateChassis(const char *strdata, const unsigned int nSize, const unsigned int index, const QDateTime *dt, const char *strmemname)
  88. {
  89. (void)index;
  90. (void)dt;
  91. (void)strmemname;
  92. iv::chassis xchassis;
  93. // static int ncount = 0;
  94. if(!xchassis.ParseFromArray(strdata,nSize))
  95. {
  96. std::cout<<"iv::decition::BrainDecition::UpdateChassis ParseFrom Array Error."<<std::endl;
  97. return;
  98. }
  99. if((xchassis.has_epsmode())&&(xchassis.epsmode() == 0))
  100. {
  101. gbChassisEPS = true;
  102. }
  103. }
  104. void ListenDeciton(const char * strdata,const unsigned int nSize,const unsigned int index,const QDateTime * dt,const char * strmemname)
  105. {
  106. (void)index;
  107. (void)dt;
  108. (void)strmemname;
  109. static qint64 oldtime= QDateTime::currentMSecsSinceEpoch();
  110. iv::brain::decition xdecition;
  111. if(!xdecition.ParseFromArray(strdata,nSize))
  112. {
  113. std::cout<<"ListenDecition parse error."<<std::endl;
  114. return;
  115. }
  116. if((oldtime - QDateTime::currentMSecsSinceEpoch())<-100)qDebug("dection time is %lld diff is %lld ",QDateTime::currentMSecsSinceEpoch(),oldtime - QDateTime::currentMSecsSinceEpoch());
  117. oldtime = QDateTime::currentMSecsSinceEpoch();
  118. gdecition.CopyFrom(xdecition);
  119. gnDecitionNum = gnDecitionNumMax;
  120. gbChassisEPS = false;
  121. }
  122. void ExecSend()
  123. {
  124. iv::can::canmsg xmsg;
  125. iv::can::canraw xraw;
  126. // unsigned char * strp = (unsigned char *)&(ServiceControlStatus.command.byte[0]);
  127. // qDebug("%02x %02x %02x %02x %02x %02x %02x %02x",strp[0]
  128. // ,strp[1],strp[2],strp[3],strp[4],strp[5],strp[6],strp[7]);
  129. xraw.set_id(ServiceControlStatus.command_ID);
  130. xraw.set_data(ServiceControlStatus.command.byte,8);
  131. xraw.set_bext(false);
  132. xraw.set_bremote(false);
  133. xraw.set_len(8);
  134. iv::can::canraw * pxraw = xmsg.add_rawmsg();
  135. pxraw->CopyFrom(xraw);
  136. xmsg.set_channel(0);
  137. xmsg.set_index(gnIndex);
  138. gnIndex++;
  139. xmsg.set_mstime(QDateTime::currentMSecsSinceEpoch());
  140. int ndatasize = xmsg.ByteSize();
  141. char * strser = new char[ndatasize];
  142. std::shared_ptr<char> pstrser;
  143. pstrser.reset(strser);
  144. if(xmsg.SerializePartialToArray(strser,ndatasize))
  145. {
  146. iv::modulecomm::ModuleSendMsg(gpacansend,strser,ndatasize);
  147. }
  148. else
  149. {
  150. std::cout<<"MainWindow::onTimer serialize error."<<std::endl;
  151. }
  152. }
  153. void initial()
  154. {
  155. ServiceControlStatus.command.byte[0] = 0xEB;
  156. ServiceControlStatus.command.byte[1] = 0;
  157. ServiceControlStatus.command.byte[2] = 0;
  158. ServiceControlStatus.command.byte[3] = 0;
  159. ServiceControlStatus.command.byte[4] = 0;
  160. ServiceControlStatus.command.byte[5] = 0;
  161. ServiceControlStatus.command.byte[6] = 0;
  162. ServiceControlStatus.command.byte[7] = 0;
  163. }
  164. void sendthread()
  165. {
  166. initial();
  167. iv::brain::decition xdecition;
  168. while(gbSendRun)
  169. {
  170. if(gnDecitionNum <= 0)
  171. {
  172. xdecition.CopyFrom(gdecition_def);
  173. }
  174. else
  175. {
  176. xdecition.CopyFrom(gdecition);
  177. gnDecitionNum--;
  178. }
  179. executeDecition(xdecition);
  180. if(gbChassisEPS == false) ExecSend();
  181. std::this_thread::sleep_for(std::chrono::milliseconds(10));
  182. }
  183. }
  184. int main(int argc, char *argv[])
  185. {
  186. RegisterIVBackTrace();
  187. showversion("controller_problue");
  188. QCoreApplication a(argc, argv);
  189. QString strpath = QCoreApplication::applicationDirPath();
  190. if(argc < 2)
  191. strpath = strpath + "/controller_vv7.xml";
  192. else
  193. strpath = argv[1];
  194. std::cout<<strpath.toStdString()<<std::endl;
  195. gdecition_def.set_accelerator(-0.5);
  196. // gdecition_def.set_brake(1.0);
  197. gdecition_def.set_brake(20); //apollo_fu 20200417
  198. gdecition_def.set_torque(0);
  199. gdecition_def.set_rightlamp(true);
  200. // gdecition_def.set_doublespark(true);
  201. gdecition_def.set_mode(1);
  202. gdecition_def.set_grade(1);
  203. gdecition_def.set_gear(1);
  204. // gdecition_def.set_gear(0);
  205. gTime.start();
  206. gcontroller = boost::shared_ptr<iv::control::Controller>(new iv::control::Controller());
  207. iv::xmlparam::Xmlparam xp(strpath.toStdString());
  208. gstrmemcansend = xp.GetParam("cansend","cansend0");
  209. gstrmemdecition = xp.GetParam("dection","deciton");
  210. gstrmemchassis = xp.GetParam("chassismsgname","chassis");
  211. gpacansend = iv::modulecomm::RegisterSend(gstrmemcansend.data(),10000,1);
  212. gpadecition = iv::modulecomm::RegisterRecv(gstrmemdecition.data(),ListenDeciton);
  213. // gpachassis = iv::modulecomm::RegisterRecv(gstrmemchassis.data(),UpdateChassis);
  214. std::thread xthread(sendthread);
  215. return a.exec();
  216. }