-
Notifications
You must be signed in to change notification settings - Fork 773
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
[ros2] Add sim_time to gazebo_ros_init #794
Changes from 7 commits
d5a6752
c317d18
0104814
b5e75a3
1959361
165cb30
4f2ff6d
bc1f710
e87b52e
7acdeb8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -60,4 +60,28 @@ std::string SensorFrameID(const gazebo::sensors::Sensor & _sensor, const sdf::El | |
return gazebo_ros::ScopedNameBase(_sensor.ParentName()); | ||
} | ||
|
||
Throttler::Throttler(const double _hz) | ||
: period_(gazebo::common::Time(1.0 / _hz)), | ||
last_time_(0, 0) | ||
{ | ||
} | ||
|
||
bool Throttler::IsReady(const gazebo::common::Time & _now) | ||
{ | ||
// If time went backwords, reset | ||
if (_now < last_time_) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. is this for a backwards time jump? if so a comment would be good |
||
last_time_ = _now; | ||
return true; | ||
} | ||
|
||
// If not enough time has passed, return false | ||
if (_now - last_time_ < period_) { | ||
return false; | ||
} | ||
|
||
// Enough time has passed, set last time to now and return true | ||
last_time_ = _now; | ||
return true; | ||
} | ||
|
||
} // namespace gazebo_ros |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -29,6 +29,7 @@ set(tests | |
test_conversions | ||
test_utils | ||
test_plugins | ||
test_sim_time | ||
) | ||
|
||
foreach(test ${tests}) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
// Copyright 2018 Open Source Robotics Foundation, Inc. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
#include <gtest/gtest.h> | ||
#include <gazebo_ros/testing_utils.hpp> | ||
#include <rclcpp/rclcpp.hpp> | ||
#include <rosgraph_msgs/msg/clock.hpp> | ||
|
||
#include <memory> | ||
#include <string> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. doesn't seem that this or utility are used |
||
#include <vector> | ||
|
||
/// Tests the simtime published to /clock by gazebo_ros_init | ||
class TestSimTime : public ::testing::Test | ||
{ | ||
public: | ||
TestSimTime() {} | ||
void SetUp() override; | ||
void TearDown() override; | ||
|
||
protected: | ||
std::unique_ptr<gazebo_ros::GazeboProcess> gazebo_process_; | ||
}; | ||
|
||
void TestSimTime::SetUp() | ||
{ | ||
gazebo_process_ = std::make_unique<gazebo_ros::GazeboProcess>(std::vector<const char *>{"-s", | ||
"libgazebo_ros_init.so"}); | ||
ASSERT_GT(gazebo_process_->Run(), 0); | ||
} | ||
|
||
void TestSimTime::TearDown() | ||
{ | ||
ASSERT_GE(gazebo_process_->Terminate(), 0); | ||
gazebo_process_.reset(); | ||
} | ||
|
||
TEST_F(TestSimTime, TestClock) | ||
{ | ||
// Create a node which will use sim time | ||
auto context = rclcpp::contexts::default_context::get_global_default_context(); | ||
std::vector<std::string> args; | ||
std::vector<rclcpp::Parameter> params = {rclcpp::Parameter("use_sim_time", true)}; | ||
auto node = std::make_shared<rclcpp::Node>("test_sim_time", "", context, args, params); | ||
|
||
using ClockMsg = rosgraph_msgs::msg::Clock; | ||
|
||
// Get two clock messages, cachine the ROS time once each is received | ||
auto first_msg = | ||
gazebo_ros::get_message_or_timeout<ClockMsg>(node, "/clock", rclcpp::Duration(5, 0)); | ||
ASSERT_NE(first_msg, nullptr); | ||
auto first_msg_time = node->now(); | ||
auto second_msg = | ||
gazebo_ros::get_message_or_timeout<ClockMsg>(node, "/clock", rclcpp::Duration(1, 0)); | ||
ASSERT_NE(second_msg, nullptr); | ||
auto second_msg_time = node->now(); | ||
|
||
// The SIM time should be close to zero (start of simulation) | ||
EXPECT_LT(rclcpp::Time(first_msg->clock), rclcpp::Time(1, 0, RCL_ROS_TIME)); | ||
EXPECT_LT(rclcpp::Time(second_msg->clock), rclcpp::Time(1, 0, RCL_ROS_TIME)); | ||
// Time should go forward | ||
EXPECT_GT(second_msg, first_msg); | ||
// The message from the clock should be the node's time | ||
EXPECT_EQ(first_msg_time, rclcpp::Time(first_msg->clock)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Seems like this is subject to a race condition (additional clock message may have been received by the node after There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I take back what I said, since spinning only happens in |
||
EXPECT_EQ(second_msg_time, rclcpp::Time(second_msg->clock)); | ||
} | ||
|
||
int main(int argc, char ** argv) | ||
{ | ||
rclcpp::init(argc, argv); | ||
::testing::InitGoogleTest(&argc, argv); | ||
return RUN_ALL_TESTS(); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@tfoote do you agree that specifying the rate of publishing to
/clock
wrt to real time is appropriate here? seems reasonable since then even if RTF is really low the clock will still be published regularlyThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There's not a real standard, but I believe that the publish rate wrt to realtime makes sense. It's mostly limited by the clock communication bandwidth requirments which are hardware based and thus connect to realtime.