Laser.cpp 5.6 KB

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