main.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. #include <QCoreApplication>
  2. #include <QDateTime>
  3. #include <QMutex>
  4. #include <iostream>
  5. #include <thread>
  6. #include "modulecomm.h"
  7. #include "ivversion.h"
  8. #include "ivbacktrace.h"
  9. #include "ivfault.h"
  10. #include "ivlog.h"
  11. #include "ivexit.h"
  12. #include "canmsg.pb.h"
  13. #include "radarobjectarray.pb.h"
  14. #include "chassis.pb.h"
  15. #include "gpsimu.pb.h"
  16. #include "decode_cfg.h"
  17. #include "can_producer_consumer.h"
  18. #include "canrecv_consumer.h"
  19. #include "cansend_consumer.h"
  20. #include "cansend_producer.h"
  21. #include "iv_msgunit.h"
  22. /*
  23. * Notice : the radar config setting function is not finished
  24. * Notice : the number of object is less than 64. so this version is used for object mode.
  25. * Notice : only can decode standard data CAN frame
  26. */
  27. QCoreApplication * gApp;
  28. iv::Ivfault *gfault = nullptr;
  29. iv::Ivlog *givlog = nullptr;
  30. setupConfig_t setupConfig;
  31. iv::msgunit shmCANRecv;
  32. iv::msgunit shmCANSend;
  33. iv::msgunit shmRadar;
  34. iv::msgunit shmGPSIMU;
  35. iv::msgunit shmChassis;
  36. CAN_Producer_Consumer CANRecv(4096);
  37. CAN_Producer_Consumer CANSend(4096);
  38. uint8_t chassisShift = 0; //1P 2R 3N 4D
  39. double chassisVelocity = 0; //m/s
  40. QMutex chassisDataLock;
  41. double GPSIMUVelocity = 0; // m/s
  42. double GPSIMUYawRate = 0; // deg/s
  43. QMutex GPSIMUDataLock;
  44. CANSend_Producer* can_send_producer;
  45. CANSend_Consumer* can_send_consumer;
  46. CANRecv_Consumer* can_recv_consumer;
  47. void ListenCANMsg(const char * strdata,const unsigned int nSize,const unsigned int index,const QDateTime * dt,const char * strmemname)
  48. {
  49. if(nSize<1)
  50. {
  51. std::cout<<"radar ListenCANMsg data empty."<<std::endl;
  52. return;
  53. }
  54. iv::can::canmsg xmsg;
  55. if(!xmsg.ParseFromArray(strdata,nSize))
  56. {
  57. givlog->error("radar Listencanmsg fail");
  58. gfault->SetFaultState(1, 0, "radar Listencanmsg error");
  59. std::cout<<"radar ListenCANMsg parse fail."<<std::endl;
  60. return;
  61. }
  62. CANRecv.Produce_Elements_From_CANMsg(xmsg);
  63. }
  64. void ListenGPSIMUMsg(const char * strdata,const unsigned int nSize,const unsigned int index,const QDateTime * dt,const char * strmemname)
  65. {
  66. if(nSize<1)
  67. {
  68. std::cout<<"radar ListenGPSIMUMsg data empty."<<std::endl;
  69. return;
  70. }
  71. iv::gps::gpsimu xdata;
  72. if(!xdata.ParseFromArray(strdata,nSize))
  73. {
  74. std::cout<<" radar ListenGPSIMUMsg parse fail."<<std::endl;
  75. return;
  76. }
  77. GPSIMUDataLock.lock();
  78. GPSIMUVelocity = xdata.speed(); //m/s
  79. GPSIMUYawRate = xdata.gyro_z(); //deg/s
  80. GPSIMUDataLock.unlock();
  81. }
  82. void ListenChassisMsg(const char * strdata,const unsigned int nSize,const unsigned int index,const QDateTime * dt,const char * strmemname)
  83. {
  84. if(nSize<1)
  85. {
  86. std::cout<<"radar ListenChassisMsg data empty."<<std::endl;
  87. return;
  88. }
  89. iv::chassis xdata;
  90. if(!xdata.ParseFromArray(strdata,nSize))
  91. {
  92. std::cout<<" radar ListenChassisMsg parse fail."<<std::endl;
  93. return;
  94. }
  95. chassisDataLock.lock();
  96. chassisShift = xdata.shift(); //1P 2R 3N 4D
  97. chassisVelocity = xdata.vel()/3.6; //km/h to m/s
  98. chassisDataLock.unlock();
  99. }
  100. void ExitFunc()
  101. {
  102. gApp->quit();
  103. std::this_thread::sleep_for(std::chrono::milliseconds(500));
  104. }
  105. void signal_handler(int sig)
  106. {
  107. if(sig == SIGINT)
  108. {
  109. ExitFunc();
  110. }
  111. }
  112. int main(int argc, char *argv[])
  113. {
  114. RegisterIVBackTrace();
  115. showversion("driver_radar_continental_ARS408_SRR308");
  116. QCoreApplication a(argc, argv);
  117. gApp = &a;
  118. givlog = new iv::Ivlog("driver_radar_continental_ARS408_SRR308");
  119. gfault = new iv::Ivfault("driver_radar_continental_ARS408_SRR308");
  120. // iv::ivexit::RegIVExitCall(ExitFunc);
  121. signal(SIGINT,signal_handler);
  122. QString strpath = QCoreApplication::applicationDirPath();
  123. if(argc < 2)
  124. strpath = strpath + "/driver_radar_continental_ARS408_SRR308.xml";
  125. else
  126. strpath = argv[1];
  127. // givlog->verbose("%s", strpath.data());
  128. // std::cout<<strpath.toStdString()<<std::endl;
  129. DecodeSetupCfgFromXML(setupConfig,strpath.toStdString());
  130. if(setupConfig.radarCfgApply!=0)
  131. {
  132. RadarConfiguration_ch0_t radarConfiguration;
  133. DecodeRadarCfgFromYAML(radarConfiguration,setupConfig.radarCfgYAML_Path);
  134. }
  135. strncpy(shmCANSend.mstrmsgname,setupConfig.strMemCANSend.data(),255);
  136. shmCANSend.mnBufferSize = 100000;
  137. shmCANSend.mnBufferCount = 3;
  138. shmCANSend.mpa = iv::modulecomm::RegisterSend(shmCANSend.mstrmsgname,shmCANSend.mnBufferSize,shmCANSend.mnBufferCount);
  139. strncpy(shmRadar.mstrmsgname,setupConfig.strMemRadar.data(),255);
  140. shmRadar.mnBufferSize = 100000;
  141. shmRadar.mnBufferCount = 1;
  142. shmRadar.mpa = iv::modulecomm::RegisterSend(shmRadar.mstrmsgname,shmRadar.mnBufferSize,shmRadar.mnBufferCount);
  143. strncpy(shmCANRecv.mstrmsgname,setupConfig.strMemCANRecv.data(),255);
  144. shmCANRecv.mnBufferSize = 100000;
  145. shmCANRecv.mnBufferCount = 3;
  146. shmCANRecv.mpa = iv::modulecomm::RegisterRecv(shmCANRecv.mstrmsgname,ListenCANMsg);
  147. strncpy(shmGPSIMU.mstrmsgname,setupConfig.strMemGPSIMU.data(),255);
  148. shmGPSIMU.mnBufferSize = 100000;
  149. shmGPSIMU.mnBufferCount = 3;
  150. shmGPSIMU.mpa = iv::modulecomm::RegisterRecv(shmGPSIMU.mstrmsgname,ListenGPSIMUMsg);
  151. strncpy(shmChassis.mstrmsgname,setupConfig.strMemChassis.data(),255);
  152. shmChassis.mnBufferSize = 100000;
  153. shmChassis.mnBufferCount = 3;
  154. shmChassis.mpa = iv::modulecomm::RegisterRecv(shmChassis.mstrmsgname,ListenChassisMsg);
  155. can_send_producer = new CANSend_Producer(&CANSend);
  156. can_send_producer->start();
  157. can_send_consumer = new CANSend_Consumer(&CANSend);
  158. can_send_consumer->start();
  159. can_recv_consumer = new CANRecv_Consumer(&CANRecv);
  160. can_recv_consumer->start();
  161. int rtn = a.exec();
  162. can_send_producer->terminate(); //function "terminate" can not be used in other position. it is dangerous.
  163. can_send_consumer->terminate(); //function "terminate" can not be used in other position. it is dangerous.
  164. can_recv_consumer->terminate(); //function "terminate" can not be used in other position. it is dangerous.
  165. if(gfault != nullptr)delete gfault;
  166. if(givlog != nullptr)delete givlog;
  167. if(shmCANRecv.mpa != nullptr)iv::modulecomm::Unregister(shmCANRecv.mpa);
  168. if(shmCANSend.mpa != nullptr)iv::modulecomm::Unregister(shmCANSend.mpa);
  169. if(shmRadar.mpa != nullptr)iv::modulecomm::Unregister(shmRadar.mpa);
  170. if(shmGPSIMU.mpa != nullptr)iv::modulecomm::Unregister(shmGPSIMU.mpa);
  171. if(shmChassis.mpa != nullptr)iv::modulecomm::Unregister(shmChassis.mpa);
  172. return rtn;
  173. }