-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c6d340e
commit 4f5966c
Showing
4 changed files
with
102 additions
and
5 deletions.
There are no files selected for viewing
This file contains 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 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 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,36 @@ | ||
#ifndef TWIST_MUX | ||
#define TWIST_MUX | ||
|
||
#include "geometry_msgs/msg/twist_stamped.hpp" | ||
#include "std_msgs/msg/bool.hpp" | ||
#include "std_msgs/msg/string.hpp" | ||
#include <rclcpp/publisher.hpp> | ||
#include <rclcpp/rclcpp.hpp> | ||
#include <rclcpp_components/register_node_macro.hpp> | ||
|
||
namespace twist_mux | ||
{ | ||
class TwistMux : public rclcpp::Node | ||
{ | ||
public: | ||
explicit TwistMux(const rclcpp::NodeOptions & options); | ||
|
||
private: | ||
rclcpp::Subscription<geometry_msgs::msg::TwistStamped>::SharedPtr | ||
autonomous_subscriber; | ||
rclcpp::Subscription<geometry_msgs::msg::TwistStamped>::SharedPtr | ||
teleop_subscriber; | ||
|
||
rclcpp::Subscription<std_msgs::msg::Bool>::SharedPtr enabled_subscriber; | ||
|
||
rclcpp::Subscription<std_msgs::msg::String>::SharedPtr mode_subscriber; | ||
|
||
std::shared_ptr<rclcpp::Publisher<geometry_msgs::msg::TwistStamped>> | ||
cmd_publisher; | ||
|
||
bool enabled; | ||
std::string mode; | ||
}; | ||
} // namespace twist_mux | ||
|
||
#endif |
This file contains 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,57 @@ | ||
#include "twist_mux.hpp" | ||
#include <rclcpp/logging.hpp> | ||
|
||
namespace twist_mux | ||
{ | ||
TwistMux::TwistMux(const rclcpp::NodeOptions & options) | ||
: Node("twist_mux", options) | ||
{ | ||
declare_parameter("autonomous_cmd_vel_topic", "/cmd_vel_autonomous"); | ||
declare_parameter("teleop_cmd_vel_topic", "/cmd_vel_teleop"); | ||
declare_parameter("enabled_topic", "/cmd_vel_enabled"); | ||
declare_parameter("mode_topic", "/cmd_vel_mode"); | ||
declare_parameter("cmd_out_topic", "/rover_drivetrain_controller/cmd_vel"); | ||
|
||
enabled = true; | ||
mode = "autonomous"; | ||
|
||
cmd_publisher = create_publisher<geometry_msgs::msg::TwistStamped>( | ||
get_parameter("cmd_out_topic").as_string(), rclcpp::SystemDefaultsQoS()); | ||
|
||
autonomous_subscriber = create_subscription<geometry_msgs::msg::TwistStamped>( | ||
get_parameter("autonomous_cmd_vel_topic").as_string(), | ||
rclcpp::SystemDefaultsQoS(), | ||
[this](const geometry_msgs::msg::TwistStamped::SharedPtr msg) { | ||
if (enabled && mode == "autonomous") { | ||
cmd_publisher->publish(*msg); | ||
} | ||
}); | ||
|
||
teleop_subscriber = create_subscription<geometry_msgs::msg::TwistStamped>( | ||
get_parameter("teleop_cmd_vel_topic").as_string(), | ||
rclcpp::SystemDefaultsQoS(), | ||
[this](const geometry_msgs::msg::TwistStamped::SharedPtr msg) { | ||
if (enabled && mode == "teleop") { | ||
cmd_publisher->publish(*msg); | ||
} | ||
}); | ||
|
||
enabled_subscriber = create_subscription<std_msgs::msg::Bool>( | ||
get_parameter("enabled_topic").as_string(), rclcpp::SystemDefaultsQoS(), | ||
[this](const std_msgs::msg::Bool::SharedPtr msg) { | ||
enabled = msg->data; | ||
if (!enabled) { | ||
geometry_msgs::msg::TwistStamped msg; | ||
msg.header.stamp = now(); | ||
cmd_publisher->publish(msg); | ||
} | ||
// RCLCPP_WARN(get_logger(), "Enabled: %d", enabled); | ||
}); | ||
|
||
mode_subscriber = create_subscription<std_msgs::msg::String>( | ||
get_parameter("mode_topic").as_string(), rclcpp::SystemDefaultsQoS(), | ||
[this](const std_msgs::msg::String::SharedPtr msg) {mode = msg->data;}); | ||
} | ||
} // namespace twist_mux | ||
|
||
RCLCPP_COMPONENTS_REGISTER_NODE(twist_mux::TwistMux) |