main.cpp 22 KB

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