feat(les1): Add publisher + subscriber

This commit is contained in:
2025-09-04 13:34:46 +02:00
parent eafa5e9d3b
commit a43632a329
3 changed files with 149 additions and 1 deletions

View File

@@ -14,13 +14,18 @@ find_package(std_msgs REQUIRED) # gebruik maken von standaard messages
add_executable(les1 src/les1.cpp)
add_executable(les1_publisher src/les1/publisher.cpp)
add_executable(les1_subscriber src/les1/subscriber.cpp)
ament_target_dependencies(les1 rclcpp)
#ament_target_dependencies(template_subscriber rclcpp std_msgs)
ament_target_dependencies(les1_publisher rclcpp std_msgs)
ament_target_dependencies(les1_subscriber rclcpp std_msgs)
install (
TARGETS
les1
les1_publisher
les1_subscriber
DESTINATION lib/${PROJECT_NAME}
)

View File

@@ -0,0 +1,73 @@
// Wessel T <contact@wessel.gg> (https://wessel.gg/)
//
// "Build your own tower.
// A kingdom freed from malice.
// Create a world of bounty, peace and beauty."
// ⠀⣠⣶⣶⣤⣁
// ⢰⣷⡟⠻⣏⠻⣧⣀⠐⠈⠀⡈⠠⠀⠂⠀⢁⠈⠀⠄⠀⠁⣠⣴⣤⡈
// ⠀⣿⢿⡀⠘⢦⡈⢻⣦⠐⠀⠀⠄⠀⠁⠈⠀⡀⠠⠀⣠⡿⣿⣯⣽⡇
// ⠀⢻⣿⠛⢦⣄⣹⠦⣌⣳⡀⠀⣠⠈⠀⢾⠀⠀⢀⣼⢯⠞⢡⣿⣿⠁
// ⠄⠘⣿⣷⣤⡀⠙⣆⠈⠻⣿⡄⠘⣇⠀⣾⠀⣴⠿⢲⣋⣤⣿⡿⠃⠀
// ⠀⡀⠹⣿⣦⡉⠛⠚⣆⠀⠈⠻⣆⢻⢠⣇⡾⠃⢠⣟⣠⣾⡞⠃⠀
// ⠂⡀⠄⠹⣿⣏⠛⠒⠾⠷⣄⠀⠙⣞⣿⠋⣀⣴⣋⣽⡿⠋
// ⠂⠠⠀⠀⣨⣿⢿⣶⣒⠲⢮⣿⣶⣼⣧⣾⣭⣿⠟⠉
// ⠐⠀⠁⣰⣿⠓⠒⣛⣻⠟⠛⣩⣿⣯⠙⡯⣿⡆
// ⠐⠀⠄⠸⣿⡟⢉⡽⢛⣿⡿⠉⠀⢸⣧⡷⣾⡇
// ⠀⢂⠀⠄⠹⢿⣿⣴⣯⠏⠀⠀⠀⣼⢸⣽⣷⠇
// ⠠⠀⠂⢀⠀⢀⠈⠉⠀⠀⠀⠂⡀⠹⠿⠛⠁⠀⠀
/* les1.cpp
* Assignment done in the first lesson explaining
* the basics of ROS2
*
* Reviewed by: <x>
* Changelog:
* [04-09-2025] Wessel T:
* - Implement template
*/
#include <cstdlib>
#include "rclcpp/rclcpp.hpp"
#include <rclcpp/clock.hpp>
#include <rclcpp/time.hpp>
#include "std_msgs/msg/int32.hpp" //header for the standard message that is used for communication
class NodeLes1 : public rclcpp::Node
{
public:
NodeLes1()
: Node("les1_publisher_node")
{
publisher_theanswer_ =
this->create_publisher<std_msgs::msg::Int32>("the_answer", 10);
timer_theanswer_ = this->create_wall_timer(
std::chrono::milliseconds(1000),
std::bind(&NodeLes1::timer_theanswer_function, this)
);
}
void timer_theanswer_function() {
message_.data = 42 ;
publisher_theanswer_ ->publish(message_) ;
}
private:
rclcpp::Publisher<std_msgs::msg::Int32>::SharedPtr publisher_theanswer_;
rclcpp::TimerBase::SharedPtr timer_theanswer_ ;
std_msgs::msg::Int32 message_ ;
};
int main(int argc,char *argv[]) {
rclcpp::init(argc,argv);
auto node = std::make_shared<NodeLes1>();
rclcpp::spin(node);
rclcpp::shutdown();
return 0;
}

View File

@@ -0,0 +1,70 @@
// Wessel T <contact@wessel.gg> (https://wessel.gg/)
//
// "Build your own tower.
// A kingdom freed from malice.
// Create a world of bounty, peace and beauty."
// ⠀⣠⣶⣶⣤⣁
// ⢰⣷⡟⠻⣏⠻⣧⣀⠐⠈⠀⡈⠠⠀⠂⠀⢁⠈⠀⠄⠀⠁⣠⣴⣤⡈
// ⠀⣿⢿⡀⠘⢦⡈⢻⣦⠐⠀⠀⠄⠀⠁⠈⠀⡀⠠⠀⣠⡿⣿⣯⣽⡇
// ⠀⢻⣿⠛⢦⣄⣹⠦⣌⣳⡀⠀⣠⠈⠀⢾⠀⠀⢀⣼⢯⠞⢡⣿⣿⠁
// ⠄⠘⣿⣷⣤⡀⠙⣆⠈⠻⣿⡄⠘⣇⠀⣾⠀⣴⠿⢲⣋⣤⣿⡿⠃⠀
// ⠀⡀⠹⣿⣦⡉⠛⠚⣆⠀⠈⠻⣆⢻⢠⣇⡾⠃⢠⣟⣠⣾⡞⠃⠀
// ⠂⡀⠄⠹⣿⣏⠛⠒⠾⠷⣄⠀⠙⣞⣿⠋⣀⣴⣋⣽⡿⠋
// ⠂⠠⠀⠀⣨⣿⢿⣶⣒⠲⢮⣿⣶⣼⣧⣾⣭⣿⠟⠉
// ⠐⠀⠁⣰⣿⠓⠒⣛⣻⠟⠛⣩⣿⣯⠙⡯⣿⡆
// ⠐⠀⠄⠸⣿⡟⢉⡽⢛⣿⡿⠉⠀⢸⣧⡷⣾⡇
// ⠀⢂⠀⠄⠹⢿⣿⣴⣯⠏⠀⠀⠀⣼⢸⣽⣷⠇
// ⠠⠀⠂⢀⠀⢀⠈⠉⠀⠀⠀⠂⡀⠹⠿⠛⠁⠀⠀
/* les1.cpp
* Assignment done in the first lesson explaining
* the basics of ROS2
*
* Reviewed by: <x>
* Changelog:
* [04-09-2025] Wessel T:
* - Implement template
*/
#include <cstdlib>
#include "rclcpp/rclcpp.hpp"
#include "std_msgs/msg/int32.hpp" //header for the standard message that is used for communication
using namespace std::placeholders;
class NodeLes1 : public rclcpp::Node {
public:
NodeLes1()
: Node("les1_subscriber_node")
{
subscriber_theanswer_ =
this->create_subscription<std_msgs::msg::Int32>(
"the_answer", 10,
std::bind(&NodeLes1::subscriber_theanswer_callback, this, _1)
);
}
void subscriber_theanswer_callback(const std_msgs::msg::Int32::SharedPtr msg) {
answer_to_all_ = msg->data;
RCLCPP_INFO(
this->get_logger(), "Hello subscriber the answer is: %d",answer_to_all_
);
}
private:
rclcpp::Subscription<std_msgs::msg::Int32>::SharedPtr subscriber_theanswer_;
int answer_to_all_ = 0;
};
int main(int argc,char *argv[]) {
rclcpp::init(argc,argv);
auto node = std::make_shared<NodeLes1>();
rclcpp::spin(node);
rclcpp::shutdown();
return 0;
}