Skip to content
This repository has been archived by the owner on Mar 23, 2024. It is now read-only.

Commit

Permalink
Update example
Browse files Browse the repository at this point in the history
  • Loading branch information
hhenry01 committed Dec 29, 2023
1 parent bc33acd commit 4a2ce3e
Show file tree
Hide file tree
Showing 9 changed files with 97 additions and 53 deletions.
26 changes: 26 additions & 0 deletions config/all_disable.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Configuration that disables everything
# Allows us to overload the config:=... arguments such that we only launch certain nodes (useful for integration testing)
network_systems:
can_transceiver_node:
ros__parameters:
enabled: false

can_transceiver_sim_node:
ros__parameters:
enabled: false

cached_fib_subscriber:
ros__parameters:
enabled: false

local_transceiver_node:
ros__parameters:
enabled: false

mock_ais_node:
ros__parameters:
enabled: false

remote_transceiver_node:
ros__parameters:
enabled: false
2 changes: 1 addition & 1 deletion config/default_prod_en.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ network_systems:
ros__parameters:
enabled: false

cached_fib_subscriber:
cached_fib_node:
ros__parameters:
enabled: false

Expand Down
2 changes: 1 addition & 1 deletion config/example/example_en.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Configuration that enables the cached_fib example module
network_systems:
cached_fib_subscriber:
cached_fib_node:
ros__parameters:
enabled: true
2 changes: 1 addition & 1 deletion launch/main_launch.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ def get_cached_fib_description(context: LaunchContext) -> Node:
Returns:
Node: The node object that launches the cached_fib node.
"""
node_name = "cached_fib_subscriber"
node_name = "cached_fib_node"
ros_parameters = [
global_launch_config,
{"mode": LaunchConfiguration("mode")},
Expand Down
2 changes: 1 addition & 1 deletion projects/example/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ make_lib(${module} "${srcs}" "${link_libs}" "${inc_dirs}" "${compile_defs}")
# Create module ROS executable
set(bin_srcs
${srcs}
${CMAKE_CURRENT_LIST_DIR}/src/cached_fib_subscriber.cpp
${CMAKE_CURRENT_LIST_DIR}/src/cached_fib_ros_intf.cpp
)
make_exe(${module} "${bin_srcs}" "${link_libs}" "${inc_dirs}" "${compile_defs}")

Expand Down
12 changes: 8 additions & 4 deletions projects/example/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,12 @@

This is a simple example program to showcase and evaluate the workspace structure for Network Systems.

Run it with `ros2 run network_systems example`, and publish to it using
`ros2 topic pub /cached_fib std_msgs/msg/UInt64 "{data: <some number>}" --once`.
Run it with `ros2 run network_systems example --ros-args -p enabled:=true`.

Alternatively, using launch files run `ros2 launch network_systems example`, and publish to it using
`ros2 topic pub /example/cached_fib std_msgs/msg/UInt64 "{data: <some number>}" --once`.
Alternatively, using launch files run `ros2 launch network_systems main_launch.py config:=example/example_en.yaml`.

Publish to the topic using: `ros2 topic pub cached_fib_in std_msgs/msg/UInt64 "{data: <some number>}" --once`. Run it
without the `--once` flag to repeatedly publish.

The output will be shown in the terminal where the example is running. You can also see the output by running
`ros2 topic echo cached_fib_out` before sending the publish command.
3 changes: 2 additions & 1 deletion projects/example/inc/cached_fib.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
#include <string>
#include <vector>

constexpr auto CACHED_FIB_TOPIC = "cached_fib";
constexpr auto CACHED_FIB_TOPIC_IN = "cached_fib_in";
constexpr auto CACHED_FIB_TOPIC_OUT = "cached_fib_out";

class CachedFib
{
Expand Down
57 changes: 57 additions & 0 deletions projects/example/src/cached_fib_ros_intf.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// Include this module
#include "cached_fib.h"
// Include ROS headers
#include <rclcpp/rclcpp.hpp>
#include <std_msgs/msg/u_int64.hpp>

namespace
{
constexpr int ROS_Q_SIZE = 10;
constexpr int INIT_FIB_SIZE = 5;
} // namespace

class CachedFibNode : public rclcpp::Node
{
public:
explicit CachedFibNode(const std::size_t initSize) : Node("cached_fib_node"), c_fib_(initSize)
{
this->declare_parameter("enabled", false);
bool enabled = this->get_parameter("enabled").as_bool();
if (enabled) {
RCLCPP_INFO(this->get_logger(), "Running example cached fib node");

// This registers a callback function that gets called whenever CACHED_FIB_TOPIC_IN gets updated
sub_ = this->create_subscription<std_msgs::msg::UInt64>(
CACHED_FIB_TOPIC_IN, ROS_Q_SIZE, std::bind(&CachedFibNode::subCb, this, std::placeholders::_1));

pub_ = this->create_publisher<std_msgs::msg::UInt64>(CACHED_FIB_TOPIC_OUT, ROS_Q_SIZE);
}
}

private:
CachedFib c_fib_;
rclcpp::Subscription<std_msgs::msg::UInt64>::SharedPtr sub_;
rclcpp::Publisher<std_msgs::msg::UInt64>::SharedPtr pub_;

/**
* @brief Callback function that runs whenever the subscribed topic is updated
*
* @param in_msg msg that was sent to the topic
*/
void subCb(const std_msgs::msg::UInt64::SharedPtr in_msg)
{
int fibNum = this->c_fib_.getFib(in_msg->data);
RCLCPP_INFO(this->get_logger(), "Fib num for '%lu' is '%d'", in_msg->data, fibNum);
std_msgs::msg::UInt64 out_msg{};
out_msg.data = fibNum;
pub_->publish(out_msg);
}
};

int main(int argc, char * argv[])
{
rclcpp::init(argc, argv);
rclcpp::spin(std::make_shared<CachedFibNode>(INIT_FIB_SIZE));
rclcpp::shutdown();
return 0;
}
44 changes: 0 additions & 44 deletions projects/example/src/cached_fib_subscriber.cpp

This file was deleted.

0 comments on commit 4a2ce3e

Please sign in to comment.