Process.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. #include "Process.h"
  2. #include "qmessagebox.h"
  3. #include "TaskQueue/TQFactory.h"
  4. #include "mainwindow.h"
  5. CProcess::CProcess(Automatic::stCalibParam param, void* mainWnd)
  6. {
  7. m_laser_calib_param = param;
  8. m_laser = NULL;
  9. m_main_wnd = mainWnd;
  10. m_thread_queue=0;
  11. connect(this,SIGNAL(MainWndSignal(QVariant)),
  12. (MainWindow*)m_main_wnd,SLOT(ProcessSlot(QVariant)));
  13. }
  14. CProcess::~CProcess()
  15. {
  16. const int n = m_laser_calib_param.laser_size();
  17. for (int i = 0; i < n; ++i)
  18. {
  19. std::string type = m_laser_calib_param.laser(i).type();
  20. if (type.find("Livox") != type.npos)
  21. {
  22. Uninit();
  23. break;
  24. }
  25. }
  26. if(m_thread_queue)
  27. {
  28. m_thread_queue->Stop();
  29. delete m_thread_queue;
  30. m_thread_queue=0;
  31. }
  32. if (m_laser)
  33. {
  34. for (int i = 0; i < n; ++i)
  35. {
  36. if (m_laser[i])
  37. {
  38. m_laser[i]->Disconnect();
  39. delete m_laser[i];
  40. m_laser[i] = NULL;
  41. }
  42. }
  43. delete[] m_laser;
  44. m_laser = NULL;
  45. }
  46. if (m_laser_calib_param.is_calib() == false)
  47. {
  48. //delete m_locate;
  49. }
  50. }
  51. bool CProcess::Init()
  52. {
  53. m_thread_queue=tq::TQFactory::CreateDefaultQueue();
  54. m_thread_queue->Start(3);
  55. if (NULL == m_laser)
  56. {
  57. const int n = m_laser_calib_param.laser_size();
  58. m_laser = new CLaser*[n];
  59. for (int i = 0; i < n; ++i)
  60. {
  61. std::string type = m_laser_calib_param.laser(i).type();
  62. ////判断是否是从文件来
  63. if (type.find("File") != type.npos)
  64. {
  65. LOG(INFO) << "create fileLaser";
  66. std::string dir = m_laser_calib_param.debug_file();
  67. m_laser_calib_param.mutable_laser(i)->set_laser_ip(dir);
  68. }
  69. //创建laser
  70. m_laser[i] = LaserRegistory::CreateLaser(type, i, m_laser_calib_param.laser(i));
  71. if (m_laser[i] == NULL)
  72. {
  73. char buf[255] = { 0 };
  74. sprintf(buf, " Laser type : %s is not exits...", type.c_str());
  75. m_last_error=buf;
  76. return false;
  77. }
  78. if (!m_laser[i]->Connect())
  79. {
  80. char error[255] = { 0 };
  81. sprintf(error, "laser %d connect failed...", i);
  82. m_last_error = error;
  83. return false;
  84. }
  85. double mat[12] = { 0 };
  86. if (m_laser_calib_param.is_calib()==false)
  87. {
  88. create_mat_param(m_laser_calib_param.laser(i), mat);
  89. m_laser[i]->SetMetrix(mat);
  90. }
  91. }
  92. for (int i = 0; i < n; ++i)
  93. {
  94. std::string type = m_laser_calib_param.laser(i).type();
  95. if (type.find("Livox") != type.npos)
  96. {
  97. if (Start() == false)
  98. {
  99. Uninit();
  100. m_last_error=("Livox laser init failed...");
  101. return false;
  102. }
  103. break;
  104. }
  105. }
  106. }
  107. // 连接到PLC,创建locate
  108. if (m_laser_calib_param.is_calib() == false)
  109. {
  110. if (0 != m_monitor.connect(m_laser_calib_param.plc().plc_ip().c_str(), m_laser_calib_param.plc().plc_port(), 1))
  111. {
  112. m_last_error=(" connect PLC failed...");
  113. return false;
  114. }
  115. else
  116. {
  117. m_monitor.set_callback(action_callback, monitor_callback,this);
  118. }
  119. /*if (m_locater == 0)
  120. {
  121. m_locater = new CLocater();
  122. }*/
  123. }
  124. return true;
  125. }
  126. void CProcess::PushTask(uint16_t param, int action_type,bool test)
  127. {
  128. char plate = 0;
  129. uint16_t laserID = param;
  130. memcpy(&plate, &param, 1);
  131. m_plate_index = plate;
  132. std::vector<CLaser*> lasers;
  133. for (int i = 0; i < 8; ++i)
  134. {
  135. if (((0x01 << i)&laserID) > 0)
  136. {
  137. const int n = m_laser_calib_param.laser_size();
  138. if (i >= 0 && i < n)
  139. lasers.push_back(m_laser[i]);
  140. }
  141. }
  142. //创建摆扫测量任务
  143. modbus::CPLCMonitor* plc = 0;
  144. if (test == false)
  145. plc = &(m_monitor);
  146. tq::BaseTask* task = 0;
  147. if(action_type==1)
  148. task= new MeasureTask(lasers, /*m_locater*/0, int(plate),plc, m_laser_calib_param);
  149. /*else if(action_type==2)
  150. task = new CFenceTask(lasers, m_locater, int(plate), plc, m_laser_calib_param);*/
  151. ((MeasureTask*)task)->SetResultCallback(this, monitor_callback);
  152. m_thread_queue->AddTask(task);
  153. }
  154. void CProcess::action_callback(bool bOn, uint16_t param, void* pointer, int action_type)
  155. {
  156. CProcess* process = reinterpret_cast<CProcess*>(pointer);
  157. if(bOn)
  158. process->PushTask(param, action_type);
  159. }
  160. void CProcess::monitor_callback(QtMessageData data, void* pointer)
  161. {
  162. static QtMessageData msg;
  163. memcpy(&msg,&data,sizeof(data));
  164. CProcess* process = reinterpret_cast<CProcess*>(pointer);
  165. static QVariant var;
  166. var.setValue(msg);
  167. emit process->MainWndSignal(var);
  168. }
  169. void CProcess::create_mat_param(Automatic::stLaserCalibParam laser, double* buf)
  170. {
  171. std::string type = laser.type();
  172. if (type.find("Linear") != type.npos)
  173. {
  174. double x_angle = laser.axis_x_theta() / 180.0*PI;
  175. double y_angle = laser.axis_y_theta() / 180.0*PI;
  176. double z_angle = laser.axis_z_theta() / 180.0*PI;
  177. double t_x = laser.translation_x();
  178. double t_y = laser.translation_y();
  179. double t_z = laser.translation_z();
  180. Eigen::AngleAxisd rollAngle(x_angle, Eigen::Matrix<double, 3, 1>::UnitX());
  181. Eigen::AngleAxisd pitchAngle(y_angle, Eigen::Matrix<double, 3, 1>::UnitY());
  182. Eigen::AngleAxisd yawAngle(z_angle, Eigen::Matrix<double, 3, 1>::UnitZ());
  183. Eigen::Quaterniond q = yawAngle * pitchAngle * rollAngle;
  184. Eigen::Matrix<double, 3, 3> rotationMatrix = q.toRotationMatrix();
  185. Eigen::Matrix<double, 3, 4> transform;
  186. Eigen::Matrix<double, 3, 1> translation;
  187. translation << t_x, t_y, t_z;
  188. transform << rotationMatrix, translation;
  189. buf[0] = transform(0, 0);
  190. buf[1] = transform(0, 1);
  191. buf[2] = transform(0, 2);
  192. buf[3] = transform(0, 3);
  193. buf[4] = transform(1, 0);
  194. buf[5] = transform(1, 1);
  195. buf[6] = transform(1, 2);
  196. buf[7] = transform(1, 3);
  197. buf[8] = transform(2, 0);
  198. buf[9] = transform(2, 1);
  199. buf[10] = transform(2, 2);
  200. buf[11] = transform(2, 3);
  201. }
  202. else
  203. {
  204. buf[0] = laser.mat_r00();
  205. buf[1] = laser.mat_r01();
  206. buf[2] = laser.mat_r02();
  207. buf[3] = laser.mat_r03();
  208. buf[4] = laser.mat_r10();
  209. buf[5] = laser.mat_r11();
  210. buf[6] = laser.mat_r12();
  211. buf[7] = laser.mat_r13();
  212. buf[8] = laser.mat_r20();
  213. buf[9] = laser.mat_r21();
  214. buf[10] = laser.mat_r22();
  215. buf[11] = laser.mat_r23();
  216. }
  217. }
  218. /*#include "MsgDef.h"
  219. void CProcess::result_callback(Locate_Message msg, void* ptr)
  220. {
  221. CProcess* process = reinterpret_cast<CProcess*>(ptr);
  222. std::lock_guard<std::mutex> lk(process->m_mutex);
  223. process->m_locate_msg = msg;
  224. if(process->m_main_wnd)
  225. ::PostMessage(process->m_main_wnd->m_hWnd, WM_MSG_MainWnd, WPARAM(WM_Draw_cloud), 0);
  226. }*/