Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

make pacledt-compose'Node' #9

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
54 changes: 54 additions & 0 deletions packet/packet_composed/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
cmake_minimum_required(VERSION 3.8)
project(packet_composed)

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)
# uncomment the following section in order to fill in
# further dependencies manually.
# find_package(<dependency> REQUIRED)
find_package(rclcpp REQUIRED)
find_package(packet_interfaces REQUIRED)

# `composed`executableを作る
add_executable(composed
src/composed_main.cpp
src/composed.cpp
)
H1rono marked this conversation as resolved.
Show resolved Hide resolved

# `composed`executableの依存関係
ament_target_dependencies(composed
rclcpp # ROS2標準ライブラリ
std_msgs # ROS2が用意している標準的なmessage型の定義を含むライブラリ
H1rono marked this conversation as resolved.
Show resolved Hide resolved
)

# `composed`executableが参照するヘッダファイルがあるフォルダー
target_include_directories(composed
PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
$<INSTALL_INTERFACE:include>
)

# `listener`executableの置き場所
# 各executableの置き場所
install(TARGETS
composed
DESTINATION lib/${PROJECT_NAME}
)
H1rono marked this conversation as resolved.
Show resolved Hide resolved

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

H1rono marked this conversation as resolved.
Show resolved Hide resolved
ament_package()
23 changes: 23 additions & 0 deletions packet/packet_composed/include/packet_composed/sensorspacket.hpp
H1rono marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#include <memory>

#include "rclcpp/rclcpp.hpp"
// talker.cppでmsgを使えるようなincludeの構文
#include "std_msgs/msg/string.hpp"

// `composed`nodeを扱うクラス
class composed : public rclcpp::Node {
H1rono marked this conversation as resolved.
Show resolved Hide resolved
private:
// `chatter`topicのpublisher
std::shared_ptr<rclcpp::Publisher<std_msgs::msg::String>> _publisher;
// 定期的に実行するためのタイマー
std::shared_ptr<rclcpp::TimerBase> _timer;
// messageをpublishするごとにカウントアップさせる
unsigned int _count;

// タイマーで定期的に実行する関数 宣言
void _loop();

public:
// コンストラクタ
composed();
};
H1rono marked this conversation as resolved.
Show resolved Hide resolved
20 changes: 20 additions & 0 deletions packet/packet_composed/package.xml
Original file line number Diff line number Diff line change
@@ -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>packet_composed</name>
<version>0.0.0</version>
<description>TODO: Package description</description>
<maintainer email="[email protected]">norikonakano</maintainer>
<license>TODO: License declaration</license>
H1rono marked this conversation as resolved.
Show resolved Hide resolved

<buildtool_depend>ament_cmake</buildtool_depend>
<depend>rclcpp</depend>
<depend>packet_interfaces</depend>

<test_depend>ament_lint_auto</test_depend>
<test_depend>ament_lint_common</test_depend>

H1rono marked this conversation as resolved.
Show resolved Hide resolved
<export>
<build_type>ament_cmake</build_type>
</export>
</package>
45 changes: 45 additions & 0 deletions packet/packet_composed/src/composed.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#include <chrono> // 時間ユーティリティ タイマー作成時に使う
#include <functional> // 関数型プログラミング笑
#include <sstream> // stringstream iostreamみたいなやつ
#include <string> // みんな大好き文字列

#include "packet_composed/sensorspacket.hpp" // composedクラス

// タイマーで定期的に実行する関数 宣言
void composed::_loop() {
// 文字列ストリーム
// std::coutの書き込み先がstd::stringになったみたいな感じ
std::stringstream ss;
// 構築 "Hello, world! 0" みたいになる
ss << "Hello, world! " << _count;
// 将来publishするmessage#include "packet_interfaces/msg/Composed.msg"
std_msgs::msg::String msg{};
// std::string型のデータを代入
msg.data = ss.str();
// ログ出力
RCLCPP_INFO(this->get_logger(), "say %s", ss.str().c_str());
// RCLCPP_INFO_STREAM(this->get_logger(), "say: [" << msg.data << "]");
// messageをpublish
_publisher->publish(msg);
// _countをインクリメント publishするたびにカウントが増加するようになる
_count++;
}

// composedコンストラクタ 定義
composed::composed() :
rclcpp::Node("composed"), // node名をcomposedに設定
_publisher(), // _publisherのshared_ptrをnullptrで初期化
_timer(), // タイマーをnullptrで初期化
_count(0) // カウントの初期値は0
{
// これで50msとかかけるようになる
using namespace std::chrono_literals;
// publisherを作成
// `chatter`topicにQoS 10で出力すると設定
_publisher = this->create_publisher<std_msgs::msg::String>("/sensorspacket", 10);
H1rono marked this conversation as resolved.
Show resolved Hide resolved
// タイマーで定期的に呼び出される関数
auto loop = std::bind(&composed::_loop, this);
// タイマー作成
// 500ミリ秒ごとにloopが呼び出される
_timer = this->create_wall_timer(500ms, loop);
Copy link
Member

@H1rono H1rono Jun 30, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

もうちょい周波数高くていいかも

Suggested change
_timer = this->create_wall_timer(500ms, loop);
_timer = this->create_wall_timer(10ms, loop);

}
H1rono marked this conversation as resolved.
Show resolved Hide resolved
17 changes: 17 additions & 0 deletions packet/packet_composed/src/composed_main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// listener_main.cppと同じなのでコメントは略

#include <memory>
#include <rclcpp/rclcpp.hpp>
#include "packet_composed/sensorspacket.hpp"

int main(int argc, char * argv[]) {
rclcpp::init(argc, argv);
// ROS2プロセスの初期化
auto node = std::make_shared<composed>();
// `listener`nodeを作成
rclcpp::spin(node);
// 作成したnodeを実行
rclcpp::shutdown();
// ROS2プロセスを終了
return 0;
}
H1rono marked this conversation as resolved.
Show resolved Hide resolved