Skip to content

Commit

Permalink
More tweaking of stratux timestamp handling
Browse files Browse the repository at this point in the history
  • Loading branch information
mutability committed Jun 24, 2019
1 parent c46b7f2 commit b02c414
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 14 deletions.
27 changes: 14 additions & 13 deletions stratux_serial.cc
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,8 @@ void StratuxSerial::ParseInput(const Bytes &buf) {
// 2000000Mbps, 8N1 = 200,000 bytes/s = 200 bytes/ms
static auto unix_epoch = std::chrono::system_clock::from_time_t(0);
auto start_of_read = std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now() - unix_epoch).count() - buf.size() / 200;
std::uint64_t previous_sys_timestamp = 0;
std::uint32_t previous_raw_timestamp = 0;

for (auto i = buf.begin(); i != buf.end();) {
switch (parser_state_) {
Expand Down Expand Up @@ -138,9 +140,17 @@ void StratuxSerial::ParseInput(const Bytes &buf) {
i += bytes_to_copy;

if (message_.size() == message_length_) {
auto previous_sys_timestamp = (messages ? messages->front().ReceivedAt() : message_start_timestamp_);
auto previous_raw_timestamp = (messages ? messages->front().RawTimestamp() : 0);
auto parsed = ParseMessage(message_, previous_sys_timestamp, previous_raw_timestamp);
// work out a suitable timestamp
std::uint32_t raw_timestamp = message_[1] | (message_[2] << 8) | (message_[3] << 16) | (message_[4] << 24);
std::uint64_t sys_timestamp;
if (previous_sys_timestamp != 0 && raw_timestamp > previous_raw_timestamp) {
sys_timestamp = previous_sys_timestamp + (raw_timestamp - previous_raw_timestamp) / 4000;
} else {
sys_timestamp = previous_sys_timestamp = message_start_timestamp_;
previous_raw_timestamp = raw_timestamp;
}

auto parsed = ParseMessage(message_, sys_timestamp);
if (parsed) {
if (!messages) {
messages = std::make_shared<MessageVector>();
Expand All @@ -164,7 +174,7 @@ void StratuxSerial::ParseInput(const Bytes &buf) {
}
}

boost::optional<RawMessage> StratuxSerial::ParseMessage(const Bytes &message, std::uint64_t previous_sys_timestamp, std::uint32_t previous_raw_timestamp) {
boost::optional<RawMessage> StratuxSerial::ParseMessage(const Bytes &message, std::uint64_t sys_timestamp) {
assert(message.size() >= 5);

// not entirely clear what the RSSI format is; here we assume it's
Expand All @@ -174,15 +184,6 @@ boost::optional<RawMessage> StratuxSerial::ParseMessage(const Bytes &message, st

std::uint32_t raw_timestamp = message[1] | (message[2] << 8) | (message[3] << 16) | (message[4] << 24);

// If this is the first message seen in a read, use the system timestamp directly.
// Otherwise, use the reported timestamp relative to the first message
std::uint64_t sys_timestamp;
if (previous_raw_timestamp == 0 || raw_timestamp < previous_raw_timestamp) {
sys_timestamp = previous_sys_timestamp;
} else {
sys_timestamp = previous_sys_timestamp + (raw_timestamp - previous_raw_timestamp) / 4000;
}

Bytes payload;
std::copy(message.begin() + 5, message.end(), std::back_inserter(payload));

Expand Down
2 changes: 1 addition & 1 deletion stratux_serial.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ namespace flightaware::uat {
private:
void StartReading();
void ParseInput(const Bytes &buf);
boost::optional<RawMessage> ParseMessage(const Bytes &message, std::uint64_t previous_sys_timestamp, std::uint32_t previous_raw_timestamp);
boost::optional<RawMessage> ParseMessage(const Bytes &message, std::uint64_t sys_timestamp);
void HandleError(const boost::system::error_code &ec);

// the size of the read buffer
Expand Down

0 comments on commit b02c414

Please sign in to comment.