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  {
45  ASSERT_LE(0, slave.node.spin(uavcan::MonotonicDuration::fromMSec(1)));
46  ASSERT_LE(0, master_low.node.spin(uavcan::MonotonicDuration::fromMSec(1)));
47  ASSERT_LE(0, master_high.node.spin(uavcan::MonotonicDuration::fromMSec(1)));
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 }
std::set< PairableCanDriver * > others
Definition: test_node.hpp:55
virtual uavcan::UtcTime getUtc() const
void spinAll(uavcan::MonotonicDuration duration=uavcan::MonotonicDuration::fromMSec(9))
int spin(MonotonicTime deadline)
NodeID getNodeID() const
int init(const TransferPriority priority=TransferPriority::OneLowerThanHighest)
static bool areTimestampsClose(const T &a, const T &b, int64_t precision_usec=100000)
GlobalTimeSyncMasterTestNode master_low
GlobalTimeSyncMasterTestNode slave
static GlobalDataTypeRegistry & instance()
GlobalTimeSyncMasterTestNode master_high
TEST(GlobalTimeSyncMaster, Basic)
static MonotonicDuration fromMSec(int64_t ms)
Definition: time.hpp:41
std::queue< uavcan::CanFrame > read_queue
Definition: test_node.hpp:56
GlobalTimeSyncMasterTestNode(uavcan::NodeID nid)


uavcan_communicator
Author(s):
autogenerated on Wed Jan 11 2023 03:59:39