4
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

ROSで湯婆婆を実装してみた。

Last updated at Posted at 2020-11-09

最近Qiitaが湯婆婆で溢れているので、便乗すればもしかしたらLGTMもらえるかも せっかくなのでまだ見ていない物を使って実装しようと思ったので、ROSを使用していきます。

srvファイル

Yubaba.srv
string name
---
string new_name

名前を入力したら、新しい名前を返してくれるサービスです。

Service Proxy

import rospy
from std_msgs.msg import String, Empty
from yubaba.srv import Yubaba

def listen_to_yubaba(msg):
    print(msg.data)


yubaba_proxy = rospy.ServiceProxy("/yubaba/name_service", Yubaba)
yubaba_words = rospy.Subscriber("/yubaba/words", String, listen_to_yubaba)
greet_yubaba = rospy.Publisher("greet_yubaba", Empty, queue_size=1)

if __name__ == "__main__":
    rospy.init_node("client")
    greet_yubaba.publish()
    name = input()
    new_name = yubaba_proxy(name)

Yubaba Node

import rospy
import random
from yubaba.srv import Yubaba
from std_msgs.msg import String, Empty

class YubabaNode:

    def __init__(self):
        rospy.init_node("yubaba")
        self.yubaba_service = rospy.Service("~name_service", Yubaba, self.name_service)
        self.word_publisher = rospy.Publisher("~words", String, queue_size=1)
        self.greet_subscriber = rospy.Subscriber("greet_yubaba", Empty, self.start)

    def start(self, msg):
        self.word_publisher.publish("契約書だよ。そこに名前を書きな。")


    def name_service(self, req):
        name = req.name
        self.word_publisher.publish(f"フン。{name}というのかい。贅沢な名だねぇ。")
        rospy.sleep(0.5)
        new_name = random.choice(name)
        self.word_publisher.publish(f"今からお前の名前は{new_name}だ。いいかい、{new_name}だよ。分かったら返事をするんだ、{new_name}!!")
        return new_name

if __name__ == "__main__":
    yubaba = YubabaNode()
    rospy.spin()

おまけのlistener

import sys
import rospy
from std_msgs.msg import String

def listen_to_yubaba(msg):
    print(msg.data)

yubaba_words = rospy.Subscriber("/yubaba/words", String, listen_to_yubaba)

if __name__ == "__main__":
    args = sys.argv
    rospy.init_node(f"listener{args[1]}")
    rospy.spin()

実行例

Peek 2020-11-09 23-26.gif

構成

  • Yubaba Node

    • /yubaba/words: 湯婆婆様のありがたいお言葉。
    • /yubaba/name_service: 名前を新しく変えてくれるやつです。
  • /greet_yubaba: まずは湯婆婆に挨拶。

まとめ

もしかして ROS + 湯婆婆 = 老婆婆??

4
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?