Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/time sync #2216

Merged
merged 33 commits into from
Dec 15, 2023
Merged

Feature/time sync #2216

merged 33 commits into from
Dec 15, 2023

Conversation

MishkaMN
Copy link
Contributor

@MishkaMN MishkaMN commented Dec 15, 2023

PR Details

Description

This PR supports usdot-fhwa-stol/carma-ns3-adapter#11
During integration testing of VRU, it was discovered that the CARLA's time which we call "game time" is not synced with the "simulation time" which is used and synced between sumo and mosaic.
CARLA's game time goes up until the sumo is hit start, which is when the all timelins are synced correctly, but the game time still has the initial offset from when CARLA started and up until sumo was hit started.

Upon trying to fix this issue, the ns3-adapter (which should be renamed to cdasim-adapter due to its new functionality) was identified as the potential source to broadcast the /clock for ROS network to provide the correct simulation time. However, doing so makes the carma-platform not behave the way it should and debugging could be non-trivial:
TODO: github link

Therefore, the only place the carma-platform has a time difference with the cdasim is when it receives SPAT message. Therefore, this specific logic is modified to adjust for that time difference.

Related GitHub Issue

Fixes this exact issue in this PR: #2230

Related Jira Key

CDAR-602

Motivation and Context

Integration testing of VRU

How Has This Been Tested?

cdasim integration tested on the simulation PC1

Types of changes

  • Defect fix (non-breaking change that fixes an issue)
  • New feature (non-breaking change that adds functionality)
  • Breaking change (fix or feature that cause existing functionality to change)

Checklist:

  • I have added any new packages to the sonar-scanner.properties file
  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • I have read the CONTRIBUTING document.
  • I have added tests to cover my changes.
  • All new and existing tests passed.

@MishkaMN MishkaMN requested a review from adamlm December 15, 2023 17:35
Comment on lines +110 to +115
ros1_clock_sub_ = rclcpp::create_subscription<rosgraph_msgs::msg::Clock>(node_topics_, "/clock", 1,
std::bind(&WMListenerWorker::ros1ClockCallback, worker_.get(), std::placeholders::_1), ros1_clock_options);

sim_clock_sub_ = rclcpp::create_subscription<rosgraph_msgs::msg::Clock>(node_topics_, "/sim_clock", 1,
std::bind(&WMListenerWorker::simClockCallback, worker_.get(), std::placeholders::_1), sim_clock_options);

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These should be lambdas instead of std::bind. Lambdas can completely replace std::bind and is the recommended implementation pattern. Consider:

ros1_clock_sub_ = rclcpp::create_subscription<rosgraph_msgs::msg::Clock>(node_topics_, "/clock", 1,
[this](const rosgraph_msgs::msg::Clock::SharedPtr msg) {
  this->worker_.simClockCallback(msg);
},
ros1_clock_options);

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is an optional suggestion, but it would be good to remove std::bind usage throughout this file.

Comment on lines +165 to +169
void setRos1Clock(rclcpp::Time time_now);

/*! \brief Set simulation clock clock (only used in simulation runs)
*/
void setSimulationClock(rclcpp::Time time_now);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These can be made const references: const rclcpp::Time &.

Comment on lines +616 to +621
void CARMAWorldModel::setRos1Clock(rclcpp::Time time_now)
{
ros1_clock_ = time_now;
}

void CARMAWorldModel::setSimulationClock(rclcpp::Time time_now)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These can be made const references: const rclcpp::Time &

// NOTE: In simulation, ROS1 clock (often coming from CARLA) can have a large time ahead.
// the timing calculated here is in Simulation time, which is behind. Therefore, the world model adds the offset to make it meaningful to carma-platform:
// https://github.com/usdot-fhwa-stol/carma-platform/issues/2217
if (is_simulation && ros1_clock_.has_value() && simulation_clock_.has_value())
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

std::optional is implicitly convertible to bool, so we don't need the .has_value() calls. Being explicit in this context also seems to go against the ISO C++ code guidelines on verbosity (see here). Consider replacing with

if (is_simulation && ros1_clock_ && simulation_clock_)

Comment on lines +98 to +102
auto ros1_clock_group = node_base_->create_callback_group(rclcpp::CallbackGroupType::MutuallyExclusive);
ros1_clock_options.callback_group = ros1_clock_group;

auto sim_clock_group = node_base_->create_callback_group(rclcpp::CallbackGroupType::MutuallyExclusive);
sim_clock_options.callback_group = sim_clock_group;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider assigning the create_callback_group to avoid having to copy the std::shared_ptr and an extra variable:

ros1_clock_options.callback_group = node_base_->create_callback_group(rclcpp::CallbackGroupType::MutuallyExclusive);

@MishkaMN MishkaMN merged commit a3d27f6 into develop Dec 15, 2023
2 of 3 checks passed
@MishkaMN MishkaMN self-assigned this Dec 15, 2023
@MishkaMN MishkaMN mentioned this pull request Dec 15, 2023
9 tasks
@MishkaMN MishkaMN deleted the feature/time-sync branch January 2, 2024 20:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants