forked from yhagiwara/ai-robot-chapter7
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Pkg-create
70 lines (51 loc) · 1.78 KB
/
Pkg-create
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
====== ワークスペースの作成 ======
1. $ mkdir -p ~/ros2_ws/src
2. $ cd ros2_ws/src
====== パッケージの作成 ======
3. $ ros2 pkg create ros2_my_package --build-type ament_python
====== 事前準備 ======
4. $ cd ros2_my_package
5. $ mv ros2_my_package scripts
6. $ cd scripts
7. $ touch talker.py
8. $ cd ..
====== setup.py の編集 ======
9. $ vim setup.py
8行目 packages=['scripts'], # 実行ファイルが存在するディレクトリを指定する
に変更する
22行目で改行を行い 'talker=scripts.talker:main', # 実行する関数の位置を指定する
を追記する
====== 実行ファイルの編集 ======
10. $ vim scripts/talker.py
import rclpy
from rclpy.node import Node
from std_msgs.msg import String
class MinimalPublisher(Node):
def __init__(self):
super().__init__('minimal_publisher')
self.publisher_ = self.create_publisher(String, 'topic', 10)
timer_period = 0.5 # seconds
self.timer = self.create_timer(timer_period, self.timer_callback)
self.i = 0
def timer_callback(self):
msg = String()
msg.data = 'Hello World: %d' % self.i
self.publisher_.publish(msg)
self.get_logger().info('Publishing: "%s"' % msg.data)
self.i += 1
def main(args=None):
rclpy.init(args=args)
minimal_publisher = MinimalPublisher()
rclpy.spin(minimal_publisher)
# Destroy the node explicitly
# (optional - otherwise it will be done automatically
# when the garbage collector destroys the node object)
minimal_publisher.destroy_node()
rclpy.shutdown()
:wqで保存
====== ビルド ======
9. $ cd ~/ros2_ws
10. $ colcon build
11. $ source install/setup.bash
====== 実行ファイルの実行 ======
12. $ ros2 run ros2_my_package talker