-
Notifications
You must be signed in to change notification settings - Fork 2
Add gtest #7
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
base: ros2
Are you sure you want to change the base?
Add gtest #7
Changes from all commits
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 |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| // Copyright 2025 amsl | ||
|
|
||
| #ifndef ROOMBA_500DRIVER_MEIJI__TWIST_TO_ROOMBACTRL_CONVERTER_HPP_ | ||
| #define ROOMBA_500DRIVER_MEIJI__TWIST_TO_ROOMBACTRL_CONVERTER_HPP_ | ||
|
|
||
| #include <memory> | ||
|
|
||
| #include <geometry_msgs/msg/twist.hpp> | ||
| #include <rclcpp/rclcpp.hpp> | ||
| #include <roomba_500driver_meiji/msg/roomba_ctrl.hpp> | ||
|
|
||
| namespace roomba_500driver_meiji | ||
| { | ||
| class TwistToRoombactrlConverter : public rclcpp::Node | ||
| { | ||
| public: | ||
| TwistToRoombactrlConverter(void); | ||
|
|
||
| void cmd_vel_callback(const std::shared_ptr<geometry_msgs::msg::Twist> msg); | ||
|
|
||
| private: | ||
| rclcpp::Publisher<roomba_500driver_meiji::msg::RoombaCtrl>::SharedPtr | ||
| ctrl_pub_; | ||
| rclcpp::Subscription<geometry_msgs::msg::Twist>::SharedPtr cmd_vel_sub_; | ||
| }; | ||
|
|
||
| } // namespace roomba_500driver_meiji | ||
|
|
||
| #endif // ROOMBA_500DRIVER_MEIJI__TWIST_TO_ROOMBACTRL_CONVERTER_HPP_ |
|
Contributor
Author
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. テスト内でTwistToRoombactrlConverterクラスを使うためにmain()を別ファイルに分離しました |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| // Copyright 2025 amsl | ||
|
|
||
| #include <rclcpp/rclcpp.hpp> | ||
| #include <roomba_500driver_meiji/twist_to_roombactrl_converter.hpp> | ||
|
|
||
| int main(int argc, char ** argv) | ||
| { | ||
| rclcpp::init(argc, argv); | ||
| rclcpp::spin( | ||
| std::make_shared<roomba_500driver_meiji::TwistToRoombactrlConverter>() | ||
| ); | ||
| rclcpp::shutdown(); | ||
| return 0; | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,75 @@ | ||
| // Copyright 2025 amsl | ||
|
|
||
| #include <gtest/gtest.h> | ||
| #include <rclcpp/rclcpp.hpp> | ||
| #include <geometry_msgs/msg/twist.hpp> | ||
| #include <roomba_500driver_meiji/msg/roomba_ctrl.hpp> | ||
| #include <roomba_500driver_meiji/twist_to_roombactrl_converter.hpp> | ||
|
|
||
| class TwistToRoombactrlConverterTest : public ::testing::Test | ||
| { | ||
| public: | ||
| void ctrl_callback(const roomba_500driver_meiji::msg::RoombaCtrl::SharedPtr msg) | ||
| { | ||
| received_control_ = msg; | ||
| } | ||
|
|
||
| rclcpp::Node::SharedPtr node_; | ||
| rclcpp::Subscription<roomba_500driver_meiji::msg::RoombaCtrl>::SharedPtr ctrl_sub_; | ||
| rclcpp::Publisher<geometry_msgs::msg::Twist>::SharedPtr cmd_vel_pub_; | ||
| roomba_500driver_meiji::msg::RoombaCtrl::SharedPtr received_control_; | ||
|
|
||
| protected: | ||
| void SetUp() override | ||
| { | ||
| node_ = std::make_shared<rclcpp::Node>("test_twist_to_roombactrl_converter"); | ||
| ctrl_sub_ = node_->create_subscription<roomba_500driver_meiji::msg::RoombaCtrl>( | ||
| "roomba/control", 10, | ||
| std::bind(&TwistToRoombactrlConverterTest::ctrl_callback, this, std::placeholders::_1) | ||
| ); | ||
| cmd_vel_pub_ = node_->create_publisher<geometry_msgs::msg::Twist>( | ||
| "cmd_vel", 10 | ||
| ); | ||
| } | ||
|
|
||
| void TearDown() override | ||
| { | ||
| received_control_.reset(); | ||
| ctrl_sub_.reset(); | ||
| node_.reset(); | ||
| } | ||
| }; | ||
|
|
||
| TEST_F(TwistToRoombactrlConverterTest, cmd_vel) | ||
| { | ||
| rclcpp::executors::SingleThreadedExecutor executor; | ||
|
Contributor
Author
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. 1プロセスで2つのノードを動かすため、Executorを使っています |
||
| const auto converter_node = | ||
| std::make_shared<roomba_500driver_meiji::TwistToRoombactrlConverter>(); | ||
|
|
||
| executor.add_node(converter_node); | ||
| executor.add_node(node_); | ||
|
|
||
| const auto twist_msg = std::make_shared<geometry_msgs::msg::Twist>(); | ||
| twist_msg->linear.x = 1.0; | ||
| twist_msg->angular.z = 0.5; | ||
|
|
||
| cmd_vel_pub_->publish(*twist_msg); | ||
| while (rclcpp::ok()) { | ||
| if (received_control_) { | ||
| break; | ||
| } | ||
| executor.spin_once(); | ||
| } | ||
|
Comment on lines
+57
to
+62
Contributor
Author
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.
|
||
| EXPECT_EQ(received_control_->mode, roomba_500driver_meiji::msg::RoombaCtrl::DRIVE_DIRECT); | ||
| EXPECT_EQ(received_control_->cntl.linear.x, twist_msg->linear.x); | ||
| EXPECT_EQ(received_control_->cntl.angular.z, twist_msg->angular.z); | ||
|
Comment on lines
+63
to
+65
Contributor
Author
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.
|
||
| } | ||
|
|
||
| int main(int argc, char ** argv) | ||
| { | ||
| rclcpp::init(argc, argv); | ||
| testing::InitGoogleTest(&argc, argv); | ||
| int ret = RUN_ALL_TESTS(); | ||
| rclcpp::shutdown(); | ||
| return ret; | ||
| } | ||
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.
テスト内でTwistToRoombactrlConverterクラスを使うために宣言をヘッダファイルに分離しました