mirror of
https://gitee.com/mirrors_PX4/PX4-Autopilot.git
synced 2026-07-18 17:10:35 +08:00
Added clock test, superheader uavcan_linux.hpp
This commit is contained in:
@@ -18,6 +18,6 @@ find_library(UAVCAN_LIB uavcan REQUIRED)
|
||||
include_directories(include)
|
||||
set(CMAKE_CXX_FLAGS "-Wall -Wextra -Werror -pedantic -std=c++0x -g3 -O1") # GCC or Clang
|
||||
|
||||
add_executable(log_subscriber test/log_subscriber.cpp)
|
||||
target_link_libraries(log_subscriber ${UAVCAN_LIB})
|
||||
target_link_libraries(log_subscriber rt)
|
||||
add_executable(test_clock test/test_clock.cpp)
|
||||
target_link_libraries(test_clock ${UAVCAN_LIB})
|
||||
target_link_libraries(test_clock rt)
|
||||
|
||||
@@ -0,0 +1,10 @@
|
||||
/*
|
||||
* Copyright (C) 2014 Pavel Kirienko <pavel.kirienko@gmail.com>
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <uavcan/uavcan.hpp>
|
||||
|
||||
#include <uavcan_linux/clock.hpp>
|
||||
#include <uavcan_linux/socketcan.hpp>
|
||||
@@ -1,30 +0,0 @@
|
||||
/*
|
||||
* Copyright (C) 2014 Pavel Kirienko <pavel.kirienko@gmail.com>
|
||||
*/
|
||||
|
||||
#include <iostream>
|
||||
#include <cerrno>
|
||||
#include <uavcan/uavcan.hpp>
|
||||
#include <uavcan_linux/socketcan.hpp>
|
||||
#include <uavcan_linux/clock.hpp>
|
||||
|
||||
int main()
|
||||
{
|
||||
double sec = 0;
|
||||
std::cout << "Enter system time adjustment in seconds: " << std::endl;
|
||||
std::cin >> sec;
|
||||
|
||||
uavcan_linux::SystemClockDriver clock;
|
||||
|
||||
try
|
||||
{
|
||||
clock.adjustUtc(uavcan::UtcDuration::fromUSec(sec * 1e6));
|
||||
}
|
||||
catch (const uavcan_linux::Exception& ex)
|
||||
{
|
||||
std::cout << ex.what() << std::endl;
|
||||
std::cout << strerror(ex.getErrno()) << std::endl;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
/*
|
||||
* Copyright (C) 2014 Pavel Kirienko <pavel.kirienko@gmail.com>
|
||||
*/
|
||||
|
||||
#include <iostream>
|
||||
#include <cerrno>
|
||||
#include <chrono>
|
||||
#include <uavcan_linux/uavcan_linux.hpp>
|
||||
|
||||
static std::string systime2str(const std::chrono::system_clock::time_point& tp)
|
||||
{
|
||||
const auto tt = std::chrono::system_clock::to_time_t(tp);
|
||||
return std::ctime(&tt);
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
double sec = 0;
|
||||
std::cout << "Enter system time adjustment in seconds (fractions allowed): " << std::endl;
|
||||
std::cin >> sec;
|
||||
|
||||
uavcan_linux::SystemClockDriver clock;
|
||||
|
||||
const auto before = std::chrono::system_clock::now();
|
||||
try
|
||||
{
|
||||
clock.adjustUtc(uavcan::UtcDuration::fromUSec(sec * 1e6));
|
||||
}
|
||||
catch (const uavcan_linux::Exception& ex)
|
||||
{
|
||||
std::cout << ex.what() << std::endl;
|
||||
std::cout << strerror(ex.getErrno()) << std::endl;
|
||||
return 1;
|
||||
}
|
||||
const auto after = std::chrono::system_clock::now();
|
||||
|
||||
std::cout << "Time before: " << systime2str(before) << "\n"
|
||||
<< "Time after: " << systime2str(after) << "\n"
|
||||
<< "Millisecond diff (after - before): "
|
||||
<< std::chrono::duration_cast<std::chrono::milliseconds>(after - before).count() << std::endl;
|
||||
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user