TcpLaser.cpp 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. #include "TcpLaser.h"
  2. #include <stdlib.h>
  3. #include <unistd.h>
  4. RegisterLaser(Tcp);
  5. CTcpLaser::CTcpLaser(int id, Laser_proto::laser_parameter laser_param)
  6. :Laser_base(id,laser_param)
  7. {
  8. }
  9. CTcpLaser::~CTcpLaser()
  10. {
  11. }
  12. //雷达链接设备,为3个线程添加线程执行函数。
  13. Error_manager CTcpLaser::connect_laser()
  14. {
  15. std::string ip = m_laser_param.laser_ip();
  16. int port = m_laser_param.laser_port();
  17. int remoteport = m_laser_param.laser_port_remote();
  18. if (ip == "" || port < 0)
  19. return Error_manager(Error_code::PARAMETER_ERROR, Error_level::MINOR_ERROR,
  20. " m_laser_param PARAMRTER ERROR ");
  21. //��ʼ��Socket
  22. //WSAStartup(MAKEWORD(1, 1), &m_wsd);
  23. //����Socket����
  24. m_socket = socket(AF_INET, SOCK_STREAM, 0);
  25. if (m_socket <= 0)
  26. return Error_manager(Error_code::ERROR, Error_level::MINOR_ERROR,
  27. " m_laser_param PARAMRTER ERROR ");
  28. m_send_addr.sin_family = AF_INET;
  29. m_send_addr.sin_addr.s_addr = inet_addr(ip.c_str());
  30. m_send_addr.sin_port = htons(remoteport);
  31. if (0 != ::connect(m_socket, (struct sockaddr *)&m_send_addr, sizeof(struct sockaddr)))
  32. return Error_manager(Error_code::ERROR, Error_level::MINOR_ERROR,
  33. " connect m_socket ERROR ");
  34. m_last_data_time = clock();
  35. return Laser_base::connect_laser();
  36. }
  37. //雷达断开链接,释放3个线程
  38. Error_manager CTcpLaser::disconnect_laser()
  39. {
  40. if (m_socket > 0)
  41. {
  42. close(m_socket);
  43. // WSACleanup();
  44. m_socket = -1;
  45. }
  46. return Laser_base::disconnect_laser();
  47. }
  48. //对外的接口函数,负责接受并处理任务单,
  49. //input:p_laser_task 雷达任务单,基类的指针,指向子类的实例,(多态)
  50. //注:这个函数为虚函数,实际的处理任务的代码由子类重载并实现。
  51. Error_manager CTcpLaser::execute_task(Task_Base* p_laser_task)
  52. {
  53. }
  54. //检查雷达状态,是否正常运行
  55. Error_manager CTcpLaser::check_laser()
  56. {
  57. }
  58. //雷达的启动接口函数, 让雷达进行扫描,一般需要子类重载,不同的雷达开始方式不同。
  59. Error_manager CTcpLaser::start_scan()
  60. {
  61. std::lock_guard<std::mutex> lk(m_mutex);
  62. if (!this->is_ready())
  63. return Error_manager(Error_code::ERROR, Error_level::MINOR_ERROR,
  64. " is_ready ERROR ");
  65. const char* sendMsg = "$SLSSTA*0A\r\n";
  66. if (Send(sendMsg, strlen(sendMsg)))
  67. {
  68. return Laser_base::start_scan();
  69. }
  70. return Error_manager(Error_code::ERROR, Error_level::MINOR_ERROR,
  71. " Send error ");
  72. }
  73. //雷达的停止接口函数, 让雷达停止扫描,一般需要子类重载,不同的雷达结束方式不同。
  74. Error_manager CTcpLaser::stop_scan()
  75. {
  76. char sendMsg[] = "$SLSSTP*1B\r\n";
  77. std::lock_guard<std::mutex> lk(m_mutex);
  78. if (Send(sendMsg, strlen(sendMsg)))
  79. return Error_code::SUCCESS;
  80. return Error_manager(Error_code::ERROR, Error_level::MINOR_ERROR,
  81. " error ");
  82. }
  83. //结束任务单,stop之后,要检查线程状态和数据结果,然后才能 end_task
  84. Error_manager CTcpLaser::end_task()
  85. {
  86. }
  87. Buf_type CTcpLaser::transform_buf_to_points(Binary_buf* pData, std::vector<CPoint3D>& points)
  88. {
  89. ////ƴ���ϴ�δ�������
  90. Buf_type type = BUF_UNKNOW;
  91. Binary_buf frame;
  92. if (m_last_data.get_length() > 0)
  93. {
  94. if (pData)
  95. frame = m_last_data + (*pData);
  96. else
  97. frame = m_last_data;
  98. m_last_data = Binary_buf();
  99. }
  100. else if (pData)
  101. {
  102. frame = (*pData);
  103. }
  104. else
  105. {
  106. return type;
  107. }
  108. int head = FindHead(frame.get_buf(), frame.get_length());
  109. int tail = FindTail(frame.get_buf(), frame.get_length());
  110. if (tail >= 0)
  111. {
  112. if (tail < frame.get_length() - 1) //���������
  113. {
  114. m_last_data = Binary_buf(frame.get_buf() + tail + 1, frame.get_length() - tail - 1);
  115. }
  116. if (head >= 0 && head < tail)
  117. {
  118. ////���
  119. Binary_buf data(frame.get_buf() + head, tail - head + 1);
  120. if (data.is_equal_front("$SLSSTP") )
  121. type = BUF_STOP;
  122. else if (data.is_equal_front( "$SLSSTA"))
  123. type = BUF_START;
  124. else if (data.is_equal_front( "$SCLRDY"))
  125. type = BUF_READY;
  126. else if (data.is_equal_front( "$SHWERR"))
  127. type = BUF_ERROR;
  128. else if (data.is_equal_front( "$N"))
  129. type = BUF_DATA;
  130. if (type == BUF_DATA)
  131. {
  132. ////��������
  133. points.clear();
  134. if (data.get_length() <= 29)
  135. return type;
  136. ////����
  137. unsigned char angle_1 = 0;
  138. unsigned char angle_2 = 0;
  139. memcpy(&angle_1, (data.get_buf())+26, 1);
  140. memcpy(&angle_2, (data.get_buf())+27, 1);
  141. float beta_a = float(angle_1 * 256 + angle_2)*0.01;
  142. float start_angle = 0.0;
  143. float freq = 0.0;
  144. std::vector<float> distance;
  145. if (GetData(&data, distance, freq, start_angle))
  146. {
  147. float beta = (beta_a)*DEGREES;
  148. float sin_beta = sin(beta);
  149. float cos_beta = cos(beta);
  150. for (int i = 0; i < distance.size(); ++i)
  151. {
  152. if (distance[i] < 0.001)
  153. continue;
  154. float alpha = (start_angle + i*freq - 90.0) * DEGREES;
  155. float sin_alpha = sin(alpha);
  156. float cos_alpha = cos(alpha);
  157. float x = distance[i] * sin_alpha;
  158. float y = distance[i] * cos_alpha * sin_beta;
  159. float z = distance[i] * cos_alpha * cos_beta;
  160. points.push_back(CPoint3D(x, y, z));
  161. }
  162. //points.push_back(CPoint3D(double(distance.size()), start_angle, freq));
  163. }
  164. }
  165. }
  166. }
  167. else if (head >= 0)
  168. {
  169. m_last_data = Binary_buf(frame.get_buf() + head, frame.get_length() - head);
  170. }
  171. return type;
  172. }
  173. bool CTcpLaser::receive_buf_to_queue(Binary_buf& data)
  174. {
  175. char buf[4096] = { 0 };
  176. int bytesRead = Recv(buf, 4096);
  177. if (bytesRead > 0)
  178. {
  179. Binary_buf bin_data(buf, bytesRead);
  180. data = bin_data;
  181. m_last_data_time = clock();
  182. return true;
  183. }
  184. return false;
  185. }
  186. int CTcpLaser::FindHead(char* buf, int b_len)
  187. {
  188. int i = 0;
  189. if (b_len < 2)
  190. return 0;
  191. for (i = 0; i <= b_len; i++)
  192. {
  193. if (b_len > 10)
  194. {
  195. if (buf[i] == '$'&& buf[i + 1] == 'N' && buf[i + 7] == 'S'
  196. &&buf[i + 8] == '5'&&buf[i + 9] == '1'&&buf[i + 10] == '1')
  197. return i;
  198. }
  199. if (b_len > 7)
  200. {
  201. if (buf[i] == '$'&& buf[i + 1] == 'S'&&buf[i + 7] == '*')
  202. return i;
  203. }
  204. }
  205. return -1;
  206. }
  207. int CTcpLaser::FindTail(char* buf, int b_len)
  208. {
  209. int i = 0;
  210. for (i = 0; i <= b_len; i++)
  211. {
  212. if (b_len >= i + 5)
  213. {
  214. if (buf[i] == '*' && buf[i + 3] == '\r'&&buf[i + 4] == '\n')
  215. return i + 4;
  216. }
  217. }
  218. return -9999999;
  219. }
  220. bool CTcpLaser::Send(const char* sendbuf, int len)
  221. {
  222. int ret = 0;
  223. do
  224. {
  225. ret = send(m_socket, sendbuf, strlen(sendbuf), 0);
  226. } while (ret < 0);
  227. return ret == len;
  228. }
  229. int CTcpLaser::Recv(char* recvbuf, int len)
  230. {
  231. struct sockaddr_in sddr_from;
  232. int ret = 0;
  233. do
  234. {
  235. ret = recv(m_socket, recvbuf, 4096, 0);
  236. } while (ret < 0);
  237. return ret;
  238. }
  239. bool CTcpLaser::GetData(Binary_buf* pData, std::vector<float>& distance,
  240. float& freq, float& start_angle)
  241. {
  242. struct stData
  243. {
  244. const char* data;
  245. int length;
  246. };
  247. std::vector<struct stData> strDatas;
  248. int start = 0;
  249. int end = 0;
  250. int LMDscandata_index = -1;
  251. for (int i = 0; i < pData->get_length(); ++i)
  252. {
  253. if ((*pData)[i] == ' ')
  254. {
  255. end = i;
  256. if (end > start)
  257. {
  258. struct stData strData;
  259. // strData.data = (*pData + start);
  260. strData.data = (pData->get_buf() + start);
  261. strData.length = end - start;
  262. strDatas.push_back(strData);
  263. if (strncmp(strData.data, "LMDscandata", 11) == 0)
  264. LMDscandata_index = strDatas.size() - 1;
  265. }
  266. end = i + 1;
  267. start = end;
  268. }
  269. }
  270. if (strDatas.size() > 26 + LMDscandata_index - 1)
  271. {
  272. struct stData start_angle_str = strDatas[23 + LMDscandata_index - 1];
  273. long start_angle_l = Str0x2Long(start_angle_str.data, start_angle_str.length);
  274. start_angle = float(start_angle_l)*0.01;
  275. struct stData freq_str = strDatas[24 + LMDscandata_index - 1];
  276. long freq_l = Str0x2Long(freq_str.data, freq_str.length);
  277. freq = float(freq_l)*0.0001;
  278. struct stData count_str = strDatas[25 + LMDscandata_index - 1];
  279. long count = Str0x2Long(count_str.data, count_str.length);
  280. if (strDatas.size() >= 26 + LMDscandata_index - 1 + count)
  281. {
  282. for (int i = 26 + LMDscandata_index - 1; i < 26 + LMDscandata_index - 1 + count; ++i)
  283. {
  284. float dis = float(Str0x2Long(strDatas[i].data, strDatas[i].length));
  285. distance.push_back(dis);
  286. }
  287. return true;
  288. }
  289. }
  290. return false;
  291. }
  292. long CTcpLaser::Str0x2Long(const char* data, int len)
  293. {
  294. long sum = 0;
  295. for (int i = 0; i < len; ++i)
  296. {
  297. char c = data[i];
  298. int n = 0;
  299. if (c >= 48 && c <= 57)
  300. n = c - 48;
  301. else if (c >= 65 && c <= 70)
  302. n = c - 65 + 10;
  303. sum += n*pow(16, len - i - 1);
  304. }
  305. return sum;
  306. }