-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathRosMsgPublisher.cs
53 lines (39 loc) · 1.22 KB
/
RosMsgPublisher.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
using UnityEngine;
using Unity.Robotics.ROSTCPConnector;
using Unity.Robotics.ROSTCPConnector.MessageGeneration;
using UnitySensors.ROS.Serializer;
namespace UnitySensors.ROS.Publisher
{
public class RosMsgPublisher<T, TT> : MonoBehaviour where T : RosMsgSerializer<TT> where TT : Message, new()
{
[SerializeField]
private float _frequency = 10.0f;
[SerializeField]
protected string _topicName;
[SerializeField]
protected T _serializer;
private ROSConnection _ros;
private float _time;
private float _dt;
private float _frequency_inv;
protected virtual void Start()
{
_dt = 0.0f;
_frequency_inv = 1.0f / _frequency;
_ros = ROSConnection.GetOrCreateInstance();
_ros.RegisterPublisher<TT>(_topicName);
_serializer.Init();
}
protected virtual void Update()
{
_dt += Time.deltaTime;
if (_dt < _frequency_inv) return;
_ros.Publish(_topicName, _serializer.Serialize());
_dt -= _frequency_inv;
}
private void OnDestroy()
{
_serializer.OnDestroy();
}
}
}