main.cpp 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  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 "ivversion.h"
  9. #include "ivbacktrace.h"
  10. #include "canmsg.pb.h"
  11. #include "decition.pb.h"
  12. #include <thread>
  13. #include "remotectrl.pb.h"
  14. #include "platform_feedback.pb.h"
  15. QMutex gMutex;
  16. void * gpacansend;
  17. void * gpadecition;
  18. void * gparemote;
  19. void * gpaPaltformFeedback;
  20. std::string gstrmemdecition;
  21. std::string gstrmemcansend;
  22. std::string gstrmemremote; //Remote Ctrl
  23. std::string gstrmemPaltformFeedback;
  24. bool gbSendRun = true;
  25. iv::brain::decition gdecition_def;
  26. iv::brain::decition gdecition;
  27. bool gbAllowRemote = false; //Default, Not Allow Remote
  28. bool gbAutoDriving = true; //if true, Auto Driving, false, remote controll
  29. bool gbChassisEPS = true;
  30. iv::brain::decition gdecition_remote;
  31. iv::platformFeedback gPlatformFeedback;
  32. qint64 gLastRemoteTime = 0;
  33. QTime gTime;
  34. int gnLastSendTime = 0;
  35. int gnLastRecvDecTime = -1000;
  36. int gnDecitionNum = 0; //when is zero,send default;
  37. const int gnDecitionNumMax = 100;
  38. int gnIndex = 0;
  39. boost::shared_ptr<iv::control::Controller> gcontroller; //实际车辆控制器
  40. void executeDecition(const iv::brain::decition decition)
  41. {
  42. std::cout<<"acc is "<<decition.torque()<<" ang is "<<decition.wheelangle()<<std::endl;
  43. std::cout<<"limitspeed is "<<decition.speed()<<" brake is "<<decition.brake()<<std::endl;
  44. std::cout<<"drive_mode is "<<decition.mode()<<" elec_brake is "<<decition.handbrake()<<std::endl;
  45. std::cout<<"brake_light is "<<decition.brakelamp()<<" dangwei is "<<decition.gear()<<std::endl;
  46. gcontroller->inialize();
  47. gcontroller->control_torque(decition.torque());
  48. gcontroller->control_wheel(decition.wheelangle());
  49. gcontroller->control_brake(decition.brake());
  50. gcontroller->control_limit_speed(decition.speed());
  51. gcontroller->control_drive_mode(decition.mode());
  52. gcontroller->control_elec_brake(decition.handbrake());
  53. gcontroller->control_brake_light(decition.brakelamp());
  54. gcontroller->control_dangwei(decition.gear()); //0P 1D 2R 3N
  55. switch (decition.gear()) {
  56. case 0:
  57. gPlatformFeedback.set_shift(3); //0N 1D 2R 3P
  58. break;
  59. case 1:
  60. gPlatformFeedback.set_shift(1); //0N 1D 2R 3P
  61. break;
  62. case 2:
  63. gPlatformFeedback.set_shift(2); //0N 1D 2R 3P
  64. break;
  65. case 3:
  66. gPlatformFeedback.set_shift(0); //0N 1D 2R 3P
  67. break;
  68. default:
  69. break;
  70. }
  71. gPlatformFeedback.set_throttle(decition.torque());
  72. gPlatformFeedback.set_brake(decition.brake());
  73. gPlatformFeedback.set_steeringwheelangle(decition.wheelangle());
  74. }
  75. void ListenRemotectrl(const char * strdata,const unsigned int nSize,const unsigned int index,const QDateTime * dt,const char * strmemname)
  76. {
  77. // qint64 oldtime;
  78. iv::brain::decition xdecition;
  79. iv::remotectrl xrc;
  80. if(!xrc.ParseFromArray(strdata,nSize))
  81. {
  82. std::cout<<"ListenRemotectrl parse error."<<std::endl;
  83. return;
  84. }
  85. if(xrc.ntype() == iv::remotectrl_CtrlType_AUTO)
  86. {
  87. gbAutoDriving = true;
  88. gPlatformFeedback.set_typefeedback(iv::platformFeedback::ctrlType::platformFeedback_ctrlType_AUTO);
  89. }
  90. else if(xrc.ntype() == iv::remotectrl_CtrlType_STOP)
  91. {
  92. gbAutoDriving = false;
  93. gPlatformFeedback.set_typefeedback(iv::platformFeedback::ctrlType::platformFeedback_ctrlType_STOP);
  94. xdecition.set_torque(0.0);
  95. xdecition.set_brake(100.0);
  96. xdecition.set_wheelangle(0.0);
  97. xdecition.set_speed(5);
  98. xdecition.set_gear(1); //0P 1D 2R 3N
  99. xdecition.set_handbrake(0);
  100. xdecition.set_grade(1);
  101. xdecition.set_mode(1);
  102. xdecition.set_speak(0);
  103. xdecition.set_headlight(false);
  104. xdecition.set_engine(0);
  105. xdecition.set_taillight(false);
  106. gMutex.lock();
  107. gdecition_remote.CopyFrom(xdecition);
  108. gMutex.unlock();
  109. gLastRemoteTime = QDateTime::currentMSecsSinceEpoch();
  110. }
  111. else
  112. {
  113. gbAutoDriving = false;
  114. gPlatformFeedback.set_typefeedback(iv::platformFeedback::ctrlType::platformFeedback_ctrlType_REMOTE);
  115. xdecition.set_torque(xrc.acc());
  116. xdecition.set_brake(xrc.brake());
  117. xdecition.set_wheelangle(xrc.wheel()*5.5f);
  118. xdecition.set_speed(105);
  119. if(xrc.shift() > 0) //D shift
  120. xdecition.set_gear(1); //0P 1D 2R 3N
  121. else if(xrc.shift() < 0)
  122. xdecition.set_gear(2);
  123. else
  124. xdecition.set_gear(3);
  125. xdecition.set_handbrake(0);
  126. xdecition.set_grade(1);
  127. xdecition.set_mode(1);
  128. xdecition.set_speak(0);
  129. xdecition.set_headlight(false);
  130. xdecition.set_engine(0);
  131. xdecition.set_taillight(false);
  132. gMutex.lock();
  133. gdecition_remote.CopyFrom(xdecition);
  134. gMutex.unlock();
  135. gLastRemoteTime = QDateTime::currentMSecsSinceEpoch();
  136. }
  137. }
  138. void ListenDeciton(const char * strdata,const unsigned int nSize,const unsigned int index,const QDateTime * dt,const char * strmemname)
  139. {
  140. iv::brain::decition xdecition;
  141. if(!xdecition.ParseFromArray(strdata,nSize))
  142. {
  143. std::cout<<"ListenDecition parse error."<<std::endl;
  144. return;
  145. }
  146. gMutex.lock();
  147. gdecition.CopyFrom(xdecition);
  148. gMutex.unlock();
  149. gnDecitionNum = gnDecitionNumMax;
  150. }
  151. void ExecSend()
  152. {
  153. iv::can::canmsg xmsg;
  154. iv::can::canraw xraw;
  155. xraw.set_id(ServiceControlStatus.command_ID);
  156. xraw.set_data(ServiceControlStatus.command.byte,8);
  157. xraw.set_bext(false);
  158. xraw.set_bremote(false);
  159. xraw.set_len(8);
  160. iv::can::canraw * pxraw = xmsg.add_rawmsg();
  161. pxraw->CopyFrom(xraw);
  162. xmsg.set_channel(0);
  163. xmsg.set_index(gnIndex);
  164. gnIndex++;
  165. xmsg.set_mstime(QDateTime::currentMSecsSinceEpoch());
  166. int ndatasize = xmsg.ByteSize();
  167. char * strser = new char[ndatasize];
  168. std::shared_ptr<char> pstrser;
  169. pstrser.reset(strser);
  170. if(xmsg.SerializePartialToArray(strser,ndatasize))
  171. {
  172. iv::modulecomm::ModuleSendMsg(gpacansend,strser,ndatasize);
  173. }
  174. else
  175. {
  176. std::cout<<"MainWindow::onTimer serialize error."<<std::endl;
  177. }
  178. int ndatasize_pf = gPlatformFeedback.ByteSize();
  179. char * str_pf = new char[ndatasize_pf];
  180. std::shared_ptr<char> pstr;pstr.reset(str_pf);
  181. if(!gPlatformFeedback.SerializeToArray(str_pf,ndatasize_pf))
  182. {
  183. std::cout<<"MainWindow::on_horizontalSlider_valueChanged serialize error."<<std::endl;
  184. return;
  185. }
  186. iv::modulecomm::ModuleSendMsg(gpaPaltformFeedback,str_pf,ndatasize_pf);
  187. }
  188. void sendthread()
  189. {
  190. iv::brain::decition xdecition;
  191. while(gbSendRun)
  192. {
  193. if(gbAutoDriving)
  194. {
  195. if(gnDecitionNum <= 0)
  196. {
  197. xdecition.CopyFrom(gdecition_def);
  198. }
  199. else
  200. {
  201. gMutex.lock();
  202. xdecition.CopyFrom(gdecition);
  203. gMutex.unlock();
  204. gnDecitionNum--;
  205. }
  206. }
  207. else
  208. {
  209. if((QDateTime::currentMSecsSinceEpoch() - gLastRemoteTime)> 1000)
  210. {
  211. xdecition.CopyFrom(gdecition_def);
  212. }
  213. else
  214. {
  215. gMutex.lock();
  216. xdecition.CopyFrom(gdecition_remote);
  217. gMutex.unlock();
  218. }
  219. }
  220. executeDecition(xdecition);
  221. ExecSend();
  222. std::this_thread::sleep_for(std::chrono::milliseconds(10));
  223. }
  224. }
  225. int main(int argc, char *argv[])
  226. {
  227. RegisterIVBackTrace();
  228. showversion("controller_midcar");
  229. QCoreApplication a(argc, argv);
  230. QString strpath = QCoreApplication::applicationDirPath();
  231. if(argc < 2)
  232. strpath = strpath + "/controller_midcar.xml";
  233. else
  234. strpath = argv[1];
  235. std::cout<<strpath.toStdString()<<std::endl;
  236. gdecition_def.set_brake(0);
  237. gdecition_def.set_torque(0);
  238. gdecition_def.set_speed(105);
  239. gPlatformFeedback.set_typefeedback(iv::platformFeedback::ctrlType::platformFeedback_ctrlType_AUTO);
  240. gPlatformFeedback.set_shift(3);
  241. gPlatformFeedback.set_throttle(0.0);
  242. gPlatformFeedback.set_brake(0.0);
  243. gPlatformFeedback.set_steeringwheelangle(0.0);
  244. gTime.start();
  245. gcontroller = boost::shared_ptr<iv::control::Controller>(new iv::control::Controller());
  246. iv::xmlparam::Xmlparam xp(strpath.toStdString());
  247. gstrmemcansend = xp.GetParam("cansend","cansend0");
  248. gstrmemdecition = xp.GetParam("dection","deciton");
  249. gstrmemremote = xp.GetParam("remotectrl","remotectrl");
  250. gstrmemPaltformFeedback = xp.GetParam("paltformFeedback","platformFeedback");
  251. std::string strremote = xp.GetParam("allowremote","true");
  252. if(strremote == "true")
  253. {
  254. gbAllowRemote = true;
  255. }
  256. gpacansend = iv::modulecomm::RegisterSend(gstrmemcansend.data(),10000,3);
  257. gpadecition = iv::modulecomm::RegisterRecv(gstrmemdecition.data(),ListenDeciton);
  258. if(gbAllowRemote)
  259. {
  260. gpaPaltformFeedback = iv::modulecomm::RegisterSend(gstrmemPaltformFeedback.data(),10000,1);
  261. gparemote = iv::modulecomm::RegisterRecv(gstrmemremote.data(),ListenRemotectrl);
  262. }
  263. std::thread xthread(sendthread);
  264. return a.exec();
  265. }