Laser.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. #include "Laser.h"
  2. CBinaryData::CBinaryData()
  3. :m_buf(0)
  4. ,m_length(0)
  5. {
  6. }
  7. CBinaryData::CBinaryData(const char* buf, int len, DATA_type type)
  8. :m_buf(0),m_length(0)
  9. {
  10. if (len > 0)
  11. {
  12. m_buf = (char*)malloc(len);
  13. memcpy(m_buf, buf, len);
  14. m_length = len;
  15. }
  16. }
  17. CBinaryData::CBinaryData(const CBinaryData& data)
  18. :m_buf(0),m_length(0)
  19. {
  20. if (m_buf)
  21. {
  22. free(m_buf);
  23. m_buf = 0;
  24. m_length = 0;
  25. }
  26. if (data.Length() > 0)
  27. {
  28. m_buf = (char*)malloc(data.Length());
  29. memcpy(m_buf, data.Data(), data.Length());
  30. m_length = data.Length();
  31. }
  32. }
  33. CBinaryData::~CBinaryData()
  34. {
  35. if (m_buf && m_length)
  36. {
  37. free(m_buf);
  38. m_length = 0;
  39. }
  40. }
  41. char* CBinaryData::Data()const {
  42. return m_buf;
  43. }
  44. int CBinaryData::Length()const {
  45. return m_length;
  46. }
  47. CBinaryData& CBinaryData::operator=(const CBinaryData& data)
  48. {
  49. if (m_buf)
  50. {
  51. free(m_buf);
  52. m_buf = 0;
  53. m_length = 0;
  54. }
  55. if (data.Length() > 0)
  56. {
  57. m_buf = (char*)malloc(data.Length());
  58. memcpy(m_buf, data.Data(), data.Length());
  59. m_length = data.Length();
  60. }
  61. return *this;
  62. }
  63. bool CBinaryData::operator==(const char* str)
  64. {
  65. int length = strlen(str);
  66. return (strncmp((const char*)m_buf, str, length) == 0);
  67. }
  68. const char* CBinaryData::operator+(int n)
  69. {
  70. if (n < m_length)
  71. return m_buf + n;
  72. else
  73. return NULL;
  74. }
  75. CBinaryData& CBinaryData::operator+(CBinaryData& data)
  76. {
  77. if (data.Length() > 0)
  78. {
  79. int length = m_length + data.Length();
  80. char* buf = (char*)malloc(length);
  81. memcpy(buf, m_buf, m_length);
  82. memcpy(buf + m_length, data.Data(), data.Length());
  83. free(m_buf);
  84. m_buf = buf;
  85. m_length = length;
  86. }
  87. return *this;
  88. }
  89. char& CBinaryData::operator[](int n)
  90. {
  91. if (n >= 0 && n < m_length)
  92. return m_buf[n];
  93. }
  94. //////////////
  95. CLaser::CLaser(int id, Automatic::stLaserCalibParam laser_param)
  96. :m_ThreadRcv(0)
  97. ,m_ThreadPro(0)
  98. ,m_id(id)
  99. ,m_statu(eLaser_disconnect)
  100. ,m_dMatrix(0)
  101. , m_point_callback_fnc(0)
  102. , m_point_callback_pointer(0)
  103. ,m_laser_param(laser_param)
  104. ,m_bSave_file(false)
  105. {
  106. }
  107. CLaser::~CLaser()
  108. {
  109. if (m_dMatrix)
  110. {
  111. free(m_dMatrix);
  112. m_dMatrix = 0;
  113. }
  114. }
  115. bool CLaser::Start()
  116. {
  117. m_statu = eLaser_busy;
  118. m_bStart_capture.Notify(true);
  119. return true;
  120. }
  121. bool CLaser::Stop()
  122. {
  123. m_bStart_capture.Notify(false);
  124. m_binary_log_tool.close();
  125. m_pts_log_tool.close();
  126. return true;
  127. }
  128. void CLaser::SetMetrix(double* data)
  129. {
  130. if (m_dMatrix == 0)
  131. {
  132. m_dMatrix = (double*)malloc(12 * sizeof(double));
  133. }
  134. memcpy(m_dMatrix, data, 12 * sizeof(double));
  135. }
  136. void CLaser::SetPointCallBack(PointCallBack fnc, void* pointer)
  137. {
  138. m_point_callback_fnc = fnc;
  139. m_point_callback_pointer = pointer;
  140. }
  141. bool CLaser::Connect()
  142. {
  143. m_bThreadRcvRun.Notify(true);
  144. if(m_ThreadRcv==0)
  145. m_ThreadRcv = new std::thread(&CLaser::thread_recv, this);
  146. m_ThreadRcv->detach();
  147. m_bThreadProRun.Notify(true);
  148. if(m_ThreadPro==0)
  149. m_ThreadPro = new std::thread(&CLaser::thread_toXYZ, this);
  150. m_ThreadPro->detach();
  151. m_statu = eLaser_connect;
  152. return true;
  153. }
  154. void CLaser::Disconnect()
  155. {
  156. if (m_ThreadRcv)
  157. {
  158. delete m_ThreadRcv;
  159. m_ThreadRcv = 0;
  160. }
  161. if (m_ThreadPro)
  162. {
  163. delete m_ThreadPro;
  164. m_ThreadPro = NULL;
  165. }
  166. m_statu = eLaser_disconnect;
  167. }
  168. void CLaser::SetSaveDir(std::string strDir,bool bSaveFile)
  169. {
  170. m_bSave_file = bSaveFile;
  171. m_pts_log_tool.close();
  172. m_binary_log_tool.close();
  173. if (bSaveFile == false)
  174. return;
  175. m_pts_save_path = strDir;
  176. char pts_file[255] = { 0 };
  177. sprintf(pts_file, "%s/pts%d.txt", strDir.c_str(), m_id+1);
  178. m_pts_log_tool.open(pts_file);
  179. char bin_file[255] = { 0 };
  180. sprintf(bin_file, "%s/laser%d.data", strDir.c_str(), m_id+1);
  181. m_binary_log_tool.open(bin_file,true);
  182. }
  183. void CLaser::thread_recv()
  184. {
  185. while (m_bThreadRcvRun.WaitFor(1))
  186. {
  187. if (m_bStart_capture.WaitFor(1) == false)
  188. continue;
  189. CBinaryData* data=new CBinaryData();
  190. if (this->RecvData(*data))
  191. {
  192. m_queue_laser_data.push(data);
  193. }
  194. else
  195. {
  196. delete data;
  197. data=0;
  198. }
  199. }
  200. }
  201. CPoint3D CLaser::transfor(CPoint3D point)
  202. {
  203. if (m_dMatrix)
  204. {
  205. CPoint3D result;
  206. double x = point.x;
  207. double y = point.y;
  208. double z = point.z;
  209. result.x = x * m_dMatrix[0] + y*m_dMatrix[1] + z*m_dMatrix[2] + m_dMatrix[3];
  210. result.y = x * m_dMatrix[4] + y*m_dMatrix[5] + z*m_dMatrix[6] + m_dMatrix[7];
  211. result.z = x * m_dMatrix[8] + y*m_dMatrix[9] + z*m_dMatrix[10] + m_dMatrix[11];
  212. return result;
  213. }
  214. else
  215. {
  216. return point;
  217. }
  218. }
  219. void CLaser::thread_toXYZ()
  220. {
  221. while (m_bThreadProRun.WaitFor(1))
  222. {
  223. if (m_bStart_capture.WaitFor(1) == false)
  224. {
  225. m_queue_laser_data.clear();
  226. continue;
  227. }
  228. CBinaryData* pData=NULL;
  229. bool bPop = m_queue_laser_data.front(pData);
  230. DATA_type type = eUnknow;
  231. if (bPop || m_last_data.Length() != 0)
  232. {
  233. m_queue_laser_data.try_pop();
  234. std::vector<CPoint3D> cloud;
  235. if (bPop)
  236. {
  237. if (pData == NULL)
  238. continue;
  239. if (m_bSave_file)
  240. m_binary_log_tool.write(pData->Data(), pData->Length());
  241. type= this->Data2PointXYZ(pData, cloud);
  242. }
  243. else
  244. {
  245. type = this->Data2PointXYZ(NULL, cloud);
  246. }
  247. if (type == eUnknow)
  248. {
  249. if(pData)
  250. {
  251. delete pData;
  252. pData=0;
  253. }
  254. continue;
  255. }
  256. if (type == eData || type == eStart || type == eStop)
  257. {
  258. for (int i = 0; i < cloud.size(); ++i)
  259. {
  260. CPoint3D point = transfor(cloud[i]);
  261. if (m_point_callback_fnc)
  262. m_point_callback_fnc(point, m_point_callback_pointer);
  263. if (m_bSave_file)
  264. {
  265. char buf[64] = { 0 };
  266. sprintf(buf, "%f %f %f\n", point.x, point.y, point.z);
  267. m_pts_log_tool.write(buf, strlen(buf));
  268. }
  269. }
  270. if (type == eStop)
  271. {
  272. if (m_bSave_file)
  273. {
  274. m_pts_log_tool.close();
  275. m_binary_log_tool.close();
  276. }
  277. m_statu = eLaser_ready;
  278. }
  279. }
  280. else if (type == eReady)
  281. {
  282. if (m_bSave_file)
  283. {
  284. m_pts_log_tool.close();
  285. m_binary_log_tool.close();
  286. }
  287. m_statu = eLaser_ready;
  288. }
  289. delete pData;
  290. pData=0;
  291. }
  292. }
  293. }