Skip to content

Commit

Permalink
add explicit time conversion functions
Browse files Browse the repository at this point in the history
  • Loading branch information
togaen committed Jun 4, 2020
1 parent 59fff7c commit 4702ee3
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 3 deletions.
21 changes: 21 additions & 0 deletions include/a2d2_to_ros/conversions.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,27 @@

namespace a2d2_to_ros {

/**
* @brief Convert seconds to microseconds
* @note This function does not protect against overflow
* TODO(jeff) should be using a dimensional analysis library
*/
uint64_t microseconds_to_nanoseconds(uint64_t s);

/**
* @brief Convert seconds to microseconds
* @note This function does not protect against overflow
* TODO(jeff) should be using a dimensional analysis library
*/
uint64_t microseconds_to_seconds(uint64_t s);

/**
* @brief Convert seconds to microseconds
* @note This function does not protect against overflow
* TODO(jeff) should be using a dimensional analysis library
*/
uint64_t seconds_to_microseconds(uint64_t s);

/**
* @brief Convert a 2D index to a 1D index according to CNPY convention.
*/
Expand Down
18 changes: 15 additions & 3 deletions src/a2d2_to_ros/conversions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,28 @@ namespace a2d2_to_ros {

//------------------------------------------------------------------------------

uint64_t microseconds_to_nanoseconds(uint64_t s) { return (s * ONE_THOUSAND); }

//------------------------------------------------------------------------------

uint64_t microseconds_to_seconds(uint64_t s) { return (s / ONE_MILLION); }

//------------------------------------------------------------------------------

uint64_t seconds_to_microseconds(uint64_t s) { return (s * ONE_MILLION); }

//------------------------------------------------------------------------------

size_t flatten_2d_index(size_t width, size_t row, size_t col) {
return ((row * width) + col);
}

//------------------------------------------------------------------------------

ros::Time a2d2_timestamp_to_ros_time(uint64_t time) {
const auto truncated_secs = (time / ONE_MILLION);
const auto mu_secs = (time - (truncated_secs * ONE_MILLION));
const auto n_secs = (mu_secs * ONE_THOUSAND);
const auto truncated_secs = microseconds_to_seconds(time);
const auto mu_secs = (time - seconds_to_microseconds(truncated_secs));
const auto n_secs = microseconds_to_nanoseconds(mu_secs);
return ros::Time(static_cast<uint32_t>(truncated_secs),
static_cast<uint32_t>(n_secs));
}
Expand Down

0 comments on commit 4702ee3

Please sign in to comment.