Skip to content

Commit

Permalink
Ros time update humble (#340)
Browse files Browse the repository at this point in the history
  • Loading branch information
Serafadam authored Jun 26, 2023
1 parent 131bff4 commit ecf772b
Show file tree
Hide file tree
Showing 31 changed files with 261 additions and 78 deletions.
24 changes: 22 additions & 2 deletions depthai_bridge/include/depthai_bridge/DisparityConverter.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,37 @@ class DisparityConverter {
DisparityConverter(
const std::string frameName, float focalLength, float baseline = 7.5, float minDepth = 80, float maxDepth = 1100, bool getBaseDeviceTimestamp = false);
~DisparityConverter();

/**
* @brief Handles cases in which the ROS time shifts forward or backward
* Should be called at regular intervals or on-change of ROS time, depending
* on monitoring.
*
*/
void updateRosBaseTime();

/**
* @brief Commands the converter to automatically update the ROS base time on message conversion based on variable
*
* @param update: bool whether to automatically update the ROS base time on message conversion
*/
void setUpdateRosBaseTimeOnToRosMsg(bool update = true) {
_updateRosBaseTimeOnToRosMsg = update;
}

void toRosMsg(std::shared_ptr<dai::ImgFrame> inData, std::deque<DisparityMsgs::DisparityImage>& outImageMsg);
DisparityImagePtr toRosMsgPtr(std::shared_ptr<dai::ImgFrame> inData);

// void toDaiMsg(const DisparityMsgs::DisparityImage& inMsg, dai::ImgFrame& outData);

private:
const std::string _frameName = "";
const float _focalLength = 882.2, _baseline = 7.5, _minDepth = 80, _maxDepth;
std::chrono::time_point<std::chrono::steady_clock> _steadyBaseTime;
rclcpp::Time _rosBaseTime;
bool _getBaseDeviceTimestamp;
// For handling ROS time shifts and debugging
int64_t _totalNsChange{0};
// Whether to update the ROS base time on each message conversion
bool _updateRosBaseTimeOnToRosMsg{false};
};

} // namespace ros
Expand Down
22 changes: 22 additions & 0 deletions depthai_bridge/include/depthai_bridge/ImageConverter.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,24 @@ class ImageConverter {
ImageConverter(const std::string frameName, bool interleaved, bool getBaseDeviceTimestamp = false);
~ImageConverter();
ImageConverter(bool interleaved, bool getBaseDeviceTimestamp = false);

/**
* @brief Handles cases in which the ROS time shifts forward or backward
* Should be called at regular intervals or on-change of ROS time, depending
* on monitoring.
*
*/
void updateRosBaseTime();

/**
* @brief Commands the converter to automatically update the ROS base time on message conversion based on variable
*
* @param update: bool whether to automatically update the ROS base time on message conversion
*/
void setUpdateRosBaseTimeOnToRosMsg(bool update = true) {
_updateRosBaseTimeOnToRosMsg = update;
}

void toRosMsgFromBitStream(std::shared_ptr<dai::ImgFrame> inData,
std::deque<ImageMsgs::Image>& outImageMsgs,
dai::RawImgFrame::Type type,
Expand Down Expand Up @@ -73,6 +91,10 @@ class ImageConverter {

rclcpp::Time _rosBaseTime;
bool _getBaseDeviceTimestamp;
// For handling ROS time shifts and debugging
int64_t _totalNsChange{0};
// Whether to update the ROS base time on each message conversion
bool _updateRosBaseTimeOnToRosMsg{false};
};

} // namespace ros
Expand Down
22 changes: 22 additions & 0 deletions depthai_bridge/include/depthai_bridge/ImgDetectionConverter.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,24 @@ class ImgDetectionConverter {
// DetectionConverter() = default;
ImgDetectionConverter(std::string frameName, int width, int height, bool normalized = false, bool getBaseDeviceTimestamp = false);
~ImgDetectionConverter();

/**
* @brief Handles cases in which the ROS time shifts forward or backward
* Should be called at regular intervals or on-change of ROS time, depending
* on monitoring.
*
*/
void updateRosBaseTime();

/**
* @brief Commands the converter to automatically update the ROS base time on message conversion based on variable
*
* @param update: bool whether to automatically update the ROS base time on message conversion
*/
void setUpdateRosBaseTimeOnToRosMsg(bool update = true) {
_updateRosBaseTimeOnToRosMsg = update;
}

void toRosMsg(std::shared_ptr<dai::ImgDetections> inNetData, std::deque<VisionMsgs::Detection2DArray>& opDetectionMsgs);

Detection2DArrayPtr toRosMsgPtr(std::shared_ptr<dai::ImgDetections> inNetData);
Expand All @@ -31,6 +49,10 @@ class ImgDetectionConverter {
std::chrono::time_point<std::chrono::steady_clock> _steadyBaseTime;
rclcpp::Time _rosBaseTime;
bool _getBaseDeviceTimestamp;
// For handling ROS time shifts and debugging
int64_t _totalNsChange{0};
// Whether to update the ROS base time on each message conversion
bool _updateRosBaseTimeOnToRosMsg{false};
};

/** TODO(sachin): Do we need to have ros msg -> dai bounding box ?
Expand Down
22 changes: 22 additions & 0 deletions depthai_bridge/include/depthai_bridge/ImuConverter.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,24 @@ class ImuConverter {
bool enable_rotation = false,
bool getBaseDeviceTimestamp = false);
~ImuConverter();

/**
* @brief Handles cases in which the ROS time shifts forward or backward
* Should be called at regular intervals or on-change of ROS time, depending
* on monitoring.
*
*/
void updateRosBaseTime();

/**
* @brief Commands the converter to automatically update the ROS base time on message conversion based on variable
*
* @param update: bool whether to automatically update the ROS base time on message conversion
*/
void setUpdateRosBaseTimeOnToRosMsg(bool update = true) {
_updateRosBaseTimeOnToRosMsg = update;
}

void toRosMsg(std::shared_ptr<dai::IMUData> inData, std::deque<ImuMsgs::Imu>& outImuMsgs);
void toRosDaiMsg(std::shared_ptr<dai::IMUData> inData, std::deque<depthai_ros_msgs::msg::ImuWithMagneticField>& outImuMsgs);

Expand Down Expand Up @@ -114,6 +132,10 @@ class ImuConverter {
std::chrono::time_point<std::chrono::steady_clock> _steadyBaseTime;
rclcpp::Time _rosBaseTime;
bool _getBaseDeviceTimestamp;
// For handling ROS time shifts and debugging
int64_t _totalNsChange{0};
// Whether to update the ROS base time on each message conversion
bool _updateRosBaseTimeOnToRosMsg{false};

void fillImuMsg(dai::IMUReportAccelerometer report, ImuMsgs::Imu& msg);
void fillImuMsg(dai::IMUReportGyroscope report, ImuMsgs::Imu& msg);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,24 @@ class SpatialDetectionConverter {
public:
SpatialDetectionConverter(std::string frameName, int width, int height, bool normalized = false, bool getBaseDeviceTimestamp = false);
~SpatialDetectionConverter();

/**
* @brief Handles cases in which the ROS time shifts forward or backward
* Should be called at regular intervals or on-change of ROS time, depending
* on monitoring.
*
*/
void updateRosBaseTime();

/**
* @brief Commands the converter to automatically update the ROS base time on message conversion based on variable
*
* @param update: bool whether to automatically update the ROS base time on message conversion
*/
void setUpdateRosBaseTimeOnToRosMsg(bool update = true) {
_updateRosBaseTimeOnToRosMsg = update;
}

void toRosMsg(std::shared_ptr<dai::SpatialImgDetections> inNetData, std::deque<SpatialMessages::SpatialDetectionArray>& opDetectionMsg);
void toRosVisionMsg(std::shared_ptr<dai::SpatialImgDetections> inNetData, std::deque<vision_msgs::msg::Detection3DArray>& opDetectionMsg);

Expand All @@ -33,6 +51,10 @@ class SpatialDetectionConverter {

rclcpp::Time _rosBaseTime;
bool _getBaseDeviceTimestamp;
// For handling ROS time shifts and debugging
int64_t _totalNsChange{0};
// Whether to update the ROS base time on each message conversion
bool _updateRosBaseTimeOnToRosMsg{false};
};

/** TODO(sachin): Do we need to have ros msg -> dai bounding box ?
Expand Down
20 changes: 20 additions & 0 deletions depthai_bridge/include/depthai_bridge/depthaiUtility.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ enum LogLevel { DEBUG, INFO, WARN, ERROR, FATAL };

#define DEPTHAI_ROS_FATAL_STREAM_ONCE(loggerName, args) DEPTHAI_ROS_LOG_STREAM(loggerName, dai::ros::LogLevel::FATAL, true, args)

static const int64_t ZERO_TIME_DELTA_NS{100};

inline rclcpp::Time getFrameTime(rclcpp::Time rclBaseTime,
std::chrono::time_point<std::chrono::steady_clock> steadyBaseTime,
std::chrono::time_point<std::chrono::steady_clock, std::chrono::steady_clock::duration> currTimePoint) {
Expand All @@ -84,5 +86,23 @@ inline rclcpp::Time getFrameTime(rclcpp::Time rclBaseTime,
return rclStamp;
}

inline void updateBaseTime(std::chrono::time_point<std::chrono::steady_clock> steadyBaseTime, rclcpp::Time rclBaseTime, int64_t& totalNsChange) {
rclcpp::Time currentRosTime = rclcpp::Clock().now();
std::chrono::time_point<std::chrono::steady_clock> currentSteadyTime = std::chrono::steady_clock::now();
// In nanoseconds
auto expectedOffset = std::chrono::duration_cast<std::chrono::nanoseconds>(currentSteadyTime - steadyBaseTime).count();
uint64_t previousBaseTimeNs = rclBaseTime.nanoseconds();
rclBaseTime = rclcpp::Time(currentRosTime.nanoseconds() - expectedOffset);
uint64_t newBaseTimeNs = rclBaseTime.nanoseconds();
int64_t diff = static_cast<int64_t>(newBaseTimeNs - previousBaseTimeNs);
totalNsChange += diff;
if(::abs(diff) > ZERO_TIME_DELTA_NS) {
// Has been updated
DEPTHAI_ROS_DEBUG_STREAM("ROS BASE TIME CHANGE: ",
"ROS base time changed by " << std::to_string(diff) << " ns. Total change: " << std::to_string(totalNsChange)
<< " ns. New time: " << std::to_string(rclBaseTime.nanoseconds()) << " ns.");
}
}

} // namespace ros
} // namespace dai
7 changes: 7 additions & 0 deletions depthai_bridge/src/DisparityConverter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,14 @@ DisparityConverter::DisparityConverter(

DisparityConverter::~DisparityConverter() = default;

void DisparityConverter::updateRosBaseTime() {
updateBaseTime(_steadyBaseTime, _rosBaseTime, _totalNsChange);
}

void DisparityConverter::toRosMsg(std::shared_ptr<dai::ImgFrame> inData, std::deque<DisparityMsgs::DisparityImage>& outDispImageMsgs) {
if(_updateRosBaseTimeOnToRosMsg) {
updateRosBaseTime();
}
std::chrono::_V2::steady_clock::time_point tstamp;
if(_getBaseDeviceTimestamp)
tstamp = inData->getTimestampDevice();
Expand Down
10 changes: 10 additions & 0 deletions depthai_bridge/src/ImageConverter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,17 @@ ImageConverter::ImageConverter(const std::string frameName, bool interleaved, bo

ImageConverter::~ImageConverter() = default;

void ImageConverter::updateRosBaseTime() {
updateBaseTime(_steadyBaseTime, _rosBaseTime, _totalNsChange);
}

void ImageConverter::toRosMsgFromBitStream(std::shared_ptr<dai::ImgFrame> inData,
std::deque<ImageMsgs::Image>& outImageMsgs,
dai::RawImgFrame::Type type,
const sensor_msgs::msg::CameraInfo& info) {
if(_updateRosBaseTimeOnToRosMsg) {
updateRosBaseTime();
}
std::chrono::_V2::steady_clock::time_point tstamp;
if(_getBaseDeviceTimestamp)
tstamp = inData->getTimestampDevice();
Expand Down Expand Up @@ -95,6 +102,9 @@ void ImageConverter::toRosMsgFromBitStream(std::shared_ptr<dai::ImgFrame> inData
}

void ImageConverter::toRosMsg(std::shared_ptr<dai::ImgFrame> inData, std::deque<ImageMsgs::Image>& outImageMsgs) {
if(_updateRosBaseTimeOnToRosMsg) {
updateRosBaseTime();
}
std::chrono::_V2::steady_clock::time_point tstamp;
if(_getBaseDeviceTimestamp)
tstamp = inData->getTimestampDevice();
Expand Down
8 changes: 7 additions & 1 deletion depthai_bridge/src/ImgDetectionConverter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,14 @@ ImgDetectionConverter::ImgDetectionConverter(std::string frameName, int width, i

ImgDetectionConverter::~ImgDetectionConverter() = default;

void ImgDetectionConverter::updateRosBaseTime() {
updateBaseTime(_steadyBaseTime, _rosBaseTime, _totalNsChange);
}

void ImgDetectionConverter::toRosMsg(std::shared_ptr<dai::ImgDetections> inNetData, std::deque<VisionMsgs::Detection2DArray>& opDetectionMsgs) {
// setting the header
if(_updateRosBaseTimeOnToRosMsg) {
updateRosBaseTime();
}
std::chrono::_V2::steady_clock::time_point tstamp;
if(_getBaseDeviceTimestamp)
tstamp = inNetData->getTimestampDevice();
Expand Down
10 changes: 10 additions & 0 deletions depthai_bridge/src/ImuConverter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ ImuConverter::ImuConverter(const std::string& frameName,

ImuConverter::~ImuConverter() = default;

void ImuConverter::updateRosBaseTime() {
updateBaseTime(_steadyBaseTime, _rosBaseTime, _totalNsChange);
}

void ImuConverter::fillImuMsg(dai::IMUReportAccelerometer report, ImuMsgs::Imu& msg) {
msg.linear_acceleration.x = report.x;
msg.linear_acceleration.y = report.y;
Expand Down Expand Up @@ -84,6 +88,9 @@ void ImuConverter::fillImuMsg(dai::IMUReportMagneticField report, depthai_ros_ms
}

void ImuConverter::toRosMsg(std::shared_ptr<dai::IMUData> inData, std::deque<ImuMsgs::Imu>& outImuMsgs) {
if(_updateRosBaseTimeOnToRosMsg) {
updateRosBaseTime();
}
if(_syncMode != ImuSyncMethod::COPY) {
FillImuData_LinearInterpolation(inData->packets, outImuMsgs);
} else {
Expand All @@ -106,6 +113,9 @@ void ImuConverter::toRosMsg(std::shared_ptr<dai::IMUData> inData, std::deque<Imu
}

void ImuConverter::toRosDaiMsg(std::shared_ptr<dai::IMUData> inData, std::deque<depthai_ros_msgs::msg::ImuWithMagneticField>& outImuMsgs) {
if(_updateRosBaseTimeOnToRosMsg) {
updateRosBaseTime();
}
if(_syncMode != ImuSyncMethod::COPY) {
FillImuData_LinearInterpolation(inData->packets, outImuMsgs);
} else {
Expand Down
10 changes: 10 additions & 0 deletions depthai_bridge/src/SpatialDetectionConverter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,15 @@ SpatialDetectionConverter::SpatialDetectionConverter(std::string frameName, int

SpatialDetectionConverter::~SpatialDetectionConverter() = default;

void SpatialDetectionConverter::updateRosBaseTime() {
updateBaseTime(_steadyBaseTime, _rosBaseTime, _totalNsChange);
}

void SpatialDetectionConverter::toRosMsg(std::shared_ptr<dai::SpatialImgDetections> inNetData,
std::deque<SpatialMessages::SpatialDetectionArray>& opDetectionMsgs) {
if(_updateRosBaseTimeOnToRosMsg) {
updateRosBaseTime();
}
std::chrono::_V2::steady_clock::time_point tstamp;
if(_getBaseDeviceTimestamp)
tstamp = inNetData->getTimestampDevice();
Expand Down Expand Up @@ -87,6 +94,9 @@ SpatialDetectionArrayPtr SpatialDetectionConverter::toRosMsgPtr(std::shared_ptr<

void SpatialDetectionConverter::toRosVisionMsg(std::shared_ptr<dai::SpatialImgDetections> inNetData,
std::deque<vision_msgs::msg::Detection3DArray>& opDetectionMsgs) {
if(_updateRosBaseTimeOnToRosMsg) {
updateRosBaseTime();
}
std::chrono::_V2::steady_clock::time_point tstamp;
if(_getBaseDeviceTimestamp)
tstamp = inNetData->getTimestampDevice();
Expand Down
9 changes: 8 additions & 1 deletion depthai_examples/launch/stereo_inertial_node.launch.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ def generate_launch_description():
enableFloodLight = LaunchConfiguration('enableFloodLight', default = False)
dotProjectormA = LaunchConfiguration('dotProjectormA', default = 200.0)
floodLightmA = LaunchConfiguration('floodLightmA', default = 200.0)
enableRosBaseTimeUpdate = LaunchConfiguration('enableRosBaseTimeUpdate', default = False)
enableRviz = LaunchConfiguration('enableRviz', default = True)


Expand Down Expand Up @@ -294,6 +295,11 @@ def generate_launch_description():
'floodLightmA',
default_value=floodLightmA,
description='Set the mA at which you intend to drive the FloodLight. Default is set to 200mA.')
declare_enableRosBaseTimeUpdate_cmd = DeclareLaunchArgument(
'enableRosBaseTimeUpdate',
default_value=enableRosBaseTimeUpdate,
description='Whether to update ROS time on each message.')


declare_enableRviz_cmd = DeclareLaunchArgument(
'enableRviz',
Expand Down Expand Up @@ -359,7 +365,8 @@ def generate_launch_description():
{'enableDotProjector': enableDotProjector},
{'enableFloodLight': enableFloodLight},
{'dotProjectormA': dotProjectormA},
{'floodLightmA': floodLightmA}
{'floodLightmA': floodLightmA},
{'enableRosBaseTimeUpdate': enableRosBaseTimeUpdate}
])

depth_metric_converter = launch_ros.descriptions.ComposableNode(
Expand Down
Loading

0 comments on commit ecf772b

Please sign in to comment.