-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
452 lines (405 loc) · 13.4 KB
/
client.go
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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
package xsens
import (
"bufio"
"context"
"fmt"
"io"
)
// Client for communicating with an Xsens device.
type Client struct {
p io.ReadWriteCloser
sc *bufio.Scanner
message Message
mtData2 MTData2
mtData2Packet MTData2Packet
nextPacketIndex int
// measurement data
packetCounter PacketCounter
statusByte StatusByte
statusWord StatusWord
sampleTimeFine SampleTimeFine
sampleTimeCoarse SampleTimeCoarse
baroPressure BaroPressure
utcTime UTCTime
deltaV DeltaV
acceleration Acceleration
freeAcceleration FreeAcceleration
accelerationHR AccelerationHR
deltaQ DeltaQ
rateOfTurn RateOfTurn
rateOfTurnHR RateOfTurnHR
quaternion Quaternion
eulerAngles EulerAngles
rotationMatrix RotationMatrix
temperature Temperature
altitudeEllipsoid AltitudeEllipsoid
positionECEF PositionECEF
latLon LatLon
velocityXYZ VelocityXYZ
magneticField MagneticField
gnssPVTData GNSSPVTData
gnssSatInfo GNSSSatInfo
}
// NewClient returns a new client using the provided ReadWriterCloser for communication.
func NewClient(p io.ReadWriteCloser) *Client {
sc := bufio.NewScanner(p)
sc.Split(ScanMessages)
return &Client{p: p, sc: sc}
}
// Close the client's ReadWriterCloser.
func (c *Client) Close() error {
if err := c.p.Close(); err != nil {
return fmt.Errorf("xsens client: close: %w", err)
}
return nil
}
// Receive an Xsens message.
//
// Clears state related to a previously received message, such as scanned measurement data.
func (c *Client) Receive(_ context.Context) error {
// clear previous received message state
c.message = nil
c.mtData2 = nil
c.mtData2Packet = nil
c.nextPacketIndex = 0
// receive new message
if !c.sc.Scan() {
if c.sc.Err() == nil {
return fmt.Errorf("xsens client: receive: %w", io.EOF)
}
return fmt.Errorf("xsens client: receive: %w", c.sc.Err())
}
c.message = c.sc.Bytes()
if err := c.message.Validate(); err != nil {
return fmt.Errorf("xsens client: receive: %w", c.sc.Err())
}
if c.message.Identifier() == MessageIdentifierMTData2 {
c.mtData2 = c.message.Data()
}
return nil
}
// RawMessage returns the raw bytes of the last received message.
func (c *Client) RawMessage() []byte {
return c.sc.Bytes()
}
// GoToConfig puts the Xsens device in config mode.
func (c *Client) GoToConfig(ctx context.Context) error {
if err := c.send(ctx, NewMessage(MessageIdentifierGotoConfig, nil)); err != nil {
return fmt.Errorf("xsens client: go to config: %w", err)
}
if err := c.receiveUntil(ctx, MessageIdentifierGotoConfigAck); err != nil {
return fmt.Errorf("xsens client: go to config: %w", err)
}
return nil
}
// SetOutputConfiguration sets the Xsens device output configuration.
func (c *Client) SetOutputConfiguration(ctx context.Context, configuration OutputConfiguration) error {
data, err := configuration.Marshal()
if err != nil {
return fmt.Errorf("xsens client: set output configuration: %w", err)
}
if err := c.send(ctx, NewMessage(MessageIdentifierSetOutputConfiguration, data)); err != nil {
return fmt.Errorf("xsens client: set output configuration: %w", err)
}
if err := c.receiveUntil(ctx, MessageIdentifierSetOutputConfigurationAck); err != nil {
return fmt.Errorf("xsens client: set output configuration: %w", err)
}
return nil
}
// GetOutputConfiguration returns the Xsens output configuration.
func (c *Client) GetOutputConfiguration(ctx context.Context) (OutputConfiguration, error) {
if err := c.send(ctx, NewMessage(MessageIdentifierReqOutputConfiguration, nil)); err != nil {
return nil, fmt.Errorf("xsens client: get output configuration: %w", err)
}
if err := c.receiveUntil(ctx, MessageIdentifierReqOutputConfigurationAck); err != nil {
return nil, fmt.Errorf("xsens client: get output configuration: %w", err)
}
var result OutputConfiguration
if err := result.Unmarshal(c.message.Data()); err != nil {
return nil, fmt.Errorf("xsens client: get output configuration: %w", err)
}
return result, nil
}
// SetCANOutputConfiguration sets the Xsens device CAN output configuration.
func (c *Client) SetCANOutputConfiguration(ctx context.Context, configuration CANOutputConfiguration) error {
data, err := configuration.MarshalBinary()
if err != nil {
return fmt.Errorf("xsens client: set CAN output configuration: %w", err)
}
if err := c.send(ctx, NewMessage(MessageIdentifierSetCANOutputConfig, data)); err != nil {
return fmt.Errorf("xsens client: set CAN output configuration: %w", err)
}
if err := c.receiveUntil(ctx, MessageIdentifierSetCANOutputConfigAck); err != nil {
return fmt.Errorf("xsens client: set CAN output configuration: %w", err)
}
return nil
}
// GetCANOutputConfiguration returns the Xsens CAN output configuration.
func (c *Client) GetCANOutputConfiguration(ctx context.Context) (CANOutputConfiguration, error) {
if err := c.send(ctx, NewMessage(MessageIdentifierReqCANOutputConfig, nil)); err != nil {
return nil, fmt.Errorf("xsens client: get output configuration: %w", err)
}
if err := c.receiveUntil(ctx, MessageIdentifierReqCANOutputConfigAck); err != nil {
return nil, fmt.Errorf("xsens client: get output configuration: %w", err)
}
var result CANOutputConfiguration
if err := result.UnmarshalBinary(c.message.Data()); err != nil {
return nil, fmt.Errorf("xsens client: get CAN output configuration: %w", err)
}
return result, nil
}
// SetCANConfiguration sets the Xsens device CAN configuration.
func (c *Client) SetCANConfiguration(ctx context.Context, configuration CANConfig) error {
data, err := configuration.MarshalBinary()
if err != nil {
return fmt.Errorf("xsens client: set CAN configuration: %w", err)
}
if err := c.send(ctx, NewMessage(MessageIdentifierSetCANConfig, data)); err != nil {
return fmt.Errorf("xsens client: set CAN output configuration: %w", err)
}
if err := c.receiveUntil(ctx, MessageIdentifierSetCANConfigAck); err != nil {
return fmt.Errorf("xsens client: set CAN configuration: %w", err)
}
return nil
}
// GetCANConfiguration returns the Xsens CAN output configuration.
func (c *Client) GetCANConfiguration(ctx context.Context) (*CANConfig, error) {
if err := c.send(ctx, NewMessage(MessageIdentifierReqCANConfig, nil)); err != nil {
return nil, fmt.Errorf("xsens client: get configuration: %w", err)
}
if err := c.receiveUntil(ctx, MessageIdentifierReqCANConfigAck); err != nil {
return nil, fmt.Errorf("xsens client: get configuration: %w", err)
}
result := &CANConfig{}
if err := result.UnmarshalBinary(c.message.Data()); err != nil {
return nil, fmt.Errorf("xsens client: get CAN configuration: %w", err)
}
return result, nil
}
// GetDeviceID returns the Xsens DeviceID.
func (c *Client) GetDeviceID(ctx context.Context) (*DeviceID, error) {
if err := c.send(ctx, NewMessage(MessageIdentifierReqDID, nil)); err != nil {
return nil, fmt.Errorf("xsens client: get device id: %w", err)
}
if err := c.receiveUntil(ctx, MessageIdentifierDeviceID); err != nil {
return nil, fmt.Errorf("xsens client: get device id: %w", err)
}
result := DeviceID(0)
if err := (&result).UnmarshalBinary(c.message.Data()); err != nil {
return nil, fmt.Errorf("xsens client: get device id: %w", err)
}
return &result, nil
}
// GetProductCode returns the Xsens ProductCode.
func (c *Client) GetProductCode(ctx context.Context) (*ProductCode, error) {
if err := c.send(ctx, NewMessage(MessageIdentifierReqProductCode, nil)); err != nil {
return nil, fmt.Errorf("xsens client: get device id: %w", err)
}
if err := c.receiveUntil(ctx, MessageIdentifierProductCode); err != nil {
return nil, fmt.Errorf("xsens client: get device id: %w", err)
}
result := ProductCode("")
if err := (&result).UnmarshalBinary(c.message.Data()); err != nil {
return nil, fmt.Errorf("xsens client: get device id: %w", err)
}
return &result, nil
}
// GetHWVersion returns the Xsens HWVersion.
func (c *Client) GetHWVersion(ctx context.Context) (*HWVersion, error) {
if err := c.send(ctx, NewMessage(MessageIdentifierReqHWVersion, nil)); err != nil {
return nil, fmt.Errorf("xsens client: get device id: %w", err)
}
if err := c.receiveUntil(ctx, MessageIdentifierHWVersion); err != nil {
return nil, fmt.Errorf("xsens client: get device id: %w", err)
}
result := HWVersion("")
if err := (&result).UnmarshalBinary(c.message.Data()); err != nil {
return nil, fmt.Errorf("xsens client: get device id: %w", err)
}
return &result, nil
}
// GoToMeasurement puts the Xsens device in measurement mode.
func (c *Client) GoToMeasurement(ctx context.Context) error {
if err := c.send(ctx, NewMessage(MessageIdentifierGotoMeasurement, nil)); err != nil {
return fmt.Errorf("xsens client: go to measurement: %w", err)
}
if err := c.receiveUntil(ctx, MessageIdentifierMTData2); err != nil {
return fmt.Errorf("xsens client: go to config: %w", err)
}
return nil
}
// MessageIdentifier returns the message identifier of the last received message.
func (c *Client) MessageIdentifier() MessageIdentifier {
return c.message.Identifier()
}
// MeasurementData returns the last scanned measurement data.
func (c *Client) MeasurementData() MeasurementData {
switch c.mtData2Packet.Identifier().DataType {
case DataTypeDeltaV:
return &c.deltaV
case DataTypeAcceleration:
return &c.acceleration
case DataTypeFreeAcceleration:
return &c.freeAcceleration
case DataTypeAccelerationHR:
return &c.accelerationHR
case DataTypeDeltaQ:
return &c.deltaQ
case DataTypeRateOfTurn:
return &c.rateOfTurn
case DataTypeRateOfTurnHR:
return &c.rateOfTurnHR
case DataTypeQuaternion:
return &c.quaternion
case DataTypeEulerAngles:
return &c.eulerAngles
case DataTypeRotationMatrix:
return &c.rotationMatrix
case DataTypeTemperature:
return &c.temperature
case DataTypeAltitudeEllipsoid:
return &c.altitudeEllipsoid
case DataTypePositionECEF:
return &c.positionECEF
case DataTypeLatLon:
return &c.latLon
case DataTypeVelocityXYZ:
return &c.velocityXYZ
case DataTypeStatusByte:
return &c.statusByte
case DataTypeStatusWord:
return &c.statusWord
case DataTypeUTCTime:
return &c.utcTime
case DataTypePacketCounter:
return &c.packetCounter
case DataTypeSampleTimeFine:
return &c.sampleTimeFine
case DataTypeSampleTimeCoarse:
return &c.sampleTimeCoarse
case DataTypeBaroPressure:
return &c.baroPressure
case DataTypeMagneticField:
return &c.magneticField
case DataTypeGNSSPVTData:
return &c.gnssPVTData
case DataTypeGNSSSatInfo:
return &c.gnssSatInfo
}
return nil
}
// ScanMeasurementData advances to the next measurement data packet, when the current message contains measurement data.
func (c *Client) ScanMeasurementData() bool {
if c.message.Identifier() != MessageIdentifierMTData2 {
return false
}
packet, err := c.mtData2.PacketAt(c.nextPacketIndex)
if err != nil {
return false
}
c.nextPacketIndex += len(packet)
c.mtData2Packet = packet
// TODO: Improve this API after removing MessageScanner
data := c.MeasurementData()
if data == nil {
return false
}
if err := data.UnmarshalMTData2Packet(c.mtData2Packet); err != nil {
return false
}
return true
}
// RawPacket returns the raw bytes of the current measurement data packet.
func (c *Client) RawPacket() []byte {
return c.mtData2Packet
}
// DataType returns the data type of the current scanned measurement data packet.
func (c *Client) DataType() DataType {
return c.mtData2Packet.Identifier().DataType
}
func (c *Client) PacketCounter() *PacketCounter {
return &c.packetCounter
}
func (c *Client) SampleTimeFine() *SampleTimeFine {
return &c.sampleTimeFine
}
func (c *Client) UTCTime() *UTCTime {
return &c.utcTime
}
func (c *Client) StatusByte() *StatusByte {
return &c.statusByte
}
func (c *Client) StatusWord() *StatusWord {
return &c.statusWord
}
func (c *Client) EulerAngles() *EulerAngles {
return &c.eulerAngles
}
func (c *Client) Acceleration() *Acceleration {
return &c.acceleration
}
func (c *Client) DeltaV() *DeltaV {
return &c.deltaV
}
func (c *Client) RateOfTurn() *RateOfTurn {
return &c.rateOfTurn
}
func (c *Client) DeltaQ() *DeltaQ {
return &c.deltaQ
}
func (c *Client) LatLon() *LatLon {
return &c.latLon
}
func (c *Client) AltitudeEllipsoid() *AltitudeEllipsoid {
return &c.altitudeEllipsoid
}
func (c *Client) VelocityXYZ() *VelocityXYZ {
return &c.velocityXYZ
}
func (c *Client) GNSSPVTData() *GNSSPVTData {
return &c.gnssPVTData
}
func (c *Client) SampleTimeCoarse() *SampleTimeCoarse {
return &c.sampleTimeCoarse
}
func (c *Client) BaroPressure() *BaroPressure {
return &c.baroPressure
}
func (c *Client) Temperature() *Temperature {
return &c.temperature
}
func (c *Client) MagneticField() *MagneticField {
return &c.magneticField
}
func (c *Client) RotationMatrix() *RotationMatrix {
return &c.rotationMatrix
}
func (c *Client) FreeAcceleration() *FreeAcceleration {
return &c.freeAcceleration
}
func (c *Client) Quaternion() *Quaternion {
return &c.quaternion
}
func (c *Client) GNSSSatInfo() *GNSSSatInfo {
return &c.gnssSatInfo
}
func (c *Client) PositionECEF() *PositionECEF {
return &c.positionECEF
}
func (c *Client) send(_ context.Context, message Message) error {
if _, err := c.p.Write(message); err != nil {
return fmt.Errorf("send %v: %w", message.Identifier(), err)
}
return nil
}
func (c *Client) receiveUntil(ctx context.Context, until MessageIdentifier) error {
for {
if err := c.Receive(ctx); err != nil {
return fmt.Errorf("receive until %v: %w", until, err)
}
if c.MessageIdentifier() != until {
continue
}
return nil
}
}