Your code snippets for accumulating the total bytes received per second seem to be on the right track, but there are a couple areas where improvements could be made for better accuracy and thread safety.
- Mutex for Total Bytes: The
total_bytes variable should be protected with a mutex to ensure thread safety when accessed from multiple threads.
- Reset the Counters Periodically: You might want to reset the counters periodically after each calculation to avoid infinite accumulation.
- Separate Timer for Byte Rate Calculation: Using a timer callback dedicated to calculating and printing the byte rate is better practice and separates the concerns of message handling and performance monitoring.
Here's the revised code:
- Class Definition: Ensure you have
imageMutex and total_bytes as member variables of your class, as well as a new timer:
class cameraLidarNode : public rclcpp::Node {
public:
cameraLidarNode();
~cameraLidarNode();
private:
void imageCallback(const sensor_msgs::msg::Image::SharedPtr msg);
void topic_callback(const sensor_msgs::msg::PointCloud2::SharedPtr msg);
void calculateByteRate();
rclcpp::Subscription<sensor_msgs::msg::Image>::SharedPtr image_subscription_;
rclcpp::Subscription<sensor_msgs::msg::PointCloud2>::SharedPtr point_cloud_subscription_;
rclcpp::Publisher<sensor_msgs::msg::Image>::SharedPtr publisher;
rclcpp::TimerBase::SharedPtr timer_;
std::shared_ptr<CameraLidarProcessor> processor;
std::thread cameraLidarProjectionThread;
cv::Mat image;
bool img;
std::mutex imageMutex;
size_t total_bytes;
rclcpp::Time start_time;
};
- Constructor: Initialize
total_bytes to zero and set up the timer:
cameraLidarNode::cameraLidarNode()
: Node("image_lidar_subscriber"), processor(std::make_shared<CameraLidarProcessor>()),
total_bytes(0), img(false), start_time(this->now()) {
image_subscription_ = this->create_subscription<sensor_msgs::msg::Image>(
"/kitti/image/color/left", 200, std::bind(&cameraLidarNode::imageCallback, this, std::placeholders::_1));
point_cloud_subscription_ = this->create_subscription<sensor_msgs::msg::PointCloud2>(
"/kitti/point_cloud", 200, std::bind(&cameraLidarNode::topic_callback, this, std::placeholders::_1));
publisher = this->create_publisher<sensor_msgs::msg::Image>("projection_frames", 10);
cameraLidarProjectionThread = std::thread(&CameraLidarProcessor::cameraLidarProjection, processor);
// Timer for periodically calculating byte rate
timer_ = this->create_wall_timer(
std::chrono::seconds(1), std::bind(&cameraLidarNode::calculateByteRate, this));
}
- Image Callback: Make sure
total_bytes is protected by a mutex:
void cameraLidarNode::imageCallback(const sensor_msgs::msg::Image::SharedPtr msg) {
std::lock_guard<std::mutex> lock(imageMutex);
image = cv::Mat(msg->height, msg->width, CV_8UC3, const_cast<unsigned char *>(msg->data.data()));
if (image.empty()) {
std::cerr << "Error: Received empty image." << std::endl;
return;
}
img = true;
// Accumulate the total bytes
total_bytes += msg->data.size();
}
- Byte Rate Calculation Method: Add a separate method to calculate and print the byte rate:
void cameraLidarNode::calculateByteRate() {
std::lock_guard<std::mutex> lock(imageMutex);
// Calculate elapsed time
auto current_time = this->now();
auto duration = current_time - start_time;
if(duration.seconds() > 0) {
double bytes_per_sec = total_bytes / duration.seconds();
std::cout << "Bytes per second: " << bytes_per_sec << std::endl;
// Reset counters
start_time = current_time;
total_bytes = 0;
}
}
Now let's compile and run your node. Ensure the necessary headers and libraries are linked correctly in your CMakeLists.txt.
By using a timer to periodically calculate the byte rate, you encapsulate the performance monitoring separately from your message processing, achieving a more robust and accurate design.