mirror of
https://gitee.com/mirrors_PX4/PX4-Autopilot.git
synced 2026-10-21 00:23:34 +08:00
Fixed Linux driver: SocketCan driver adds time offset from the provided clock instance, which fixes time synchronization in PerDriverPrivate clock adjustment mode
This commit is contained in:
@@ -31,12 +31,12 @@ class DefaultLogSink : public uavcan::ILogSink
|
|||||||
*/
|
*/
|
||||||
struct DriverPack
|
struct DriverPack
|
||||||
{
|
{
|
||||||
SocketCanDriver can;
|
|
||||||
SystemClock clock;
|
SystemClock clock;
|
||||||
|
SocketCanDriver can;
|
||||||
|
|
||||||
DriverPack(ClockAdjustmentMode clock_adjustment_mode)
|
DriverPack(ClockAdjustmentMode clock_adjustment_mode)
|
||||||
: can()
|
: clock(clock_adjustment_mode)
|
||||||
, clock(clock_adjustment_mode)
|
, can(clock)
|
||||||
{ }
|
{ }
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -116,7 +116,7 @@ class SocketCanIface : public uavcan::ICanIface
|
|||||||
{ }
|
{ }
|
||||||
};
|
};
|
||||||
|
|
||||||
const SystemClock clock_;
|
const SystemClock& clock_;
|
||||||
const int fd_;
|
const int fd_;
|
||||||
|
|
||||||
const unsigned max_frames_in_socket_tx_queue_;
|
const unsigned max_frames_in_socket_tx_queue_;
|
||||||
@@ -280,6 +280,7 @@ class SocketCanIface : public uavcan::ICanIface
|
|||||||
}
|
}
|
||||||
if (accept)
|
if (accept)
|
||||||
{
|
{
|
||||||
|
rx.ts_utc += clock_.getPrivateAdjustment();
|
||||||
rx_queue_.push(rx);
|
rx_queue_.push(rx);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -298,8 +299,9 @@ public:
|
|||||||
/**
|
/**
|
||||||
* Takes ownership of socket's file descriptor.
|
* Takes ownership of socket's file descriptor.
|
||||||
*/
|
*/
|
||||||
explicit SocketCanIface(int socket_fd, int max_frames_in_socket_tx_queue = 3)
|
SocketCanIface(const SystemClock& clock, int socket_fd, int max_frames_in_socket_tx_queue = 3)
|
||||||
: fd_(socket_fd)
|
: clock_(clock)
|
||||||
|
, fd_(socket_fd)
|
||||||
, max_frames_in_socket_tx_queue_(max_frames_in_socket_tx_queue)
|
, max_frames_in_socket_tx_queue_(max_frames_in_socket_tx_queue)
|
||||||
, frames_in_socket_tx_queue_(0)
|
, frames_in_socket_tx_queue_(0)
|
||||||
{
|
{
|
||||||
@@ -487,14 +489,15 @@ public:
|
|||||||
static constexpr unsigned MaxIfaces = uavcan::MaxCanIfaces;
|
static constexpr unsigned MaxIfaces = uavcan::MaxCanIfaces;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
const SystemClock clock_;
|
const SystemClock& clock_;
|
||||||
uavcan::LazyConstructor<SocketCanIface> ifaces_[MaxIfaces];
|
uavcan::LazyConstructor<SocketCanIface> ifaces_[MaxIfaces];
|
||||||
::pollfd pollfds_[MaxIfaces];
|
::pollfd pollfds_[MaxIfaces];
|
||||||
std::uint8_t num_ifaces_;
|
std::uint8_t num_ifaces_;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
SocketCanDriver()
|
explicit SocketCanDriver(const SystemClock& clock)
|
||||||
: num_ifaces_(0)
|
: clock_(clock)
|
||||||
|
, num_ifaces_(0)
|
||||||
{
|
{
|
||||||
for (auto& p : pollfds_)
|
for (auto& p : pollfds_)
|
||||||
{
|
{
|
||||||
@@ -601,7 +604,7 @@ public:
|
|||||||
// Construct the iface - upon successful construction the iface will take ownership of the fd.
|
// Construct the iface - upon successful construction the iface will take ownership of the fd.
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
ifaces_[num_ifaces_].construct<int>(fd);
|
ifaces_[num_ifaces_].construct<const SystemClock&, int>(clock_, fd);
|
||||||
}
|
}
|
||||||
catch (...)
|
catch (...)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -32,8 +32,16 @@ static void testSocketRxTx(const std::string& iface_name)
|
|||||||
const int sock2 = uavcan_linux::SocketCanIface::openSocket(iface_name);
|
const int sock2 = uavcan_linux::SocketCanIface::openSocket(iface_name);
|
||||||
ENFORCE(sock1 >= 0 && sock2 >= 0);
|
ENFORCE(sock1 >= 0 && sock2 >= 0);
|
||||||
|
|
||||||
uavcan_linux::SocketCanIface if1(sock1);
|
/*
|
||||||
uavcan_linux::SocketCanIface if2(sock2);
|
* Clocks will have some offset from the true system time
|
||||||
|
* SocketCAN driver must handle this correctly
|
||||||
|
*/
|
||||||
|
uavcan_linux::SystemClock clock_impl(uavcan_linux::ClockAdjustmentMode::PerDriverPrivate);
|
||||||
|
clock_impl.adjustUtc(uavcan::UtcDuration::fromMSec(100000));
|
||||||
|
const uavcan_linux::SystemClock& clock = clock_impl;
|
||||||
|
|
||||||
|
uavcan_linux::SocketCanIface if1(clock, sock1);
|
||||||
|
uavcan_linux::SocketCanIface if2(clock, sock2);
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Sending two frames, one of which must be returned back
|
* Sending two frames, one of which must be returned back
|
||||||
@@ -69,8 +77,6 @@ static void testSocketRxTx(const std::string& iface_name)
|
|||||||
uavcan::UtcTime ts_utc;
|
uavcan::UtcTime ts_utc;
|
||||||
uavcan::CanIOFlags flags = 0;
|
uavcan::CanIOFlags flags = 0;
|
||||||
|
|
||||||
const uavcan_linux::SystemClock clock(uavcan_linux::ClockAdjustmentMode::PerDriverPrivate);
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Read first
|
* Read first
|
||||||
*/
|
*/
|
||||||
@@ -132,8 +138,16 @@ static void testSocketFilters(const std::string& iface_name)
|
|||||||
const int sock2 = uavcan_linux::SocketCanIface::openSocket(iface_name);
|
const int sock2 = uavcan_linux::SocketCanIface::openSocket(iface_name);
|
||||||
ENFORCE(sock1 >= 0 && sock2 >= 0);
|
ENFORCE(sock1 >= 0 && sock2 >= 0);
|
||||||
|
|
||||||
uavcan_linux::SocketCanIface if1(sock1);
|
/*
|
||||||
uavcan_linux::SocketCanIface if2(sock2);
|
* Clocks will have some offset from the true system time
|
||||||
|
* SocketCAN driver must handle this correctly
|
||||||
|
*/
|
||||||
|
uavcan_linux::SystemClock clock_impl(uavcan_linux::ClockAdjustmentMode::PerDriverPrivate);
|
||||||
|
clock_impl.adjustUtc(uavcan::UtcDuration::fromMSec(-1000));
|
||||||
|
const uavcan_linux::SystemClock& clock = clock_impl;
|
||||||
|
|
||||||
|
uavcan_linux::SocketCanIface if1(clock, sock1);
|
||||||
|
uavcan_linux::SocketCanIface if2(clock, sock2);
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Configuring filters
|
* Configuring filters
|
||||||
@@ -199,7 +213,15 @@ static void testSocketFilters(const std::string& iface_name)
|
|||||||
|
|
||||||
static void testDriver(const std::vector<std::string>& iface_names)
|
static void testDriver(const std::vector<std::string>& iface_names)
|
||||||
{
|
{
|
||||||
uavcan_linux::SocketCanDriver driver;
|
/*
|
||||||
|
* Clocks will have some offset from the true system time
|
||||||
|
* SocketCAN driver must handle this correctly
|
||||||
|
*/
|
||||||
|
uavcan_linux::SystemClock clock_impl(uavcan_linux::ClockAdjustmentMode::PerDriverPrivate);
|
||||||
|
clock_impl.adjustUtc(uavcan::UtcDuration::fromMSec(9000000));
|
||||||
|
const uavcan_linux::SystemClock& clock = clock_impl;
|
||||||
|
|
||||||
|
uavcan_linux::SocketCanDriver driver(clock);
|
||||||
for (auto ifn : iface_names)
|
for (auto ifn : iface_names)
|
||||||
{
|
{
|
||||||
std::cout << "Adding iface " << ifn << std::endl;
|
std::cout << "Adding iface " << ifn << std::endl;
|
||||||
@@ -254,7 +276,6 @@ static void testDriver(const std::vector<std::string>& iface_names)
|
|||||||
/*
|
/*
|
||||||
* Receive loopback
|
* Receive loopback
|
||||||
*/
|
*/
|
||||||
const uavcan_linux::SystemClock clock(uavcan_linux::ClockAdjustmentMode::PerDriverPrivate);
|
|
||||||
for (int i = 0; i < driver.getNumIfaces(); i++)
|
for (int i = 0; i < driver.getNumIfaces(); i++)
|
||||||
{
|
{
|
||||||
uavcan::CanFrame frame;
|
uavcan::CanFrame frame;
|
||||||
|
|||||||
Reference in New Issue
Block a user