main.cpp 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. #include <QCoreApplication>
  2. #include <QDateTime>
  3. #include <QMutex>
  4. #include <iostream>
  5. #include "modulecomm.h"
  6. #include "ivversion.h"
  7. #include "ivbacktrace.h"
  8. #include "ivfault.h"
  9. #include "ivlog.h"
  10. #include "ivexit.h"
  11. #include "canmsg.pb.h"
  12. #include "radarobjectarray.pb.h"
  13. #include "chassis.pb.h"
  14. #include "gpsimu.pb.h"
  15. #include "decode_cfg.h"
  16. #include "can_producer_consumer.h"
  17. #include "canrecv_consumer.h"
  18. #include "cansend_consumer.h"
  19. #include "cansend_producer.h"
  20. #include "iv_msgunit.h"
  21. /*
  22. * Notice : the radar config setting function is not finished
  23. * Notice : the number of object is less than 64. so this version is used for object mode.
  24. */
  25. QCoreApplication * gApp;
  26. iv::Ivfault *gfault = nullptr;
  27. iv::Ivlog *givlog = nullptr;
  28. setupConfig_t setupConfig;
  29. iv::msgunit shmCANRecv;
  30. iv::msgunit shmCANSend;
  31. iv::msgunit shmRadar;
  32. iv::msgunit shmGPSIMU;
  33. iv::msgunit shmChassis;
  34. CAN_Producer_Consumer CANRecv(4096);
  35. CAN_Producer_Consumer CANSend(4096);
  36. uint8_t chassisShift = 0; //1P 2R 3N 4D
  37. double chassisVelocity = 0; //m/s
  38. QMutex chassisDataLock;
  39. double GPSIMUVelocity = 0; // m/s
  40. double GPSIMUYawRate = 0; // deg/s
  41. QMutex GPSIMUDataLock;
  42. //------------------------------------
  43. iv::radar::radarobjectarray gobj;
  44. int gntemp = 0;
  45. static int gnNotSend = 10;
  46. static bool gbinit = false;
  47. iv::radar::radarobjectarray mradararray;
  48. void ShareResult()
  49. {
  50. char * str = new char[mradararray.ByteSize()];
  51. int nsize = mradararray.ByteSize();
  52. if(mradararray.SerializeToArray(str,nsize))
  53. {
  54. iv::modulecomm::ModuleSendMsg(shmCANSend.mpa,str,nsize);
  55. }
  56. givlog->verbose("obj size is %d ",mradararray.obj_size());
  57. // qDebug("share time is %d ",gTime.elapsed());
  58. delete str;
  59. }
  60. void ProcRadarMsg(iv::can::canraw xmsg)
  61. {
  62. if(gbinit == false)gnNotSend = 10;
  63. if(gnNotSend > 0)
  64. {
  65. givlog->verbose("send valid data.");
  66. iv::can::canraw x;
  67. x.set_id(0x200);
  68. x.set_bext(false);
  69. x.set_bremote(false);
  70. x.set_len(8);
  71. unsigned char strdata[8];
  72. strdata[0] = 0x08;
  73. strdata[4] = 0x08;
  74. x.set_data(strdata,8);
  75. iv::can::canmsg xsend;
  76. iv::can::canraw * praw = xsend.add_rawmsg();
  77. praw->CopyFrom(x);
  78. int nsize = xsend.ByteSize();
  79. char * str = new char[nsize];
  80. if(xsend.SerializeToArray(str,nsize))
  81. {
  82. iv::modulecomm::ModuleSendMsg(shmRadar.mpa,str,nsize);
  83. }
  84. else
  85. {
  86. }
  87. delete str;
  88. gnNotSend--;
  89. }
  90. int i;
  91. if(xmsg.id() == 0x60A)
  92. {
  93. gbinit = true;
  94. mradararray.set_mstime(QDateTime::currentMSecsSinceEpoch());
  95. ShareResult();
  96. mradararray.clear_obj();
  97. }
  98. if(xmsg.id() == 0x60B)
  99. {
  100. if(xmsg.len()<8)
  101. {
  102. std::cout<<"ProcRadarMsg can message length is small "<<xmsg.len()<<std::endl;
  103. return;
  104. }
  105. char data[8];
  106. memcpy(data,xmsg.data().data(),8);
  107. unsigned char id;
  108. id = data[0];
  109. float y;
  110. float x;
  111. float vx,vy;
  112. unsigned char a,b;
  113. a = data[1];
  114. b = data[2];
  115. b = b&0xf8;
  116. y = a;
  117. y = y*256 + b;
  118. y = y/8;
  119. y = y*0.2 - 500;
  120. a = data[2];b = data[3];
  121. a = a&0x07;
  122. x = a;
  123. x = x*256 + b;
  124. x = x*0.2 -204.6;
  125. a = data[4];b = data[5];
  126. b = b&0xc0;b = b/64;
  127. vy = a; vy = vy*4 + b;
  128. vy = vy *0.25 - 128;
  129. a = data[5];b = data[6];
  130. a = a&0x3f;b = b&0xe0;b = b/32;
  131. vx = a; vx = vx*8 + b;
  132. vx = vx *0.25 - 64;
  133. iv::radar::radarobject xobj;
  134. xobj.set_bvalid(true);
  135. xobj.set_x(x*(-1));
  136. xobj.set_y(y);
  137. xobj.set_vx(vx*(-1));
  138. xobj.set_vy(vy);
  139. xobj.set_vel(vy);
  140. iv::radar::radarobject * pxobj = mradararray.add_obj();
  141. pxobj->CopyFrom(xobj);
  142. }
  143. }
  144. void DecodeRadar(iv::can::canmsg xmsgvetor)
  145. {
  146. int i;
  147. for(i=0;i<xmsgvetor.rawmsg_size();i++)
  148. {
  149. ProcRadarMsg(xmsgvetor.rawmsg(i));
  150. }
  151. }
  152. void ListenCANMsg(const char * strdata,const unsigned int nSize,const unsigned int index,const QDateTime * dt,const char * strmemname)
  153. {
  154. if(nSize<1)
  155. {
  156. std::cout<<"radar ListenCANMsg data empty."<<std::endl;
  157. return;
  158. }
  159. iv::can::canmsg xmsg;
  160. if(!xmsg.ParseFromArray(strdata,nSize))
  161. {
  162. givlog->error("radar Listencanmsg fail");
  163. gfault->SetFaultState(1, 0, "radar Listencanmsg error");
  164. std::cout<<"radar ListenCANMsg parse fail."<<std::endl;
  165. return;
  166. }
  167. CANRecv.Produce_Elements_From_CANMsg(xmsg);
  168. }
  169. void ListenGPSIMUMsg(const char * strdata,const unsigned int nSize,const unsigned int index,const QDateTime * dt,const char * strmemname)
  170. {
  171. if(nSize<1)
  172. {
  173. std::cout<<"radar ListenGPSIMUMsg data empty."<<std::endl;
  174. return;
  175. }
  176. iv::gps::gpsimu xdata;
  177. if(!xdata.ParseFromArray(strdata,nSize))
  178. {
  179. std::cout<<" radar ListenGPSIMUMsg parse fail."<<std::endl;
  180. return;
  181. }
  182. GPSIMUDataLock.lock();
  183. GPSIMUVelocity = xdata.speed(); //m/s
  184. GPSIMUYawRate = xdata.gyro_z(); //deg/s
  185. GPSIMUDataLock.unlock();
  186. }
  187. void ListenChassisMsg(const char * strdata,const unsigned int nSize,const unsigned int index,const QDateTime * dt,const char * strmemname)
  188. {
  189. if(nSize<1)
  190. {
  191. std::cout<<"radar ListenChassisMsg data empty."<<std::endl;
  192. return;
  193. }
  194. iv::chassis xdata;
  195. if(!xdata.ParseFromArray(strdata,nSize))
  196. {
  197. std::cout<<" radar ListenChassisMsg parse fail."<<std::endl;
  198. return;
  199. }
  200. chassisDataLock.lock();
  201. chassisShift = xdata.shift(); //1P 2R 3N 4D
  202. chassisVelocity = xdata.vel()/3.6; //km/h to m/s
  203. chassisDataLock.unlock();
  204. }
  205. void ExitFunc()
  206. {
  207. gApp->quit();
  208. std::this_thread::sleep_for(std::chrono::milliseconds(1000));
  209. }
  210. void signal_handler(int sig)
  211. {
  212. if(sig == SIGINT)
  213. {
  214. ExitFunc();
  215. }
  216. }
  217. int main(int argc, char *argv[])
  218. {
  219. RegisterIVBackTrace();
  220. showversion("driver_radar_continental_ARS408_SRR308");
  221. QCoreApplication a(argc, argv);
  222. gApp = &a;
  223. givlog = new iv::Ivlog("driver_radar_continental_ARS408_SRR308");
  224. gfault = new iv::Ivfault("driver_radar_continental_ARS408_SRR308");
  225. // iv::ivexit::RegIVExitCall(ExitFunc);
  226. signal(SIGINT,signal_handler);
  227. QString strpath = QCoreApplication::applicationDirPath();
  228. if(argc < 2)
  229. strpath = strpath + "/driver_radar_continental_ARS408_SRR308.xml";
  230. else
  231. strpath = argv[1];
  232. // givlog->verbose("%s", strpath.data());
  233. // std::cout<<strpath.toStdString()<<std::endl;
  234. decode_setup_cfg_from_xml(setupConfig,strpath.toStdString());
  235. if(setupConfig.radarCfgApply!=0)
  236. {
  237. RadarConfiguration_ch0_t radarConfiguration;
  238. decode_radar_cfg_from_yaml(radarConfiguration,setupConfig.radarCfgYAML_Path);
  239. }
  240. strncpy(shmCANSend.mstrmsgname,setupConfig.strMemCANSend.data(),255);
  241. shmCANSend.mnBufferSize = 100000;
  242. shmCANSend.mnBufferCount = 3;
  243. shmCANSend.mpa = iv::modulecomm::RegisterSend(shmCANSend.mstrmsgname,shmCANSend.mnBufferSize,shmCANSend.mnBufferCount);
  244. strncpy(shmRadar.mstrmsgname,setupConfig.strMemRadar.data(),255);
  245. shmRadar.mnBufferSize = 100000;
  246. shmRadar.mnBufferCount = 1;
  247. shmRadar.mpa = iv::modulecomm::RegisterSend(shmRadar.mstrmsgname,shmRadar.mnBufferSize,shmRadar.mnBufferCount);
  248. strncpy(shmCANRecv.mstrmsgname,setupConfig.strMemCANRecv.data(),255);
  249. shmCANRecv.mnBufferSize = 100000;
  250. shmCANRecv.mnBufferCount = 3;
  251. shmCANRecv.mpa = iv::modulecomm::RegisterRecv(shmCANRecv.mstrmsgname,ListenCANMsg);
  252. strncpy(shmGPSIMU.mstrmsgname,setupConfig.strMemGPSIMU.data(),255);
  253. shmGPSIMU.mnBufferSize = 100000;
  254. shmGPSIMU.mnBufferCount = 3;
  255. shmGPSIMU.mpa = iv::modulecomm::RegisterRecv(shmGPSIMU.mstrmsgname,ListenGPSIMUMsg);
  256. strncpy(shmChassis.mstrmsgname,setupConfig.strMemChassis.data(),255);
  257. shmChassis.mnBufferSize = 100000;
  258. shmChassis.mnBufferCount = 3;
  259. shmChassis.mpa = iv::modulecomm::RegisterRecv(shmChassis.mstrmsgname,ListenChassisMsg);
  260. // CANSend_Producer can_send_producer(&CANSend);
  261. // can_send_producer.start();
  262. // CANSend_Consumer can_send_consumer(&CANSend);
  263. // can_send_consumer.start();
  264. int rtn = a.exec();
  265. if(gfault != nullptr)delete gfault;
  266. if(givlog != nullptr)delete givlog;
  267. if(shmCANRecv.mpa != nullptr)iv::modulecomm::Unregister(shmCANRecv.mpa);
  268. if(shmCANSend.mpa != nullptr)iv::modulecomm::Unregister(shmCANSend.mpa);
  269. if(shmRadar.mpa != nullptr)iv::modulecomm::Unregister(shmRadar.mpa);
  270. if(shmGPSIMU.mpa != nullptr)iv::modulecomm::Unregister(shmGPSIMU.mpa);
  271. if(shmChassis.mpa != nullptr)iv::modulecomm::Unregister(shmChassis.mpa);
  272. return rtn;
  273. }