feat(hw1): First iteration of clock homework

This commit is contained in:
2025-09-07 16:07:57 +02:00
commit c1d7c55ee6
5 changed files with 227 additions and 0 deletions

37
.gitignore vendored Normal file
View File

@@ -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

View File

@@ -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(<dependency> 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()

View 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>hw_1_clock</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>

View File

@@ -0,0 +1,57 @@
// 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."
// ⠀⣠⣶⣶⣤⣁
// ⢰⣷⡟⠻⣏⠻⣧⣀⠐⠈⠀⡈⠠⠀⠂⠀⢁⠈⠀⠄⠀⠁⣠⣴⣤⡈
// ⠀⣿⢿⡀⠘⢦⡈⢻⣦⠐⠀⠀⠄⠀⠁⠈⠀⡀⠠⠀⣠⡿⣿⣯⣽⡇
// ⠀⢻⣿⠛⢦⣄⣹⠦⣌⣳⡀⠀⣠⠈⠀⢾⠀⠀⢀⣼⢯⠞⢡⣿⣿⠁
// ⠄⠘⣿⣷⣤⡀⠙⣆⠈⠻⣿⡄⠘⣇⠀⣾⠀⣴⠿⢲⣋⣤⣿⡿⠃⠀
// ⠀⡀⠹⣿⣦⡉⠛⠚⣆⠀⠈⠻⣆⢻⢠⣇⡾⠃⢠⣟⣠⣾⡞⠃⠀
// ⠂⡀⠄⠹⣿⣏⠛⠒⠾⠷⣄⠀⠙⣞⣿⠋⣀⣴⣋⣽⡿⠋
// ⠂⠠⠀⠀⣨⣿⢿⣶⣒⠲⢮⣿⣶⣼⣧⣾⣭⣿⠟⠉
// ⠐⠀⠁⣰⣿⠓⠒⣛⣻⠟⠛⣩⣿⣯⠙⡯⣿⡆
// ⠐⠀⠄⠸⣿⡟⢉⡽⢛⣿⡿⠉⠀⢸⣧⡷⣾⡇
// ⠀⢂⠀⠄⠹⢿⣿⣴⣯⠏⠀⠀⠀⣼⢸⣽⣷⠇
// ⠠⠀⠂⢀⠀⢀⠈⠉⠀⠀⠀⠂⡀⠹⠿⠛⠁⠀⠀
/* 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: <x>
* Changelog:
* [04-09-2025] Wessel T: Implement template
* [07-09-2025] Wessel T: (BASE) Work out assignment
*/
#include <cstdlib>
#include <chrono>
#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<NodeHw1ClockWait>();
rclcpp::shutdown();
return 0;
}

View File

@@ -0,0 +1,75 @@
// 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."
// ⠀⣠⣶⣶⣤⣁
// ⢰⣷⡟⠻⣏⠻⣧⣀⠐⠈⠀⡈⠠⠀⠂⠀⢁⠈⠀⠄⠀⠁⣠⣴⣤⡈
// ⠀⣿⢿⡀⠘⢦⡈⢻⣦⠐⠀⠀⠄⠀⠁⠈⠀⡀⠠⠀⣠⡿⣿⣯⣽⡇
// ⠀⢻⣿⠛⢦⣄⣹⠦⣌⣳⡀⠀⣠⠈⠀⢾⠀⠀⢀⣼⢯⠞⢡⣿⣿⠁
// ⠄⠘⣿⣷⣤⡀⠙⣆⠈⠻⣿⡄⠘⣇⠀⣾⠀⣴⠿⢲⣋⣤⣿⡿⠃⠀
// ⠀⡀⠹⣿⣦⡉⠛⠚⣆⠀⠈⠻⣆⢻⢠⣇⡾⠃⢠⣟⣠⣾⡞⠃⠀
// ⠂⡀⠄⠹⣿⣏⠛⠒⠾⠷⣄⠀⠙⣞⣿⠋⣀⣴⣋⣽⡿⠋
// ⠂⠠⠀⠀⣨⣿⢿⣶⣒⠲⢮⣿⣶⣼⣧⣾⣭⣿⠟⠉
// ⠐⠀⠁⣰⣿⠓⠒⣛⣻⠟⠛⣩⣿⣯⠙⡯⣿⡆
// ⠐⠀⠄⠸⣿⡟⢉⡽⢛⣿⡿⠉⠀⢸⣧⡷⣾⡇
// ⠀⢂⠀⠄⠹⢿⣿⣴⣯⠏⠀⠀⠀⣼⢸⣽⣷⠇
// ⠠⠀⠂⢀⠀⢀⠈⠉⠀⠀⠀⠂⡀⠹⠿⠛⠁⠀⠀
/* 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: <x>
* Changelog:
* [04-09-2025] Wessel T: Implement template
* [07-09-2025] Wessel T: (BASE) Work out assignment
*/
#include <cstdlib>
#include <chrono>
#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<NodeHw1AlternatingClock>();
rclcpp::spin(node);
rclcpp::shutdown();
return 0;
}