-
Notifications
You must be signed in to change notification settings - Fork 5
/
RosWriter.cs
253 lines (221 loc) · 7.62 KB
/
RosWriter.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
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
/**
* Copyright (c) 2019 LG Electronics, Inc.
*
* This software contains code licensed as described in LICENSE.
*
*/
using System;
using System.Globalization;
using System.Text;
using Simulator.Bridge.Data;
using UnityEngine;
namespace Simulator.Bridge.Ros
{
public class RosWriter<BridgeType>
{
ROS Instance;
string Topic;
public RosWriter(ROS instance, string topic)
{
Instance = instance;
Topic = topic;
}
public void Write(BridgeType message, Action completed)
{
var sb = new StringBuilder(4096);
sb.Append('{');
{
sb.Append("\"op\":\"publish\",");
sb.Append("\"topic\":\"");
sb.Append(Topic);
sb.Append("\",");
sb.Append("\"msg\":");
try
{
RosSerialization.Serialize(message, sb);
}
catch (Exception ex)
{
// explicit logging of exception because this method is often called
// from background threads for which Unity does not log exceptions
Debug.LogException(ex);
throw;
}
}
sb.Append('}');
var data = sb.ToString();
Instance.SendAsync(data, completed);
}
}
class RosPointCloudWriter
{
RosWriter<Ros.PointCloud2> Writer;
byte[] Buffer;
static readonly Ros.PointField[] PointFields = new[]
{
new Ros.PointField()
{
name = "x",
offset = 0,
datatype = 7,
count = 1,
},
new Ros.PointField()
{
name = "y",
offset = 4,
datatype = 7,
count = 1,
},
new Ros.PointField()
{
name = "z",
offset = 8,
datatype = 7,
count = 1,
},
new Ros.PointField()
{
name = "intensity",
offset = 16,
datatype = 2,
count = 1,
},
new Ros.PointField()
{
name = "timestamp",
offset = 24,
datatype = 8,
count = 1,
},
};
public RosPointCloudWriter(ROS instance, string topic)
{
Writer = new RosWriter<Ros.PointCloud2>(instance, topic);
}
public void Write(PointCloudData data, Action completed)
{
if (Buffer == null || Buffer.Length != data.Points.Length)
{
Buffer = new byte[32 * data.Points.Length];
}
int count = 0;
unsafe
{
fixed (byte* ptr = Buffer)
{
int offset = 0;
for (int i = 0; i < data.Points.Length; i++)
{
var point = data.Points[i];
if (point == UnityEngine.Vector4.zero)
{
continue;
}
var pos = new UnityEngine.Vector3(point.x, point.y, point.z);
float intensity = point.w;
*(UnityEngine.Vector3*)(ptr + offset) = data.Transform.MultiplyPoint3x4(pos);
*(ptr + offset + 16) = (byte)(intensity * 255);
offset += 32;
count++;
}
}
}
var msg = new Ros.PointCloud2()
{
header = new Ros.Header()
{
seq = data.Sequence,
stamp = Conversions.ConvertTime(data.Time),
frame_id = data.Frame,
},
height = 1,
width = (uint)count,
fields = PointFields,
is_bigendian = false,
point_step = 32,
row_step = (uint)count * 32,
data = new PartialByteArray()
{
Array = Buffer,
Length = count * 32,
},
is_dense = true,
};
Writer.Write(msg, completed);
}
}
class RosNmeaWriter
{
RosWriter<Ros.Sentence> Writer;
const float Accuracy = 0.01f; // just a number to report
const double Height = 0; // sea level to WGS84 ellipsoid
public RosNmeaWriter(ROS instance, string topic)
{
Writer = new RosWriter<Ros.Sentence>(instance, topic);
}
public void Write(GpsData message, Action completed)
{
char latitudeS = message.Latitude < 0 ? 'S' : 'N';
char longitudeS = message.Longitude < 0 ? 'W' : 'E';
double lat = Math.Abs(message.Latitude);
double lon = Math.Abs(message.Longitude);
lat = Math.Floor(lat) * 100 + (lat % 1) * 60.0f;
lon = Math.Floor(lon) * 100 + (lon % 1) * 60.0f;
var dt = DateTimeOffset.FromUnixTimeMilliseconds((long)(message.Time * 1000.0)).UtcDateTime;
var utc = dt.ToString("HHmmss.fff");
var gga = string.Format("GPGGA,{0},{1},{2},{3},{4},{5},{6},{7},{8},M,{9},M,,",
utc,
lat.ToString("0.000000", CultureInfo.InvariantCulture), latitudeS,
lon.ToString("0.000000", CultureInfo.InvariantCulture), longitudeS,
1, // GPX fix
10, // sattelites tracked
Accuracy,
message.Altitude.ToString("0.000000", CultureInfo.InvariantCulture),
Height.ToString("0.000000", CultureInfo.InvariantCulture));
var angles = message.Orientation.eulerAngles;
float roll = -angles.z;
float pitch = -angles.x;
float yaw = angles.y;
var qq = string.Format("QQ02C,INSATT,V,{0},{1},{2},{3},",
utc,
roll.ToString("0.000", CultureInfo.InvariantCulture),
pitch.ToString("0.000", CultureInfo.InvariantCulture),
yaw.ToString("0.000", CultureInfo.InvariantCulture));
// http://www.plaisance-pratique.com/IMG/pdf/NMEA0183-2.pdf
// 5.2.3 Checksum Field
byte ggaChecksum = 0;
for (int i = 0; i < gga.Length; i++)
{
ggaChecksum ^= (byte)gga[i];
}
byte qqChecksum = 0;
for (int i = 0; i < qq.Length; i++)
{
qqChecksum ^= (byte)qq[i];
}
var ggaMessage = new Ros.Sentence()
{
header = new Ros.Header()
{
stamp = Conversions.ConvertTime(message.Time),
seq = 2 * message.Sequence + 0,
frame_id = message.Frame,
},
sentence = "$" + gga + "*" + ggaChecksum.ToString("X2"),
};
Writer.Write(ggaMessage, null);
var qqMessage = new Ros.Sentence()
{
header = new Ros.Header()
{
stamp = ggaMessage.header.stamp,
seq = 2 * message.Sequence + 1,
frame_id = message.Frame,
},
sentence = qq + "@" + qqChecksum.ToString("X2"),
};
Writer.Write(qqMessage, completed);
}
}
}