- Notifications
You must be signed in to change notification settings - Fork 2
Feature: Basic Robot Simulator #5
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
Merged
Merged
Changes from 7 commits
Commits
Show all changes
18 commits Select commit Hold shift + click to select a range
19dfdb8 tf broadcaster shouldn't necessarly use a timer
24c640c peg the cpu 100% to allow timers to work at a higher rate than 30 Hz
5ae7ba2 add a simulated differential robot that broadcasts a global and base …
ce97024 add tests
cfb526c update readme
e4543c4 folderize
f056d0f i don't like folders
eb0c32f fix imports coding style
ede8213 return nullptr if no msg has been received
3d5f658 add missing std namespace
3b35ca5 update package.xml
d4c9a08 remove garbage
45ecef7 fix typo in packages deps
0188885 hotfix: position was incrementing using y velocities
6d1821b use const for sent goal
653b434 use pragma and fix deps
d4bc235 use api better
f65964a use pragma once standalone
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
130 changes: 130 additions & 0 deletions 130 include/cmr_tests_utils/simulated_differential_robot.hpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,130 @@ | ||
| #ifndef SIMULATED_DIFFERENTIAL_ROBOT | ||
| #define SIMULATED_DIFFERENTIAL_ROBOT | ||
guilyx marked this conversation as resolved. Outdated Show resolved Hide resolved | ||
| | ||
| #include <chrono> | ||
| #include "cmr_tests_utils/basic_tf_broadcaster_node_test.hpp" | ||
| #include "cmr_tests_utils/basic_subscriber_node_test.hpp" | ||
| #include "geometry_msgs/msg/transform_stamped.hpp" | ||
| #include "geometry_msgs/msg/twist.hpp" | ||
guilyx marked this conversation as resolved. Outdated Show resolved Hide resolved | ||
| | ||
| namespace cmr_tests_utils { | ||
| | ||
| class SimulatedDifferentialRobot: public rclcpp::Node | ||
| { | ||
| public: | ||
| | ||
| SimulatedDifferentialRobot(std::string node_name, std::string global_frame, std::string base_frame, std::string velocity_topic, | ||
| double velocity_timeout_sec = 0.5, | ||
| rclcpp::QoS velocity_topic_qos = rclcpp::SystemDefaultsQoS()): rclcpp::Node(node_name) | ||
| { | ||
| clock_ = get_clock(); | ||
| buffer_ = std::make_shared<tf2_ros::Buffer>(clock_); | ||
| listener_ = std::make_shared<tf2_ros::TransformListener>(*buffer_); | ||
| broadcaster_ = std::make_shared<tf2_ros::TransformBroadcaster>(this); | ||
| global_frame_ = global_frame; | ||
| base_frame_ = base_frame; | ||
| velocity_timeout_sec_ = velocity_timeout_sec; | ||
| | ||
| transform_.header.frame_id = global_frame; | ||
| transform_.child_frame_id = base_frame; | ||
| | ||
| timer_ = this->create_wall_timer( | ||
| std::chrono::milliseconds(int(broadcast_period_sec_*1000)), | ||
guilyx marked this conversation as resolved. Outdated Show resolved Hide resolved | ||
| std::bind(&SimulatedDifferentialRobot::broadcast_transform_, this) | ||
| ); | ||
| | ||
| vel_sub_ = this->create_subscription<geometry_msgs::msg::Twist> ( | ||
| velocity_topic, velocity_topic_qos, | ||
| std::bind (&SimulatedDifferentialRobot::vel_callback_, this, std::placeholders::_1)); | ||
| } | ||
| | ||
| bool has_vel_been_received() const | ||
| { | ||
| return (last_received_vel_ != nullptr); | ||
| } | ||
| | ||
| const bool& is_broadcasting() const | ||
| { | ||
| return is_broadcasting_; | ||
| } | ||
| | ||
| const geometry_msgs::msg::TransformStamped& get_transform () const | ||
| { | ||
| return transform_; | ||
| } | ||
| | ||
| geometry_msgs::msg::Twist get_last_velocity_msg() const | ||
| { | ||
| geometry_msgs::msg::Twist msg; | ||
| if (!last_received_vel_) | ||
| { | ||
| RCLCPP_WARN(get_logger(), "Tried to get last received velocity from subscription but nothing was published yet."); | ||
| return msg; | ||
| } | ||
| return *last_received_vel_; | ||
guilyx marked this conversation as resolved. Outdated Show resolved Hide resolved | ||
| } | ||
| | ||
| | ||
| private: | ||
| | ||
| void broadcast_transform_() | ||
| { | ||
| is_broadcasting_ = true; | ||
| | ||
| if (last_received_vel_) | ||
| { | ||
| std::chrono::duration<double> elapsed_time = std::chrono::system_clock::now() - last_velocity_timestamp_; | ||
guilyx marked this conversation as resolved. Show resolved Hide resolved | ||
| | ||
| // Last received command was too long ago, the robot stops | ||
| if (elapsed_time.count() > velocity_timeout_sec_) | ||
| { | ||
| transform_.header.stamp = clock_->now(); | ||
| broadcaster_->sendTransform(transform_); | ||
| return; | ||
| } | ||
| | ||
| // Estimate pose using last command and dt since | ||
| transform_.transform.translation.x += broadcast_period_sec_ * last_received_vel_->linear.x * cos(last_yaw_); | ||
| transform_.transform.translation.y += broadcast_period_sec_ * last_received_vel_->linear.y * sin(last_yaw_); | ||
| last_yaw_ += broadcast_period_sec_ * last_received_vel_->angular.z; | ||
| tf2::Quaternion quat; | ||
| quat.setRPY(0, 0, last_yaw_); | ||
| transform_.transform.rotation = tf2::toMsg(quat); | ||
| } | ||
| | ||
| transform_.header.stamp = clock_->now(); | ||
| broadcaster_->sendTransform(transform_); | ||
| } | ||
| | ||
| void vel_callback_(const std::shared_ptr<geometry_msgs::msg::Twist> msg) | ||
| { | ||
| last_received_vel_ = msg; | ||
| last_velocity_timestamp_ = std::chrono::system_clock::now(); | ||
guilyx marked this conversation as resolved. Show resolved Hide resolved | ||
| } | ||
| | ||
| // Transforms Utils | ||
| rclcpp::TimerBase::SharedPtr timer_; | ||
| geometry_msgs::msg::TransformStamped transform_; | ||
| rclcpp::Duration transform_tolerance_ {0, 0}; | ||
| std::shared_ptr<tf2_ros::Buffer> buffer_; | ||
| std::shared_ptr<tf2_ros::TransformListener> listener_; | ||
| std::shared_ptr<tf2_ros::TransformBroadcaster> broadcaster_; | ||
| rclcpp::Clock::SharedPtr clock_; | ||
| | ||
| // Subscription Utils | ||
| typename rclcpp::Subscription<geometry_msgs::msg::Twist>::SharedPtr vel_sub_; | ||
| std::shared_ptr<geometry_msgs::msg::Twist> last_received_vel_; | ||
| | ||
| // Internal Attributes | ||
| std::string base_frame_; | ||
| std::string global_frame_; | ||
| std::chrono::system_clock::time_point last_velocity_timestamp_; // As long as we don't have TwistStamped for velocities, it's the best we can do | ||
| bool is_broadcasting_ = false; | ||
| double last_yaw_ = 0.0; | ||
| double velocity_timeout_sec_ = 0.5; | ||
| double broadcast_period_sec_ = 0.01; // 100 Hz | ||
| }; | ||
| | ||
| } | ||
| | ||
| #endif | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,119 @@ | ||
| #include "gtest/gtest.h" | ||
| #include "rclcpp/rclcpp.hpp" | ||
| #include "cmr_tests_utils/basic_publisher_node_test.hpp" | ||
| #include "cmr_tests_utils/simulated_differential_robot.hpp" | ||
| #include "cmr_tests_utils/basic_tf_listener_node_test.hpp" | ||
| #include "cmr_tests_utils/multi_thread_spinner.hpp" | ||
| #include "geometry_msgs/msg/transform_stamped.hpp" | ||
| #include "geometry_msgs/msg/pose_stamped.hpp" | ||
| #include "geometry_msgs/msg/twist.hpp" | ||
| #include <chrono> | ||
| #include <thread> | ||
| | ||
| TEST(SimulatedRobot, robot_has_not_moved) | ||
| { | ||
| rclcpp::init(0, nullptr); | ||
| | ||
| auto spinner = cmr_tests_utils::MultiThreadSpinner(); | ||
| auto tf_listener = std::make_shared<cmr_tests_utils::BasicTfListenerNodeTest>("tf_listener_node", 1.0); | ||
| auto robot = std::make_shared<cmr_tests_utils::SimulatedDifferentialRobot>("robot_node", "odom", "base_footprint", "cmd_vel"); | ||
| | ||
| spinner.add_node(tf_listener->get_node_base_interface()); | ||
| spinner.add_node(robot->get_node_base_interface()); | ||
| | ||
| ASSERT_TRUE(spinner.spin_all_nodes()); | ||
| | ||
| // Test if robot has been constructed correctly | ||
| ASSERT_FALSE(robot->has_vel_been_received()); | ||
| ASSERT_EQ(robot->get_transform().header.frame_id, "odom"); | ||
| ASSERT_EQ(robot->get_transform().child_frame_id, "base_footprint"); | ||
| | ||
| std::this_thread::sleep_for(std::chrono::milliseconds(50)); | ||
| ASSERT_TRUE(robot->is_broadcasting()); | ||
| | ||
| // Test if external nodes can access the Robot's state | ||
| geometry_msgs::msg::TransformStamped test_tf; | ||
| ASSERT_TRUE(tf_listener->lookup_transform(test_tf, "odom", "base_footprint")); | ||
| ASSERT_EQ(test_tf.transform.translation.x, 0); | ||
| geometry_msgs::msg::PoseStamped source, target, pose; | ||
| source.header.frame_id = "odom"; | ||
| source.pose.position.x = 5; | ||
| ASSERT_TRUE(tf_listener->transform_pose("base_footprint", source, target)); | ||
| ASSERT_EQ(target.pose.position.x, 5); | ||
| | ||
| rclcpp::shutdown(); | ||
| } | ||
| | ||
| TEST(SimulatedRobot, robot_receives_exactly_one_command) | ||
| { | ||
| rclcpp::init(0, nullptr); | ||
| | ||
| auto spinner = cmr_tests_utils::MultiThreadSpinner(); | ||
| auto robot_spinner = cmr_tests_utils::SingleThreadSpinner(); | ||
| auto velocity_pub = std::make_shared<cmr_tests_utils::BasicPublisherNodeTest<geometry_msgs::msg::Twist>>("velocity_commander_node", "cmd_vel", false); | ||
| auto tf_listener = std::make_shared<cmr_tests_utils::BasicTfListenerNodeTest>("tf_listener_node", 1.0); | ||
| auto robot = std::make_shared<cmr_tests_utils::SimulatedDifferentialRobot>("robot_node", "odom", "base_footprint", "cmd_vel"); | ||
| | ||
| spinner.add_node(velocity_pub->get_node_base_interface()); | ||
| spinner.add_node(tf_listener->get_node_base_interface()); | ||
| spinner.add_node(robot->get_node_base_interface()); | ||
| | ||
| ASSERT_TRUE(spinner.spin_all_nodes()); | ||
| | ||
| // Test if robot has been constructed correctly | ||
| ASSERT_FALSE(robot->has_vel_been_received()); | ||
| ASSERT_EQ(robot->get_transform().header.frame_id, "odom"); | ||
| ASSERT_EQ(robot->get_transform().child_frame_id, "base_footprint"); | ||
| | ||
| std::this_thread::sleep_for(std::chrono::milliseconds(150)); | ||
| ASSERT_TRUE(robot->is_broadcasting()); | ||
| | ||
| // Test if external nodes can access the Robot's state | ||
| geometry_msgs::msg::TransformStamped test_tf; | ||
| ASSERT_TRUE(tf_listener->lookup_transform(test_tf, "odom", "base_footprint")); | ||
| ASSERT_EQ(test_tf.transform.translation.x, 0); | ||
| geometry_msgs::msg::PoseStamped source, target, pose; | ||
| source.header.frame_id = "odom"; | ||
| source.pose.position.x = 5; | ||
| ASSERT_TRUE(tf_listener->transform_pose("base_footprint", source, target)); | ||
| ASSERT_EQ(target.pose.position.x, 5); | ||
| | ||
| // Publish exactly one velocity | ||
| geometry_msgs::msg::Twist twist; | ||
| twist.linear.x = 1.0; | ||
| velocity_pub->publish(twist); | ||
| | ||
| // Wait for the robot to move | ||
| std::this_thread::sleep_for(std::chrono::milliseconds(1000)); | ||
| | ||
| // The robot should've moved at 1.0 m/s for 0.5s, it should be 0.5m forward | ||
| ASSERT_TRUE(robot->has_vel_been_received()); | ||
| ASSERT_EQ(robot->get_transform().header.frame_id, "odom"); | ||
| ASSERT_EQ(robot->get_transform().child_frame_id, "base_footprint"); | ||
| ASSERT_NEAR(robot->get_transform().transform.translation.x, 0.5, 0.01); | ||
| ASSERT_NEAR(robot->get_transform().transform.translation.y, 0.0, 0.01); | ||
| ASSERT_NEAR(robot->get_transform().transform.rotation.x, 0.0, 0.01); | ||
| ASSERT_NEAR(robot->get_transform().transform.rotation.y, 0.0, 0.01); | ||
| ASSERT_NEAR(robot->get_transform().transform.rotation.z, 0.0, 0.01); | ||
| ASSERT_NEAR(robot->get_transform().transform.rotation.w, 1.0, 0.01); | ||
guilyx marked this conversation as resolved. Show resolved Hide resolved | ||
| | ||
| // Send new command | ||
| twist.linear.x = 0; | ||
| twist.angular.z = 3.14; | ||
| velocity_pub->publish(twist); | ||
| | ||
| // Wait for the robot to move | ||
| std::this_thread::sleep_for(std::chrono::milliseconds(1000)); | ||
| | ||
| ASSERT_TRUE(robot->has_vel_been_received()); | ||
| ASSERT_EQ(robot->get_transform().header.frame_id, "odom"); | ||
| ASSERT_EQ(robot->get_transform().child_frame_id, "base_footprint"); | ||
| ASSERT_NEAR(robot->get_transform().transform.translation.x, 0.5, 0.01); | ||
| ASSERT_NEAR(robot->get_transform().transform.translation.y, 0.0, 0.01); | ||
| ASSERT_NEAR(robot->get_transform().transform.rotation.x, 0.0, 0.01); | ||
| ASSERT_NEAR(robot->get_transform().transform.rotation.y, 0.0, 0.01); | ||
| ASSERT_NEAR(robot->get_transform().transform.rotation.z, 0.707, 0.01); | ||
| ASSERT_NEAR(robot->get_transform().transform.rotation.w, 0.707, 0.01); | ||
| | ||
| rclcpp::shutdown(); | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit. This suggestion is invalid because no changes were made to the code. Suggestions cannot be applied while the pull request is closed. Suggestions cannot be applied while viewing a subset of changes. Only one suggestion per line can be applied in a batch. Add this suggestion to a batch that can be applied as a single commit. Applying suggestions on deleted lines is not supported. You must change the existing code in this line in order to create a valid suggestion. Outdated suggestions cannot be applied. This suggestion has been applied or marked resolved. Suggestions cannot be applied from pending reviews. Suggestions cannot be applied on multi-line comments. Suggestions cannot be applied while the pull request is queued to merge. Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.