/
imu_v3_bricklet.go
2250 lines (1833 loc) · 75 KB
/
imu_v3_bricklet.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
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* ***********************************************************
* This file was automatically generated on 2021-05-06. *
* *
* Go Bindings Version 2.0.11 *
* *
* If you have a bugfix for this file and want to commit it, *
* please fix the bug in the generator. You can find a link *
* to the generators git repository on tinkerforge.com *
*************************************************************/
// Full fledged AHRS with 9 degrees of freedom.
//
//
// See also the documentation here: https://www.tinkerforge.com/en/doc/Software/Bricklets/IMUV3_Bricklet_Go.html.
package imu_v3_bricklet
import (
"encoding/binary"
"bytes"
"fmt"
. "github.com/Tinkerforge/go-api-bindings/internal"
"github.com/Tinkerforge/go-api-bindings/ipconnection"
)
type Function = uint8
const (
FunctionGetAcceleration Function = 1
FunctionGetMagneticField Function = 2
FunctionGetAngularVelocity Function = 3
FunctionGetTemperature Function = 4
FunctionGetOrientation Function = 5
FunctionGetLinearAcceleration Function = 6
FunctionGetGravityVector Function = 7
FunctionGetQuaternion Function = 8
FunctionGetAllData Function = 9
FunctionSaveCalibration Function = 10
FunctionSetSensorConfiguration Function = 11
FunctionGetSensorConfiguration Function = 12
FunctionSetSensorFusionMode Function = 13
FunctionGetSensorFusionMode Function = 14
FunctionSetAccelerationCallbackConfiguration Function = 15
FunctionGetAccelerationCallbackConfiguration Function = 16
FunctionSetMagneticFieldCallbackConfiguration Function = 17
FunctionGetMagneticFieldCallbackConfiguration Function = 18
FunctionSetAngularVelocityCallbackConfiguration Function = 19
FunctionGetAngularVelocityCallbackConfiguration Function = 20
FunctionSetTemperatureCallbackConfiguration Function = 21
FunctionGetTemperatureCallbackConfiguration Function = 22
FunctionSetOrientationCallbackConfiguration Function = 23
FunctionGetOrientationCallbackConfiguration Function = 24
FunctionSetLinearAccelerationCallbackConfiguration Function = 25
FunctionGetLinearAccelerationCallbackConfiguration Function = 26
FunctionSetGravityVectorCallbackConfiguration Function = 27
FunctionGetGravityVectorCallbackConfiguration Function = 28
FunctionSetQuaternionCallbackConfiguration Function = 29
FunctionGetQuaternionCallbackConfiguration Function = 30
FunctionSetAllDataCallbackConfiguration Function = 31
FunctionGetAllDataCallbackConfiguration Function = 32
FunctionGetSPITFPErrorCount Function = 234
FunctionSetBootloaderMode Function = 235
FunctionGetBootloaderMode Function = 236
FunctionSetWriteFirmwarePointer Function = 237
FunctionWriteFirmware Function = 238
FunctionSetStatusLEDConfig Function = 239
FunctionGetStatusLEDConfig Function = 240
FunctionGetChipTemperature Function = 242
FunctionReset Function = 243
FunctionWriteUID Function = 248
FunctionReadUID Function = 249
FunctionGetIdentity Function = 255
FunctionCallbackAcceleration Function = 33
FunctionCallbackMagneticField Function = 34
FunctionCallbackAngularVelocity Function = 35
FunctionCallbackTemperature Function = 36
FunctionCallbackLinearAcceleration Function = 37
FunctionCallbackGravityVector Function = 38
FunctionCallbackOrientation Function = 39
FunctionCallbackQuaternion Function = 40
FunctionCallbackAllData Function = 41
)
type MagnetometerRate = uint8
const (
MagnetometerRate2Hz MagnetometerRate = 0
MagnetometerRate6Hz MagnetometerRate = 1
MagnetometerRate8Hz MagnetometerRate = 2
MagnetometerRate10Hz MagnetometerRate = 3
MagnetometerRate15Hz MagnetometerRate = 4
MagnetometerRate20Hz MagnetometerRate = 5
MagnetometerRate25Hz MagnetometerRate = 6
MagnetometerRate30Hz MagnetometerRate = 7
)
type GyroscopeRange = uint8
const (
GyroscopeRange2000DPS GyroscopeRange = 0
GyroscopeRange1000DPS GyroscopeRange = 1
GyroscopeRange500DPS GyroscopeRange = 2
GyroscopeRange250DPS GyroscopeRange = 3
GyroscopeRange125DPS GyroscopeRange = 4
)
type GyroscopeBandwidth = uint8
const (
GyroscopeBandwidth523Hz GyroscopeBandwidth = 0
GyroscopeBandwidth230Hz GyroscopeBandwidth = 1
GyroscopeBandwidth116Hz GyroscopeBandwidth = 2
GyroscopeBandwidth47Hz GyroscopeBandwidth = 3
GyroscopeBandwidth23Hz GyroscopeBandwidth = 4
GyroscopeBandwidth12Hz GyroscopeBandwidth = 5
GyroscopeBandwidth64Hz GyroscopeBandwidth = 6
GyroscopeBandwidth32Hz GyroscopeBandwidth = 7
)
type AccelerometerRange = uint8
const (
AccelerometerRange2G AccelerometerRange = 0
AccelerometerRange4G AccelerometerRange = 1
AccelerometerRange8G AccelerometerRange = 2
AccelerometerRange16G AccelerometerRange = 3
)
type AccelerometerBandwidth = uint8
const (
AccelerometerBandwidth7_81Hz AccelerometerBandwidth = 0
AccelerometerBandwidth15_63Hz AccelerometerBandwidth = 1
AccelerometerBandwidth31_25Hz AccelerometerBandwidth = 2
AccelerometerBandwidth62_5Hz AccelerometerBandwidth = 3
AccelerometerBandwidth125Hz AccelerometerBandwidth = 4
AccelerometerBandwidth250Hz AccelerometerBandwidth = 5
AccelerometerBandwidth500Hz AccelerometerBandwidth = 6
AccelerometerBandwidth1000Hz AccelerometerBandwidth = 7
)
type SensorFusion = uint8
const (
SensorFusionOff SensorFusion = 0
SensorFusionOn SensorFusion = 1
SensorFusionOnWithoutMagnetometer SensorFusion = 2
SensorFusionOnWithoutFastMagnetometerCalibration SensorFusion = 3
)
type BootloaderMode = uint8
const (
BootloaderModeBootloader BootloaderMode = 0
BootloaderModeFirmware BootloaderMode = 1
BootloaderModeBootloaderWaitForReboot BootloaderMode = 2
BootloaderModeFirmwareWaitForReboot BootloaderMode = 3
BootloaderModeFirmwareWaitForEraseAndReboot BootloaderMode = 4
)
type BootloaderStatus = uint8
const (
BootloaderStatusOK BootloaderStatus = 0
BootloaderStatusInvalidMode BootloaderStatus = 1
BootloaderStatusNoChange BootloaderStatus = 2
BootloaderStatusEntryFunctionNotPresent BootloaderStatus = 3
BootloaderStatusDeviceIdentifierIncorrect BootloaderStatus = 4
BootloaderStatusCRCMismatch BootloaderStatus = 5
)
type StatusLEDConfig = uint8
const (
StatusLEDConfigOff StatusLEDConfig = 0
StatusLEDConfigOn StatusLEDConfig = 1
StatusLEDConfigShowHeartbeat StatusLEDConfig = 2
StatusLEDConfigShowStatus StatusLEDConfig = 3
)
type IMUV3Bricklet struct {
device Device
}
const DeviceIdentifier = 2161
const DeviceDisplayName = "IMU Bricklet 3.0"
// Creates an object with the unique device ID `uid`. This object can then be used after the IP Connection `ipcon` is connected.
func New(uid string, ipcon *ipconnection.IPConnection) (IMUV3Bricklet, error) {
internalIPCon := ipcon.GetInternalHandle().(IPConnection)
dev, err := NewDevice([3]uint8{ 2,0,0 }, uid, &internalIPCon, 0, DeviceIdentifier, DeviceDisplayName)
if err != nil {
return IMUV3Bricklet{}, err
}
dev.ResponseExpected[FunctionGetAcceleration] = ResponseExpectedFlagAlwaysTrue;
dev.ResponseExpected[FunctionGetMagneticField] = ResponseExpectedFlagAlwaysTrue;
dev.ResponseExpected[FunctionGetAngularVelocity] = ResponseExpectedFlagAlwaysTrue;
dev.ResponseExpected[FunctionGetTemperature] = ResponseExpectedFlagAlwaysTrue;
dev.ResponseExpected[FunctionGetOrientation] = ResponseExpectedFlagAlwaysTrue;
dev.ResponseExpected[FunctionGetLinearAcceleration] = ResponseExpectedFlagAlwaysTrue;
dev.ResponseExpected[FunctionGetGravityVector] = ResponseExpectedFlagAlwaysTrue;
dev.ResponseExpected[FunctionGetQuaternion] = ResponseExpectedFlagAlwaysTrue;
dev.ResponseExpected[FunctionGetAllData] = ResponseExpectedFlagAlwaysTrue;
dev.ResponseExpected[FunctionSaveCalibration] = ResponseExpectedFlagAlwaysTrue;
dev.ResponseExpected[FunctionSetSensorConfiguration] = ResponseExpectedFlagFalse;
dev.ResponseExpected[FunctionGetSensorConfiguration] = ResponseExpectedFlagAlwaysTrue;
dev.ResponseExpected[FunctionSetSensorFusionMode] = ResponseExpectedFlagFalse;
dev.ResponseExpected[FunctionGetSensorFusionMode] = ResponseExpectedFlagAlwaysTrue;
dev.ResponseExpected[FunctionSetAccelerationCallbackConfiguration] = ResponseExpectedFlagTrue;
dev.ResponseExpected[FunctionGetAccelerationCallbackConfiguration] = ResponseExpectedFlagAlwaysTrue;
dev.ResponseExpected[FunctionSetMagneticFieldCallbackConfiguration] = ResponseExpectedFlagTrue;
dev.ResponseExpected[FunctionGetMagneticFieldCallbackConfiguration] = ResponseExpectedFlagAlwaysTrue;
dev.ResponseExpected[FunctionSetAngularVelocityCallbackConfiguration] = ResponseExpectedFlagTrue;
dev.ResponseExpected[FunctionGetAngularVelocityCallbackConfiguration] = ResponseExpectedFlagAlwaysTrue;
dev.ResponseExpected[FunctionSetTemperatureCallbackConfiguration] = ResponseExpectedFlagTrue;
dev.ResponseExpected[FunctionGetTemperatureCallbackConfiguration] = ResponseExpectedFlagAlwaysTrue;
dev.ResponseExpected[FunctionSetOrientationCallbackConfiguration] = ResponseExpectedFlagTrue;
dev.ResponseExpected[FunctionGetOrientationCallbackConfiguration] = ResponseExpectedFlagAlwaysTrue;
dev.ResponseExpected[FunctionSetLinearAccelerationCallbackConfiguration] = ResponseExpectedFlagTrue;
dev.ResponseExpected[FunctionGetLinearAccelerationCallbackConfiguration] = ResponseExpectedFlagAlwaysTrue;
dev.ResponseExpected[FunctionSetGravityVectorCallbackConfiguration] = ResponseExpectedFlagTrue;
dev.ResponseExpected[FunctionGetGravityVectorCallbackConfiguration] = ResponseExpectedFlagAlwaysTrue;
dev.ResponseExpected[FunctionSetQuaternionCallbackConfiguration] = ResponseExpectedFlagTrue;
dev.ResponseExpected[FunctionGetQuaternionCallbackConfiguration] = ResponseExpectedFlagAlwaysTrue;
dev.ResponseExpected[FunctionSetAllDataCallbackConfiguration] = ResponseExpectedFlagTrue;
dev.ResponseExpected[FunctionGetAllDataCallbackConfiguration] = ResponseExpectedFlagAlwaysTrue;
dev.ResponseExpected[FunctionGetSPITFPErrorCount] = ResponseExpectedFlagAlwaysTrue;
dev.ResponseExpected[FunctionSetBootloaderMode] = ResponseExpectedFlagAlwaysTrue;
dev.ResponseExpected[FunctionGetBootloaderMode] = ResponseExpectedFlagAlwaysTrue;
dev.ResponseExpected[FunctionSetWriteFirmwarePointer] = ResponseExpectedFlagFalse;
dev.ResponseExpected[FunctionWriteFirmware] = ResponseExpectedFlagAlwaysTrue;
dev.ResponseExpected[FunctionSetStatusLEDConfig] = ResponseExpectedFlagFalse;
dev.ResponseExpected[FunctionGetStatusLEDConfig] = ResponseExpectedFlagAlwaysTrue;
dev.ResponseExpected[FunctionGetChipTemperature] = ResponseExpectedFlagAlwaysTrue;
dev.ResponseExpected[FunctionReset] = ResponseExpectedFlagFalse;
dev.ResponseExpected[FunctionWriteUID] = ResponseExpectedFlagFalse;
dev.ResponseExpected[FunctionReadUID] = ResponseExpectedFlagAlwaysTrue;
dev.ResponseExpected[FunctionGetIdentity] = ResponseExpectedFlagAlwaysTrue;
return IMUV3Bricklet{dev}, nil
}
// Returns the response expected flag for the function specified by the function ID parameter.
// It is true if the function is expected to send a response, false otherwise.
//
// For getter functions this is enabled by default and cannot be disabled, because those
// functions will always send a response. For callback configuration functions it is enabled
// by default too, but can be disabled by SetResponseExpected.
// For setter functions it is disabled by default and can be enabled.
//
// Enabling the response expected flag for a setter function allows to detect timeouts
// and other error conditions calls of this setter as well. The device will then send a response
// for this purpose. If this flag is disabled for a setter function then no response is sent
// and errors are silently ignored, because they cannot be detected.
//
// See SetResponseExpected for the list of function ID constants available for this function.
func (device *IMUV3Bricklet) GetResponseExpected(functionID Function) (bool, error) {
return device.device.GetResponseExpected(uint8(functionID))
}
// Changes the response expected flag of the function specified by the function ID parameter.
// This flag can only be changed for setter (default value: false) and callback configuration
// functions (default value: true). For getter functions it is always enabled.
//
// Enabling the response expected flag for a setter function allows to detect timeouts and
// other error conditions calls of this setter as well. The device will then send a response
// for this purpose. If this flag is disabled for a setter function then no response is sent
// and errors are silently ignored, because they cannot be detected.
func (device *IMUV3Bricklet) SetResponseExpected(functionID Function, responseExpected bool) error {
return device.device.SetResponseExpected(uint8(functionID), responseExpected)
}
// Changes the response expected flag for all setter and callback configuration functions of this device at once.
func (device *IMUV3Bricklet) SetResponseExpectedAll(responseExpected bool) {
device.device.SetResponseExpectedAll(responseExpected)
}
// Returns the version of the API definition (major, minor, revision) implemented by this API bindings. This is neither the release version of this API bindings nor does it tell you anything about the represented Brick or Bricklet.
func (device *IMUV3Bricklet) GetAPIVersion() [3]uint8 {
return device.device.GetAPIVersion()
}
// This callback is triggered periodically with the period that is set by
// SetAccelerationCallbackConfiguration. The parameters are the acceleration
// for the x, y and z axis.
func (device *IMUV3Bricklet) RegisterAccelerationCallback(fn func(int16, int16, int16)) uint64 {
wrapper := func(byteSlice []byte) {
var header PacketHeader
header.FillFromBytes(byteSlice)
if header.Length != 14 {
return
}
buf := bytes.NewBuffer(byteSlice[8:])
var x int16
var y int16
var z int16
binary.Read(buf, binary.LittleEndian, &x)
binary.Read(buf, binary.LittleEndian, &y)
binary.Read(buf, binary.LittleEndian, &z)
fn(x, y, z)
}
return device.device.RegisterCallback(uint8(FunctionCallbackAcceleration), wrapper)
}
// Remove a registered Acceleration callback.
func (device *IMUV3Bricklet) DeregisterAccelerationCallback(registrationId uint64) {
device.device.DeregisterCallback(uint8(FunctionCallbackAcceleration), registrationId)
}
// This callback is triggered periodically with the period that is set by
// SetMagneticFieldCallbackConfiguration. The parameters are the magnetic
// field for the x, y and z axis.
func (device *IMUV3Bricklet) RegisterMagneticFieldCallback(fn func(int16, int16, int16)) uint64 {
wrapper := func(byteSlice []byte) {
var header PacketHeader
header.FillFromBytes(byteSlice)
if header.Length != 14 {
return
}
buf := bytes.NewBuffer(byteSlice[8:])
var x int16
var y int16
var z int16
binary.Read(buf, binary.LittleEndian, &x)
binary.Read(buf, binary.LittleEndian, &y)
binary.Read(buf, binary.LittleEndian, &z)
fn(x, y, z)
}
return device.device.RegisterCallback(uint8(FunctionCallbackMagneticField), wrapper)
}
// Remove a registered Magnetic Field callback.
func (device *IMUV3Bricklet) DeregisterMagneticFieldCallback(registrationId uint64) {
device.device.DeregisterCallback(uint8(FunctionCallbackMagneticField), registrationId)
}
// This callback is triggered periodically with the period that is set by
// SetAngularVelocityCallbackConfiguration. The parameters are the angular
// velocity for the x, y and z axis.
func (device *IMUV3Bricklet) RegisterAngularVelocityCallback(fn func(int16, int16, int16)) uint64 {
wrapper := func(byteSlice []byte) {
var header PacketHeader
header.FillFromBytes(byteSlice)
if header.Length != 14 {
return
}
buf := bytes.NewBuffer(byteSlice[8:])
var x int16
var y int16
var z int16
binary.Read(buf, binary.LittleEndian, &x)
binary.Read(buf, binary.LittleEndian, &y)
binary.Read(buf, binary.LittleEndian, &z)
fn(x, y, z)
}
return device.device.RegisterCallback(uint8(FunctionCallbackAngularVelocity), wrapper)
}
// Remove a registered Angular Velocity callback.
func (device *IMUV3Bricklet) DeregisterAngularVelocityCallback(registrationId uint64) {
device.device.DeregisterCallback(uint8(FunctionCallbackAngularVelocity), registrationId)
}
// This callback is triggered periodically with the period that is set by
// SetTemperatureCallbackConfiguration. The parameter is the temperature.
func (device *IMUV3Bricklet) RegisterTemperatureCallback(fn func(int8)) uint64 {
wrapper := func(byteSlice []byte) {
var header PacketHeader
header.FillFromBytes(byteSlice)
if header.Length != 9 {
return
}
buf := bytes.NewBuffer(byteSlice[8:])
var temperature int8
binary.Read(buf, binary.LittleEndian, &temperature)
fn(temperature)
}
return device.device.RegisterCallback(uint8(FunctionCallbackTemperature), wrapper)
}
// Remove a registered Temperature callback.
func (device *IMUV3Bricklet) DeregisterTemperatureCallback(registrationId uint64) {
device.device.DeregisterCallback(uint8(FunctionCallbackTemperature), registrationId)
}
// This callback is triggered periodically with the period that is set by
// SetLinearAccelerationCallbackConfiguration. The parameters are the
// linear acceleration for the x, y and z axis.
func (device *IMUV3Bricklet) RegisterLinearAccelerationCallback(fn func(int16, int16, int16)) uint64 {
wrapper := func(byteSlice []byte) {
var header PacketHeader
header.FillFromBytes(byteSlice)
if header.Length != 14 {
return
}
buf := bytes.NewBuffer(byteSlice[8:])
var x int16
var y int16
var z int16
binary.Read(buf, binary.LittleEndian, &x)
binary.Read(buf, binary.LittleEndian, &y)
binary.Read(buf, binary.LittleEndian, &z)
fn(x, y, z)
}
return device.device.RegisterCallback(uint8(FunctionCallbackLinearAcceleration), wrapper)
}
// Remove a registered Linear Acceleration callback.
func (device *IMUV3Bricklet) DeregisterLinearAccelerationCallback(registrationId uint64) {
device.device.DeregisterCallback(uint8(FunctionCallbackLinearAcceleration), registrationId)
}
// This callback is triggered periodically with the period that is set by
// SetGravityVectorCallbackConfiguration. The parameters gravity vector
// for the x, y and z axis.
func (device *IMUV3Bricklet) RegisterGravityVectorCallback(fn func(int16, int16, int16)) uint64 {
wrapper := func(byteSlice []byte) {
var header PacketHeader
header.FillFromBytes(byteSlice)
if header.Length != 14 {
return
}
buf := bytes.NewBuffer(byteSlice[8:])
var x int16
var y int16
var z int16
binary.Read(buf, binary.LittleEndian, &x)
binary.Read(buf, binary.LittleEndian, &y)
binary.Read(buf, binary.LittleEndian, &z)
fn(x, y, z)
}
return device.device.RegisterCallback(uint8(FunctionCallbackGravityVector), wrapper)
}
// Remove a registered Gravity Vector callback.
func (device *IMUV3Bricklet) DeregisterGravityVectorCallback(registrationId uint64) {
device.device.DeregisterCallback(uint8(FunctionCallbackGravityVector), registrationId)
}
// This callback is triggered periodically with the period that is set by
// SetOrientationCallbackConfiguration. The parameters are the orientation
// (heading (yaw), roll, pitch) of the IMU Brick in Euler angles. See
// GetOrientation for details.
func (device *IMUV3Bricklet) RegisterOrientationCallback(fn func(int16, int16, int16)) uint64 {
wrapper := func(byteSlice []byte) {
var header PacketHeader
header.FillFromBytes(byteSlice)
if header.Length != 14 {
return
}
buf := bytes.NewBuffer(byteSlice[8:])
var heading int16
var roll int16
var pitch int16
binary.Read(buf, binary.LittleEndian, &heading)
binary.Read(buf, binary.LittleEndian, &roll)
binary.Read(buf, binary.LittleEndian, &pitch)
fn(heading, roll, pitch)
}
return device.device.RegisterCallback(uint8(FunctionCallbackOrientation), wrapper)
}
// Remove a registered Orientation callback.
func (device *IMUV3Bricklet) DeregisterOrientationCallback(registrationId uint64) {
device.device.DeregisterCallback(uint8(FunctionCallbackOrientation), registrationId)
}
// This callback is triggered periodically with the period that is set by
// SetQuaternionCallbackConfiguration. The parameters are the orientation
// (w, x, y, z) of the IMU Brick in quaternions. See GetQuaternion
// for details.
func (device *IMUV3Bricklet) RegisterQuaternionCallback(fn func(int16, int16, int16, int16)) uint64 {
wrapper := func(byteSlice []byte) {
var header PacketHeader
header.FillFromBytes(byteSlice)
if header.Length != 16 {
return
}
buf := bytes.NewBuffer(byteSlice[8:])
var w int16
var x int16
var y int16
var z int16
binary.Read(buf, binary.LittleEndian, &w)
binary.Read(buf, binary.LittleEndian, &x)
binary.Read(buf, binary.LittleEndian, &y)
binary.Read(buf, binary.LittleEndian, &z)
fn(w, x, y, z)
}
return device.device.RegisterCallback(uint8(FunctionCallbackQuaternion), wrapper)
}
// Remove a registered Quaternion callback.
func (device *IMUV3Bricklet) DeregisterQuaternionCallback(registrationId uint64) {
device.device.DeregisterCallback(uint8(FunctionCallbackQuaternion), registrationId)
}
// This callback is triggered periodically with the period that is set by
// SetAllDataCallbackConfiguration. The parameters are as for
// GetAllData.
func (device *IMUV3Bricklet) RegisterAllDataCallback(fn func([3]int16, [3]int16, [3]int16, [3]int16, [4]int16, [3]int16, [3]int16, int8, uint8)) uint64 {
wrapper := func(byteSlice []byte) {
var header PacketHeader
header.FillFromBytes(byteSlice)
if header.Length != 54 {
return
}
buf := bytes.NewBuffer(byteSlice[8:])
var acceleration [3]int16
var magneticField [3]int16
var angularVelocity [3]int16
var eulerAngle [3]int16
var quaternion [4]int16
var linearAcceleration [3]int16
var gravityVector [3]int16
var temperature int8
var calibrationStatus uint8
binary.Read(buf, binary.LittleEndian, &acceleration)
binary.Read(buf, binary.LittleEndian, &magneticField)
binary.Read(buf, binary.LittleEndian, &angularVelocity)
binary.Read(buf, binary.LittleEndian, &eulerAngle)
binary.Read(buf, binary.LittleEndian, &quaternion)
binary.Read(buf, binary.LittleEndian, &linearAcceleration)
binary.Read(buf, binary.LittleEndian, &gravityVector)
binary.Read(buf, binary.LittleEndian, &temperature)
binary.Read(buf, binary.LittleEndian, &calibrationStatus)
fn(acceleration, magneticField, angularVelocity, eulerAngle, quaternion, linearAcceleration, gravityVector, temperature, calibrationStatus)
}
return device.device.RegisterCallback(uint8(FunctionCallbackAllData), wrapper)
}
// Remove a registered All Data callback.
func (device *IMUV3Bricklet) DeregisterAllDataCallback(registrationId uint64) {
device.device.DeregisterCallback(uint8(FunctionCallbackAllData), registrationId)
}
// Returns the calibrated acceleration from the accelerometer for the
// x, y and z axis. The acceleration is in the range configured with
// SetSensorConfiguration.
//
// If you want to get the acceleration periodically, it is recommended
// to use the RegisterAccelerationCallback callback and set the period with
// SetAccelerationCallbackConfiguration.
func (device *IMUV3Bricklet) GetAcceleration() (x int16, y int16, z int16, err error) {
var buf bytes.Buffer
resultBytes, err := device.device.Get(uint8(FunctionGetAcceleration), buf.Bytes())
if err != nil {
return x, y, z, err
}
if len(resultBytes) > 0 {
var header PacketHeader
header.FillFromBytes(resultBytes)
if header.Length != 14 {
return x, y, z, fmt.Errorf("Received packet of unexpected size %d, instead of %d", header.Length, 14)
}
if header.ErrorCode != 0 {
return x, y, z, DeviceError(header.ErrorCode)
}
resultBuf := bytes.NewBuffer(resultBytes[8:])
binary.Read(resultBuf, binary.LittleEndian, &x)
binary.Read(resultBuf, binary.LittleEndian, &y)
binary.Read(resultBuf, binary.LittleEndian, &z)
}
return x, y, z, nil
}
// Returns the calibrated magnetic field from the magnetometer for the
// x, y and z axis.
//
// If you want to get the magnetic field periodically, it is recommended
// to use the RegisterMagneticFieldCallback callback and set the period with
// SetMagneticFieldCallbackConfiguration.
func (device *IMUV3Bricklet) GetMagneticField() (x int16, y int16, z int16, err error) {
var buf bytes.Buffer
resultBytes, err := device.device.Get(uint8(FunctionGetMagneticField), buf.Bytes())
if err != nil {
return x, y, z, err
}
if len(resultBytes) > 0 {
var header PacketHeader
header.FillFromBytes(resultBytes)
if header.Length != 14 {
return x, y, z, fmt.Errorf("Received packet of unexpected size %d, instead of %d", header.Length, 14)
}
if header.ErrorCode != 0 {
return x, y, z, DeviceError(header.ErrorCode)
}
resultBuf := bytes.NewBuffer(resultBytes[8:])
binary.Read(resultBuf, binary.LittleEndian, &x)
binary.Read(resultBuf, binary.LittleEndian, &y)
binary.Read(resultBuf, binary.LittleEndian, &z)
}
return x, y, z, nil
}
// Returns the calibrated angular velocity from the gyroscope for the
// x, y and z axis. The angular velocity is in the range configured with
// SetSensorConfiguration.
//
// If you want to get the angular velocity periodically, it is recommended
// to use the RegisterAngularVelocityCallback acallback nd set the period with
// SetAngularVelocityCallbackConfiguration.
func (device *IMUV3Bricklet) GetAngularVelocity() (x int16, y int16, z int16, err error) {
var buf bytes.Buffer
resultBytes, err := device.device.Get(uint8(FunctionGetAngularVelocity), buf.Bytes())
if err != nil {
return x, y, z, err
}
if len(resultBytes) > 0 {
var header PacketHeader
header.FillFromBytes(resultBytes)
if header.Length != 14 {
return x, y, z, fmt.Errorf("Received packet of unexpected size %d, instead of %d", header.Length, 14)
}
if header.ErrorCode != 0 {
return x, y, z, DeviceError(header.ErrorCode)
}
resultBuf := bytes.NewBuffer(resultBytes[8:])
binary.Read(resultBuf, binary.LittleEndian, &x)
binary.Read(resultBuf, binary.LittleEndian, &y)
binary.Read(resultBuf, binary.LittleEndian, &z)
}
return x, y, z, nil
}
// Returns the temperature of the IMU Brick.
// The temperature is measured in the core of the BNO055 IC, it is not the
// ambient temperature
func (device *IMUV3Bricklet) GetTemperature() (temperature int8, err error) {
var buf bytes.Buffer
resultBytes, err := device.device.Get(uint8(FunctionGetTemperature), buf.Bytes())
if err != nil {
return temperature, err
}
if len(resultBytes) > 0 {
var header PacketHeader
header.FillFromBytes(resultBytes)
if header.Length != 9 {
return temperature, fmt.Errorf("Received packet of unexpected size %d, instead of %d", header.Length, 9)
}
if header.ErrorCode != 0 {
return temperature, DeviceError(header.ErrorCode)
}
resultBuf := bytes.NewBuffer(resultBytes[8:])
binary.Read(resultBuf, binary.LittleEndian, &temperature)
}
return temperature, nil
}
// Returns the current orientation (heading, roll, pitch) of the IMU Brick as
// independent Euler angles. Note that Euler angles always
// experience a https://en.wikipedia.org/wiki/Gimbal_lock.
// We recommend that you use quaternions instead, if you need the absolute
// orientation.
//
// If you want to get the orientation periodically, it is recommended
// to use the RegisterOrientationCallback callback and set the period with
// SetOrientationCallbackConfiguration.
func (device *IMUV3Bricklet) GetOrientation() (heading int16, roll int16, pitch int16, err error) {
var buf bytes.Buffer
resultBytes, err := device.device.Get(uint8(FunctionGetOrientation), buf.Bytes())
if err != nil {
return heading, roll, pitch, err
}
if len(resultBytes) > 0 {
var header PacketHeader
header.FillFromBytes(resultBytes)
if header.Length != 14 {
return heading, roll, pitch, fmt.Errorf("Received packet of unexpected size %d, instead of %d", header.Length, 14)
}
if header.ErrorCode != 0 {
return heading, roll, pitch, DeviceError(header.ErrorCode)
}
resultBuf := bytes.NewBuffer(resultBytes[8:])
binary.Read(resultBuf, binary.LittleEndian, &heading)
binary.Read(resultBuf, binary.LittleEndian, &roll)
binary.Read(resultBuf, binary.LittleEndian, &pitch)
}
return heading, roll, pitch, nil
}
// Returns the linear acceleration of the IMU Brick for the
// x, y and z axis. The acceleration is in the range configured with
// SetSensorConfiguration.
//
// The linear acceleration is the acceleration in each of the three
// axis of the IMU Brick with the influences of gravity removed.
//
// It is also possible to get the gravity vector with the influence of linear
// acceleration removed, see GetGravityVector.
//
// If you want to get the linear acceleration periodically, it is recommended
// to use the RegisterLinearAccelerationCallback callback and set the period with
// SetLinearAccelerationCallbackConfiguration.
func (device *IMUV3Bricklet) GetLinearAcceleration() (x int16, y int16, z int16, err error) {
var buf bytes.Buffer
resultBytes, err := device.device.Get(uint8(FunctionGetLinearAcceleration), buf.Bytes())
if err != nil {
return x, y, z, err
}
if len(resultBytes) > 0 {
var header PacketHeader
header.FillFromBytes(resultBytes)
if header.Length != 14 {
return x, y, z, fmt.Errorf("Received packet of unexpected size %d, instead of %d", header.Length, 14)
}
if header.ErrorCode != 0 {
return x, y, z, DeviceError(header.ErrorCode)
}
resultBuf := bytes.NewBuffer(resultBytes[8:])
binary.Read(resultBuf, binary.LittleEndian, &x)
binary.Read(resultBuf, binary.LittleEndian, &y)
binary.Read(resultBuf, binary.LittleEndian, &z)
}
return x, y, z, nil
}
// Returns the current gravity vector of the IMU Brick for the
// x, y and z axis.
//
// The gravity vector is the acceleration that occurs due to gravity.
// Influences of additional linear acceleration are removed.
//
// It is also possible to get the linear acceleration with the influence
// of gravity removed, see GetLinearAcceleration.
//
// If you want to get the gravity vector periodically, it is recommended
// to use the RegisterGravityVectorCallback callback and set the period with
// SetGravityVectorCallbackConfiguration.
func (device *IMUV3Bricklet) GetGravityVector() (x int16, y int16, z int16, err error) {
var buf bytes.Buffer
resultBytes, err := device.device.Get(uint8(FunctionGetGravityVector), buf.Bytes())
if err != nil {
return x, y, z, err
}
if len(resultBytes) > 0 {
var header PacketHeader
header.FillFromBytes(resultBytes)
if header.Length != 14 {
return x, y, z, fmt.Errorf("Received packet of unexpected size %d, instead of %d", header.Length, 14)
}
if header.ErrorCode != 0 {
return x, y, z, DeviceError(header.ErrorCode)
}
resultBuf := bytes.NewBuffer(resultBytes[8:])
binary.Read(resultBuf, binary.LittleEndian, &x)
binary.Read(resultBuf, binary.LittleEndian, &y)
binary.Read(resultBuf, binary.LittleEndian, &z)
}
return x, y, z, nil
}
// Returns the current orientation (w, x, y, z) of the IMU Brick as
// https://en.wikipedia.org/wiki/Quaternions_and_spatial_rotation.
//
// You have to divide the return values by 16383 (14 bit) to get
// the usual range of -1.0 to +1.0 for quaternions.
//
// If you want to get the quaternions periodically, it is recommended
// to use the RegisterQuaternionCallback callback and set the period with
// SetQuaternionCallbackConfiguration.
func (device *IMUV3Bricklet) GetQuaternion() (w int16, x int16, y int16, z int16, err error) {
var buf bytes.Buffer
resultBytes, err := device.device.Get(uint8(FunctionGetQuaternion), buf.Bytes())
if err != nil {
return w, x, y, z, err
}
if len(resultBytes) > 0 {
var header PacketHeader
header.FillFromBytes(resultBytes)
if header.Length != 16 {
return w, x, y, z, fmt.Errorf("Received packet of unexpected size %d, instead of %d", header.Length, 16)
}
if header.ErrorCode != 0 {
return w, x, y, z, DeviceError(header.ErrorCode)
}
resultBuf := bytes.NewBuffer(resultBytes[8:])
binary.Read(resultBuf, binary.LittleEndian, &w)
binary.Read(resultBuf, binary.LittleEndian, &x)
binary.Read(resultBuf, binary.LittleEndian, &y)
binary.Read(resultBuf, binary.LittleEndian, &z)
}
return w, x, y, z, nil
}
// Return all of the available data of the IMU Brick.
//
// * acceleration (see GetAcceleration)
// * magnetic field (see GetMagneticField)
// * angular velocity (see GetAngularVelocity)
// * Euler angles (see GetOrientation)
// * quaternion (see GetQuaternion)
// * linear acceleration (see GetLinearAcceleration)
// * gravity vector (see GetGravityVector)
// * temperature (see GetTemperature)
// * calibration status (see below)
//
// The calibration status consists of four pairs of two bits. Each pair
// of bits represents the status of the current calibration.
//
// * bit 0-1: Magnetometer
// * bit 2-3: Accelerometer
// * bit 4-5: Gyroscope
// * bit 6-7: System
//
// A value of 0 means for not calibrated and a value of 3 means
// fully calibrated. In your program you should always be able to
// ignore the calibration status, it is used by the calibration
// window of the Brick Viewer and it can be ignored after the first
// calibration. See the documentation in the calibration window for
// more information regarding the calibration of the IMU Brick.
//
// If you want to get the data periodically, it is recommended
// to use the RegisterAllDataCallback callback and set the period with
// SetAllDataCallbackConfiguration.
func (device *IMUV3Bricklet) GetAllData() (acceleration [3]int16, magneticField [3]int16, angularVelocity [3]int16, eulerAngle [3]int16, quaternion [4]int16, linearAcceleration [3]int16, gravityVector [3]int16, temperature int8, calibrationStatus uint8, err error) {
var buf bytes.Buffer
resultBytes, err := device.device.Get(uint8(FunctionGetAllData), buf.Bytes())
if err != nil {
return acceleration, magneticField, angularVelocity, eulerAngle, quaternion, linearAcceleration, gravityVector, temperature, calibrationStatus, err
}
if len(resultBytes) > 0 {
var header PacketHeader
header.FillFromBytes(resultBytes)
if header.Length != 54 {
return acceleration, magneticField, angularVelocity, eulerAngle, quaternion, linearAcceleration, gravityVector, temperature, calibrationStatus, fmt.Errorf("Received packet of unexpected size %d, instead of %d", header.Length, 54)
}
if header.ErrorCode != 0 {
return acceleration, magneticField, angularVelocity, eulerAngle, quaternion, linearAcceleration, gravityVector, temperature, calibrationStatus, DeviceError(header.ErrorCode)
}
resultBuf := bytes.NewBuffer(resultBytes[8:])
binary.Read(resultBuf, binary.LittleEndian, &acceleration)
binary.Read(resultBuf, binary.LittleEndian, &magneticField)
binary.Read(resultBuf, binary.LittleEndian, &angularVelocity)
binary.Read(resultBuf, binary.LittleEndian, &eulerAngle)
binary.Read(resultBuf, binary.LittleEndian, &quaternion)
binary.Read(resultBuf, binary.LittleEndian, &linearAcceleration)
binary.Read(resultBuf, binary.LittleEndian, &gravityVector)
binary.Read(resultBuf, binary.LittleEndian, &temperature)
binary.Read(resultBuf, binary.LittleEndian, &calibrationStatus)
}
return acceleration, magneticField, angularVelocity, eulerAngle, quaternion, linearAcceleration, gravityVector, temperature, calibrationStatus, nil
}
// A call of this function saves the current calibration to be used
// as a starting point for the next restart of continuous calibration
// of the IMU Brick.
//
// A return value of *true* means that the calibration could be used and
// *false* means that it could not be used (this happens if the calibration
// status is not fully calibrated).
//
// This function is used by the calibration window of the Brick Viewer, you
// should not need to call it in your program.
func (device *IMUV3Bricklet) SaveCalibration() (calibrationDone bool, err error) {
var buf bytes.Buffer
resultBytes, err := device.device.Get(uint8(FunctionSaveCalibration), buf.Bytes())
if err != nil {
return calibrationDone, err
}
if len(resultBytes) > 0 {
var header PacketHeader
header.FillFromBytes(resultBytes)
if header.Length != 9 {
return calibrationDone, fmt.Errorf("Received packet of unexpected size %d, instead of %d", header.Length, 9)
}
if header.ErrorCode != 0 {
return calibrationDone, DeviceError(header.ErrorCode)
}
resultBuf := bytes.NewBuffer(resultBytes[8:])
binary.Read(resultBuf, binary.LittleEndian, &calibrationDone)
}
return calibrationDone, nil
}
// Sets the available sensor configuration for the Magnetometer, Gyroscope and
// Accelerometer. The Accelerometer Range is user selectable in all fusion modes,
// all other configurations are auto-controlled in fusion mode.
//
// Associated constants:
//
// * MagnetometerRate2Hz
// * MagnetometerRate6Hz
// * MagnetometerRate8Hz
// * MagnetometerRate10Hz
// * MagnetometerRate15Hz
// * MagnetometerRate20Hz
// * MagnetometerRate25Hz
// * MagnetometerRate30Hz
// * GyroscopeRange2000DPS
// * GyroscopeRange1000DPS
// * GyroscopeRange500DPS
// * GyroscopeRange250DPS
// * GyroscopeRange125DPS
// * GyroscopeBandwidth523Hz
// * GyroscopeBandwidth230Hz
// * GyroscopeBandwidth116Hz
// * GyroscopeBandwidth47Hz
// * GyroscopeBandwidth23Hz
// * GyroscopeBandwidth12Hz
// * GyroscopeBandwidth64Hz
// * GyroscopeBandwidth32Hz
// * AccelerometerRange2G
// * AccelerometerRange4G
// * AccelerometerRange8G
// * AccelerometerRange16G
// * AccelerometerBandwidth781Hz
// * AccelerometerBandwidth1563Hz
// * AccelerometerBandwidth3125Hz
// * AccelerometerBandwidth625Hz
// * AccelerometerBandwidth125Hz
// * AccelerometerBandwidth250Hz
// * AccelerometerBandwidth500Hz
// * AccelerometerBandwidth1000Hz