Skip to content

Commit

Permalink
Add loopback parameter
Browse files Browse the repository at this point in the history
Signed-off-by: Joshua Whitley <[email protected]>
  • Loading branch information
JWhitleyWork committed Dec 4, 2024
1 parent fb45862 commit ef3e8ac
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,9 @@ class SOCKETCAN_PUBLIC SocketCanReceiver
{
public:
/// Constructor
explicit SocketCanReceiver(const std::string & interface = "can0", const bool enable_fd = false);
explicit SocketCanReceiver(
const std::string & interface = "can0", const bool enable_fd = false,
const bool enable_loopback = false);
/// Destructor
~SocketCanReceiver() noexcept;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ class SOCKETCAN_PUBLIC SocketCanReceiverNode final
std::chrono::nanoseconds interval_ns_;
bool enable_fd_;
bool use_bus_time_;
bool enable_loopback_;
};
} // namespace socketcan
} // namespace drivers
Expand Down
2 changes: 2 additions & 0 deletions ros2_socketcan/launch/socket_can_bridge.launch.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
<arg name="receiver_interval_sec" default="0.01" />
<arg name="sender_timeout_sec" default="0.01" />
<arg name="enable_can_fd" default="false" />
<arg name="enable_frame_loopback" default="false" />
<arg name="from_can_bus_topic" default="from_can_bus" />
<arg name="to_can_bus_topic" default="to_can_bus" />
<arg name="use_bus_time" default="false" />
Expand All @@ -12,6 +13,7 @@
<arg name="interface" value="$(var interface)" />
<arg name="interval_sec" value="$(var receiver_interval_sec)" />
<arg name="enable_can_fd" value="$(var enable_can_fd)" />
<arg name="enable_frame_loopback" value="$(var enable_frame_loopback)" />
<arg name="from_can_bus_topic" value="$(var from_can_bus_topic)" />
<arg name="use_bus_time" value="$(var use_bus_time)" />
</include>
Expand Down
2 changes: 2 additions & 0 deletions ros2_socketcan/launch/socket_can_receiver.launch.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ def generate_launch_description():
parameters=[{
'interface': LaunchConfiguration('interface'),
'enable_can_fd': LaunchConfiguration('enable_can_fd'),
'enable_frame_loopback': LaunchConfiguration('enable_frame_loopback'),
'interval_sec':
LaunchConfiguration('interval_sec'),
'filters': LaunchConfiguration('filters'),
Expand Down Expand Up @@ -80,6 +81,7 @@ def generate_launch_description():
return LaunchDescription([
DeclareLaunchArgument('interface', default_value='can0'),
DeclareLaunchArgument('enable_can_fd', default_value='false'),
DeclareLaunchArgument('enable_frame_loopback', default_value='false'),
DeclareLaunchArgument('interval_sec', default_value='0.01'),
DeclareLaunchArgument('use_bus_time', default_value='false'),
DeclareLaunchArgument('filters', default_value='0:0',
Expand Down
6 changes: 4 additions & 2 deletions ros2_socketcan/src/socket_can_receiver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,10 @@ namespace socketcan
{

////////////////////////////////////////////////////////////////////////////////
SocketCanReceiver::SocketCanReceiver(const std::string & interface, const bool enable_fd)
: m_file_descriptor{bind_can_socket(interface, enable_fd)},
SocketCanReceiver::SocketCanReceiver(
const std::string & interface, const bool enable_fd,
const bool enable_loopback)
: m_file_descriptor{bind_can_socket(interface, enable_fd, enable_loopback)},
m_enable_fd(enable_fd)
{
}
Expand Down
6 changes: 5 additions & 1 deletion ros2_socketcan/src/socket_can_receiver_node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ SocketCanReceiverNode::SocketCanReceiverNode(rclcpp::NodeOptions options)
interface_ = this->declare_parameter("interface", "can0");
use_bus_time_ = this->declare_parameter<bool>("use_bus_time", false);
enable_fd_ = this->declare_parameter<bool>("enable_can_fd", false);
enable_loopback_ = this->declare_parameter<bool>("enable_frame_loopback", false);
double interval_sec = this->declare_parameter("interval_sec", 0.01);
this->declare_parameter("filters", "0:0");
interval_ns_ = std::chrono::duration_cast<std::chrono::nanoseconds>(
Expand All @@ -46,6 +47,9 @@ SocketCanReceiverNode::SocketCanReceiverNode(rclcpp::NodeOptions options)
RCLCPP_INFO(this->get_logger(), "interface: %s", interface_.c_str());
RCLCPP_INFO(this->get_logger(), "use bus time: %d", use_bus_time_);
RCLCPP_INFO(this->get_logger(), "can fd enabled: %s", enable_fd_ ? "true" : "false");
RCLCPP_INFO(
this->get_logger(), "frame loopback enabled: %s",
enable_loopback_ ? "true" : "false");
RCLCPP_INFO(this->get_logger(), "interval(s): %f", interval_sec);
}

Expand All @@ -54,7 +58,7 @@ LNI::CallbackReturn SocketCanReceiverNode::on_configure(const lc::State & state)
(void)state;

try {
receiver_ = std::make_unique<SocketCanReceiver>(interface_, enable_fd_);
receiver_ = std::make_unique<SocketCanReceiver>(interface_, enable_fd_, enable_loopback_);
// apply CAN filters
auto filters = get_parameter("filters").as_string();
receiver_->SetCanFilters(SocketCanReceiver::CanFilterList(filters));
Expand Down

0 comments on commit ef3e8ac

Please sign in to comment.