void RawDataStreamPa::getRosParams() { if (is_ros_subscriber_) { pnh_.param<std::string>("dir", file_dir_, ""); } else { pnh_.param<std::string>("raw_data_stream/dir", file_dir_, ""); pnh_.param("raw_data_stream/publish", flag_publish_, false); } }
时间: 2023-02-09 11:16:30 浏览: 85
这个函数主要是用于获取 ROS 参数的。如果变量 `is_ros_subscriber_` 为真,就从参数服务器中获取参数 `dir` 的值,并将其赋值给变量 `file_dir_`。否则,就从参数服务器中获取参数 `raw_data_stream/dir` 的值,并将其赋值给变量 `file_dir_`。然后,从参数服务器中获取参数 `raw_data_stream/publish` 的值,并将其赋值给变量 `flag_publish_`。
相关问题
RawDataStreamPa::RawDataStreamPa(bool is_ros_subscriber) : pnh_(ros::NodeHandle("~")), flag_publish_(false), is_ros_subscriber_(is_ros_subscriber) { }
这是一个C++程序,它定义了一个名为RawDataStreamPa的类的构造函数。
在这个构造函数中,会创建一个ros::NodeHandle对象,并将一个bool类型的变量初始化为输入参数is_ros_subscriber。
其中,ros::NodeHandle("~")表示创建一个名为"~"的节点句柄,用于访问ROS系统的各种服务。
flag_publish_和is_ros_subscriber_变量都被初始化为false。
#include<iostream> #include<ctime> #include<chrono> #include<string> #include<filesystem> #include<fstream> #include<sstream> #include<thread> #include<boost/filesystem.hpp> const uintmax_t MAX_LOGS_SIZE = 10ull * 1024ull * 1024ull * 1024ull; //const uintmax_t MAX_LOGS_SIZE = 10ull; void create_folder(std::string folder_name) { boost::filesystem::create_directory(folder_name); std::string sub_foldername=folder_name+"/logs_ros"; boost::filesystem::create_directory(sub_foldername); } std::string get_current_time() { auto now = std::chrono::system_clock::now(); std::time_t now_c = std::chrono::system_clock::to_time_t(now); std::tm parts = *std::localtime(&now_c); char buffer[20]; std::strftime(buffer, sizeof(buffer), "%Y-%m-%d-%H-%M", &parts); return buffer; } void check_logs_size() { std::string logs_path = "/home/sage/logs/"; boost::filesystem::path logs_dir(logs_path); std::uintmax_t total_size = 0; for (const auto& file : boost::filesystem::recursive_directory_iterator(logs_dir)) { if (boost::filesystem::is_regular_file(file)) { total_size += boost::filesystem::file_size(file); } } if (total_size > MAX_LOGS_SIZE) { boost::filesystem::path earliest_dir; std::time_t earliest_time = std::time(nullptr); for (const auto& dir : boost::filesystem::directory_iterator(logs_dir)) { if (boost::filesystem::is_directory(dir)) { std::string dir_name = dir.path().filename().string(); std::tm time_parts = {}; std::istringstream ss(dir_name); std::string part; std::getline(ss, part, '-'); time_parts.tm_year = std::stoi(part) - 1900; std::getline(ss, part, '-'); time_parts.tm_mon = std::stoi(part) - 1; std::getline(ss, part, '-'); time_parts.tm_mday = std::stoi(part); std::getline(ss, part, '-'); time_parts.tm_hour = std::stoi(part); std::getline(ss, part, '-'); time_parts.tm_min = std::stoi(part); std::time_t dir_time = std::mktime(&time_parts); if (dir_time < earliest_time) { earliest_time = dir_time; earliest_dir = dir.path(); } } } if (!earliest_dir.empty()) { boost::filesystem::remove_all(earliest_dir); } } } int main() { std::string logs_path = "/home/sage/logs/"; while (true) { std::chrono::system_clock::time_point now = std::chrono::system_clock::now(); std::time_t now_c = std::chrono::system_clock::to_time_t(now); std::tm parts = *std::localtime(&now_c); if (parts.tm_min % 10 == 0) { std::string folder_name = logs_path + get_current_time(); create_folder(folder_name); } check_logs_size(); std::this_thread::sleep_for(std::chrono::minutes(1)); } return 0; }修改为ros节点
好的,我可以帮您将这段代码修改为ROS节点。首先,我们需要包含ROS所需的头文件,例如:
```cpp
#include <ros/ros.h>
```
然后,我们需要在main函数中初始化ROS节点:
```cpp
int main(int argc, char** argv) {
ros::init(argc, argv, "log_manager_node");
ros::NodeHandle nh;
// ...
return 0;
}
```
接下来,我们需要将定时任务和日志删除任务分别封装为ROS的定时器和服务。在main函数中,我们可以这样定义一个定时器:
```cpp
ros::Timer timer = nh.createTimer(ros::Duration(60.0), timerCallback);
```
其中,`timerCallback` 是一个你需要定义的回调函数,它将在每隔60秒被定时器调用一次。在回调函数中,你可以像之前的代码一样执行创建文件夹和检查日志大小的任务。
同样地,我们可以定义一个ROS服务来执行删除任务。我们需要在main函数中定义一个服务服务器:
```cpp
ros::ServiceServer delete_logs_server = nh.advertiseService("delete_logs", deleteLogsCallback);
```
其中,`deleteLogsCallback` 是一个你需要定义的服务回调函数,它将在收到删除日志服务请求时被调用。在回调函数中,你可以像之前的代码一样执行删除日志的任务。
最后,完整的ROS节点代码如下:
```cpp
#include <ros/ros.h>
#include <ctime>
#include <chrono>
#include <string>
#include <filesystem>
#include <fstream>
#include <sstream>
#include <thread>
#include <boost/filesystem.hpp>
const uintmax_t MAX_LOGS_SIZE = 10ull * 1024ull * 1024ull * 1024ull;
void create_folder(std::string folder_name) {
boost::filesystem::create_directory(folder_name);
std::string sub_foldername = folder_name + "/logs_ros";
boost::filesystem::create_directory(sub_foldername);
}
std::string get_current_time() {
auto now = std::chrono::system_clock::now();
std::time_t now_c = std::chrono::system_clock::to_time_t(now);
std::tm parts = *std::localtime(&now_c);
char buffer[20];
std::strftime(buffer, sizeof(buffer), "%Y-%m-%d-%H-%M", &parts);
return buffer;
}
void check_logs_size() {
std::string logs_path = "/home/sage/logs/";
boost::filesystem::path logs_dir(logs_path);
std::uintmax_t total_size = 0;
for (const auto& file : boost::filesystem::recursive_directory_iterator(logs_dir)) {
if (boost::filesystem::is_regular_file(file)) {
total_size += boost::filesystem::file_size(file);
}
}
if (total_size > MAX_LOGS_SIZE) {
boost::filesystem::path earliest_dir;
std::time_t earliest_time = std::time(nullptr);
for (const auto& dir : boost::filesystem::directory_iterator(logs_dir)) {
if (boost::filesystem::is_directory(dir)) {
std::string dir_name = dir.path().filename().string();
std::tm time_parts = {};
std::istringstream ss(dir_name);
std::string part;
std::getline(ss, part, '-');
time_parts.tm_year = std::stoi(part) - 1900;
std::getline(ss, part, '-');
time_parts.tm_mon = std::stoi(part) - 1;
std::getline(ss, part, '-');
time_parts.tm_mday = std::stoi(part);
std::getline(ss, part, '-');
time_parts.tm_hour = std::stoi(part);
std::getline(ss, part, '-');
time_parts.tm_min = std::stoi(part);
std::time_t dir_time = std::mktime(&time_parts);
if (dir_time < earliest_time) {
earliest_time = dir_time;
earliest_dir = dir.path();
}
}
}
if (!earliest_dir.empty()) {
boost::filesystem::remove_all(earliest_dir);
}
}
}
void timerCallback(const ros::TimerEvent& event) {
std::string logs_path = "/home/sage/logs/";
std::chrono::system_clock::time_point now = std::chrono::system_clock::now();
std::time_t now_c = std::chrono::system_clock::to_time_t(now);
std::tm parts = *std::localtime(&now_c);
if (parts.tm_min % 10 == 0) {
std::string folder_name = logs_path + get_current_time();
create_folder(folder_name);
}
check_logs_size();
}
bool deleteLogsCallback(std_srvs::Empty::Request& req, std_srvs::Empty::Response& res) {
std::string logs_path = "/home/sage/logs/";
boost::filesystem::path logs_dir(logs_path);
boost::filesystem::remove_all(logs_dir);
return true;
}
int main(int argc, char** argv) {
ros::init(argc, argv, "log_manager_node");
ros::NodeHandle nh;
ros::Timer timer = nh.createTimer(ros::Duration(60.0), timerCallback);
ros::ServiceServer delete_logs_server = nh.advertiseService("delete_logs", deleteLogsCallback);
ros::spin();
return 0;
}
```
在这个节点中,我们定义了一个名为 `log_manager_node` 的ROS节点,并在其中实现了一个定时器和一个服务。定时器每隔60秒检查一次日志大小并创建新的日志文件夹,服务则用于删除所有日志文件。
阅读全文