メッセージを1度ずつ送信する。
tkinterを使う。tkinterはPythonの標準ライブラリのはずだけど、無い場合がある。
インストールはこんな感じ。
1 |
apt install python3-tk |
コンテナで使う場合はホストでXを設定しておく。
1 |
sudo xhost si:localuser:root |
コード例
publisher.py
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 |
#!/usr/bin/env python # -*- coding: utf8 -*- import sys import tkinter as tk import rclpy from rclpy.node import Node from std_msgs.msg import String rclpy.init() node = Node("talker") pub = node.create_publisher(String, "chatter",10) msg = String() def get_entry(): msg = EditBox.get() print(msg) msg.data = msg pub.publish(msg) def end(): node.destroy_node() rclpy.shutdown() root.destroy() print("end") root = tk.Tk() root.title(u"Publisher") root.geometry("400x100") # button1 = tk.Button() button1["text"] = "publish" button1["command"] = get_entry button1.pack() # EditBox = tk.Entry(width=50) EditBox.insert(tk.END,"Hello World!") EditBox.pack() # button2 = tk.Button() button2["text"] = "End" button2["command"] = end button2.pack() root.mainloop() |
ちなみにsubscriber.py
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
#!/usr/bin/env python3 #-*- coding:utf-8 -*- import rclpy from rclpy.node import Node from std_msgs.msg import String rclpy.init() node = Node("listener") def chatter_callback(msg): print("{0}".format(msg)) node.create_subscription(String, "chatter", chatter_callback,10) try: rclpy.spin(node) except KeyboardInterrupt: pass node.destroy_node() rclpy.shutdown() |
Leave a Reply