YKpages

ロボット分野で勉強したことのまとめ

Unity, ROS 間での JSON パースメモ (nav_msgs/Odometry.msg)

はじめに

/opt/ros/kinetic/share/nav_msgs/msg/Odometry.msg

この型のデータを ROS から Unity アプリへ送信したときのパースの方法のメモ

Odometryメッセージの型の中身

階層構造が重要

  • Header header
    • uint32 seq
    • time stamp
    • string frame_id
  • string child_frame_id
  • geometry_msgs/PoseWithCovariance pose
    • Pose pose
      • Point position
        • float64 x
        • float64 y
        • float64 z
      • Quaternion orientation
        • float64 x
        • float64 y
        • float64 z
        • float64 w
    • float64[36] covariance
  • geometry_msgs/TwistWithCovariance twist
    • Twist twist
      • Vector3 linear
      • Vector3 angular
    • float64[36] covariance

気をつけること

poseとtwistの変数名が重複しているので区別する

コード例

[System.Serializable]
    public class Odom
    {
        public string topic; //トピック名
        public Msg msg; // メッセージ内容
        public string op; // Operation : publish, subscribe など
    }

    [System.Serializable]
    public class Msg
    {
        public Header header;
        public string child_frame_id;
        public Pose pose;
        public Twist twist;
    }

    [System.Serializable]
    public class Header
    {
        public Stamp stamp;
        public string frame_id;
        public string seq;
    }

    [System.Serializable]
    public class Stamp
    {
        public int secs;
        public int nsecs;
    }

    [System.Serializable]
    public class Pose
    {
        public Pose2 pose; // 変数名がJSONのキーと合っていれば良い
        float[] covariance;
    }

    [System.Serializable]
    public class Pose2
    {
        public Vector3 position;
        public Quaternion orientation;
    }

    [System.Serializable]
    public class Twist
    {
        public Twist2 twist; // 変数名がJSONのキーと合っていれば良い
        float[] covariance;
    }

    [System.Serializable]
    public class Twist2
    {
        public Vector3 linear;
        public Vector3 angular;
    }

受け取るとき

// message = 受け取ったJSONデータ
Odom odomMsg = JsonUtility.FromJson<Odom>(message);

websocket-sharp を使った通信例

github.com

おわりに

ROS から Unity アプリへJSONデータを送ったときのパースの一例をまとめた