lidar_manager.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. #include "lidar_manager.h"
  2. #define VIEWER 0
  3. CloudDataManager::CloudDataManager() = default;
  4. CloudDataManager::~CloudDataManager() = default;
  5. void CloudDataManager::receiveClient() {
  6. // 172 120
  7. // 250 104
  8. RslidarMqttAsyncClient client;
  9. client.init();
  10. google::protobuf::RepeatedPtrField<SubscribeEtc> subs = client.getSubscribeEtc();
  11. int view_port[4];
  12. std::map<std::string, pcl::PointCloud<pcl::PointXYZ>::Ptr> clouds_xyz;
  13. std::map<std::string, pcl::PointCloud<pcl::PointXYZI>::Ptr> clouds_xyzi;
  14. for (const auto &sub: subs) {
  15. clouds_xyz.insert(std::pair<std::string, pcl::PointCloud<pcl::PointXYZ>::Ptr>(sub.topic(),
  16. new pcl::PointCloud<pcl::PointXYZ>));
  17. clouds_xyzi.insert(std::pair<std::string, pcl::PointCloud<pcl::PointXYZI>::Ptr>(sub.topic(),
  18. new pcl::PointCloud<pcl::PointXYZI>));
  19. }
  20. pcl::visualization::PCLVisualizer viewer("viewer_0");
  21. viewer.createViewPort(0.0, 0.5, 0.5, 1, view_port[0]);
  22. viewer.createViewPort(0.5, 0.5, 1.0, 1, view_port[1]);
  23. viewer.createViewPort(0.0, 0, 0.5, 0.5, view_port[2]);
  24. viewer.createViewPort(0.5, 0, 1.0, 0.5, view_port[3]);
  25. viewer.addText("view_port_1", 10, 10, 20, 0, 1, 0, "view_port_1", view_port[0]);
  26. viewer.addText("view_port_2", 10, 10, 20, 0, 1, 0, "view_port_2", view_port[1]);
  27. viewer.addText("view_port_3", 10, 10, 20, 0, 1, 0, "view_port_3", view_port[2]);
  28. viewer.addText("view_port_4", 10, 10, 20, 0, 1, 0, "view_port_4", view_port[3]);
  29. viewer.addCoordinateSystem(1, "v1_axis", view_port[0]);
  30. viewer.addCoordinateSystem(1, "v2_axis", view_port[1]);
  31. viewer.addCoordinateSystem(1, "v3_axis", view_port[2]);
  32. viewer.addCoordinateSystem(1, "v4_axis", view_port[3]);
  33. sleep(3);
  34. pcl::PointCloud<pcl::PointXYZ>::Ptr zx_points(new pcl::PointCloud<pcl::PointXYZ>);
  35. for (auto iter = clouds_xyz.begin(); iter != clouds_xyz.end(); iter++) {
  36. client.getCloudData(iter->first, iter->second);
  37. zx_points->operator+=(*iter->second);
  38. }
  39. WriteTxtCloud(ETC_PATH"DataToCloud/data/catch_cloud.txt", zx_points);
  40. for (auto & iter : clouds_xyz) {
  41. client.getCloudData(iter.first, iter.second);
  42. zx_points->operator+=(*iter.second);
  43. }
  44. WriteTxtCloud(ETC_PATH"DataToCloud/data/catch_cloud.txt", zx_points);
  45. return;
  46. while (true) {
  47. for (auto iter = clouds_xyz.begin(); iter != clouds_xyz.end(); iter++) {
  48. client.getCloudData(iter->first, iter->second);
  49. }
  50. int i = 0;
  51. for (const auto &iter: clouds_xyz) {
  52. viewer.removeAllPointClouds(view_port[i]);
  53. viewer.addPointCloud(iter.second, iter.first, view_port[i++]);
  54. }
  55. viewer.spinOnce();
  56. }
  57. }
  58. int CloudDataManager::sendClient() {
  59. DLOG(INFO) << "\n---Debug testRslidar begain\n";
  60. // XYZ2RYRBin("../etc/data/car.txt", "../etc/data/car.bin");
  61. RslidarManager lidar_manager;
  62. lidar_manager.init();
  63. // get lidar
  64. if (lidar_manager.getLidarNumber() == 0) {
  65. return false;
  66. }
  67. std::vector<int> id_list;
  68. lidar_manager.getIdList(id_list);
  69. RslidarDevice *lidar[id_list.size()];
  70. for (int i = 0; i < id_list.size(); i++) {
  71. lidar[i] = lidar_manager.getRslidar(id_list[i]);
  72. }
  73. RslidarMqttAsyncClient mqtt_client;
  74. mqtt_client.init();
  75. CloudPointPacket *packet = new LidarMessageRsHelios();
  76. CloudPointUnpacket unpacket;
  77. std::vector<unsigned char> data;
  78. std::map<std::string, pcl::PointCloud<pcl::PointXYZRGB>::Ptr> clouds_xyz;
  79. for(auto &l:lidar) {
  80. clouds_xyz.insert(std::pair<std::string, pcl::PointCloud<pcl::PointXYZRGB>::Ptr>(LIDAR_SEND_TOPIC + std::to_string(l->id()), new pcl::PointCloud<pcl::PointXYZRGB>));
  81. }
  82. #if VIEWER
  83. pcl::PointCloud<pcl::PointXYZRGB>::Ptr rgb_pointcloud(new pcl::PointCloud<pcl::PointXYZRGB>);
  84. // 添加显示窗口
  85. pcl::PointCloud<pcl::PointALL>::Ptr zx_points(new pcl::PointCloud<pcl::PointALL>);
  86. pcl::visualization::PCLVisualizer viewer("viewer_0");
  87. viewer.setBackgroundColor(0, 0, 0);
  88. viewer.addCoordinateSystem(0.1);
  89. uint8_t color_list[6][3] = {{255, 0, 0},
  90. {0, 255, 0},
  91. {0, 0, 255},
  92. {255, 255, 255},
  93. {255, 255, 0},
  94. {255, 0, 255}
  95. };
  96. sleep(1);
  97. #endif
  98. auto t = std::chrono::steady_clock::now();
  99. std::chrono::duration<double> cost = std::chrono::steady_clock::now() - t;
  100. while (true) {
  101. static int ttt = 0;
  102. if (ttt > 100) {
  103. ttt = 0;
  104. }
  105. ttt++;
  106. for (int i = 0; i < lidar_manager.getLidarNumber(); i++) {
  107. t = std::chrono::steady_clock::now();
  108. lidar_manager.updateRSTransformParam(lidar[i]->id());
  109. data.clear();
  110. // cost = std::chrono::steady_clock::now() - t;
  111. // LOG(INFO) << "before get_last_cloud " << cost.count() * 1000 << " ms.";
  112. lidar[i]->get_last_cloud(data);
  113. // cost = std::chrono::steady_clock::now() - t;
  114. // LOG(INFO) << "after get_last_cloud " << cost.count() * 1000 << " ms.";
  115. // 测试代码,直接发的本地数据
  116. // GetRYRBin(ETC_PATH"DataToCloud/data/car.bin", data);
  117. // usleep(1000 * 25);
  118. if (!data.empty()) {
  119. // LOG(INFO) << "Get cloud data, data size: " << data.size();
  120. // cost = std::chrono::steady_clock::now() - t;
  121. // LOG(INFO) << "before packet " << cost.count() * 1000 << " ms.";
  122. if (packet->packet(data.data(), data.size())) {
  123. // cost = std::chrono::steady_clock::now() - t;
  124. // LOG(INFO) << "after packet " << cost.count() * 1000 << " ms.";
  125. // DLOG(INFO) << "sned topic: " << LIDAR_SEND_TOPIC + std::to_string(lidar[i]->id());
  126. mqtt_client.SendMessage(LIDAR_SEND_TOPIC + std::to_string(lidar[i]->id()), packet->data(),
  127. packet->size());
  128. // cost = std::chrono::steady_clock::now() - t;
  129. // LOG(INFO) << "SendMessage " << cost.count() * 1000 << " ms.";
  130. #if VIEWER
  131. zx_points->clear();
  132. auto iter = clouds_xyz.find(LIDAR_SEND_TOPIC + std::to_string(lidar[i]->id()));
  133. unpacket.unpacket(packet->data(), packet->size(), zx_points);
  134. WriteTxtCloud(ETC_PATH"DataToCloud/data/" + std::to_string(lidar[i]->id() * 100 + ttt) + "_cloud.txt", zx_points);
  135. // if(zx_points->size() > 0) {
  136. // LOG(INFO) << zx_points->begin()->x << " " << zx_points->begin()->y;
  137. // }
  138. mqtt_client.setCloudData(iter->first, zx_points);
  139. #endif
  140. }
  141. DLOG(INFO) << "Send cloud data over";
  142. }
  143. }
  144. #if VIEWER
  145. viewer.removeAllPointClouds();
  146. int i = 0;
  147. for(auto &iter:clouds_xyz) {
  148. // LOG(INFO) << i;
  149. // if (iter.second->empty()) {
  150. // continue;
  151. // }
  152. mqtt_client.getCloudData(iter.first, iter.second, color_list[i++]);
  153. pcl::visualization::PointCloudColorHandlerRGBField<pcl::PointXYZRGB>rgb_color_handler(iter.second);
  154. viewer.addPointCloud<pcl::PointXYZRGB>(iter.second, rgb_color_handler, LIDAR_SEND_TOPIC + iter.first);
  155. // viewer.setPointCloudRenderingProperties(pcl::visualization::PCL_VISUALIZER_POINT_SIZE, 1, LIDAR_SEND_TOPIC + iter.first);
  156. // viewer.addPointCloud(iter.second, LIDAR_SEND_TOPIC + iter.first);
  157. }
  158. viewer.spinOnce();
  159. #endif
  160. }
  161. return 0;
  162. }
  163. int CloudDataManager::testRslidar() {
  164. RslidarManager lidar_manager;
  165. lidar_manager.init();
  166. // get lidar
  167. if (lidar_manager.getLidarNumber() == 0) {
  168. return false;
  169. }
  170. RslidarDevice *lidar[lidar_manager.getLidarNumber()];
  171. for (int i = 0; i < lidar_manager.getLidarNumber(); i++) {
  172. lidar[i] = lidar_manager.getRslidar(i);
  173. }
  174. std::vector<unsigned char> data;
  175. auto now = std::chrono::steady_clock::now();
  176. while (true) {
  177. for (int i = 0; i < lidar_manager.getLidarNumber(); i++) {
  178. std::chrono::duration<double> diff = std::chrono::steady_clock::now() - now;
  179. printf("---Debug time: %fms\n", diff.count() * 1000);
  180. lidar[i]->get_last_cloud(data);
  181. if (!data.empty()) {
  182. LOG(INFO) << "lidar data size" << data.size() << std::endl;
  183. }
  184. }
  185. }
  186. }
  187. int
  188. CloudDataManager::test_call_back(MqttAsyncClient *client, char *topicName, int topicLen, MQTTAsync_message *message) {
  189. // 将unsigned char数组转换为时间戳(单位:毫秒)
  190. uint64_t timestamp = 0;
  191. std::memcpy(&timestamp, message->payload, message->payloadlen);
  192. // 还原为时间点
  193. auto time_point = std::chrono::time_point<std::chrono::steady_clock>(std::chrono::milliseconds(timestamp));
  194. std::chrono::duration<double> diffa = std::chrono::steady_clock::now() - time_point;
  195. printf("---Debug time: %fms\n", diffa.count() * 1000);
  196. return 1;
  197. }
  198. void CloudDataManager::testClient() {
  199. RslidarMqttAsyncClient mqtt_client;
  200. mqtt_client.init();
  201. auto subs = mqtt_client.getSubscribeEtc();
  202. // 循环等待消息
  203. while (true) {
  204. // 获取当前时间点
  205. auto now = std::chrono::steady_clock::now();
  206. // 将时间点转换为时间戳(单位:毫秒)
  207. auto timestamp = std::chrono::duration_cast<std::chrono::milliseconds>(now.time_since_epoch()).count();
  208. // 将时间戳转换为unsigned char数组
  209. unsigned char buffer[sizeof(timestamp)];
  210. for (size_t i = 0; i < sizeof(timestamp); i++) {
  211. buffer[i] = static_cast<unsigned char>((timestamp >> (8 * i)) & 0xFF);
  212. }
  213. mqtt_client.SendMessage(subs[0].topic() + "test-b", buffer, sizeof(buffer));
  214. usleep(1000 * 50);
  215. }
  216. }
  217. bool CloudDataManager::XYZ2RYRBin(const std::string &file, const std::string &file_bin) {
  218. std::vector<unsigned char> save_data;
  219. pcl::PointCloud<pcl::PointXYZ>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZ>);
  220. ReadTxtCloud(file, cloud);
  221. double x, y, z, x_y, r, roll, yaw;
  222. int i = 0;
  223. DLOG(INFO) << "get cloud number:" << cloud->size();
  224. for (auto &point: *cloud) {
  225. x = point.x * 100;
  226. y = point.y * 100;
  227. z = point.z * 100;
  228. r = sqrt(x * x + y * y + z * z);
  229. // DLOG(INFO) << point.x << " " << point.y << " " << point.z << " " << r;
  230. x_y = sqrt(x * x + y * y);
  231. roll = atan2(z, x_y);
  232. yaw = atan2(x, y);
  233. roll = (roll * 18000) / M_PI;
  234. yaw = (yaw * 18000) / M_PI;
  235. if (roll < 0) {
  236. roll += 36000;
  237. }
  238. if (yaw < 0) {
  239. yaw += 36000;
  240. }
  241. r = r / 0.25;
  242. // DLOG(INFO) << roll << " " << yaw << " " << r;
  243. save_data.push_back((unsigned short) r >> 8);
  244. save_data.push_back((unsigned short) r);
  245. save_data.push_back((unsigned short) roll >> 8);
  246. save_data.push_back((unsigned short) roll);
  247. save_data.push_back((unsigned short) yaw >> 8);
  248. save_data.push_back((unsigned short) yaw);
  249. save_data.push_back((unsigned short) 0);
  250. // ZX::PointALL pointa{};
  251. // double distance, roll_radian, yaw_radian;
  252. // distance = (double) ((save_data[i++] << 8 | save_data[i++]) * 0.25) * 0.01;
  253. // pointa.roll = (double) ((save_data[i++] << 8 | save_data[i++]) * 0.01);
  254. // pointa.yaw = (double) ((save_data[i++] << 8 | save_data[i++]) * 0.01);
  255. // pointa.pitch = 0;
  256. // roll_radian = pointa.roll * ZX::PI / 180.0;
  257. // yaw_radian = pointa.yaw * ZX::PI / 180.0;
  258. // pointa.x = distance * cos(roll_radian) * sin(yaw_radian);
  259. // pointa.y = distance * cos(roll_radian) * cos(yaw_radian);
  260. // pointa.z = distance * sin(roll_radian);
  261. // pointa.r3 = distance;
  262. // pointa.i = save_data[i++];
  263. // DLOG(INFO) << pointa.x << " " << pointa.y << " " << pointa.z << " " << distance;
  264. // DLOG(INFO) << pointa.roll << " " << pointa.yaw;
  265. // DLOG(INFO) << "===========================";
  266. }
  267. DLOG(INFO) << "save_data :" << save_data.size();
  268. std::fstream bin_file(file_bin, std::ofstream::trunc | ios::binary | std::ofstream::out);
  269. bin_file.write((const char *) save_data.data(), save_data.size());
  270. bin_file.close();
  271. // exit(EXIT_SUCCESS);
  272. return true;
  273. }
  274. bool CloudDataManager::GetRYRBin(const std::string &file_bin, std::vector<unsigned char> &data) {
  275. std::fstream bin_file(file_bin, ios::binary | std::ofstream::in);
  276. unsigned char uc;
  277. while (bin_file.read((char *) &uc, 1)) {
  278. data.emplace_back(uc);
  279. }
  280. bin_file.close();
  281. DLOG(INFO) << "data :" << data.size();
  282. usleep(1000 * 10);
  283. return true;
  284. }