main.cpp 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878
  1. /*
  2. * esmini - Environment Simulator Minimalistic
  3. * https://github.com/esmini/esmini
  4. *
  5. * This Source Code Form is subject to the terms of the Mozilla Public
  6. * License, v. 2.0. If a copy of the MPL was not distributed with this
  7. * file, You can obtain one at https://mozilla.org/MPL/2.0/.
  8. *
  9. * Copyright (c) partners of Simulation Scenarios
  10. * https://sites.google.com/view/simulationscenarios
  11. */
  12. /*
  13. * The purpose of this application is to support development of the RoadManager by visualizing the road network and moving objects on top.
  14. * Bascially it loads an OpenDRIVE file, and optionally a corresponding 3D model, and then populate vehicles at specified density. The
  15. * vehicles will simply follow it's lane until a potential junction where the choice of route is randomized.
  16. *
  17. * The application can be used both to debug the RoadManager and to check OpenDRIVE files, e.g. w.r.t. gemoetry, lanes and connectivity.
  18. *
  19. * New road/track segments is indicated by a yellow large dot. Geometry segments within a road are indicated by red dots.
  20. * Red line is the reference lane, blue lines shows drivable lanes. Non-drivable lanes are currently not indicated.
  21. */
  22. #include <random>
  23. #include <iostream>
  24. #define _USE_MATH_DEFINES
  25. #include <math.h>
  26. #include "viewer.hpp"
  27. #include "RoadManager.hpp"
  28. #include "CommonMini.hpp"
  29. #include "alog.h"
  30. #include "xmlparam.h"
  31. #include <QMutex>
  32. #include <QCoreApplication>
  33. #include <objectarray.pb.h>
  34. #define DEFAULT_SPEED 70 // km/h
  35. #define DEFAULT_DENSITY 1 // Cars per 100 m
  36. #define ROAD_MIN_LENGTH 30
  37. #define SIGN(X) ((X<0)?-1:1)
  38. static bool run_only_once = false;
  39. static const double stepSize = 0.01;
  40. static const double maxStepSize = 0.1;
  41. static const double minStepSize = 0.01;
  42. static const bool freerun = true;
  43. static std::mt19937 mt_rand;
  44. static double density = DEFAULT_DENSITY;
  45. static double speed = DEFAULT_SPEED;
  46. static double global_speed_factor = 1.0;
  47. static int first_car_in_focus = -1;
  48. double deltaSimTime; // external - used by Viewer::RubberBandCamera
  49. typedef struct
  50. {
  51. int road_id_init;
  52. int lane_id_init;
  53. double heading_init;
  54. double s_init;
  55. roadmanager::Position *pos;
  56. double speed_factor; // speed vary bewtween lanes, m/s
  57. viewer::EntityModel *model;
  58. int id;
  59. } Car;
  60. typedef struct
  61. {
  62. double mrel_x;
  63. double mrel_y;
  64. viewer::EntityModel * model;
  65. } RadarObj;
  66. typedef struct
  67. {
  68. double m_x;
  69. double m_y;
  70. double m_z;
  71. double mlength;
  72. double mwidth;
  73. double mheight;
  74. double mfYaw;
  75. viewer::EntityModel * model;
  76. } LidarObj;
  77. std::vector<Car*> cars;
  78. std::vector<RadarObj *> gradarobj;
  79. const int NRADARMX = 100;
  80. Car * gADCIV_car;
  81. viewer::EntityModel * gtestRadar;
  82. const int LIDARMAX = 300;
  83. iv::lidar::objectarray glidar;
  84. bool gbLidarupdate;
  85. QMutex gMutexLidar;
  86. std::vector<LidarObj *> glidarobj;
  87. // Car models used for populating the road network
  88. // path should be relative the OpenDRIVE file
  89. static const char* carModelsFiles_[] =
  90. {
  91. "car_white.osgb",
  92. "car_blue.osgb",
  93. "car_red.osgb",
  94. "car_yellow.osgb",
  95. "truck_yellow.osgb",
  96. "van_red.osgb",
  97. "bus_blue.osgb",
  98. "BMWZ4_10.hdr.ive",
  99. };
  100. std::vector<osg::ref_ptr<osg::LOD>> carModels_;
  101. #include "modulecomm.h"
  102. #include "gpsimu.pb.h"
  103. #include "radarobjectarray.pb.h"
  104. #include "gnss_coordinate_convert.h"
  105. double glat0 = 39.0677643;//39.1201777;
  106. double glon0 = 117.3548368;//117.02802270;
  107. double gfrel_x = 0;
  108. double gfrel_y = 0;
  109. double gfrel_z = 0;
  110. double gvehicle_hdg = 0;
  111. double gvehicleheight = 1.6;//7.6;//1.6
  112. double gfspeed = 0;
  113. void Listengpsimu(const char * strdata,const unsigned int nSize,const unsigned int index,const QDateTime * dt,const char * strmemname)
  114. {
  115. // ggpsimu->UpdateGPSIMU(strdata,nSize);
  116. iv::gps::gpsimu xgpsimu;
  117. if(!xgpsimu.ParseFromArray(strdata,nSize))
  118. {
  119. std::cout<<"ListenRaw Parse error."<<std::endl;
  120. return;
  121. }
  122. (void)&index;
  123. (void)dt;
  124. (void)strmemname;
  125. double x0,y0;
  126. double x,y;
  127. double rel_x, rel_y;
  128. GaussProjCal(glon0,glat0,&x0,&y0);
  129. GaussProjCal(xgpsimu.lon(),xgpsimu.lat(),&x,&y);
  130. rel_x = x - x0;
  131. rel_y = y - y0;
  132. gfrel_x = rel_x;
  133. gfrel_y = rel_y;
  134. gfrel_z = xgpsimu.height();
  135. if(xgpsimu.has_speed())
  136. {
  137. gfspeed = xgpsimu.speed();
  138. }
  139. else
  140. {
  141. gfspeed = 3.6*sqrt(pow(xgpsimu.vn(),2)+pow(xgpsimu.ve(),2));
  142. }
  143. double hdg = xgpsimu.heading();
  144. hdg = (90-hdg) * M_PI/180.0;
  145. if(hdg < 0)hdg = hdg + M_PI*2.0;
  146. gvehicle_hdg = hdg;
  147. }
  148. void ListenRADAR(const char * strdata,const unsigned int nSize,const unsigned int index,const QDateTime * dt,const char * strmemname)
  149. {
  150. static qint64 oldrecvtime;
  151. iv::radar::radarobjectarray xradararray;
  152. if(!xradararray.ParseFromArray(strdata,nSize))
  153. {
  154. std::cout<<"ListenRadar Parse Error."<<std::endl;
  155. return;
  156. }
  157. int i;
  158. for(i=0;i<xradararray.obj_size();i++)
  159. {
  160. iv::radar::radarobject * pobj = xradararray.mutable_obj(i);
  161. if(i<gradarobj.size())
  162. {
  163. if(pobj->bvalid())
  164. {
  165. gradarobj.at(i)->mrel_x = pobj->x();
  166. gradarobj.at(i)->mrel_y = pobj->y();
  167. }
  168. else
  169. {
  170. gradarobj.at(i)->mrel_x = 10000;
  171. gradarobj.at(i)->mrel_y = 10000;
  172. }
  173. }
  174. }
  175. for(i=xradararray.obj_size();i<gradarobj.size();i++)
  176. {
  177. gradarobj.at(i)->mrel_x = 10000;
  178. gradarobj.at(i)->mrel_y = 10000;
  179. }
  180. }
  181. void Listenlidarcnndectect(const char * strdata,const unsigned int nSize,const unsigned int index,const QDateTime * dt,const char * strmemname)
  182. {
  183. // std::vector<iv::lidar::object> lidarobjvec;
  184. // strtolidarobj(lidarobjvec,strdata,nSize);
  185. iv::lidar::objectarray lidarobjvec;
  186. std::string in;
  187. in.append(strdata,nSize);
  188. lidarobjvec.ParseFromString(in);
  189. gMutexLidar.lock();
  190. glidar.CopyFrom(lidarobjvec);
  191. gbLidarupdate = true;
  192. gMutexLidar.unlock();
  193. // qDebug("obj size is %d ",lidarobjvec.obj_size());
  194. // if(lidarobjvec.obj_size() > 1)
  195. // {
  196. // qDebug("type is %s",lidarobjvec.obj(0).type_name().data());
  197. // }
  198. // gw->UpdateLidarObj(lidarobjvec);
  199. }
  200. void log_callback(const char *str)
  201. {
  202. printf("%s\n", str);
  203. }
  204. #include <osgDB/ReadFile>
  205. #include <osg/ComputeBoundsVisitor>
  206. #include <osg/LineWidth>
  207. #include <osg/Point>
  208. #include <osg/BlendFunc>
  209. #include <osg/BlendColor>
  210. #include <osg/Geode>
  211. #include <osg/Group>
  212. #include <osg/ShapeDrawable>
  213. #include <osg/CullFace>
  214. #include <osgGA/StateSetManipulator>
  215. #include <osgGA/TrackballManipulator>
  216. #include <osgGA/KeySwitchMatrixManipulator>
  217. #include <osgGA/FlightManipulator>
  218. #include <osgGA/DriveManipulator>
  219. #include <osgGA/TerrainManipulator>
  220. #include <osgGA/SphericalManipulator>
  221. #include <osgViewer/ViewerEventHandlers>
  222. #include <osgDB/Registry>
  223. #include <osgShadow/StandardShadowMap>
  224. #include <osgShadow/ShadowMap>
  225. #include <osgShadow/ShadowedScene>
  226. #include <osgUtil/SmoothingVisitor>
  227. #include <osgUtil/Tessellator> // to tessellate multiple contours
  228. void createosgbox()
  229. {
  230. osg::ref_ptr<osg::Group> group = 0;
  231. osg::ref_ptr<osg::Geode> geode = new osg::Geode;
  232. geode->addDrawable(new osg::ShapeDrawable(new osg::Box()));
  233. osg::ref_ptr<osg::Group> bbGroup = new osg::Group;
  234. osg::ref_ptr<osg::PositionAttitudeTransform> tx = new osg::PositionAttitudeTransform;
  235. osg::Material* material = new osg::Material();
  236. // Set color of vehicle based on its index
  237. double* color;
  238. double b = 1.5; // brighness
  239. int index = 2;
  240. if (index == 0) color = color_white;
  241. else if (index == 1) color = color_red;
  242. else if (index == 2) color = color_blue;
  243. else color = color_yellow;
  244. material->setDiffuse(osg::Material::FRONT, osg::Vec4(b * color[0], b * color[1], b * color[2], 1.0));
  245. material->setAmbient(osg::Material::FRONT, osg::Vec4(b * color[0], b * color[1], b * color[2], 1.0));
  246. osg::ref_ptr<osg::Geode> geodeCopy = dynamic_cast<osg::Geode*>(geode->clone(osg::CopyOp::DEEP_COPY_ALL));
  247. group = new osg::Group;
  248. tx->addChild(geodeCopy);
  249. geodeCopy->setNodeMask(viewer::NodeMask::NODE_MASK_ENTITY_MODEL);
  250. }
  251. int SetupADCIVCar( viewer::Viewer *viewer)
  252. {
  253. // int carModelID = 2;
  254. int carModelID = 7;
  255. Car *car_ = new Car;
  256. // Higher speeds in lanes closer to reference lane
  257. car_->speed_factor = 0.5 ; // Speed vary between 0.5 to 1.0 times default speed
  258. car_->road_id_init = 0;
  259. car_->lane_id_init = 0;
  260. car_->s_init = 0;
  261. // car_->pos = new roadmanager::Position(odrManager->GetRoadByIdx(r)->GetId(), lane->GetId(), s, 0);
  262. // car_->pos->SetHeadingRelative(lane->GetId() < 0 ? 0 : M_PI);
  263. // car_->heading_init = car_->pos->GetHRelative();
  264. // viewer->AddOSGBEnv("/home/nvidia/xiali_fangzhen.opt.osgb",osg::Vec3(1.0, 1.0, 1.0),"env");
  265. // viewer->AddOSGBEnv("/home/nvidia/xiali_fangzhen.shadow.osgb",osg::Vec3(0.5, 0.5, 0.5),"env1");
  266. // viewer->AddOSGBEnv("/home/nvidia/BMWZ4_10.hdr.ive",osg::Vec3(1.0, 1.0, 1.0),"env");
  267. if ((car_->model = viewer->AddEntityModel(carModelsFiles_[carModelID], osg::Vec3(0.5, 0.5, 0.5),
  268. viewer::EntityModel::EntityType::ENTITY_TYPE_VEHICLE, false, "", 0)) == 0)
  269. {
  270. return -1;
  271. }
  272. car_->id = 0;
  273. if (first_car_in_focus == -1 )
  274. {
  275. first_car_in_focus = car_->id;
  276. }
  277. gADCIV_car = car_;
  278. return 0;
  279. }
  280. int SetupRadar(viewer::Viewer *viewer)
  281. {
  282. int i;
  283. for(i=0;i<NRADARMX;i++)
  284. {
  285. RadarObj * pRadar = new RadarObj;
  286. pRadar->mrel_x = 10000;
  287. pRadar->mrel_y = 10000;
  288. pRadar->model = viewer->AddRadarModel(osg::Vec3(0.5, 0.5, 0.5),"");
  289. gradarobj.push_back(pRadar);
  290. }
  291. // viewer->AddBackModel(osg::Vec3(0.5, 0.5, 0.5),"");
  292. return 0;
  293. }
  294. int SetupLidar(viewer::Viewer *viewer)
  295. {
  296. int i;
  297. for(i=0;i<LIDARMAX;i++)
  298. {
  299. LidarObj * pLidar = new LidarObj;
  300. pLidar->m_x = 10000;
  301. pLidar->m_y = 10000;
  302. pLidar->model = viewer->AddLidarModel(osg::Vec3(0.5, 0.5, 0.5),"",0.01,0.01,0.01);
  303. glidarobj.push_back(pLidar);
  304. }
  305. return 0;
  306. }
  307. int SetupCars(roadmanager::OpenDrive *odrManager, viewer::Viewer *viewer)
  308. {
  309. if (density < 1E-10)
  310. {
  311. // Basically no scenario vehicles
  312. return 0;
  313. }
  314. // for (int r = 0; r < 10; r++)
  315. for (int r = 0; r < odrManager->GetNumOfRoads(); r++)
  316. {
  317. roadmanager::Road *road = odrManager->GetRoadByIdx(r);
  318. double average_distance = 100.0 / density;
  319. if (road->GetLength() > ROAD_MIN_LENGTH)
  320. {
  321. // Populate road lanes with vehicles at some random distances
  322. for (double s = 10; s < road->GetLength() - average_distance; s += average_distance + 0.2 * average_distance * mt_rand() / mt_rand.max())
  323. {
  324. // Pick lane by random
  325. int lane_idx = ((double)road->GetNumberOfDrivingLanes(s) * mt_rand()) / (mt_rand.max)();
  326. roadmanager::Lane *lane = road->GetDrivingLaneByIdx(s, lane_idx);
  327. if (lane == 0)
  328. {
  329. LOG("Failed locate driving lane %d at s %d", lane_idx, s);
  330. continue;
  331. }
  332. if ((SIGN(lane->GetId()) < 0) && (road->GetLength() - s < 50) && (road->GetLink(roadmanager::LinkType::SUCCESSOR) == 0) ||
  333. (SIGN(lane->GetId()) > 0) && (s < 50) && (road->GetLink(roadmanager::LinkType::PREDECESSOR) == 0))
  334. {
  335. // Skip vehicles too close to road end - and where connecting road is missing
  336. continue;
  337. }
  338. // randomly choose model
  339. int carModelID = (double(sizeof(carModelsFiles_) / sizeof(carModelsFiles_[0])) * mt_rand()) / (mt_rand.max)();
  340. LOG("Adding car of model %d to road nr %d (road id %d s %.2f lane id %d), ", carModelID, r, road->GetId(), s, lane->GetId());
  341. carModelID = 2;
  342. Car *car_ = new Car;
  343. // Higher speeds in lanes closer to reference lane
  344. car_->speed_factor = 0.5 + 0.5 / abs(lane->GetId()); // Speed vary between 0.5 to 1.0 times default speed
  345. car_->road_id_init = odrManager->GetRoadByIdx(r)->GetId();
  346. car_->lane_id_init = lane->GetId();
  347. car_->s_init = s;
  348. car_->pos = new roadmanager::Position(odrManager->GetRoadByIdx(r)->GetId(), lane->GetId(), s, 0);
  349. car_->pos->SetHeadingRelative(lane->GetId() < 0 ? 0 : M_PI);
  350. car_->heading_init = car_->pos->GetHRelative();
  351. if ((car_->model = viewer->AddEntityModel(carModelsFiles_[carModelID], osg::Vec3(0.5, 0.5, 0.5),
  352. viewer::EntityModel::EntityType::ENTITY_TYPE_VEHICLE, false, "", 0)) == 0)
  353. {
  354. return -1;
  355. }
  356. car_->id = cars.size();
  357. cars.push_back(car_);
  358. if (first_car_in_focus == -1 && lane->GetId() < 0)
  359. {
  360. first_car_in_focus = car_->id;
  361. }
  362. }
  363. }
  364. }
  365. if (first_car_in_focus == -1)
  366. {
  367. first_car_in_focus = 0;
  368. }
  369. return 0;
  370. }
  371. int SetupCarsSpecial(roadmanager::OpenDrive* odrManager, viewer::Viewer* viewer)
  372. {
  373. // Setup one single vehicle in a dedicated pos
  374. Car* car_ = new Car;
  375. car_->speed_factor = 1.0;
  376. car_->road_id_init = 1;
  377. car_->lane_id_init = -1;
  378. car_->s_init = 40;
  379. car_->pos = new roadmanager::Position(car_->road_id_init, car_->lane_id_init, car_->s_init, 0);
  380. car_->pos->SetHeadingRelative(car_->lane_id_init < 0 ? 0 : M_PI);
  381. car_->heading_init = car_->pos->GetHRelative();
  382. if ((car_->model = viewer->AddEntityModel(carModelsFiles_[0], osg::Vec3(0.5, 0.5, 0.5),
  383. viewer::EntityModel::EntityType::ENTITY_TYPE_VEHICLE, false, "", 0)) == 0)
  384. {
  385. return -1;
  386. }
  387. car_->id = cars.size();
  388. cars.push_back(car_);
  389. first_car_in_focus = 0;
  390. return 0;
  391. }
  392. void updateADCIVCar(Car * car,viewer::Viewer *viewer)
  393. {
  394. static float x=0;
  395. static float y=0;
  396. static float z=0;
  397. static float head = M_PI/2.0;
  398. static float pitch =0;
  399. static float roll = 0;
  400. if (car->model->txNode_ != 0)
  401. {
  402. // car->model->txNode_->setPosition(osg::Vec3(car->pos->GetX(), car->pos->GetY(), car->pos->GetZ()));
  403. // car->model->quat_.makeRotate(
  404. // car->pos->GetR(), osg::Vec3(1, 0, 0),
  405. // car->pos->GetP(), osg::Vec3(0, 1, 0),
  406. // car->pos->GetH(), osg::Vec3(0, 0, 1));
  407. y = y+ 0.1;
  408. x = gfrel_x;
  409. y = gfrel_y;
  410. z = gfrel_z + gvehicleheight;
  411. head = gvehicle_hdg;
  412. car->model->txNode_->setPosition(osg::Vec3(x, y, z));
  413. car->model->quat_.makeRotate(
  414. roll, osg::Vec3(1, 0, 0),
  415. pitch, osg::Vec3(0, 1, 0),
  416. head -M_PI/2.0, osg::Vec3(0, 0, 1));//改车头方向
  417. car->model->txNode_->setAttitude(car->model->quat_);
  418. int k;
  419. // head = head +M_PI/2.0;
  420. for(k=0;k<gradarobj.size();k++)
  421. {
  422. double relx,rely;
  423. relx = gradarobj.at(k)->mrel_y*cos(head) + gradarobj.at(k)->mrel_x * sin(head);
  424. rely = gradarobj.at(k)->mrel_y*sin(head) - gradarobj.at(k)->mrel_x*cos(head);
  425. gradarobj.at(k)->model->txNode_->setPosition(osg::Vec3(x+relx, y+rely, z));
  426. gradarobj.at(k)->model->quat_.makeRotate(
  427. roll, osg::Vec3(1, 0, 0),
  428. pitch, osg::Vec3(0, 1, 0),
  429. head, osg::Vec3(0, 0, 1));
  430. gradarobj.at(k)->model->txNode_->setAttitude(car->model->quat_);
  431. }
  432. bool bLidarUpdate = gbLidarupdate;
  433. if(bLidarUpdate)
  434. {
  435. gMutexLidar.lock();
  436. for(k=0;k<glidar.obj_size();k++)
  437. {
  438. iv::lidar::lidarobject * pobj = glidar.mutable_obj(k);
  439. if(k<LIDARMAX)
  440. {
  441. glidarobj.at(k)->m_x = pobj->position().x();
  442. glidarobj.at(k)->m_y = pobj->position().y();
  443. glidarobj.at(k)->m_z = pobj->position().z();
  444. glidarobj.at(k)->mlength = pobj->dimensions().x();
  445. glidarobj.at(k)->mwidth = pobj->dimensions().y();
  446. glidarobj.at(k)->mheight = pobj->dimensions().z();
  447. glidarobj.at(k)->mfYaw = pobj->tyaw();
  448. }
  449. }
  450. for(k=glidar.obj_size();k<LIDARMAX;k++)
  451. {
  452. glidarobj.at(k)->m_x = 10000;
  453. glidarobj.at(k)->m_y = 10000;
  454. glidarobj.at(k)->m_z = 10000;
  455. glidarobj.at(k)->mlength = 0.01;
  456. glidarobj.at(k)->mwidth = 0.01;
  457. glidarobj.at(k)->mheight = 0.01;
  458. }
  459. gbLidarupdate = false;
  460. gMutexLidar.unlock();
  461. for(k=0;k<LIDARMAX;k++)
  462. {
  463. double rel_x,rel_y,rel_z;
  464. // rel_x = glidarobj.at(k)->m_x;
  465. // rel_y = glidarobj.at(k)->m_y;
  466. rel_z = glidarobj.at(k)->m_z;
  467. rel_x = glidarobj.at(k)->m_y*cos(head) + glidarobj.at(k)->m_x * sin(head);
  468. rel_y = glidarobj.at(k)->m_y*sin(head) - glidarobj.at(k)->m_x*cos(head);
  469. glidarobj.at(k)->m_x = x + rel_x;
  470. glidarobj.at(k)->m_y = y + rel_y;
  471. glidarobj.at(k)->m_z = z + rel_z;
  472. // if(glidarobj.at(k)->mlength>0.1)qDebug("len is %f width is %f ,height is %f",glidarobj.at(k)->mlength,glidarobj.at(k)->mwidth,glidarobj.at(k)->mheight);
  473. glidarobj.at(k)->model->ChangeScale(glidarobj.at(k)->mlength,glidarobj.at(k)->mwidth,glidarobj.at(k)->mheight);
  474. glidarobj.at(k)->model->txNode_->setPosition(osg::Vec3(glidarobj.at(k)->m_x, glidarobj.at(k)->m_y, glidarobj.at(k)->m_z));
  475. glidarobj.at(k)->model->quat_.makeRotate(
  476. roll, osg::Vec3(1, 0, 0),
  477. pitch, osg::Vec3(0, 1, 0),
  478. head + glidarobj.at(k)->mfYaw, osg::Vec3(0, 0, 1));
  479. glidarobj.at(k)->model->txNode_->setAttitude(car->model->quat_);
  480. }
  481. }
  482. }
  483. }
  484. void updateCar(roadmanager::OpenDrive *odrManager, Car *car, double dt)
  485. {
  486. double new_speed = car->pos->GetSpeedLimit() * car->speed_factor * global_speed_factor;
  487. double ds = new_speed * dt; // right lane is < 0 in road dir;
  488. if (car->pos->MoveAlongS(ds) != 0)
  489. {
  490. // Start from beginning of lane section - not initial s-position
  491. roadmanager::LaneSection* ls = odrManager->GetRoadById(car->road_id_init)->GetLaneSectionByS(car->s_init);
  492. double start_s = ls->GetS() + 5;
  493. if (car->lane_id_init > 0)
  494. {
  495. start_s = ls->GetS() + ls->GetLength() - 5;
  496. }
  497. car->pos->SetLanePos(car->road_id_init, car->lane_id_init, start_s, 0);
  498. car->pos->SetHeadingRelative(car->heading_init);
  499. }
  500. if (car->model->txNode_ != 0)
  501. {
  502. car->model->txNode_->setPosition(osg::Vec3(car->pos->GetX(), car->pos->GetY(), car->pos->GetZ()));
  503. car->model->quat_.makeRotate(
  504. car->pos->GetR(), osg::Vec3(1, 0, 0),
  505. car->pos->GetP(), osg::Vec3(0, 1, 0),
  506. car->pos->GetH(), osg::Vec3(0, 0, 1));
  507. car->model->txNode_->setAttitude(car->model->quat_);
  508. }
  509. }
  510. int main(int argcx, char** argvx)
  511. {
  512. static char str_buf[128];
  513. SE_Options opt;
  514. int argc;
  515. char** argv;
  516. argc = 3;
  517. argv = new char*[3];
  518. argv[0] = "ui_osgviewer";
  519. argv[1] = "--odr";
  520. argv[2] = "/home/nvidia/map/map.xodr";
  521. std::string mappath = getenv("HOME");
  522. // mappath = mappath + "/map/mapa.xodr";
  523. mappath = mappath + "/map/map.xodr";
  524. argv[2] = new char[256];
  525. strncpy(argv[2],mappath.c_str(),256);
  526. QString strpath = QCoreApplication::applicationDirPath();
  527. // QString apppath = strpath;
  528. if(argcx < 2)
  529. strpath = strpath + "/ui_osgviewer.xml";
  530. else
  531. strpath = argvx[1];
  532. std::cout<<strpath.toStdString()<<std::endl;
  533. iv::xmlparam::Xmlparam xp(strpath.toStdString());
  534. std::string strgpsmsg = xp.GetParam("gpsmsg","hcp2_gpsimu");
  535. std::string strradarmsg = xp.GetParam("radarmsg","radar");
  536. std::string strcnnmsg = xp.GetParam("cnnmsg","lidar_track");
  537. std::string strheightajust = xp.GetParam("heightajust","-1.6");
  538. gvehicleheight = atof(strheightajust.data());
  539. void * pa = iv::modulecomm::RegisterRecv(strgpsmsg.data(),Listengpsimu);
  540. pa = iv::modulecomm::RegisterRecv(strradarmsg.data(),ListenRADAR);
  541. pa = iv::modulecomm::RegisterRecv(strcnnmsg.data(),Listenlidarcnndectect);
  542. (void)pa;
  543. // Use logger callback
  544. Logger::Inst().SetCallback(log_callback);
  545. mt_rand.seed((unsigned int)time(0));
  546. std::vector<std::string> args;
  547. for (int i = 0; i < argc; i++) args.push_back(argv[i]);
  548. // use an ArgumentParser object to manage the program arguments.
  549. opt.AddOption("odr", "OpenDRIVE filename", "odr_filename");
  550. opt.AddOption("model", "3D Model filename", "model_filename");
  551. opt.AddOption("density", "density (cars / 100 m)", "density");
  552. opt.AddOption("speed_factor", "speed_factor <number>", "speed_factor");
  553. opt.AddOption("osi_lines", "Show OSI road lines (toggle during simulation by press 'u') ");
  554. opt.AddOption("osi_points", "Show OSI road points (toggle during simulation by press 'y') ");
  555. opt.AddOption("road_features", "Show OpenDRIVE road features (toggle during simulation by press 'o') ");
  556. opt.AddOption("path", "Search path prefix for assets, e.g. car and sign model files", "path");
  557. opt.AddOption("logfile_path", "logfile path/filename, e.g. \"../esmini.log\" (default: log.txt)", "path");
  558. opt.AddOption("disable_log", "Prevent logfile from being created");
  559. opt.AddOption("help", "Show this help message");
  560. if (argc < 2 || opt.GetOptionSet("help"))
  561. {
  562. opt.PrintUsage();
  563. return -1;
  564. }
  565. opt.ParseArgs(&argc, argv);
  566. std::string arg_str;
  567. if (opt.GetOptionSet("disable_log"))
  568. {
  569. SE_Env::Inst().SetLogFilePath("");
  570. printf("Disable logfile\n");
  571. }
  572. else if (opt.IsOptionArgumentSet("logfile_path"))
  573. {
  574. arg_str = opt.GetOptionArg("logfile_path");
  575. SE_Env::Inst().SetLogFilePath(arg_str);
  576. if (arg_str.empty())
  577. {
  578. printf("Custom logfile path empty, disable logfile\n");
  579. }
  580. else
  581. {
  582. printf("Custom logfile path: %s\n", arg_str.c_str());
  583. }
  584. }
  585. Logger::Inst().OpenLogfile();
  586. if ((arg_str = opt.GetOptionArg("path")) != "")
  587. {
  588. SE_Env::Inst().AddPath(arg_str);
  589. LOG("Added path %s", arg_str.c_str());
  590. }
  591. std::string odrFilename = opt.GetOptionArg("odr");
  592. // odrFilename = "/home/yuchuli/map/models/mapxiali.xodr";
  593. if (odrFilename.empty())
  594. {
  595. printf("Missing required argument --odr\n");
  596. opt.PrintUsage();
  597. return -1;
  598. }
  599. std::string modelFilename = opt.GetOptionArg("model");
  600. if (opt.GetOptionArg("density") != "")
  601. {
  602. density = strtod(opt.GetOptionArg("density"));
  603. }
  604. printf("density: %.2f\n", density);
  605. if (opt.GetOptionArg("speed_factor") != "")
  606. {
  607. global_speed_factor = strtod(opt.GetOptionArg("speed_factor"));
  608. }
  609. printf("global speed factor: %.2f\n", global_speed_factor);
  610. roadmanager::Position *lane_pos = new roadmanager::Position();
  611. roadmanager::Position *track_pos = new roadmanager::Position();
  612. try
  613. {
  614. if (!roadmanager::Position::LoadOpenDrive(odrFilename.c_str()))
  615. {
  616. printf("Failed to load ODR %s\n", odrFilename.c_str());
  617. return -1;
  618. }
  619. roadmanager::OpenDrive *odrManager = roadmanager::Position::GetOpenDrive();
  620. osg::ArgumentParser arguments(&argc, argv);
  621. viewer::Viewer *viewer = new viewer::Viewer(
  622. odrManager,
  623. modelFilename.c_str(),
  624. NULL,
  625. argv[0],
  626. arguments,
  627. &opt);
  628. viewer->SetWindowTitleFromArgs(args);
  629. if (opt.GetOptionSet("road_features"))
  630. {
  631. viewer->SetNodeMaskBits(viewer::NodeMask::NODE_MASK_ODR_FEATURES);
  632. }
  633. else
  634. {
  635. viewer->ClearNodeMaskBits(viewer::NodeMask::NODE_MASK_ODR_FEATURES);
  636. }
  637. if (opt.GetOptionSet("osi_lines"))
  638. {
  639. viewer->SetNodeMaskBits(viewer::NodeMask::NODE_MASK_OSI_LINES);
  640. }
  641. if (opt.GetOptionSet("osi_points"))
  642. {
  643. viewer->SetNodeMaskBits(viewer::NodeMask::NODE_MASK_OSI_POINTS);
  644. }
  645. printf("osi_features: lines %s points %s \n",
  646. viewer->GetNodeMaskBit(viewer::NodeMask::NODE_MASK_OSI_LINES) ? "on" : "off",
  647. viewer->GetNodeMaskBit(viewer::NodeMask::NODE_MASK_OSI_POINTS) ? "on" : "off");
  648. SetupADCIVCar(viewer);
  649. SetupRadar(viewer);
  650. // SetupLidar(viewer);
  651. // viewer->AddBackModel(osg::Vec3(0.5, 0.5, 0.5),"");
  652. // gtestRadar = viewer->AddRadarModel(osg::Vec3(0.5, 0.5, 0.5),"");
  653. // gtestRadar = viewer->AddEntityModel(carModelsFiles_[0], osg::Vec3(0.5, 0.5, 0.5),
  654. // viewer::EntityModel::EntityType::ENTITY_TYPE_VEHICLE, false, "", 0);
  655. // if (SetupCars(odrManager, viewer) == -1)
  656. // {
  657. // return 4;
  658. // }
  659. printf("%d cars added\n", (int)cars.size());
  660. viewer->SetVehicleInFocus(first_car_in_focus);
  661. __int64 now, lastTimeStamp = 0;
  662. static bool first_time = true;
  663. while (!viewer->osgViewer_->done())
  664. {
  665. // Get milliseconds since Jan 1 1970
  666. now = SE_getSystemTime();
  667. deltaSimTime = (now - lastTimeStamp) / 1000.0; // step size in seconds
  668. lastTimeStamp = now;
  669. if (deltaSimTime > maxStepSize) // limit step size
  670. {
  671. deltaSimTime = maxStepSize;
  672. }
  673. else if (deltaSimTime < minStepSize) // avoid CPU rush, sleep for a while
  674. {
  675. SE_sleep(now - lastTimeStamp);
  676. deltaSimTime = minStepSize;
  677. }
  678. if (!(run_only_once && !first_time))
  679. {
  680. updateADCIVCar(gADCIV_car,viewer);
  681. for (size_t i = 0; i < cars.size(); i++)
  682. {
  683. updateCar(odrManager, cars[i], deltaSimTime);
  684. }
  685. first_time = false;
  686. }
  687. snprintf(str_buf, sizeof(str_buf), "(%6.2f,%6.2f,%6.2f,%6.2f) (%6.2fkm/h)", gfrel_x,
  688. gfrel_y,gfrel_z,gvehicle_hdg,gfspeed);
  689. viewer->SetInfoText(str_buf);
  690. // Set info text
  691. if (cars.size() > 0 && viewer->currentCarInFocus_ >= 0 && viewer->currentCarInFocus_ < cars.size())
  692. {
  693. Car* car = cars[viewer->currentCarInFocus_];
  694. snprintf(str_buf, sizeof(str_buf), "entity[%d]: %.2fkm/h (%d, %d, %.2f, %.2f) / (%.2f, %.2f %.2f)", viewer->currentCarInFocus_,
  695. 3.6 * car->pos->GetSpeedLimit() * car->speed_factor * global_speed_factor, car->pos->GetTrackId(), car->pos->GetLaneId(),
  696. fabs(car->pos->GetOffset()) < SMALL_NUMBER ? 0 : car->pos->GetOffset(), car->pos->GetS(), car->pos->GetX(), car->pos->GetY(), car->pos->GetH());
  697. viewer->SetInfoText(str_buf);
  698. }
  699. // Step the simulation
  700. viewer->osgViewer_->frame();
  701. }
  702. delete viewer;
  703. }
  704. catch (std::logic_error &e)
  705. {
  706. printf("%s\n", e.what());
  707. return 2;
  708. }
  709. catch (std::runtime_error &e)
  710. {
  711. printf("%s\n", e.what());
  712. return 3;
  713. }
  714. for (size_t i = 0; i < cars.size(); i++)
  715. {
  716. delete(cars[i]);
  717. }
  718. delete track_pos;
  719. delete lane_pos;
  720. return 0;
  721. }