commit c1d7c55ee6d7e9b58af436444618aacde2e6c471 Author: Wessel Tip Date: Sun Sep 7 16:07:57 2025 +0200 feat(hw1): First iteration of clock homework diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..c32553f --- /dev/null +++ b/.gitignore @@ -0,0 +1,37 @@ + +# Created by https://www.toptal.com/developers/gitignore/api/ros2 +# Edit at https://www.toptal.com/developers/gitignore?templates=ros2 + +### ROS2 ### +install/ +log/ +build/ + +# Ignore generated docs +*.dox +*.wikidoc + +# eclipse stuff +.project +.cproject + +# qcreator stuff +CMakeLists.txt.user + +srv/_*.py +*.pcd +*.pyc +qtcreator-* +*.user + +*~ + +# Emacs +.#* + +# Colcon custom files +COLCON_IGNORE +AMENT_IGNORE + +# End of https://www.toptal.com/developers/gitignore/api/ros2 + diff --git a/src/hw_1_clock/CMakeLists.txt b/src/hw_1_clock/CMakeLists.txt new file mode 100644 index 0000000..cd2bf98 --- /dev/null +++ b/src/hw_1_clock/CMakeLists.txt @@ -0,0 +1,40 @@ +cmake_minimum_required(VERSION 3.8) +project(hw_1_clock) + +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) +# uncomment the following section in order to fill in +# further dependencies manually. +# find_package( REQUIRED) + +add_executable(1_clock_print src/1-clock-print.cpp) +add_executable(2_clock_steady_continuous src/2-clock-steady-continuous.cpp) + +ament_target_dependencies(1_clock_print rclcpp) +ament_target_dependencies(2_clock_steady_continuous rclcpp) + +install ( + TARGETS + 1_clock_print + 2_clock_steady_continuous + 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() diff --git a/src/hw_1_clock/package.xml b/src/hw_1_clock/package.xml new file mode 100644 index 0000000..d34eb92 --- /dev/null +++ b/src/hw_1_clock/package.xml @@ -0,0 +1,18 @@ + + + + hw_1_clock + 0.0.0 + TODO: Package description + wessel + TODO: License declaration + + ament_cmake + + ament_lint_auto + ament_lint_common + + + ament_cmake + + diff --git a/src/hw_1_clock/src/1-clock-print.cpp b/src/hw_1_clock/src/1-clock-print.cpp new file mode 100644 index 0000000..d1eb2ad --- /dev/null +++ b/src/hw_1_clock/src/1-clock-print.cpp @@ -0,0 +1,57 @@ +// Wessel T (https://wessel.gg/) +// +// "Build your own tower. +// A kingdom freed from malice. +// Create a world of bounty, peace and beauty." +// ⠀⣠⣶⣶⣤⣁ +// ⢰⣷⡟⠻⣏⠻⣧⣀⠐⠈⠀⡈⠠⠀⠂⠀⢁⠈⠀⠄⠀⠁⣠⣴⣤⡈ +// ⠀⣿⢿⡀⠘⢦⡈⢻⣦⠐⠀⠀⠄⠀⠁⠈⠀⡀⠠⠀⣠⡿⣿⣯⣽⡇ +// ⠀⢻⣿⠛⢦⣄⣹⠦⣌⣳⡀⠀⣠⠈⠀⢾⠀⠀⢀⣼⢯⠞⢡⣿⣿⠁ +// ⠄⠘⣿⣷⣤⡀⠙⣆⠈⠻⣿⡄⠘⣇⠀⣾⠀⣴⠿⢲⣋⣤⣿⡿⠃⠀ +// ⠀⡀⠹⣿⣦⡉⠛⠚⣆⠀⠈⠻⣆⢻⢠⣇⡾⠃⢠⣟⣠⣾⡞⠃⠀ +// ⠂⡀⠄⠹⣿⣏⠛⠒⠾⠷⣄⠀⠙⣞⣿⠋⣀⣴⣋⣽⡿⠋ +// ⠂⠠⠀⠀⣨⣿⢿⣶⣒⠲⢮⣿⣶⣼⣧⣾⣭⣿⠟⠉ +// ⠐⠀⠁⣰⣿⠓⠒⣛⣻⠟⠛⣩⣿⣯⠙⡯⣿⡆ +// ⠐⠀⠄⠸⣿⡟⢉⡽⢛⣿⡿⠉⠀⢸⣧⡷⣾⡇ +// ⠀⢂⠀⠄⠹⢿⣿⣴⣯⠏⠀⠀⠀⣼⢸⣽⣷⠇ +// ⠠⠀⠂⢀⠀⢀⠈⠉⠀⠀⠀⠂⡀⠹⠿⠛⠁⠀⠀ + +/* 1-clock-print.cpp + * A node that will wait for 20 seconds and 88778567 nanoseconds, after which it will print the + * current system time and exit. + * + * Reviewed by: + * Changelog: + * [04-09-2025] Wessel T: Implement template + * [07-09-2025] Wessel T: (BASE) Work out assignment + */ + +#include +#include +#include "rclcpp/rclcpp.hpp" + +using namespace std::chrono; + +class NodeHw1ClockWait : public rclcpp::Node { +public: + NodeHw1ClockWait() : Node("node_hw1_clock_wait") { + // Wait for 20 seconds and 88778567 nanoseconds + auto duration = seconds(20) + nanoseconds(88778567); + rclcpp::sleep_for(duration, this->get_node_options().context()); + + print_time(); + } + + void print_time() { + auto now = rclcpp::Clock().now(); + RCLCPP_INFO(this->get_logger(), "Current system time: %.0f", now.seconds()); + } +}; + +int main(int argc, char *argv[]) { + rclcpp::init(argc, argv); + // Node will start in constructor. Will exit after print is done + auto node = std::make_shared(); + rclcpp::shutdown(); + return 0; +} \ No newline at end of file diff --git a/src/hw_1_clock/src/2-clock-steady-continuous.cpp b/src/hw_1_clock/src/2-clock-steady-continuous.cpp new file mode 100644 index 0000000..d9ec07e --- /dev/null +++ b/src/hw_1_clock/src/2-clock-steady-continuous.cpp @@ -0,0 +1,75 @@ +// Wessel T (https://wessel.gg/) +// +// "Build your own tower. +// A kingdom freed from malice. +// Create a world of bounty, peace and beauty." +// ⠀⣠⣶⣶⣤⣁ +// ⢰⣷⡟⠻⣏⠻⣧⣀⠐⠈⠀⡈⠠⠀⠂⠀⢁⠈⠀⠄⠀⠁⣠⣴⣤⡈ +// ⠀⣿⢿⡀⠘⢦⡈⢻⣦⠐⠀⠀⠄⠀⠁⠈⠀⡀⠠⠀⣠⡿⣿⣯⣽⡇ +// ⠀⢻⣿⠛⢦⣄⣹⠦⣌⣳⡀⠀⣠⠈⠀⢾⠀⠀⢀⣼⢯⠞⢡⣿⣿⠁ +// ⠄⠘⣿⣷⣤⡀⠙⣆⠈⠻⣿⡄⠘⣇⠀⣾⠀⣴⠿⢲⣋⣤⣿⡿⠃⠀ +// ⠀⡀⠹⣿⣦⡉⠛⠚⣆⠀⠈⠻⣆⢻⢠⣇⡾⠃⢠⣟⣠⣾⡞⠃⠀ +// ⠂⡀⠄⠹⣿⣏⠛⠒⠾⠷⣄⠀⠙⣞⣿⠋⣀⣴⣋⣽⡿⠋ +// ⠂⠠⠀⠀⣨⣿⢿⣶⣒⠲⢮⣿⣶⣼⣧⣾⣭⣿⠟⠉ +// ⠐⠀⠁⣰⣿⠓⠒⣛⣻⠟⠛⣩⣿⣯⠙⡯⣿⡆ +// ⠐⠀⠄⠸⣿⡟⢉⡽⢛⣿⡿⠉⠀⢸⣧⡷⣾⡇ +// ⠀⢂⠀⠄⠹⢿⣿⣴⣯⠏⠀⠀⠀⣼⢸⣽⣷⠇ +// ⠠⠀⠂⢀⠀⢀⠈⠉⠀⠀⠀⠂⡀⠹⠿⠛⠁⠀⠀ + +/* 1-clock-print.cpp + * A node that will wait for 20 seconds and 88778567 nanoseconds, after which it will print the + * current system time and exit. + * + * Reviewed by: + * Changelog: + * [04-09-2025] Wessel T: Implement template + * [07-09-2025] Wessel T: (BASE) Work out assignment + */ + +#include +#include +#include "rclcpp/rclcpp.hpp" + +using namespace std::chrono; + +class NodeHw1AlternatingClock : public rclcpp::Node { +public: + NodeHw1AlternatingClock() + : rclcpp::Node("node_hw1_clock_alternate"), use_steady_(true) + { + timer_ = this->create_wall_timer( + seconds(5), + std::bind(&NodeHw1AlternatingClock::timer_callback, this) + ); + } + +private: + rclcpp::TimerBase::SharedPtr timer_; + bool use_steady_; + + void timer_callback() { + if (use_steady_) { + rclcpp::Clock steady_clock(RCL_STEADY_TIME); + auto now = steady_clock.now(); + + RCLCPP_INFO(this->get_logger(), "[STEADY] %.0f", now.seconds()); + } else { + rclcpp::Clock system_clock(RCL_SYSTEM_TIME); + auto now = system_clock.now(); + + RCLCPP_INFO(this->get_logger(), "[SYSTEM] %.0f", now.seconds()); + } + + use_steady_ = !use_steady_; + } +}; + +int main(int argc, char *argv[]) { + rclcpp::init(argc, argv); + + auto node = std::make_shared(); + rclcpp::spin(node); + rclcpp::shutdown(); + + return 0; +} \ No newline at end of file