スクリプトは以下の通り。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Unity.Robotics.ROSTCPConnector;
using OculusMsgMsg = RosMessageTypes.CppPubsub.OculusMsgMsg;
using UnityEngine.XR;
using Node = UnityEngine.XR.XRNode;
public class Publisher : MonoBehaviour
{
ROSConnection ros;
public string topicName = "oculus_topic";
public float publishMessageFrequency = 0.015f;
private float timeElapsed;
// Start is called before the first frame update
void Start()
{
ros = ROSConnection.GetOrCreateInstance();
ros.RegisterPublisher<OculusMsgMsg>(topicName);
}
// Update is called once per frame
void Update()
{
timeElapsed += Time.deltaTime;
if (timeElapsed > publishMessageFrequency)
{
List<XRNodeState> DevStat = new List<XRNodeState>();
InputTracking.GetNodeStates(DevStat);
Quaternion hmdRotation;
foreach (XRNodeState s in DevStat)
{
if (s.nodeType == XRNode.Head)
{
s.TryGetRotation(out hmdRotation);
OculusMsgMsg outData = new OculusMsgMsg(hmdRotation.eulerAngles.ToVector3f());
ros.Publish(topicName, outData);
Debug.Log("Hello: " + outData);
}
}
// Finally send the message to server_endpoint.py running in ROS
timeElapsed = 0;
}
}
}