main.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528
  1. #include <QCoreApplication>
  2. #include <iostream>
  3. #include <QDateTime>
  4. #include <QDebug>
  5. #include "modulecomm.h"
  6. #include "xmlparam.h"
  7. #include "ivversion.h"
  8. #include "ivbacktrace.h"
  9. #include "canmsg.pb.h"
  10. #include "mobileye.pb.h"
  11. #include "mobileye_lane.pb.h"
  12. #include "mobileye_obs.pb.h"
  13. #include "mobileye_tsr.pb.h"
  14. #include "ivfault.h"
  15. #include "ivlog.h"
  16. int gntemp = 0;
  17. void * gpa;
  18. void * gpb;
  19. iv::Ivfault *gMobEyeFault = nullptr;
  20. iv::Ivlog *gMobEyeIvlog = nullptr;
  21. //static int gnNotSend = 10;
  22. QTime gTime;
  23. int8_t gnum_obstacles = 0;
  24. int8_t gobs_count = 0;
  25. int glane_sig = 0;
  26. //static bool gbinit = false;
  27. iv::mobileye::mobileye gmobileye;
  28. iv::mobileye::obs gobs;
  29. iv::mobileye::lane glane;
  30. void ShareResult()
  31. {
  32. char * str = new char[gmobileye.ByteSize()];
  33. int nsize = gmobileye.ByteSize();
  34. gobs.pos_y();
  35. if(gmobileye.SerializeToArray(str,nsize))
  36. {
  37. iv::modulecomm::ModuleSendMsg(gpa,str,nsize);
  38. }
  39. gMobEyeIvlog->info("mobileye","obj size is %d",gmobileye.xobj_size());
  40. // mMobEyeIvlog->info("share time is %d ",gTime.elapsed());
  41. delete str;
  42. }
  43. void ProcCANMsg(iv::can::canraw xmsg)
  44. {
  45. unsigned char xdata[8];
  46. memcpy(xdata,xmsg.data().data(),8);
  47. int32_t tmp;
  48. /******************************************************mobileye lane***********************************************************
  49. * message ID:0x737
  50. * member: type physical unit range note
  51. * curvature: double 1/meter [-0.12, 0.12] "a" in the equation:y = ax^2+bx+c
  52. * heading: double / [-1.0, 1.0] "b" in the equation:y = ax^2+bx+c
  53. * construction area: bool / / /
  54. * pitch angle: double radians [-0.05, 0.05] pitch of the host vehicle(derived from lanes analysis)
  55. * yaw angle: double radians / yaw of the host vehicle(derived from lanes analysis)
  56. * right_LDW: bit/bool / 0,1 0 stands for unavailable, 1 for available
  57. * left_LDW: bit/bool / 0,1 0 stands for unavailable, 1 for available
  58. *******************************************************************************************************************************/
  59. if(xmsg.id() == 0x737)
  60. {
  61. double curv, head, yaw, pitch;
  62. int ldw_left, ldw_right;
  63. // short ihead;
  64. // for(int i = 0; i< 8; i++)
  65. // {
  66. // gMobEyeIvlog->verbose("mobileye","i:%d %x %d\n ",i, xdata[i], xdata[i]);
  67. // }
  68. tmp = 0;
  69. //lane curv
  70. tmp = (xdata[1] << 8) | xdata[0];
  71. tmp <<=16;
  72. tmp >>=16;
  73. curv = tmp * 3.81 * 1e-6;
  74. tmp = 0;
  75. //lane heading
  76. tmp = (((xdata[3] & 0xf) << 8) | xdata[2]);
  77. tmp <<= 20;
  78. tmp >>= 20;
  79. head = tmp * 0.0005;
  80. tmp = 0;
  81. //construction area
  82. //is_ca = (xdata[3] >> 4) & 0x1;
  83. //left_LDW
  84. ldw_left = (xdata[3] >> 5) & 0x1;
  85. //right_LDW
  86. ldw_right = (xdata[3] >> 6) & 0x1;
  87. //yaw
  88. tmp = (xdata[5] << 8) | xdata[4];
  89. tmp <<= 16;
  90. tmp >>= 16;
  91. //ServiceCarStatus.Lane.yaw = (tmp - 0x7fff) / 1024.0;
  92. yaw = tmp * 0.000977;
  93. tmp = 0;
  94. //pitch
  95. tmp = (xdata[7] << 8) | xdata[6];
  96. tmp <<= 16;
  97. tmp >>= 16;
  98. //ServiceCarStatus.Lane.pitch = (tmp - 0x7fff) / 1024.0 / 512;
  99. pitch = tmp * 0.000002;
  100. gMobEyeIvlog->verbose("mobileye","%d ldw_left:%d, ldw_right:%d, curv:%f, head:%f, yaw:%f, pitch:%f \n ",\
  101. QTime::currentTime().toString("HH-mm-ss").data() ,ldw_left, ldw_right, curv, head, yaw, pitch);
  102. glane.set_bleftldwavil(ldw_left);
  103. glane.set_brightldwavil(ldw_right);
  104. glane.set_curvature(curv);
  105. glane.set_heading(head);
  106. glane.set_pitchang(pitch);
  107. glane.set_yawang(yaw);
  108. glane_sig = glane_sig | 0x01;
  109. // ShareResult();
  110. }
  111. /**************************************************** mobileye obstacle status ****************************************************************************
  112. * message ID:0x738
  113. * member: type physical unit range note
  114. * num_obstacles int / [0:255] /
  115. * timestamp int ms [0:255] only the lowest 8 bits of the timestamp is given
  116. * app_version: int / [0:255] vesion info consists of X.Y.Z.W
  117. * active_version int/2bits / [0:3] index of the active section of app_version signal
  118. * left_close_rang_cut_in bool / 0,1 0 false, 1 true
  119. * right_close_rang_cut_in bool / 0,1 0 false, 1 true
  120. * go enum / 0,1...15 0 stop, 1 go, 2 undecided, 3 driver decisions is required, 4-14 unused, 15 not calculated
  121. * protocol version uchar/8bit / 0x00...0xff /
  122. * is_close_car bool / 0,1 0 no close, 1 close car exists
  123. * failsafe int/4bit / [0:7] failsafe situation, 0000 no failsafe, 0001 low sun, 0010 blur image, 0100 1000 unused
  124. *************************************************************************************************************************************************************/
  125. else if(xmsg.id() == 0x738)
  126. {
  127. int num_obstacles;
  128. int msgtime;
  129. (void)msgtime;
  130. //num_obstacles
  131. num_obstacles = xdata[0];
  132. //std::cout << ServiceCarStatus.obstacleStatus.num_obstacles<< std::endl;
  133. gnum_obstacles = num_obstacles;
  134. gobs_count = 0;
  135. gmobileye.clear_xobj();
  136. gmobileye.clear_xmsgtime();
  137. gmobileye.clear_numobstacles();
  138. glane_sig = 0;
  139. //timestamp
  140. msgtime = xdata[1];
  141. //app version
  142. // ServiceCarStatus.obstacleStatus.app_version = xdata[2];
  143. // //active version
  144. // ServiceCarStatus.obstacleStatus.active_version = xdata[3] & 0x3;
  145. // //is left close rang cut in
  146. // ServiceCarStatus.obstacleStatus.is_left_close_rang_cut_in = (xdata[3] >> 2) & 0x1;
  147. // //is right close rang cut in
  148. // ServiceCarStatus.obstacleStatus.is_right_close_rang_cut_in = (xdata[3] >> 3) & 0x1;
  149. // //go
  150. // ServiceCarStatus.obstacleStatus.go = xdata[3] >> 4;
  151. // //protocol version
  152. // ServiceCarStatus.obstacleStatus.protocol_version = xdata[4];
  153. // //close car
  154. // ServiceCarStatus.obstacleStatus.is_close_car = xdata[5] & 0x1;
  155. // //failsafe
  156. // ServiceCarStatus.obstacleStatus.failsafe = (xdata[5] >> 1) & 0xf;
  157. gmobileye.set_numobstacles(num_obstacles);
  158. gmobileye.set_xmsgtime(QDateTime::currentMSecsSinceEpoch());
  159. glane_sig = glane_sig | 0x02;
  160. }
  161. /**************************************************** mobileye obstacle data a ****************************************************************************
  162. * message ID:0x739
  163. * member: type physical unit range note
  164. * obstacle_ID int / [0:63] new obstacles are given the last used free ID
  165. * obstacle_pos_x double m [0,250] longtitude position of the obstacle relative to the ref point
  166. * obstacle_pos_y double m [-31.93,31.93] lateral position of the obstacle, error relative to pos_x below 10% or 2meters
  167. * blinker_info int/enum / [0:4] 0 unavailable, 1 off, 2 left, 3 right, 4 both indicated blinkers status
  168. * cut_in_and_out int / [0:4] 0 undefined, 1 in_host_lane, 2 out_host_lane, 3 cut_in, 4 cut_out
  169. * cut_in: target entering the host lane, cut_out: exiting but not distinguish between sides
  170. * obstacle_rel_vel_x double m/s [-127.93,127.93] relative longtitude velocity of the obstacle
  171. * obstacle_status int/enum / [0:6] 0 undefined, 1 standing(never moved, back lights on), 2 stopped(moveable), 3 moving
  172. * 4 oncoming, 5 parked(never moved, back lights off), 6 unused
  173. * is_obstacle_brake_lights bool / 0,1 0 object's brake light off or not identified, 1 object's brake light on
  174. * obstacle_valid int/enum / [1:2] 1 new valid(detected this frame), 2 older valid
  175. *************************************************************************************************************************************************************/
  176. else if(xmsg.id() > 0x738 && xmsg.id() < 0x766)
  177. {
  178. if((xmsg.id()-0x739)%3 == 0)
  179. {
  180. //obstacle id
  181. gobs.set_id(xdata[0]);
  182. //obstacle pos x
  183. tmp = ((xdata[2] & 0xf) << 8) | xdata[1];
  184. gobs.set_pos_x(tmp * 0.0625);
  185. tmp = 0;
  186. //obstacle pos y
  187. tmp = ((xdata[4] & 0x3) << 8) | xdata[3];
  188. tmp <<= 22;
  189. tmp >>= 22;
  190. gobs.set_pos_y(tmp * 0.0625);
  191. //blinker info
  192. gobs.set_blinkerinfo((xdata[4] >> 2) & 0x7);
  193. //cut in and out
  194. gobs.set_cutstate(iv::mobileye::obs_CUTSTATE(xdata[4] >> 5));
  195. // gobs.set_cutstate(xdata[4] >> 5);
  196. //obstacle rel vel x
  197. tmp = ((xdata[6] & 0xf) << 8) | xdata[5];
  198. tmp <<= 20;
  199. tmp >>= 20;
  200. gobs.set_obs_rel_vel_x(tmp * 0.0625);
  201. //obstacle type
  202. gobs.set_obstype(iv::mobileye::obs_OBSTYPE((xdata[6] >> 4) & 0x7));
  203. //obstacle status
  204. gobs.set_obssatus(iv::mobileye::obs_OBSSTATE(xdata[7] & 0x7));
  205. //obstacle brake lights
  206. gobs.set_obsbrakelights((xdata[7] >> 3) & 0x1);
  207. //obstacle valid
  208. gobs.set_obsvalid((xdata[7] >> 6) & 0x7);
  209. }
  210. /**************************************************** mobileye obstacle data b ****************************************************************************
  211. * message ID:0x73a
  212. * member: type physical unit range note
  213. * obstacle_length double m [0,31] length of the obstacle(longitude axis)
  214. * obstacle_width double m [0,12.5] width of the obstacle(lateral axis)
  215. * obstacle_age int / [0:255] value starts at 1 when it is first detected, increaments in i each frame
  216. * obstacle_lane int/enum / [0:3] 0 not assigned, 1 ego lane, 2 next lane(left or right) or next next lane,
  217. * 3 invalid signal
  218. * is_cipv_flag int/enum / [0:1] 0 not cipv, 1 cipv(closest in path vehicle)
  219. * radar_pos_x double m [0,250] longtitude postion of the primary radar target matched to the vision target, dist=relative to ref point
  220. * if no radar target is matched, value = 0xfff
  221. * radar_vel_x double m/s [-127.93,127.93] longitude velocity of the radar target matched to the vision targets, if no radar target is matched value=0xfff
  222. * 4 oncoming, 5 parked(never moved, back lights off), 6 unused
  223. * radar_match_confidence int / [0:5] 0 no match, 1 multi match: radar doesn't describe well, 2-4 vision-radar match:with bounded error between
  224. * vision and radar measurements higher->smaller error, 5 high confidence match
  225. * matched_radar_id int / [0:127] ID of Primary radar target matched to the vision target if applicable
  226. *************************************************************************************************************************************************************/
  227. else if((xmsg.id()-0x73a)%3 == 0)
  228. {
  229. //obstacle length
  230. tmp = xdata[0];
  231. gobs.set_obslen(tmp * 0.5);
  232. tmp = 0;
  233. //obstacle width
  234. tmp = xdata[1];
  235. gobs.set_obswidth(tmp * 0.05);
  236. tmp = 0;
  237. //obstacle age
  238. gobs.set_obsage(xdata[2]);
  239. //obstacle lane
  240. gobs.set_obslane(xdata[3] & 0x3);
  241. //is cipv flag
  242. gobs.set_cipvflag((xdata[3] >> 2) & 0x1);
  243. //radar pos x
  244. tmp = (xdata[4] << 4) | (xdata[3] >> 4);
  245. gobs.set_radarposx(tmp * 0.0625);
  246. tmp = 0;
  247. //radar vel x
  248. tmp = ((xdata[6] & 0xf) << 8) | xdata[5];
  249. tmp <<= 20;
  250. tmp >>= 20;
  251. gobs.set_radarvelx(tmp * 0.0625);
  252. tmp = 0;
  253. //radar match confidence
  254. gobs.set_radarmatchconfi((xdata[6] >> 4) & 0x7);
  255. //matched radar ID
  256. gobs.set_matchedradarid(xdata[7] & 0x7f);
  257. }
  258. /**************************************************** mobileye obstacle data c ****************************************************************************
  259. * message ID:0x73b
  260. * member: type physical unit range note
  261. * obstacle_angle_rate double degree/s [-327.68, 327.68] Angle rate of Center of Obstacle, negative->moved to left
  262. * obstcale_scale_change double pix/s [-6.5532, 6.5532] /
  263. * object_accel_x double m/s2 [-14.97, 14.97] longtitude acceleration of the object
  264. * obstacle_replaced bool / 0,1 0 not replaced in this frame, 1 replace in this frame
  265. * obstacle_angle double degree [-327.68,327.68] Angle to Center of Obstacle in degrees, 0 indicates that the obstacle is in
  266. * exactly in front of us, a positive angle indicates that theobstacle is to the right
  267. *************************************************************************************************************************************************************/
  268. else if((xmsg.id()-0x73b)%3 == 0)
  269. {
  270. //obstacle angle rate
  271. tmp = (xdata[1] << 8) | xdata[0];
  272. tmp <<= 16;
  273. tmp >>= 16;
  274. gobs.set_obsangrate(tmp * 0.01);
  275. tmp = 0;
  276. //obstacle scale change
  277. tmp = (xdata[3] << 8) | xdata[2];
  278. tmp <<= 16;
  279. tmp >>= 16;
  280. gobs.set_obsscalechange(tmp * 0.0002);
  281. tmp = 0;
  282. //object accel x
  283. tmp = ((xdata[5] & 0x3) << 8) | xdata[4];
  284. tmp <<= 22;
  285. tmp >>= 22;
  286. gobs.set_accelx(tmp * 0.03);
  287. tmp = 0;
  288. //obstacle replaced
  289. gobs.set_obsreplaced((xdata[5] >> 4) & 0x1);
  290. //obstacle angle
  291. tmp = (xdata[7] << 8) | xdata[6];
  292. tmp <<= 16;
  293. tmp >>= 16;
  294. gobs.set_obsang(tmp * 0.01);
  295. iv::mobileye::obs * pxobs = gmobileye.add_xobj();
  296. pxobs->CopyFrom(gobs);
  297. gobs_count=(xmsg.id()-0x73b)/3+1;
  298. gMobEyeIvlog->verbose("mobileyeobs", "nums:%d cur_count:%d, obs_id:%d, pos_x:%f, pos_y:%f \n",\
  299. gnum_obstacles, gobs_count, gobs.id(), gobs.pos_x(), gobs.pos_y());
  300. }
  301. }
  302. /**************************************************** mobileye aftermarket lane ****************************************************************************
  303. * message ID:0x669
  304. * member: type physical unit range note
  305. * lane_conf_left int/2bit / [0:3] 0 lowest, 3 highest
  306. * is_ldw_availablity_left bool / 0,1 lane_conf>=2->on, speed>55km/h, configuration of LDW>=1
  307. * lane_type_left int/enum / [0:6] 0 dashed, 1 solid, 2 none, 3 road edge, 4 double lane mark, 5 sbott's dots, 6 invalid
  308. * dist_to_lane_l double m [-40:40] "c" in the equation:y = ax^2+bx+c
  309. * lane_conf_right int/2bit / [0:3] 0 lowest, 3 highest
  310. * is_ldw_availablity_right bool / 0,1 lane_conf>=2->on, speed>55km/h, configuration of LDW>=1
  311. * lane_type_right int/enum / [0:6] 0 dashed, 1 solid, 2 none, 3 road edge, 4 double lane mark, 5 sbott's dots, 6 invalid
  312. * dist_to_lane_r double m [-40:40] "c" in the equation:y = ax^2+bx+c
  313. *************************************************************************************************************************************************************/
  314. else if(xmsg.id() == 0x669)
  315. {
  316. //lane confidence left
  317. uint8_t conf;
  318. conf = xdata[0] & 0x3;
  319. // gMobEyeIvlog->error("mobileye","conf l %d",conf);
  320. glane.set_laneconfleft(conf);
  321. tmp = 0;
  322. //is LDW availability left
  323. glane.set_isldwavaileft((xdata[0] >> 2) & 0x1);
  324. //lane type left
  325. tmp = (xdata[0] >> 4) & 0xf;
  326. // tmp <<= 28;
  327. // tmp >>= 28;
  328. glane.set_lanetypeleft(tmp);
  329. tmp = 0;
  330. //dist to left lane
  331. tmp = (xdata[2] << 4) | ((xdata[1] >> 4) & 0xf);
  332. tmp <<= 20;
  333. tmp >>= 20;
  334. glane.set_disttolaneleft(tmp * 0.02);
  335. tmp = 0;
  336. //lane confidence right
  337. conf = xdata[5] & 0x3;
  338. // tmp <<= 30;
  339. // tmp >>= 30;
  340. glane.set_laneconfright(conf);
  341. // gMobEyeIvlog->error("mobileye","conf r %d",conf);
  342. tmp = 0;
  343. //is LDW availability right
  344. glane.set_isldwavairight((xdata[5] >> 2) & 0x1);
  345. //lane type right
  346. tmp = (xdata[5] >> 4) & 0xf;
  347. // tmp <<= 28;
  348. // tmp >>= 28;
  349. glane.set_lanetyperight(tmp);
  350. tmp = 0;
  351. //dist to right lane
  352. tmp = (xdata[7] << 4) | ((xdata[6] >> 4) & 0xf);
  353. tmp <<= 20;
  354. tmp >>= 20;
  355. glane.set_distolaneright(tmp * 0.02);
  356. gMobEyeIvlog->verbose("mobileye",\
  357. "aftermark lane:\n con_l:%d, ldw_avai_l:%d,type_l:%d, dist_to_l:%f \n con_r:%d, ldw_avai_r:%d, type_r:%d, dist_to_r:%f", \
  358. glane.laneconfleft(), glane.isldwavaileft(), glane.lanetypeleft(), glane.disttolaneleft(), \
  359. glane.laneconfright(), glane.isldwavairight(), glane.lanetyperight(), glane.distolaneright());
  360. }
  361. if (gobs_count == gnum_obstacles && glane_sig == 3)
  362. {
  363. gmobileye.clear_xlane();
  364. iv::mobileye::lane * pxlane = gmobileye.add_xlane();
  365. pxlane->CopyFrom(glane);
  366. ShareResult();
  367. gobs_count = 0;
  368. gmobileye.clear_xobj();
  369. gmobileye.clear_xmsgtime();
  370. gmobileye.clear_numobstacles();
  371. gnum_obstacles = 0;
  372. glane_sig = 0;
  373. }
  374. }
  375. void DecodeMsg(iv::can::canmsg xmsgvetor)
  376. {
  377. int i;
  378. for(i=0;i<xmsgvetor.rawmsg_size();i++)
  379. {
  380. ProcCANMsg(xmsgvetor.rawmsg(i));
  381. }
  382. }
  383. void Listencanmsg(const char * strdata,const unsigned int nSize,const unsigned int index,const QDateTime * dt,const char * strmemname)
  384. {
  385. (void)index;
  386. (void)dt;
  387. (void)strmemname;
  388. if(nSize<1)return;
  389. iv::can::canmsg xmsg;
  390. if(false == xmsg.ParseFromArray(strdata,nSize))
  391. {
  392. gMobEyeIvlog->error("mobileye","mobileye Listencanmsg fail");
  393. gMobEyeFault->SetFaultState(0, 0, "mobileye Listencanmsg fail!");
  394. return;
  395. }
  396. DecodeMsg(xmsg);
  397. gMobEyeFault->SetFaultState(0, 0, "ok");
  398. // qDebug("can size is %d",xmsg.rawmsg_size());
  399. // xt = QDateTime::currentMSecsSinceEpoch();
  400. // qDebug("latence = %ld ",xt-pic.time());
  401. }
  402. int main(int argc, char *argv[])
  403. {
  404. RegisterIVBackTrace();
  405. showversion("detection_mobileye");
  406. QCoreApplication a(argc, argv);
  407. gMobEyeFault = new iv::Ivfault("detection_mobileye");
  408. gMobEyeIvlog = new iv::Ivlog("detection_mobileye");
  409. QString strpath = QCoreApplication::applicationDirPath();
  410. if(argc < 2)
  411. strpath = strpath + "/detection_mobileye.xml";
  412. else
  413. strpath = argv[1];
  414. std::cout<<strpath.toStdString()<<std::endl;
  415. gMobEyeIvlog->info("%s", strpath.data());
  416. iv::xmlparam::Xmlparam xp(strpath.toStdString());
  417. std::string strmemcan = xp.GetParam("canrecv","canrecv1");
  418. std::string strmemsend = xp.GetParam("cansend","cansend1");
  419. std::string strmemmobileye = xp.GetParam("mobileye","mobileye");
  420. gTime.start();
  421. gpa = iv::modulecomm::RegisterSend(strmemmobileye.data(),100000,3);
  422. gpb = iv::modulecomm::RegisterSend(strmemsend.data(),100000,3);
  423. void * pa = iv::modulecomm::RegisterRecv(strmemcan.data(),Listencanmsg);
  424. (void)pa;
  425. return a.exec();
  426. }