您最多选择25个主题
主题必须以中文或者字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
45 行
1.2 KiB
45 行
1.2 KiB
using RosMessageTypes.RoboticsDemo;
|
|
using UnityEngine;
|
|
using Random = UnityEngine.Random;
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
public class RosPublisherExample : MonoBehaviour
|
|
{
|
|
public ROSConnection ros;
|
|
public string topicName = "pos_rot";
|
|
|
|
// The game object
|
|
public GameObject cube;
|
|
// Publish the cube's position and rotation every N seconds
|
|
public float publishMessageFrequency = 0.5f;
|
|
|
|
// Used to determine how much time has elapsed since the last message was published
|
|
private float timeElapsed;
|
|
|
|
private void Update()
|
|
{
|
|
timeElapsed += Time.deltaTime;
|
|
|
|
if (timeElapsed > publishMessageFrequency)
|
|
{
|
|
cube.transform.rotation = Random.rotation;
|
|
|
|
PosRot cubePos = new PosRot(
|
|
cube.transform.position.x,
|
|
cube.transform.position.y,
|
|
cube.transform.position.z,
|
|
cube.transform.rotation.x,
|
|
cube.transform.rotation.y,
|
|
cube.transform.rotation.z,
|
|
cube.transform.rotation.w
|
|
);
|
|
|
|
// Finally send the message to server_endpoint.py running in ROS
|
|
ros.Send(topicName, cubePos);
|
|
|
|
timeElapsed = 0;
|
|
}
|
|
}
|
|
}
|