feat(hw1): First iteration of pubsub random number
This commit is contained in:
41
src/hw1_pubsub/CMakeLists.txt
Normal file
41
src/hw1_pubsub/CMakeLists.txt
Normal file
@@ -0,0 +1,41 @@
|
|||||||
|
cmake_minimum_required(VERSION 3.8)
|
||||||
|
project(hw1_pubsub)
|
||||||
|
|
||||||
|
if(CMAKE_COMPILER_IS_GNUCXX OR CMAKE_CXX_COMPILER_ID MATCHES "Clang")
|
||||||
|
add_compile_options(-Wall -Wextra -Wpedantic)
|
||||||
|
endif()
|
||||||
|
|
||||||
|
# find dependencies
|
||||||
|
find_package(ament_cmake REQUIRED)
|
||||||
|
find_package(rclcpp REQUIRED)
|
||||||
|
find_package(std_msgs REQUIRED)
|
||||||
|
# uncomment the following section in order to fill in
|
||||||
|
# further dependencies manually.
|
||||||
|
# find_package(<dependency> REQUIRED)
|
||||||
|
|
||||||
|
add_executable(1_pub src/1-publisher.cpp)
|
||||||
|
add_executable(2_sub src/2-subscriber.cpp)
|
||||||
|
|
||||||
|
ament_target_dependencies(1_pub rclcpp std_msgs)
|
||||||
|
ament_target_dependencies(2_sub rclcpp std_msgs)
|
||||||
|
|
||||||
|
install (
|
||||||
|
TARGETS
|
||||||
|
1_pub
|
||||||
|
2_sub
|
||||||
|
DESTINATION lib/${PROJECT_NAME}
|
||||||
|
)
|
||||||
|
|
||||||
|
if(BUILD_TESTING)
|
||||||
|
find_package(ament_lint_auto REQUIRED)
|
||||||
|
# the following line skips the linter which checks for copyrights
|
||||||
|
# comment the line when a copyright and license is added to all source files
|
||||||
|
set(ament_cmake_copyright_FOUND TRUE)
|
||||||
|
# the following line skips cpplint (only works in a git repo)
|
||||||
|
# comment the line when this package is in a git repo and when
|
||||||
|
# a copyright and license is added to all source files
|
||||||
|
set(ament_cmake_cpplint_FOUND TRUE)
|
||||||
|
ament_lint_auto_find_test_dependencies()
|
||||||
|
endif()
|
||||||
|
|
||||||
|
ament_package()
|
||||||
18
src/hw1_pubsub/package.xml
Normal file
18
src/hw1_pubsub/package.xml
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
<?xml version="1.0"?>
|
||||||
|
<?xml-model href="http://download.ros.org/schema/package_format3.xsd" schematypens="http://www.w3.org/2001/XMLSchema"?>
|
||||||
|
<package format="3">
|
||||||
|
<name>hw1_pubsub</name>
|
||||||
|
<version>0.0.0</version>
|
||||||
|
<description>TODO: Package description</description>
|
||||||
|
<maintainer email="wessel@todo.todo">wessel</maintainer>
|
||||||
|
<license>TODO: License declaration</license>
|
||||||
|
|
||||||
|
<buildtool_depend>ament_cmake</buildtool_depend>
|
||||||
|
|
||||||
|
<test_depend>ament_lint_auto</test_depend>
|
||||||
|
<test_depend>ament_lint_common</test_depend>
|
||||||
|
|
||||||
|
<export>
|
||||||
|
<build_type>ament_cmake</build_type>
|
||||||
|
</export>
|
||||||
|
</package>
|
||||||
51
src/hw1_pubsub/src/1-publisher.cpp
Normal file
51
src/hw1_pubsub/src/1-publisher.cpp
Normal file
@@ -0,0 +1,51 @@
|
|||||||
|
/* hw1_pubsub/1-publisher.cpp
|
||||||
|
* Node that publishes a random integer every 3 seconds.
|
||||||
|
*
|
||||||
|
* Reviewed by: <x>
|
||||||
|
* Changelog:
|
||||||
|
* [04-09-2025] Wessel T: Implement template
|
||||||
|
* [09-09-2025] Wessel T: (BASE) Work out assignment
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <cstdlib>
|
||||||
|
|
||||||
|
#include "rclcpp/rclcpp.hpp"
|
||||||
|
#include "std_msgs/msg/int32.hpp" // For random integer publisher
|
||||||
|
|
||||||
|
class NodeHw1RandomPublisher : public rclcpp::Node {
|
||||||
|
|
||||||
|
public:
|
||||||
|
NodeHw1RandomPublisher()
|
||||||
|
: Node("node_hw1_random_publisher")
|
||||||
|
{
|
||||||
|
publisher_random_number_ =
|
||||||
|
this->create_publisher<std_msgs::msg::Int32>("random_number", 10);
|
||||||
|
|
||||||
|
timer_random_number_ = this->create_wall_timer(
|
||||||
|
std::chrono::seconds(3),
|
||||||
|
std::bind(&NodeHw1RandomPublisher::timer_random_number_function, this)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
void timer_random_number_function() {
|
||||||
|
std_msgs::msg::Int32 msg;
|
||||||
|
msg.data = std::rand() % 101;
|
||||||
|
publisher_random_number_->publish(msg);
|
||||||
|
|
||||||
|
RCLCPP_INFO(this->get_logger(), "Published random number: %d", msg.data);
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
rclcpp::Publisher<std_msgs::msg::Int32>::SharedPtr publisher_random_number_;
|
||||||
|
rclcpp::TimerBase::SharedPtr timer_random_number_;
|
||||||
|
};
|
||||||
|
|
||||||
|
int main(int argc,char *argv[]) {
|
||||||
|
rclcpp::init(argc,argv);
|
||||||
|
|
||||||
|
auto node = std::make_shared<NodeHw1RandomPublisher>();
|
||||||
|
rclcpp::spin(node);
|
||||||
|
rclcpp::shutdown();
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
51
src/hw1_pubsub/src/2-subscriber.cpp
Normal file
51
src/hw1_pubsub/src/2-subscriber.cpp
Normal file
@@ -0,0 +1,51 @@
|
|||||||
|
|
||||||
|
/* hw1_pubsub/2-subscriber.cpp
|
||||||
|
* Node that subscribes and expect a random integer. Adds 1000 to it.
|
||||||
|
*
|
||||||
|
* Reviewed by: <x>
|
||||||
|
* Changelog:
|
||||||
|
* [04-09-2025] Wessel T: Implement template
|
||||||
|
* [09-09-2025] Wessel T: (BASE) Work out assignment
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <cstdlib>
|
||||||
|
|
||||||
|
#include "rclcpp/rclcpp.hpp"
|
||||||
|
#include "std_msgs/msg/int32.hpp" // For random integer publisher
|
||||||
|
|
||||||
|
using namespace std::placeholders;
|
||||||
|
|
||||||
|
class NodeHw1RandomSubscriber : public rclcpp::Node {
|
||||||
|
|
||||||
|
public:
|
||||||
|
NodeHw1RandomSubscriber()
|
||||||
|
: Node("node_hw1_random_subscriber")
|
||||||
|
{
|
||||||
|
subscriber_random_number_ =
|
||||||
|
this->create_subscription<std_msgs::msg::Int32>(
|
||||||
|
"random_number", 10,
|
||||||
|
std::bind(&NodeHw1RandomSubscriber::callback_random_number, this, _1)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
void callback_random_number(const std_msgs::msg::Int32::SharedPtr msg) {
|
||||||
|
int result = msg->data + 1000;
|
||||||
|
|
||||||
|
RCLCPP_INFO(this->get_logger(),
|
||||||
|
"Received: %d, after adding 1000: %d", msg->data, result
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
rclcpp::Subscription<std_msgs::msg::Int32>::SharedPtr subscriber_random_number_;
|
||||||
|
};
|
||||||
|
|
||||||
|
int main(int argc,char *argv[]) {
|
||||||
|
rclcpp::init(argc,argv);
|
||||||
|
|
||||||
|
auto node = std::make_shared<NodeHw1RandomSubscriber>();
|
||||||
|
rclcpp::spin(node);
|
||||||
|
rclcpp::shutdown();
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user