global_time_sync_master.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2014 Pavel Kirienko <pavel.kirienko@gmail.com>
3  */
4 
5 #include <gtest/gtest.h>
8 #include "helpers.hpp"
9 
11 {
15 
17  : can(clock)
18  , node(can, clock, nid)
19  { }
20 };
21 
23 {
27 
29  : slave(64)
30  , master_low(120)
31  , master_high(8)
32  {
33  slave.can.others.insert(&master_low.can);
34  master_low.can.others.insert(&slave.can);
35  master_high.can.others.insert(&slave.can);
36  }
37 
39  {
40  assert(!duration.isNegative());
41  unsigned nspins3 = unsigned(duration.toMSec() / 3);
42  nspins3 = nspins3 ? nspins3 : 2;
43  while (nspins3 --> 0)
44  {
48  }
49  }
50 };
51 
52 TEST(GlobalTimeSyncMaster, Basic)
53 {
55 
58 
62 
63  ASSERT_FALSE(master_low.isInitialized());
64 
65  ASSERT_LE(0, slave.start());
66  ASSERT_LE(0, master_low.init());
67  ASSERT_LE(0, master_high.init());
68 
69  ASSERT_TRUE(master_low.isInitialized());
70  ASSERT_FALSE(slave.isActive());
71 
72  /*
73  * Simple synchronization
74  */
75  ASSERT_LE(0, master_low.publish()); // Update
76  nwk.spinAll();
77 
78  usleep(400000);
79  ASSERT_LE(0, master_low.publish()); // Adjustment
80  nwk.spinAll();
81 
82  // Synchronization complete.
83  ASSERT_TRUE(areTimestampsClose(nwk.slave.clock.getUtc(), nwk.master_low.clock.getUtc()));
84  ASSERT_TRUE(slave.isActive());
85  ASSERT_EQ(nwk.master_low.node.getNodeID(), slave.getMasterNodeID());
86 
87  /*
88  * Moving clocks forward and re-syncing with another master
89  */
90  static const uavcan::UtcDuration OneDay = uavcan::UtcDuration::fromMSec(24 * 3600 * 1000);
91  nwk.master_high.clock.utc_adjustment = OneDay;
92 
93  usleep(400000);
94  ASSERT_LE(0, master_low.publish()); // Update from the old master
95  nwk.spinAll();
96 
97  ASSERT_LE(0, master_high.publish()); // Update from the new master
98  nwk.spinAll();
99 
100  usleep(400000);
101  ASSERT_LE(0, master_low.publish()); // Adjustment from the old master (ignored now)
102  ASSERT_LE(0, master_high.publish()); // Adjustment from the new master (accepted)
103  nwk.spinAll();
104 
105  // Synchronization complete.
106  ASSERT_TRUE(areTimestampsClose(nwk.slave.clock.getUtc(), nwk.master_high.clock.getUtc()));
107  ASSERT_FALSE(areTimestampsClose(nwk.slave.clock.getUtc(), nwk.master_low.clock.getUtc()));
108  ASSERT_TRUE(slave.isActive());
109  ASSERT_EQ(nwk.master_high.node.getNodeID(), slave.getMasterNodeID());
110 
111  /*
112  * Frequent calls to publish()
113  */
114  ASSERT_LE(0, master_low.publish()); // Dropped
115  ASSERT_LE(0, master_low.publish()); // Dropped
116  ASSERT_LE(0, master_low.publish()); // Dropped
117 
118  ASSERT_TRUE(nwk.slave.can.read_queue.empty());
119 
120  usleep(400000);
121  ASSERT_LE(0, master_low.publish()); // Accepted
122  ASSERT_FALSE(nwk.slave.can.read_queue.empty());
123 
124  nwk.spinAll();
125 
126  // Synchronization did not change
127  ASSERT_TRUE(areTimestampsClose(nwk.slave.clock.getUtc(), nwk.master_high.clock.getUtc()));
128  ASSERT_FALSE(areTimestampsClose(nwk.slave.clock.getUtc(), nwk.master_low.clock.getUtc()));
129  ASSERT_TRUE(slave.isActive());
130  ASSERT_EQ(nwk.master_high.node.getNodeID(), slave.getMasterNodeID());
131 }
GlobalTimeSyncMasterTestNode
Definition: global_time_sync_master.cpp:10
uavcan::GlobalTimeSyncSlave::isActive
bool isActive() const
Definition: global_time_sync_slave.hpp:177
uavcan::DefaultDataTypeRegistrator
Definition: global_data_type_registry.hpp:186
uavcan::UtcDuration
Definition: time.hpp:189
GlobalTimeSyncMasterTestNode::node
TestNode node
Definition: global_time_sync_master.cpp:14
uavcan::NodeID
Definition: transfer.hpp:112
GlobalTimeSyncMasterTestNode::clock
SystemClockDriver clock
Definition: global_time_sync_master.cpp:12
uavcan::DurationBase< MonotonicDuration >::fromMSec
static MonotonicDuration fromMSec(int64_t ms)
Definition: time.hpp:41
GlobalTimeSyncTestNetwork::GlobalTimeSyncTestNetwork
GlobalTimeSyncTestNetwork()
Definition: global_time_sync_master.cpp:28
GlobalTimeSyncTestNetwork
Definition: global_time_sync_master.cpp:22
uavcan::GlobalTimeSyncSlave::getMasterNodeID
NodeID getMasterNodeID() const
Definition: global_time_sync_slave.hpp:188
uavcan::GlobalTimeSyncSlave::start
int start()
Definition: global_time_sync_slave.hpp:149
GlobalTimeSyncTestNetwork::spinAll
void spinAll(uavcan::MonotonicDuration duration=uavcan::MonotonicDuration::fromMSec(9))
Definition: global_time_sync_master.cpp:38
uavcan::MonotonicDuration
Definition: time.hpp:182
uavcan::INode::spin
int spin(MonotonicTime deadline)
Definition: abstract_node.hpp:63
GlobalTimeSyncTestNetwork::master_high
GlobalTimeSyncMasterTestNode master_high
Definition: global_time_sync_master.cpp:26
TestNode
Definition: test_node.hpp:20
SystemClockDriver::utc_adjustment
uavcan::UtcDuration utc_adjustment
Definition: libuavcan/libuavcan/test/clock.hpp:65
areTimestampsClose
static bool areTimestampsClose(const T &a, const T &b, int64_t precision_usec=100000)
Definition: libuavcan/libuavcan/test/clock.hpp:103
helpers.hpp
uavcan::GlobalTimeSyncMaster::init
int init(const TransferPriority priority=TransferPriority::OneLowerThanHighest)
Definition: global_time_sync_master.hpp:154
global_time_sync_slave.hpp
PairableCanDriver
Definition: test_node.hpp:52
uavcan::GlobalTimeSyncMaster
Definition: global_time_sync_master.hpp:32
SystemClockDriver::getUtc
virtual uavcan::UtcTime getUtc() const
Definition: libuavcan/libuavcan/test/clock.hpp:79
uavcan::INode::getNodeID
NodeID getNodeID() const
Definition: abstract_node.hpp:39
GlobalTimeSyncTestNetwork::master_low
GlobalTimeSyncMasterTestNode master_low
Definition: global_time_sync_master.cpp:25
TEST
TEST(GlobalTimeSyncMaster, Basic)
Definition: global_time_sync_master.cpp:52
global_time_sync_master.hpp
GlobalTimeSyncTestNetwork::slave
GlobalTimeSyncMasterTestNode slave
Definition: global_time_sync_master.cpp:24
PairableCanDriver::read_queue
std::queue< uavcan::CanFrame > read_queue
Definition: test_node.hpp:56
uavcan::GlobalTimeSyncMaster::isInitialized
bool isInitialized() const
Definition: global_time_sync_master.hpp:197
SystemClockDriver
Definition: libuavcan/libuavcan/test/clock.hpp:62
GlobalTimeSyncMasterTestNode::can
PairableCanDriver can
Definition: global_time_sync_master.cpp:13
uavcan::GlobalDataTypeRegistry::instance
static GlobalDataTypeRegistry & instance()
Definition: uc_global_data_type_registry.cpp:128
uavcan::GlobalTimeSyncSlave
Definition: global_time_sync_slave.hpp:28
PairableCanDriver::others
std::set< PairableCanDriver * > others
Definition: test_node.hpp:55
GlobalTimeSyncMasterTestNode::GlobalTimeSyncMasterTestNode
GlobalTimeSyncMasterTestNode(uavcan::NodeID nid)
Definition: global_time_sync_master.cpp:16
uavcan::GlobalTimeSyncMaster::publish
int publish()
Definition: global_time_sync_master.hpp:210


uavcan_communicator
Author(s):
autogenerated on Fri Dec 13 2024 03:10:02