Skip to content
Snippets Groups Projects
Commit ab06979b authored by Nicolas Pope's avatar Nicolas Pope
Browse files

Vision nodes sync to each other

parent ea25a72a
No related branches found
No related tags found
No related merge requests found
......@@ -612,7 +612,7 @@ void ftl::gui::Screen::draw(NVGcontext *ctx) {
msg = string("Frame rate: ") + std::to_string((int)stats.fps);
nvgText(ctx, screenSize[0]-10, 20, msg.c_str(), NULL);
msg = string("Latency: ") + std::to_string((int)stats.latency) + string("ms");
nvgText(ctx, screenSize[0]-10, 40, msg.c_str(), NULL);
//nvgText(ctx, screenSize[0]-10, 40, msg.c_str(), NULL);
msg = string("Bitrate: ") + to_string_with_precision(stats.bitrate, 2) + string("Mbps");
nvgText(ctx, screenSize[0]-10, 60, msg.c_str(), NULL);
......
......@@ -202,7 +202,10 @@ bool SourceWindow::_processFrameset(ftl::rgbd::FrameSet &fs, bool fromstream) {
}
// Make sure there are enough framesets allocated
_checkFrameSets(fs.id);
{
UNIQUE_LOCK(mutex_, lk);
_checkFrameSets(fs.id);
}
if (!paused_) {
if (!fs.test(ftl::data::FSFlag::PARTIAL) || !screen_->root()->value("drop_partial_framesets", false)) {
......@@ -229,7 +232,10 @@ bool SourceWindow::_processFrameset(ftl::rgbd::FrameSet &fs, bool fromstream) {
}
const auto *cstream = interceptor_;
_createDefaultCameras(*framesets_[fs.id], true); // cstream->available(fs.id).has(Channel::Depth)
{
UNIQUE_LOCK(mutex_, lk);
_createDefaultCameras(*framesets_[fs.id], true); // cstream->available(fs.id).has(Channel::Depth)
}
//LOG(INFO) << "Channels = " << (unsigned int)cstream->available(fs.id);
......
......@@ -53,10 +53,62 @@ using json = nlohmann::json;
static void run(ftl::Configurable *root) {
Universe *net = ftl::create<Universe>(root, "net");
ftl::ctrl::Master ctrl(root, net);
ftl::timer::setHighPrecision(true);
if (root->value("time_master", false)) {
net->bind("time_master", [net]() {
return net->id();
});
LOG(INFO) << "Becoming a time master";
}
if (root->get<string>("time_peer")) {
if (!net->connect(*root->get<string>("time_peer"))->waitConnection()) {
LOG(ERROR) << "Could not connect to time master";
} else {
LOG(INFO) << "Connected to time master";
}
}
auto opt_time_master = net->findOne<ftl::UUID>(string("time_master"));
ftl::UUID time_peer(0);
if (opt_time_master) {
time_peer = *opt_time_master;
LOG(INFO) << "Found a time master: " << time_peer.to_string();
}
int sync_counter = 0;
ftl::ctrl::Master ctrl(root, net);
// Sync clocks!
ftl::timer::add(ftl::timer::kTimerMain, [&time_peer,&sync_counter,net](int64_t ts) {
if (sync_counter-- <= 0 && time_peer != ftl::UUID(0) ) {
sync_counter = 20;
auto start = std::chrono::high_resolution_clock::now();
int64_t now = ftl::timer::get_time();
try {
net->asyncCall<int64_t>(time_peer, "__ping__", [start](const int64_t &mastertime) {
auto elapsed = std::chrono::high_resolution_clock::now() - start;
int64_t latency = std::chrono::duration_cast<std::chrono::microseconds>(elapsed).count();
auto clock_adjust = mastertime + ((latency+500)/2000) - ftl::timer::get_time();
//LOG(INFO) << "LATENCY: " << float(latency)/1000.0f << "ms";
if (clock_adjust != 0) {
LOG(INFO) << "Clock adjustment: " << clock_adjust << ", latency=" << float(latency)/1000.0f << "ms";
ftl::timer::setClockAdjustment(clock_adjust);
}
});
} catch (const std::exception &e) {
LOG(ERROR) << "Ping failed, could not time sync: " << e.what();
return true;
}
}
return true;
});
auto paths = root->get<vector<string>>("paths");
string file = "";
if (paths && (*paths).size() > 0) file = (*paths)[(*paths).size()-1];
......
......@@ -348,39 +348,10 @@ bool Net::_processRequest(ftl::net::Peer &p, const ftl::codecs::Packet &pkt) {
}
// First connected peer (or reconnecting peer) becomes a time server
if (time_peer_ == ftl::UUID(0)) {
/*if (time_peer_ == ftl::UUID(0)) {
time_peer_ = p.id();
DLOG(INFO) << "Adding time peer";
}
}
// Sync clocks!
if (ftl::timer::isClockSlave() && p.id() == time_peer_) {
auto start = std::chrono::high_resolution_clock::now();
int64_t now = ftl::timer::get_time();
if (last_ping_ < now-500) {
last_ping_ = now;
try {
net_->asyncCall<int64_t>(time_peer_, "__ping__", [this, start](const int64_t &mastertime) {
auto elapsed = std::chrono::high_resolution_clock::now() - start;
int64_t latency = std::chrono::duration_cast<std::chrono::milliseconds>(elapsed).count();
auto clock_adjust = mastertime - (ftl::timer::get_time() + (latency/2));
if (clock_adjust != 0) {
LOG(INFO) << "Clock adjustment: " << clock_adjust << ", latency=" << latency/2;
//LOG(INFO) << "Latency: " << (latency / 2);
//LOG(INFO) << "Local: " << std::chrono::time_point_cast<std::chrono::milliseconds>(start).time_since_epoch().count() << ", master: " << mastertime;
ftl::timer::setClockAdjustment(clock_adjust);
}
});
} catch (...) {
LOG(ERROR) << "Ping failed";
// Reset time peer and remove timer
time_peer_ = ftl::UUID(0);
return false;
}
}
}*/
}
return false;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment