feat(hw1): First iteration of custom interface, rename clock
- Rename `hw1-clock` -> `hw1_clock`
This commit is contained in:
@@ -1,5 +1,5 @@
|
|||||||
cmake_minimum_required(VERSION 3.8)
|
cmake_minimum_required(VERSION 3.8)
|
||||||
project(hw_1_clock)
|
project(hw1-clock)
|
||||||
|
|
||||||
if(CMAKE_COMPILER_IS_GNUCXX OR CMAKE_CXX_COMPILER_ID MATCHES "Clang")
|
if(CMAKE_COMPILER_IS_GNUCXX OR CMAKE_CXX_COMPILER_ID MATCHES "Clang")
|
||||||
add_compile_options(-Wall -Wextra -Wpedantic)
|
add_compile_options(-Wall -Wextra -Wpedantic)
|
||||||
@@ -1,7 +1,7 @@
|
|||||||
<?xml version="1.0"?>
|
<?xml version="1.0"?>
|
||||||
<?xml-model href="http://download.ros.org/schema/package_format3.xsd" schematypens="http://www.w3.org/2001/XMLSchema"?>
|
<?xml-model href="http://download.ros.org/schema/package_format3.xsd" schematypens="http://www.w3.org/2001/XMLSchema"?>
|
||||||
<package format="3">
|
<package format="3">
|
||||||
<name>hw_1_clock</name>
|
<name>hw1-clock</name>
|
||||||
<version>0.0.0</version>
|
<version>0.0.0</version>
|
||||||
<description>TODO: Package description</description>
|
<description>TODO: Package description</description>
|
||||||
<maintainer email="wessel@todo.todo">wessel</maintainer>
|
<maintainer email="wessel@todo.todo">wessel</maintainer>
|
||||||
18
src/hw1_interface/CMakeLists.txt
Normal file
18
src/hw1_interface/CMakeLists.txt
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
cmake_minimum_required(VERSION 3.8)
|
||||||
|
project(hw1_interface)
|
||||||
|
|
||||||
|
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(rosidl_default_generators REQUIRED)
|
||||||
|
|
||||||
|
rosidl_generate_interfaces(${PROJECT_NAME}
|
||||||
|
"msg/StudentResult.msg"
|
||||||
|
)
|
||||||
|
|
||||||
|
ament_export_dependencies(rosidl_default_runtime)
|
||||||
|
|
||||||
|
ament_package()
|
||||||
2
src/hw1_interface/msg/StudentResult.msg
Normal file
2
src/hw1_interface/msg/StudentResult.msg
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
string student
|
||||||
|
float64 result
|
||||||
22
src/hw1_interface/package.xml
Normal file
22
src/hw1_interface/package.xml
Normal file
@@ -0,0 +1,22 @@
|
|||||||
|
<?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_interface</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>
|
||||||
|
|
||||||
|
<build_depend>rosidl_default_generators</build_depend>
|
||||||
|
<exec_depend>rosidl_default_runtime</exec_depend>
|
||||||
|
<member_of_group>rosidl_interface_packages</member_of_group>
|
||||||
|
|
||||||
|
<test_depend>ament_lint_auto</test_depend>
|
||||||
|
<test_depend>ament_lint_common</test_depend>
|
||||||
|
|
||||||
|
<export>
|
||||||
|
<build_type>ament_cmake</build_type>
|
||||||
|
</export>
|
||||||
|
</package>
|
||||||
41
src/hw1_interface_usage/CMakeLists.txt
Normal file
41
src/hw1_interface_usage/CMakeLists.txt
Normal file
@@ -0,0 +1,41 @@
|
|||||||
|
cmake_minimum_required(VERSION 3.8)
|
||||||
|
project(hw1_interface_usage)
|
||||||
|
|
||||||
|
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(hw1_interface 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 hw1_interface)
|
||||||
|
ament_target_dependencies(2_sub rclcpp hw1_interface)
|
||||||
|
|
||||||
|
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()
|
||||||
20
src/hw1_interface_usage/package.xml
Normal file
20
src/hw1_interface_usage/package.xml
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
<?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_interface_usage</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>
|
||||||
|
|
||||||
|
<depend>hw1_interface</depend>
|
||||||
|
|
||||||
|
<test_depend>ament_lint_auto</test_depend>
|
||||||
|
<test_depend>ament_lint_common</test_depend>
|
||||||
|
|
||||||
|
<export>
|
||||||
|
<build_type>ament_cmake</build_type>
|
||||||
|
</export>
|
||||||
|
</package>
|
||||||
54
src/hw1_interface_usage/src/1-publisher.cpp
Normal file
54
src/hw1_interface_usage/src/1-publisher.cpp
Normal file
@@ -0,0 +1,54 @@
|
|||||||
|
/* hw1_interface_usage/1-publisher.cpp.cpp
|
||||||
|
* A node that will publish a custom static message
|
||||||
|
* 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 "hw1_interface/msg/student_result.hpp"
|
||||||
|
|
||||||
|
class NodeHw1StudentPublisher : public rclcpp::Node {
|
||||||
|
public:
|
||||||
|
NodeHw1StudentPublisher()
|
||||||
|
: Node("node_hw1_student_publisher")
|
||||||
|
{
|
||||||
|
publisher_student_result_ =
|
||||||
|
this->create_publisher<hw1_interface::msg::StudentResult>("student_result", 10);
|
||||||
|
|
||||||
|
timer_student_result_ = this->create_wall_timer(
|
||||||
|
std::chrono::seconds(3),
|
||||||
|
std::bind(&NodeHw1StudentPublisher::timer_student_result_function, this)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
void timer_student_result_function() {
|
||||||
|
hw1_interface::msg::StudentResult msg;
|
||||||
|
msg.student = "Wessel T";
|
||||||
|
msg.result = 7.0;
|
||||||
|
|
||||||
|
publisher_student_result_->publish(msg);
|
||||||
|
RCLCPP_INFO(this->get_logger(),
|
||||||
|
"Published: student=%s, result=%f", msg.student.c_str(), msg.result
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
rclcpp::Publisher<hw1_interface::msg::StudentResult>::SharedPtr publisher_student_result_;
|
||||||
|
rclcpp::TimerBase::SharedPtr timer_student_result_;
|
||||||
|
};
|
||||||
|
|
||||||
|
int main(int argc, char *argv[]) {
|
||||||
|
rclcpp::init(argc, argv);
|
||||||
|
|
||||||
|
auto node = std::make_shared<NodeHw1StudentPublisher>();
|
||||||
|
rclcpp::spin(node);
|
||||||
|
rclcpp::shutdown();
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
48
src/hw1_interface_usage/src/2-subscriber.cpp
Normal file
48
src/hw1_interface_usage/src/2-subscriber.cpp
Normal file
@@ -0,0 +1,48 @@
|
|||||||
|
/* hw1_interface_usage/2-subscriber.cpp
|
||||||
|
* A node that will subscribe to a custom static message
|
||||||
|
*
|
||||||
|
* 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 "hw1_interface/msg/student_result.hpp"
|
||||||
|
|
||||||
|
using namespace std::placeholders;
|
||||||
|
|
||||||
|
class NodeHw1StudentSubscriber : public rclcpp::Node {
|
||||||
|
public:
|
||||||
|
NodeHw1StudentSubscriber()
|
||||||
|
: Node("node_hw1_student_subscriber")
|
||||||
|
{
|
||||||
|
subscriber_student_result_ =
|
||||||
|
this->create_subscription<hw1_interface::msg::StudentResult>(
|
||||||
|
"student_result", 10,
|
||||||
|
std::bind(&NodeHw1StudentSubscriber::callback_student_result, this, _1)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
void callback_student_result(const hw1_interface::msg::StudentResult::SharedPtr msg) {
|
||||||
|
RCLCPP_INFO(this->get_logger(),
|
||||||
|
"Received: student=%s, result=%f", msg->student.c_str(), msg->result
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
rclcpp::Subscription<hw1_interface::msg::StudentResult>::SharedPtr subscriber_student_result_;
|
||||||
|
};
|
||||||
|
|
||||||
|
int main(int argc,char *argv[]) {
|
||||||
|
rclcpp::init(argc,argv);
|
||||||
|
|
||||||
|
auto node = std::make_shared<NodeHw1StudentSubscriber>();
|
||||||
|
|
||||||
|
rclcpp::spin(node);
|
||||||
|
rclcpp::shutdown();
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user