0
0

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 1 year has passed since last update.

ros msgとjsonの相互変換 (python)

Last updated at Posted at 2022-08-27

はじめに

このライブラリの使い方

github

環境

ubuntu20.04
ROS noetic

install

pip3 install rospy-message-converter

サンプルプログラム

jsonからrosに変換

json_to_ros.py
from rospy_message_converter import json_message_converter

msg_type = "std_msgs/String"
json_msg = '{"data": "Hello, Robot"}'
ros_msg = json_message_converter.convert_json_to_ros_message(msg_type, json_msg)

print(ros_msg)

結果

data: "Hello, Robot"

rosからjsonに変換

std_msgs/String

ros_to_json.py
from rospy_message_converter import json_message_converter
import std_msgs.msg
ros_message = std_msgs.msg.String(data="Hello, Robot")
json = json_message_converter.convert_ros_message_to_json(ros_message)
print(json)

結果

'{"data": "Hello, Robot"}'

geometry_msgs/PoseStamped

ros2json.py
from rospy_message_converter import json_message_converter
from geometry_msgs.msg import *
msg = PoseStamped()
msg.header.frame_id = "map"
json = json_message_converter.convert_ros_message_to_json(msg)
print(json)
{"header": {"seq": 0, "stamp": {"secs": 0, "nsecs": 0}, "frame_id": "map"}, "pose": {"position": {"x": 0.0, "y": 0.0, "z": 0.0}, "orientation": {"x": 0.0, "y": 0.0, "z": 0.0, "w": 0.0}}}

geometry_msgs/PointStamped

ros2json.py
from rospy_message_converter import json_message_converter
from geometry_msgs.msg import *
msg = PointStamped()
msg.header.frame_id = "map"
json = json_message_converter.convert_ros_message_to_json(msg)
print(json)
{"header": {"seq": 0, "stamp": {"secs": 0, "nsecs": 0}, "frame_id": "map"}, "point": {"x": 0.0, "y": 0.0, "z": 0.0}}

geometry_msgs/Polygon

polygon2json.py
from rospy_message_converter import json_message_converter
import std_msgs.msg
from geometry_msgs.msg import *

msg = Polygon()
json = json_message_converter.convert_ros_message_to_json(msg)
print(json)

msg = Polygon()
msg.points.append(Point32(1,2,3))
msg.points.append(Point32(4,5,6))
json = json_message_converter.convert_ros_message_to_json(msg)
print(json)
{"points": []}
{"points": [{"x": 1, "y": 2, "z": 3}, {"x": 4, "y": 5, "z": 6}]}

geometry_msgs/PolygonStamped

ps2json.py
from rospy_message_converter import json_message_converter
import std_msgs.msg
from geometry_msgs.msg import *

msg = PolygonStamped()
json = json_message_converter.convert_ros_message_to_json(msg)
print(json)

msg = PolygonStamped()
msg.header.frame_id = "map"
msg.polygon.points.append(Point32(1,2,3))
msg.polygon.points.append(Point32(4,5,6))
json = json_message_converter.convert_ros_message_to_json(msg)
print(json)

output

{"header": {"seq": 0, "stamp": {"secs": 0, "nsecs": 0}, "frame_id": ""}, "polygon": {"points": []}}
{"header": {"seq": 0, "stamp": {"secs": 0, "nsecs": 0}, "frame_id": "map"}, "polygon": {"points": [{"x": 1, "y": 2, "z": 3}, {"x": 4, "y": 5, "z": 6}]}}
0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?