robot_time/WALLTIME_QUICKSTART.md

1.9 KiB

WallTime - Quick Start Guide

Sử dụng nhanh

1. Include header

#include <robot/walltime.h>

2. Đo thời gian thực thi

robot::WallTime start = robot::WallTime::now();
// ... your code ...
robot::WallDuration elapsed = robot::WallTime::now() - start;
std::cout << "Took " << elapsed.toSec() << " seconds" << std::endl;

3. Timeout

robot::WallTime deadline = robot::WallTime::now() + robot::WallDuration(5.0);
while (robot::WallTime::now() < deadline) {
    // Do work
}

4. Sleep

// Sleep for 1 second
robot::WallDuration(1.0).sleep();

// Sleep until specific time
robot::WallTime target = robot::WallTime::now() + robot::WallDuration(5.0);
robot::WallTime::sleepUntil(target);

Đặc điểm

  • Header-only: Chỉ cần include một file
  • Không cần khởi tạo: Dùng ngay
  • Thread-safe: An toàn với multi-threading
  • Cross-platform: Linux, Windows, macOS

Tài liệu đầy đủ

  • WALLTIME_LIBRARY.md - API reference đầy đủ
  • WALLTIME_USAGE.md - Hướng dẫn chi tiết và ví dụ
  • examples/walltime_example.cpp - Nhiều ví dụ thực tế

So sánh với Time

Time WallTime
Cần init Không
Simulated time Không
Dùng cho ROS messages Nên Không nên
Đo thời gian thực Không phù hợp Phù hợp

Ví dụ hoàn chỉnh

#include <robot/walltime.h>
#include <iostream>

int main()
{
    // Đo thời gian
    robot::WallTime start = robot::WallTime::now();
    
    // Simulate work
    robot::WallDuration(0.5).sleep();
    
    robot::WallDuration elapsed = robot::WallTime::now() - start;
    std::cout << "Elapsed: " << elapsed.toSec() << "s" << std::endl;
    
    return 0;
}

Compile:

g++ -std=c++17 -I./include your_file.cpp -o your_program