-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIscp_ControlPublisher.cs
More file actions
118 lines (98 loc) · 3.24 KB
/
Copy pathIscp_ControlPublisher.cs
File metadata and controls
118 lines (98 loc) · 3.24 KB
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
using UnityEngine;
using iSCP.Model;
/**
* Control DataFormat ( string -> JSON )
* "{
* "steering" : 0.0,
* "accel" : 0.0,
* "footbrake" : 0.0,
* "handbrake" : 0.0
* }"
*/
public class Iscp_ControlPublisher : MonoBehaviour, IIscpUpstreamCallbacks
{
[SerializeField]
private string dataType = "string";
[SerializeField]
private string dataName = "v1/1/control";
[SerializeField]
private bool persist;
public float SamplingRate = 15f;
private float? _SamplingRate;
private float elapsedTime = 0;
private float targetTime = 0;
public float Steering;
public float Accel;
public float Footbrake;
public float Handbrake;
// MEMO: Basically, IscpConnection attachment from Inspector is not required, but can be specified.
[SerializeField] private IscpConnection iscp;
private IscpUpstream upstream;
// Called when the script is first activated.
private void Awake()
{
// Setup iSCP.
if (iscp == null)
{
this.iscp = IscpConnection.GetOrCreateSharedInstance();
}
// Register upstream.
upstream = iscp.RegisterUpstream(persist);
upstream.Callbacks = this;
}
void Update()
{
// Time management so that transmission can be performed at the desired sampling rate.
if (SamplingRate != _SamplingRate)
{
_SamplingRate = SamplingRate;
this.targetTime = 1f / SamplingRate;
this.elapsedTime = 0f;
}
if (SamplingRate <= 0) return;
elapsedTime += Time.deltaTime;
if (this.elapsedTime > this.targetTime)
{
this.elapsedTime -= this.targetTime;
// Generation in accordance with data format.
var json = "";
json += "{";
json += " \"steering\" : " + Steering + ",";
json += " \"accel\" : " + Accel + ",";
json += " \"footbrake\" : " + Footbrake + ",";
json += " \"handbrake\" : " + Handbrake;
json += " }";
var payload = System.Text.Encoding.UTF8.GetBytes(json);
// Send data points to upstream.
upstream.SendDataPoint(dataName, dataType, payload);
}
}
private void OnEnable() { }
private void OnDisable() { }
#region IIscpUpstreamCallbacks
// Called when Upstream opens.
public void OnOpen(IscpUpstream upstream, string sequenceId)
{
if (upstream.Persist)
{
// ToDo: If retransmission is performed when data is missing, the data storage process is initiated.
}
}
// Called when data is created in units of sent data.
public void OnGenerateChunk(IscpUpstream upstream, string sequenceId, UpstreamChunk message)
{
if (upstream.Persist)
{
// ToDo: When retransmitting data when data is missing, the transmitted data is saved.
}
}
// Called when it is confirmed that the sent data has reached the server correctly.
public void OnReceiveAck(IscpUpstream upstream, string sequenceId, UpstreamChunkAck message)
{
if (upstream.Persist)
{
// ToDo: If you want to calculate the missing ratio, do it here.
}
}
#endregion
}