This repository was archived by the owner on Jul 10, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 783
[ros2] Port elevator to ROS2 #945
Merged
Merged
Changes from all commits
Commits
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
71 changes: 0 additions & 71 deletions
71
.ros1_unported/gazebo_plugins/include/gazebo_plugins/gazebo_ros_elevator.h
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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
68 changes: 68 additions & 0 deletions
68
gazebo_plugins/include/gazebo_plugins/gazebo_ros_elevator.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,68 @@ | ||
| // Copyright 2019 Open Source Robotics Foundation | ||
| // | ||
| // 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. | ||
|
|
||
| #ifndef GAZEBO_PLUGINS__GAZEBO_ROS_ELEVATOR_HPP_ | ||
| #define GAZEBO_PLUGINS__GAZEBO_ROS_ELEVATOR_HPP_ | ||
|
|
||
| #include <gazebo/plugins/ElevatorPlugin.hh> | ||
| #include <std_msgs/msg/string.hpp> | ||
|
|
||
| #include <memory> | ||
|
|
||
| namespace gazebo_plugins | ||
| { | ||
| class GazeboRosElevatorPrivate; | ||
|
|
||
| /// A elevator plugin for gazebo. | ||
| /** | ||
| Example Usage: | ||
| \code{.xml} | ||
| <!-- Plugin to control the elevator --> | ||
| <plugin name="elevator" filename="libgazebo_ros_elevator.so"> | ||
| <ros> | ||
| <namespace>demo</namespace> | ||
|
|
||
| <!-- topic remapping --> | ||
| <argument>elevator:=elevator_demo</argument> | ||
| </ros> | ||
|
|
||
| <!-- min and max floor constraints --> | ||
| <bottom_floor>0</bottom_floor> | ||
| <top_floor>1</top_floor> | ||
| \endcode | ||
| */ | ||
| class GazeboRosElevator : public gazebo::ElevatorPlugin | ||
| { | ||
| public: | ||
| /// Constructor | ||
| GazeboRosElevator(); | ||
|
|
||
| /// Destructor | ||
| ~GazeboRosElevator(); | ||
|
|
||
| protected: | ||
| /// Callback for receiving message on elevator's topic. | ||
| /// \param[in] msg String message that contains a elevator command. | ||
| void OnElevator(const std_msgs::msg::String::ConstSharedPtr msg); | ||
|
|
||
| // Documentation inherited | ||
| void Load(gazebo::physics::ModelPtr _model, sdf::ElementPtr _sdf) override; | ||
|
|
||
| private: | ||
| /// Private data pointer | ||
| std::unique_ptr<GazeboRosElevatorPrivate> impl_; | ||
| }; | ||
| } // namespace gazebo_plugins | ||
|
|
||
| #endif // GAZEBO_PLUGINS__GAZEBO_ROS_ELEVATOR_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,86 @@ | ||
| // Copyright 2019 Open Source Robotics Foundation | ||
| // | ||
| // 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 <gazebo_plugins/gazebo_ros_elevator.hpp> | ||
| #include <gazebo_ros/node.hpp> | ||
| #include <std_msgs/msg/string.hpp> | ||
|
|
||
| #include <limits> | ||
| #include <memory> | ||
|
|
||
| namespace gazebo_plugins | ||
| { | ||
| class GazeboRosElevatorPrivate | ||
| { | ||
| public: | ||
| /// A pointer to the GazeboROS node. | ||
| gazebo_ros::Node::SharedPtr ros_node_; | ||
|
|
||
| /// Subscriber to elevator commands | ||
| rclcpp::Subscription<std_msgs::msg::String>::SharedPtr sub_; | ||
|
|
||
| /// Lower most floor | ||
| int bottom_; | ||
|
|
||
| /// Top most floor | ||
| int top_; | ||
| }; | ||
|
|
||
| GazeboRosElevator::GazeboRosElevator() | ||
| : impl_(std::make_unique<GazeboRosElevatorPrivate>()) | ||
| { | ||
| } | ||
|
|
||
| GazeboRosElevator::~GazeboRosElevator() | ||
| { | ||
| } | ||
|
|
||
| void GazeboRosElevator::Load(gazebo::physics::ModelPtr _model, sdf::ElementPtr _sdf) | ||
| { | ||
| ElevatorPlugin::Load(_model, _sdf); | ||
|
|
||
| // Initialize ROS node | ||
| impl_->ros_node_ = gazebo_ros::Node::Get(_sdf); | ||
|
|
||
| impl_->sub_ = impl_->ros_node_->create_subscription<std_msgs::msg::String>( | ||
| "elevator", rclcpp::QoS(rclcpp::KeepLast(1)), | ||
| std::bind(&GazeboRosElevator::OnElevator, this, std::placeholders::_1)); | ||
|
|
||
| RCLCPP_INFO(impl_->ros_node_->get_logger(), "Subscribed to [%s]", impl_->sub_->get_topic_name()); | ||
|
|
||
| impl_->bottom_ = _sdf->Get<int>("bottom_floor", std::numeric_limits<int>::min()).first; | ||
|
|
||
| impl_->top_ = _sdf->Get<int>("top_floor", std::numeric_limits<int>::max()).first; | ||
| } | ||
|
|
||
| void GazeboRosElevator::OnElevator(const std_msgs::msg::String::ConstSharedPtr msg) | ||
| { | ||
| try { | ||
| int target_floor = std::stoi(msg->data); | ||
| if (target_floor < impl_->bottom_) { | ||
| RCLCPP_ERROR(impl_->ros_node_->get_logger(), "Target floor number below lowermost floor"); | ||
| return; | ||
| } | ||
| if (target_floor > impl_->top_) { | ||
| RCLCPP_ERROR(impl_->ros_node_->get_logger(), "Target floor number above topmost floor"); | ||
| return; | ||
| } | ||
| MoveToFloor(target_floor); | ||
| } catch (const std::exception & /*e*/) { | ||
| RCLCPP_ERROR(impl_->ros_node_->get_logger(), "Invalid floor number for elevator"); | ||
| } | ||
| } | ||
|
|
||
| GZ_REGISTER_MODEL_PLUGIN(GazeboRosElevator) | ||
| } // namespace gazebo_plugins | ||
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
Oops, something went wrong.
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.
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.
So strange that the original plugin takes a string when we need an int 🤔 We could easily change this to
Int32for a cleaner implementation.I wonder if it was done like this because Gazebo's
ElevatorPluginuses a string message. And the reason for that is thatgazebo::msgsdoesn't have an integer message...