mainwindow.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  1. #include "mainwindow.h"
  2. #include "ui_mainwindow.h"
  3. #include <pcl/io/ply_io.h>
  4. #include <pcl/io/obj_io.h>
  5. #include <QMessageBox>
  6. static pose gCurPose;
  7. static double gView_Z = 100.0;
  8. static bool gbAutoCam = true;
  9. static int gnViewMode = 0;
  10. static double gfViewAngle = 30.0;
  11. #include <cmath>
  12. struct Quaternion {
  13. double w, x, y, z;
  14. };
  15. struct EulerAngles {
  16. double roll, pitch, yaw;
  17. };
  18. EulerAngles ToEulerAngles(Quaternion q) {
  19. EulerAngles angles;
  20. // roll (x-axis rotation)
  21. double sinr_cosp = 2 * (q.w * q.x + q.y * q.z);
  22. double cosr_cosp = 1 - 2 * (q.x * q.x + q.y * q.y);
  23. angles.roll = std::atan2(sinr_cosp, cosr_cosp);
  24. // pitch (y-axis rotation)
  25. double sinp = 2 * (q.w * q.y - q.z * q.x);
  26. if (std::abs(sinp) >= 1)
  27. angles.pitch = std::copysign(M_PI / 2, sinp); // use 90 degrees if out of range
  28. else
  29. angles.pitch = std::asin(sinp);
  30. // yaw (z-axis rotation)
  31. double siny_cosp = 2 * (q.w * q.z + q.x * q.y);
  32. double cosy_cosp = 1 - 2 * (q.y * q.y + q.z * q.z);
  33. angles.yaw = std::atan2(siny_cosp, cosy_cosp);
  34. return angles;
  35. }
  36. Quaternion ToQuaternion(double yaw, double pitch, double roll) // yaw (Z), pitch (Y), roll (X)
  37. {
  38. // Abbreviations for the various angular functions
  39. double cy = cos(yaw * 0.5);
  40. double sy = sin(yaw * 0.5);
  41. double cp = cos(pitch * 0.5);
  42. double sp = sin(pitch * 0.5);
  43. double cr = cos(roll * 0.5);
  44. double sr = sin(roll * 0.5);
  45. Quaternion q;
  46. q.w = cy * cp * cr + sy * sp * sr;
  47. q.x = cy * cp * sr - sy * sp * cr;
  48. q.y = sy * cp * sr + cy * sp * cr;
  49. q.z = sy * cp * cr - cy * sp * sr;
  50. return q;
  51. }
  52. double mpos_x = 0;
  53. void viewerOneOff (pcl::visualization::PCLVisualizer& viewer)
  54. {
  55. //设置背景颜色
  56. viewer.setBackgroundColor(0.0,0.0,0.0);
  57. viewer.resetCamera();
  58. // pcl::PointXYZ mass_center(0,0,0);
  59. // pcl::PointXYZ kinectZ(0,0,-100);
  60. // viewer.addLine(mass_center,kinectZ,1.0f,1.0f,1.0f,"KinectZ");
  61. // pcl::PointXYZ kinectZ2(0,0,100);
  62. // viewer.addLine(mass_center,kinectZ2,1.0f,1.0f,1.0f,"KinectZ2");
  63. viewer.addCube(-0.9,0.9,-2.3,2.3,-1.9,-0.4,0.0,0.0,1.0,"car",0);
  64. }
  65. void viewerPsycho (pcl::visualization::PCLVisualizer& viewer)
  66. {
  67. // std::cout<<std::chrono::system_clock::now().time_since_epoch().count()/1000000<<" psycho."<<std::endl;
  68. viewer.removeShape("car");
  69. Eigen::Vector3f trans;
  70. Quaternion q = ToQuaternion(gCurPose.yaw,gCurPose.pitch,gCurPose.roll);
  71. Eigen::Quaternionf quat(q.w,q.x,q.y,q.z);
  72. trans(0) = gCurPose.x;trans(1) = gCurPose.y;trans(2) = gCurPose.z;
  73. // viewer.addCube(mpos_x+20 -0.9,mpos_x + 20+0.9,-2.3,2.3,-1.9,-0.4,0.0,0.0,1.0,"car",0);
  74. viewer.addCube(trans,quat,6.0,3.0,1.0,"car");
  75. double yaw_calc = M_PI/2.0 - gCurPose.yaw;
  76. if(gbAutoCam)
  77. {
  78. if(gnViewMode == 0)
  79. viewer.setCameraPosition(gCurPose.x,gCurPose.y,gView_Z,gCurPose.x,gCurPose.y,gCurPose.z,sin(yaw_calc),cos(yaw_calc),0);
  80. if(gnViewMode == 1)
  81. {
  82. double gfZsin = gView_Z * sin(gfViewAngle * M_PI/180.0);
  83. double gfZcos = gView_Z * cos(gfViewAngle * M_PI/180.0);
  84. double fcam_x = gCurPose.x + gfZcos * cos(gCurPose.yaw+M_PI);
  85. double fcam_y = gCurPose.y + gfZcos * sin(gCurPose.yaw+M_PI);
  86. viewer.setCameraPosition(fcam_x,fcam_y,gfZsin,gCurPose.x,gCurPose.y,gCurPose.z,sin(yaw_calc),cos(yaw_calc),0);
  87. }
  88. }
  89. std::stringstream ss;
  90. // viewer.setCameraPosition(mpos_x,0,30,mpos_x + 20,0,0,0,0,0);
  91. // ss << "Once per viewer loop: " << count++;
  92. }
  93. MainWindow::MainWindow(QWidget *parent) :
  94. QMainWindow(parent),
  95. ui(new Ui::MainWindow)
  96. {
  97. ui->setupUi(this);
  98. mpLabelStatus = new QLabel("No NDT Pos",ui->statusbar);
  99. mpLabelStatus->setMinimumWidth(350);
  100. // ui->pushButton_EnableRelocation->setEnabled(false);
  101. ui->pushButton_Test->setVisible(false);
  102. ui->horizontalSlider->setRange(5,300);
  103. ui->horizontalSlider->setValue(100);
  104. ui->comboBox->addItem("Look Down");
  105. ui->comboBox->addItem("Tracking");
  106. ui->checkBox->setChecked(true);
  107. mCurPose.x = 0; mCurPose.y = 0; mCurPose.z = 0;
  108. mCurPose.yaw = 0; mCurPose.pitch = 0; mCurPose.roll = 0;
  109. gCurPose = mCurPose;
  110. \
  111. mpthreadpcd = new std::thread(&MainWindow::threadpcdview,this);
  112. ModuleFun fun1 =std::bind(&MainWindow::UpdateNDTPos,this,std::placeholders::_1,std::placeholders::_2,std::placeholders::_3,std::placeholders::_4,std::placeholders::_5);
  113. mpandtpos = iv::modulecomm::RegisterRecvPlus("ndtpos",fun1);
  114. ModuleFun fun2 =std::bind(&MainWindow::UpdatePCDMap,this,std::placeholders::_1,std::placeholders::_2,std::placeholders::_3,std::placeholders::_4,std::placeholders::_5);
  115. mpapcdmap = iv::modulecomm::RegisterRecvPlus("pcdmap",fun2);
  116. mparelocate = iv::modulecomm::RegisterSend("relocate",10000,1);
  117. connect(this,SIGNAL(ndtposupdate()),this,SLOT(onndtposupdate()));
  118. setWindowTitle("NDT Matching View & Relocation Enable");
  119. }
  120. MainWindow::~MainWindow()
  121. {
  122. mbRun = false;
  123. mpthreadpcd->join();
  124. iv::modulecomm::Unregister(mpandtpos);
  125. iv::modulecomm::Unregister(mparelocate);
  126. iv::modulecomm::Unregister(mpapcdmap);
  127. delete ui;
  128. }
  129. void MainWindow::threadpcdview()
  130. {
  131. mpviewer = new pcl::visualization::CloudViewer("Cloud View");
  132. pcl::PointCloud<pcl::PointXYZI>::Ptr point_cloud(
  133. new pcl::PointCloud<pcl::PointXYZI>());
  134. pcl::PointCloud<pcl::PointXYZI>::Ptr point_cloud2(
  135. new pcl::PointCloud<pcl::PointXYZI>());
  136. char strpcpath[256];
  137. snprintf(strpcpath,256,"%s/map/map.pcd",getenv("HOME"));
  138. pcl::io::loadPCDFile<pcl::PointXYZI>(strpcpath,*point_cloud);
  139. // pcl::io::loadOBJFile("/home/yuchuli/car.obj",*point_cloud);
  140. // pcl::io::loadPLYFile("/home/yuchuli/car.ply",*point_cloud);
  141. mpviewer->showCloud(point_cloud);
  142. //This will only get called once
  143. mpviewer->runOnVisualizationThreadOnce (viewerOneOff);
  144. mpviewer->runOnVisualizationThread (viewerPsycho);
  145. while((!mpviewer->wasStopped()) && mbRun)
  146. {
  147. if(mbpcdmapupdate)
  148. {
  149. std::string strpcdmap;
  150. mmutexpcdmap.lock();
  151. strpcdmap = mstrpcdmappath;
  152. mbpcdmapupdate = false;
  153. mmutexpcdmap.unlock();
  154. pcl::PointCloud<pcl::PointXYZI>::Ptr point_cloud2(
  155. new pcl::PointCloud<pcl::PointXYZI>());
  156. pcl::io::loadPCDFile<pcl::PointXYZI>(strpcdmap,*point_cloud2);
  157. mpviewer->showCloud(point_cloud2);
  158. std::cout<<" use new pcd map. path: "<<strpcdmap.data()<<std::endl;
  159. }
  160. std::this_thread::sleep_for(std::chrono::milliseconds(10));
  161. }
  162. delete mpviewer;
  163. }
  164. void MainWindow::on_pushButton_Test_clicked()
  165. {
  166. double x = ui->lineEdit_x->text().toDouble();
  167. double y = ui->lineEdit_y->text().toDouble();
  168. double z = ui->lineEdit_z->text().toDouble();
  169. double yaw = ui->lineEdit_yaw->text().toDouble();
  170. double pitch = ui->lineEdit_pitch->text().toDouble();
  171. double roll = ui->lineEdit_roll->text().toDouble();
  172. mCurPose.x = x;
  173. mCurPose.y = y;
  174. mCurPose.z = z;
  175. mCurPose.yaw = yaw;
  176. mCurPose.pitch = pitch;
  177. mCurPose.roll = roll;
  178. gCurPose = mCurPose;
  179. }
  180. void MainWindow::on_pushButton_EnableRelocation_clicked()
  181. {
  182. static int64_t nLastSend = 0;
  183. int64_t nnow = std::chrono::system_clock::now().time_since_epoch().count()/1000000;
  184. if(abs(nnow - nLastSend) < 180000)
  185. {
  186. QMessageBox::warning(this,tr("Warning"),tr("EnableRelocation Must Interval 3 minutes. "),QMessageBox::YesAll);
  187. return;
  188. }
  189. if(mfTransProb >= 2.0)
  190. {
  191. QMessageBox::StandardButton button;
  192. button=QMessageBox::question(this,tr("Relocate"),QString(tr("TransProblity is more than 2.0,do you want relocation?")),QMessageBox::Yes|QMessageBox::No);
  193. if(button==QMessageBox::No)
  194. {
  195. return;
  196. }
  197. else if(button==QMessageBox::Yes)
  198. {
  199. }
  200. }
  201. iv::ndt::relocate xreloc;
  202. xreloc.set_benable(true);
  203. int nbytesize = xreloc.ByteSize();
  204. std::shared_ptr<char> pstr_ptr = std::shared_ptr<char>(new char[nbytesize]);
  205. if(xreloc.SerializeToArray(pstr_ptr.get(),nbytesize))
  206. {
  207. iv::modulecomm::ModuleSendMsg(mparelocate,pstr_ptr.get(),nbytesize);
  208. }
  209. else
  210. {
  211. QMessageBox::warning(this,tr("Warning"),tr("Relocate Message Serialize Fail."),QMessageBox::YesAll);
  212. }
  213. nLastSend = nnow;
  214. }
  215. void MainWindow::on_horizontalSlider_valueChanged(int value)
  216. {
  217. gView_Z = value;
  218. }
  219. void MainWindow::on_checkBox_clicked()
  220. {
  221. if(ui->checkBox->isChecked())
  222. {
  223. gbAutoCam = true;
  224. }
  225. else
  226. {
  227. gbAutoCam = false;
  228. }
  229. }
  230. void MainWindow::on_comboBox_currentIndexChanged(int index)
  231. {
  232. gnViewMode = index;
  233. }
  234. void MainWindow::UpdateNDTPos(const char * strdata,const unsigned int nSize,const unsigned int index,const QDateTime * dt,const char * strmemname)
  235. {
  236. (void)index;
  237. (void)dt;
  238. (void)strmemname;
  239. iv::lidar::ndtpos xndtpos;
  240. if(xndtpos.ParseFromArray(strdata,nSize))
  241. {
  242. mmutexndtpos.lock();
  243. mndtpos.CopyFrom(xndtpos);
  244. mbndtposupdate = true;
  245. mmutexndtpos.unlock();
  246. emit ndtposupdate();
  247. }
  248. else
  249. {
  250. std::cout<<"MainWindow::UpdateNDTPos Parse NDTPos Fail."<<std::endl;
  251. }
  252. }
  253. void MainWindow::onndtposupdate()
  254. {
  255. if(mbndtposupdate)
  256. {
  257. iv::lidar::ndtpos xndtpos;
  258. mmutexndtpos.lock();
  259. xndtpos.CopyFrom(mndtpos);
  260. mbndtposupdate = false;
  261. mmutexndtpos.unlock();
  262. gCurPose.x = xndtpos.pose_x();
  263. gCurPose.y = xndtpos.pose_y();
  264. gCurPose.z = xndtpos.pose_z();
  265. gCurPose.yaw = xndtpos.pose_yaw();
  266. gCurPose.pitch = xndtpos.pose_pitch();
  267. gCurPose.roll = xndtpos.pose_roll();
  268. ui->lineEdit_prob->setText(QString::number(xndtpos.trans_probability(),'f',3));
  269. ui->lineEdit_score->setText(QString::number(xndtpos.fitness_score(),'f',3));
  270. ui->lineEdit_x->setText(QString::number(gCurPose.x,'f',3));
  271. ui->lineEdit_y->setText(QString::number(gCurPose.y,'f',3));
  272. ui->lineEdit_z->setText(QString::number(gCurPose.z,'f',3));
  273. ui->lineEdit_yaw->setText(QString::number(gCurPose.yaw,'f',3));
  274. ui->lineEdit_pitch->setText(QString::number(gCurPose.pitch,'f',3));
  275. ui->lineEdit_roll->setText(QString::number(gCurPose.roll,'f',3));
  276. mfTransProb = xndtpos.trans_probability();
  277. mpLabelStatus->setText("NDT Update Time:" + QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss:zzz"));
  278. }
  279. }
  280. void MainWindow::UpdatePCDMap(const char *strdata, const unsigned int nSize, const unsigned int index, const QDateTime *dt, const char *strmemname)
  281. {
  282. (void)index;
  283. (void)dt;
  284. (void)strmemname;
  285. iv::ndt::pcdmap xpcdmap;
  286. std::cout<<" update ndt map "<<std::endl;
  287. if(xpcdmap.ParseFromArray(strdata,nSize))
  288. {
  289. mmutexpcdmap.lock();
  290. mstrpcdmappath = xpcdmap.strpath();
  291. mbpcdmapupdate = true;
  292. mmutexpcdmap.unlock();
  293. }
  294. else
  295. {
  296. std::cout<<"UpdatePCDMap Fail."<<std::endl;
  297. }
  298. }