Process.cpp 6.0 KB

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