main.cpp 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  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 <thread>
  14. void * gpacansend;
  15. void * gpadecition;
  16. void * gpachassis;
  17. std::string gstrmemdecition;
  18. std::string gstrmemcansend;
  19. std::string gstrmemchassis;
  20. bool gbSendRun = true;
  21. bool gbChassisEPS = false;
  22. iv::brain::decition gdecition_def;
  23. iv::brain::decition gdecition;
  24. QTime gTime;
  25. int gnLastSendTime = 0;
  26. int gnLastRecvDecTime = -1000;
  27. int gnDecitionNum = 0; //when is zero,send default;
  28. const int gnDecitionNumMax = 100;
  29. int gnIndex = 0;
  30. boost::shared_ptr<iv::control::Controller> gcontroller; //实际车辆控制器
  31. static QMutex gMutex;
  32. //void executeDecition(const iv::decition::Decition decition)
  33. //{
  34. // std::cout<<"acc is"<<decition->accelerator<<" ang is "<<decition->wheel_angle<<std::endl;
  35. // controller_->control_wheel(decition->wheel_angle);
  36. // controller_->control_accelerate(decition->accelerator);
  37. // if(!decition->leftlamp && !decition->rightlamp){
  38. // ServiceControlStatus.set_turnsignals_control(0,0);
  39. // }else if(decition->leftlamp){
  40. // ServiceControlStatus.set_turnsignals_control(decition->leftlamp,0);
  41. // }else if(decition->rightlamp){
  42. // ServiceControlStatus.set_turnsignals_control(0,decition->rightlamp);
  43. // }
  44. //}
  45. // controller_->control_accelerate(decition->accelerator);
  46. void executeDecition(const iv::brain::decition decition)
  47. {
  48. std::cout<<"acc is "<<decition.torque()<<" ang is "<<decition.wheelangle()<<std::endl;
  49. gcontroller->control_acc_en(true);
  50. gcontroller->control_angle_enable(true);
  51. gcontroller->control_gear_en(true);
  52. gcontroller->control_speed_limit(30);
  53. gcontroller->control_wheel(decition.wheelangle());
  54. gcontroller->control_angle_speed(decition.wheelspeed());
  55. gcontroller->control_torque(decition.torque());
  56. gcontroller->control_brake(decition.brake());
  57. gcontroller->control_gear(decition.gear());
  58. gcontroller->control_handBrake(decition.handbrake());
  59. gcontroller->control_mode(decition.mode());
  60. // gcontroller->control_mode(1);
  61. // gcontroller->control_near_light(1);
  62. gcontroller->control_air_enable(decition.air_enable());
  63. gcontroller->control_air_on(decition.air_on());
  64. gcontroller->control_air_temp(decition.air_temp());
  65. gcontroller->control_air_mode(decition.air_mode());
  66. gcontroller->control_wind_level(decition.wind_level());
  67. gcontroller->control_roof_light(decition.roof_light());
  68. gcontroller->control_home_light(decition.home_light());
  69. gcontroller->control_air_worktime(decition.air_worktime());
  70. gcontroller->control_air_offtime(decition.air_offtime());
  71. if(decition.door())
  72. gcontroller->control_door(2);
  73. else
  74. gcontroller->control_door(3);
  75. gcontroller->cmd_checksum(0x10);
  76. gcontroller->cmd_checksum(0x11);
  77. gcontroller->cmd_checksum(0x12);
  78. // qDebug("dangwei is %d mode is %d",decition.gear(),decition.mode());
  79. }
  80. void UpdateChassis(const char *strdata, const unsigned int nSize, const unsigned int index, const QDateTime *dt, const char *strmemname)
  81. {
  82. iv::chassis xchassis;
  83. static int ncount = 0;
  84. if(!xchassis.ParseFromArray(strdata,nSize))
  85. {
  86. std::cout<<"iv::decition::BrainDecition::UpdateChassis ParseFrom Array Error."<<std::endl;
  87. return;
  88. }
  89. if(xchassis.epsmode() == 0)
  90. {
  91. gbChassisEPS = true;
  92. }
  93. }
  94. void ListenDeciton(const char * strdata,const unsigned int nSize,const unsigned int index,const QDateTime * dt,const char * strmemname)
  95. {
  96. qint64 oldtime;
  97. iv::brain::decition xdecition;
  98. if(!xdecition.ParseFromArray(strdata,nSize))
  99. {
  100. std::cout<<"ListenDecition parse error."<<std::endl;
  101. return;
  102. }
  103. if(xdecition.gear() != 4)
  104. {
  105. qDebug("not D");
  106. }
  107. if((oldtime - QDateTime::currentMSecsSinceEpoch())<-100)qDebug("dection time is %ld diff is %ld ",QDateTime::currentMSecsSinceEpoch(),oldtime - QDateTime::currentMSecsSinceEpoch());
  108. oldtime = QDateTime::currentMSecsSinceEpoch();
  109. gMutex.lock();
  110. gdecition.CopyFrom(xdecition);
  111. gMutex.unlock();
  112. gnDecitionNum = gnDecitionNumMax;
  113. gbChassisEPS = false;
  114. }
  115. void ExecSend()
  116. {
  117. iv::can::canmsg xmsg;
  118. iv::can::canraw xraw;
  119. unsigned char * strp = (unsigned char *)&(ServiceControlStatus.command10.byte[0]);
  120. // qDebug("%02x %02x %02x %02x %02x %02x %02x %02x",strp[0]
  121. // ,strp[1],strp[2],strp[3],strp[4],strp[5],strp[6],strp[7]);
  122. xraw.set_id(ServiceControlStatus.command_ID10);
  123. xraw.set_data(ServiceControlStatus.command10.byte,8);
  124. xraw.set_bext(false);
  125. xraw.set_bremote(false);
  126. xraw.set_len(8);
  127. iv::can::canraw * pxraw = xmsg.add_rawmsg();
  128. pxraw->CopyFrom(xraw);
  129. xmsg.set_channel(0);
  130. xmsg.set_index(gnIndex);
  131. xraw.set_id(ServiceControlStatus.command_ID11);
  132. xraw.set_data(ServiceControlStatus.command11.byte,8);
  133. int a = ServiceControlStatus.command11.byte[5]&0x0f;
  134. if(a != 0x04)
  135. {
  136. qDebug("not D.");
  137. }
  138. xraw.set_bext(false);
  139. xraw.set_bremote(false);
  140. xraw.set_len(8);
  141. iv::can::canraw * pxraw1 = xmsg.add_rawmsg();
  142. pxraw1->CopyFrom(xraw);
  143. xmsg.set_channel(0);
  144. xmsg.set_index(gnIndex);
  145. xraw.set_id(ServiceControlStatus.command_ID12);
  146. xraw.set_data(ServiceControlStatus.command12.byte,8);
  147. xraw.set_bext(false);
  148. xraw.set_bremote(false);
  149. xraw.set_len(8);
  150. iv::can::canraw * pxraw2 = xmsg.add_rawmsg();
  151. pxraw2->CopyFrom(xraw);
  152. xmsg.set_channel(0);
  153. xmsg.set_index(gnIndex);
  154. gnIndex++;
  155. xmsg.set_mstime(QDateTime::currentMSecsSinceEpoch());
  156. int ndatasize = xmsg.ByteSize();
  157. char * strser = new char[ndatasize];
  158. std::shared_ptr<char> pstrser;
  159. pstrser.reset(strser);
  160. if(xmsg.SerializeToArray(strser,ndatasize))
  161. {
  162. iv::modulecomm::ModuleSendMsg(gpacansend,strser,ndatasize);
  163. }
  164. else
  165. {
  166. std::cout<<"MainWindow::onTimer serialize error."<<std::endl;
  167. }
  168. }
  169. void initial()
  170. {
  171. // ServiceControlStatus.command.byte[0] = 0;
  172. // ServiceControlStatus.command.byte[1] = 0;
  173. // ServiceControlStatus.command.byte[2] = 0;
  174. // ServiceControlStatus.command.byte[3] = 0;
  175. // ServiceControlStatus.command.byte[4] = 0;
  176. // ServiceControlStatus.command.byte[5] = 0;
  177. // ServiceControlStatus.command.byte[6] = 0;
  178. // ServiceControlStatus.command.byte[7] = 0;
  179. }
  180. void sendthread()
  181. {
  182. initial();
  183. iv::brain::decition xdecition;
  184. while(gbSendRun)
  185. {
  186. if(gnDecitionNum <= 0)
  187. {
  188. xdecition.CopyFrom(gdecition_def);
  189. }
  190. else
  191. {
  192. gMutex.lock();
  193. xdecition.CopyFrom(gdecition);
  194. gMutex.unlock();
  195. gnDecitionNum--;
  196. }
  197. executeDecition(xdecition);
  198. if(gbChassisEPS == false) ExecSend();
  199. std::this_thread::sleep_for(std::chrono::milliseconds(10));
  200. }
  201. }
  202. int main(int argc, char *argv[])
  203. {
  204. RegisterIVBackTrace();
  205. showversion("controller_hapo");
  206. QCoreApplication a(argc, argv);
  207. QString strpath = QCoreApplication::applicationDirPath();
  208. if(argc < 2)
  209. strpath = strpath + "/controller_vv7.xml";
  210. else
  211. strpath = argv[1];
  212. std::cout<<strpath.toStdString()<<std::endl;
  213. gdecition_def.set_accelerator(-0.5);
  214. // gdecition_def.set_brake(1.0);
  215. gdecition_def.set_brake(20); //apollo_fu 20200417
  216. gdecition_def.set_torque(0);
  217. // gdecition_def.set_rightlamp(true);
  218. // gdecition_def.set_doublespark(true);
  219. gdecition_def.set_wheelangle(0);
  220. gdecition_def.set_mode(1);
  221. gdecition_def.set_grade(1);
  222. gdecition_def.set_gear(1);
  223. gdecition_def.set_engine(0);
  224. // gdecition_def.set_gear(0);
  225. gTime.start();
  226. gcontroller = boost::shared_ptr<iv::control::Controller>(new iv::control::Controller());
  227. iv::xmlparam::Xmlparam xp(strpath.toStdString());
  228. gstrmemcansend = xp.GetParam("cansend","cansend0");
  229. gstrmemdecition = xp.GetParam("dection","deciton");
  230. gstrmemchassis = xp.GetParam("chassismsgname","chassis");
  231. gpacansend = iv::modulecomm::RegisterSend(gstrmemcansend.data(),10000,1);
  232. gpadecition = iv::modulecomm::RegisterRecv(gstrmemdecition.data(),ListenDeciton);
  233. gpachassis = iv::modulecomm::RegisterRecv(gstrmemchassis.data(),UpdateChassis);
  234. std::thread xthread(sendthread);
  235. return a.exec();
  236. }