From 4d769918356cc39826a2bce433a10cfa4cd342e8 Mon Sep 17 00:00:00 2001 From: Baptiste LEROY Date: Tue, 18 Nov 2025 15:08:21 +0100 Subject: [PATCH] Midterm --- CMakeLists.txt | 44 ++++++++++++++++++++++++++++++++++++++++++++ cmd_pub.cpp | 39 +++++++++++++++++++++++++++++++++++++++ imu_sub.cpp | 32 ++++++++++++++++++++++++++++++++ package.xml | 23 +++++++++++++++++++++++ 4 files changed, 138 insertions(+) create mode 100644 CMakeLists.txt create mode 100644 cmd_pub.cpp create mode 100644 imu_sub.cpp create mode 100644 package.xml diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..749d913 --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,44 @@ +cmake_minimum_required(VERSION 3.8) +project(ros_midterm) + +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) +find_package(sensor_msgs REQUIRED) +find_package(geometry_msgs REQUIRED) + +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() + + +add_executable(imu_sub src/imu_sub.cpp) +ament_target_dependencies(imu_sub rclcpp sensor_msgs) + +add_executable(cmd_pub src/cmd_pub.cpp) +ament_target_dependencies(cmd_pub rclcpp geometry_msgs) + + +install(TARGETS + imu_sub + cmd_pub + DESTINATION lib/${PROJECT_NAME} +) + +ament_package() + + + diff --git a/cmd_pub.cpp b/cmd_pub.cpp new file mode 100644 index 0000000..2d87017 --- /dev/null +++ b/cmd_pub.cpp @@ -0,0 +1,39 @@ +#include "rclcpp/rclcpp.hpp" +#include "geometry_msgs/msg/twist.hpp" +#include +#include + +class CmdPublisher : public rclcpp::Node +{ +public: + CmdPublisher() : Node("cmd_pub") + { + publisher_ = this->create_publisher("/cmd_vel", 10); + timer_ = this->create_wall_timer( + std::chrono::seconds(1), + std::bind(&CmdPublisher::timer_callback, this) + ); + std::srand(std::time(nullptr)); // initialisation du générateur aléatoire + } + +private: + void timer_callback() + { + geometry_msgs::msg::Twist msg; + msg.angular.z = ((double)std::rand() / RAND_MAX) * 2.0 - 1.0; // entre -1 et 1 + publisher_->publish(msg); + RCLCPP_INFO(this->get_logger(), "Published angular velocity around z: %.3f rad/s", msg.angular.z); + } + + rclcpp::Publisher::SharedPtr publisher_; + rclcpp::TimerBase::SharedPtr timer_; +}; + +int main(int argc, char *argv[]) +{ + rclcpp::init(argc, argv); + rclcpp::spin(std::make_shared()); + rclcpp::shutdown(); + return 0; +} + diff --git a/imu_sub.cpp b/imu_sub.cpp new file mode 100644 index 0000000..8c97e5f --- /dev/null +++ b/imu_sub.cpp @@ -0,0 +1,32 @@ +#include "rclcpp/rclcpp.hpp" +#include "sensor_msgs/msg/imu.hpp" + +class IMUSubscriber : public rclcpp::Node +{ +public: + IMUSubscriber() : Node("imu_sub") + { + subscription_ = this->create_subscription( + "/imu", + 10, + std::bind(&IMUSubscriber::imu_callback, this, std::placeholders::_1) + ); + } + +private: + void imu_callback(const sensor_msgs::msg::Imu::SharedPtr msg) + { + RCLCPP_INFO(this->get_logger(), "Angular velocity around z: %.3f rad/s", msg->angular_velocity.z); + } + + rclcpp::Subscription::SharedPtr subscription_; +}; + +int main(int argc, char *argv[]) +{ + rclcpp::init(argc, argv); + rclcpp::spin(std::make_shared()); + rclcpp::shutdown(); + return 0; +} + diff --git a/package.xml b/package.xml new file mode 100644 index 0000000..5301a18 --- /dev/null +++ b/package.xml @@ -0,0 +1,23 @@ + + + + ros_midterm + 0.0.0 + TODO: Package description + ros + TODO: License declaration + + ament_cmake + + rclcpp + std_msgs + sensor_msgs + geometry_msgs + + ament_lint_auto + ament_lint_common + + + ament_cmake + +