Process.cpp 6.1 KB

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