-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathKMConnectorBrowser.js
3605 lines (3290 loc) · 357 KB
/
KMConnectorBrowser.js
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
/******/ (function(modules) { // webpackBootstrap
/******/ // The module cache
/******/ var installedModules = {};
/******/
/******/ // The require function
/******/ function __webpack_require__(moduleId) {
/******/
/******/ // Check if module is in cache
/******/ if(installedModules[moduleId]) {
/******/ return installedModules[moduleId].exports;
/******/ }
/******/ // Create a new module (and put it into the cache)
/******/ var module = installedModules[moduleId] = {
/******/ i: moduleId,
/******/ l: false,
/******/ exports: {}
/******/ };
/******/
/******/ // Execute the module function
/******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__);
/******/
/******/ // Flag the module as loaded
/******/ module.l = true;
/******/
/******/ // Return the exports of the module
/******/ return module.exports;
/******/ }
/******/
/******/
/******/ // expose the modules object (__webpack_modules__)
/******/ __webpack_require__.m = modules;
/******/
/******/ // expose the module cache
/******/ __webpack_require__.c = installedModules;
/******/
/******/ // define getter function for harmony exports
/******/ __webpack_require__.d = function(exports, name, getter) {
/******/ if(!__webpack_require__.o(exports, name)) {
/******/ Object.defineProperty(exports, name, {
/******/ configurable: false,
/******/ enumerable: true,
/******/ get: getter
/******/ });
/******/ }
/******/ };
/******/
/******/ // getDefaultExport function for compatibility with non-harmony modules
/******/ __webpack_require__.n = function(module) {
/******/ var getter = module && module.__esModule ?
/******/ function getDefault() { return module['default']; } :
/******/ function getModuleExports() { return module; };
/******/ __webpack_require__.d(getter, 'a', getter);
/******/ return getter;
/******/ };
/******/
/******/ // Object.prototype.hasOwnProperty.call
/******/ __webpack_require__.o = function(object, property) { return Object.prototype.hasOwnProperty.call(object, property); };
/******/
/******/ // __webpack_public_path__
/******/ __webpack_require__.p = "";
/******/
/******/ // Load entry module and return exports
/******/ return __webpack_require__(__webpack_require__.s = 3);
/******/ })
/************************************************************************/
/******/ ([
/* 0 */
/***/ (function(module, exports, __webpack_require__) {
"use strict";
/***
* KMStructures.js
* Created by Harada Hiroshi on 2017/12/07.
*
* Copyright (c) 2017 Keigan Inc. https://keigan-motor.com/
* This software is released under the MIT License.
* http://opensource.org/licenses/mit-license.php
*/
let KMUtl = __webpack_require__(1);
/**
* @classdesc 構造体の基底クラス
* @ignore
*/
class KMStructureBase{
constructor(){
}
/**
* 同じ値を持つかの比較
* @param {object} tar 比較する構造体
* @returns {boolean} 結果
*/
EQ (tar) {
if(! tar ){return false;}
if(this.constructor===tar.constructor){
if(this.GetValArray){
return this.GetValArray().toString()===tar.GetValArray().toString();
}else if(this.GetValObj){
return JSON.stringify(this.GetValObj())===JSON.stringify(tar.GetValObj());// bad::遅い
}
}
return false;
}
/**
* 複製
* @returns {object} 複製された構造体
*/
Clone () {
return Object.assign(new this.constructor(),this);
}
/**
* 値の取得(Obj)
* @returns {object}
*/
GetValObj () {
return Object.assign({},this);
}
/**
* 値の取得(配列)
* @returns {Array}
*/
GetValArray () {
let k=Object.keys(this);
let r=[];
for(let i=0;i<k.length;i++){
r.push(this[k[i]]);
}
return r;
}
/**
* 値の一括設定(Obj)
* @param {object} propsObj 設定するプロパティ
*/
SetValObj (propsObj) {
if(typeof propsObj !=="object"){return;}
let keys=Object.keys(propsObj);
for(let k=0;k<keys.length;k++){
let pn=keys[k];
if(this.hasOwnProperty(pn)){
this[pn]=propsObj[pn];
}
}
}
}
/**
* @classdesc XY座標の構造体オブジェクト
* @ignore
*/
class KMVector2 extends KMStructureBase {
/**
* constructor
* @extends KMStructureBase
* @param {number} x
* @param {number} y
*
* @instance
*/
constructor (x=0, y=0) {
super();
this.x = x;
this.y = y;
}
/**
* 相対位置へ移動
* @param {number} dx
* @param {number} dy
*/
Move (dx =0, dy = 0) {
this.x += dx;
this.y += dy;
}
/**
* 2点間の距離
* @param {KMVector2} vector2
* @returns {number}
*/
Distance (vector2) {
if (!(vector2 instanceof KMVector2)) {return;}
return Math.sqrt(Math.pow((this.x-vector2.x),2) + Math.pow((this.y-vector2.y),2));
}
/**
* 2点間の角度
* @param {KMVector2} vector2
* @returns {number}
*/
Radian (vector2) {
if (!(vector2 instanceof KMVector2)) {return;}
return Math.atan2(this.y-vector2.y,this.x-vector2.x);
}
/**
* 0,0からの距離
* @returns {number}
*/
DistanceFromZero() {
return Math.sqrt(Math.pow(this.x,2) + Math.pow(this.y,2));
}
/**
* 0,0からの角度
* @returns {number} radian
*/
RadianFromZero() {
return Math.atan2(this.y,this.x);
}
}
/**
* @classdesc ジャイロセンサー値
*/
class KMImuState extends KMStructureBase {
/**
* constructor
* @extends KMStructureBase
* @param {number} accelX 加速度(x) [± 1]
* @param {number} accelY 加速度(y) [± 1]
* @param {number} accelZ 加速度(z) [± 1]
* @param {number} temp 温度 C
* @param {number} gyroX 角速度(x) [± 1]
* @param {number} gyroY 角速度(y) [± 1]
* @param {number} gyroZ 角速度(z) [± 1]
* @instance
*/
constructor (accelX, accelY, accelZ, temp, gyroX, gyroY, gyroZ ) {
super();
this.accelX= KMUtl.toNumber(accelX);
this.accelY= KMUtl.toNumber(accelY);
this.accelZ= KMUtl.toNumber(accelZ);
this.temp= KMUtl.toNumber(temp);
this.gyroX= KMUtl.toNumber(gyroX);
this.gyroY= KMUtl.toNumber(gyroY);
this.gyroZ= KMUtl.toNumber(gyroZ);
}
}
/**
* KEIGANモーターLED 点灯・色状態
* State MOTOR_LED_STATE
* colorR 0-255
* colorG 0-255
* colorB 0-255
* */
/**
* @classdesc モーターLED 点灯・色状態
*/
class KMLedState extends KMStructureBase {
/**
* モーターのLED状態
* @readonly
* @enum {number}
* @property {number} MOTOR_LED_STATE_OFF - 0:LED消灯
* @property {number} MOTOR_LED_STATE_ON_SOLID - 1:LED点灯
* @property {number} MOTOR_LED_STATE_ON_FLASH - 2:LED点滅(一定間隔で点滅)
* @property {number} MOTOR_LED_STATE_ON_DIM - 3:LEDがゆっくり輝度変化する
*/
static get MOTOR_LED_STATE(){
return{
"MOTOR_LED_STATE_OFF":0,
"MOTOR_LED_STATE_ON_SOLID":1,
"MOTOR_LED_STATE_ON_FLASH":2,
"MOTOR_LED_STATE_ON_DIM":3
}
}
/**
* constructor
* @extends KMStructureBase
* @param {KMLedState.MOTOR_LED_STATE} state
* @param {number} colorR int [0-255]
* @param {number} colorG int [0-255]
* @param {number} colorB int [0-255]
* @instance
*/
constructor(state,colorR,colorG,colorB) {
super();
this.state=KMUtl.toNumber(state);
this.colorR=KMUtl.toNumber(colorR);
this.colorG=KMUtl.toNumber(colorG);
this.colorB=KMUtl.toNumber(colorB);
}
}
/**
* @classdesc モーター回転情報
*/
class KMRotState extends KMStructureBase {
/**
* 最大トルク定数
* @readonly
* @type {number}
*/
static get MAX_TORQUE(){
return 0.3;//0.3 N・m
}
/**
* 最大速度定数(rpm)
* @readonly
* @type {number}
*/
static get MAX_SPEED_RPM(){
return 300;//300rpm
}
/**
* 最大速度定数(radian/sec)
* @readonly
* @type {number}
*/
static get MAX_SPEED_RADIAN(){
return KMUtl.rpmToRadianSec(300);
}
/**
* 最大座標定数
* @readonly
* @type {number}
*/
static get MAX_POSITION(){
return 3*Math.pow(10,38);//info::「return 3e+38」はminifyでエラー
//return 3e+38;//radian 4byte float 1.175494 10-38 < 3.402823 10+38
}
/**
* constructor
* @extends KMStructureBase
* @param {number} position 座標
* @param {number} velocity 速度
* @param {number} torque トルク
* @instance
*/
constructor(position, velocity, torque) {
//有効桁数 小数第4位
super();
this.position = Math.floor(KMUtl.toNumber(position)*10000)/10000;
this.velocity = Math.floor(KMUtl.toNumber(velocity)*10000)/10000;
this.torque = Math.floor(KMUtl.toNumber(torque)*10000)/10000;
}
}
/**
* @classdesc デバイス情報
*/
class KMDeviceInfo extends KMStructureBase {
/**
* constructor
* @extends KMStructureBase
* @param {KMMotorCommandKMOne.KM_CONNECT_TYPE} type 接続方式
* @param {string} id デバイスUUID
* @param {string} name モーター名(形式 ID LEDColor)
* @param {boolean} isConnect 接続状態
* @param {string} manufacturerName 製造会社名
* @param {string} hardwareRevision
* @param {string} firmwareRevision
* @instance
*/
constructor(type=0,id="",name="",isConnect=false,manufacturerName=null,hardwareRevision=null,firmwareRevision=null) {
super();
this.type=type;
this.id=id;
this.name=name;
this.isConnect=isConnect;
this.manufacturerName=manufacturerName;
this.hardwareRevision=hardwareRevision;
this.firmwareRevision=firmwareRevision;
}
}
/**
* @classdesc モーターログ情報
*/
class KMMotorLog extends KMStructureBase {
/**
* @param id {number} シーケンスID(ユニーク値)
* @param cmdName {string} 実行コマンド名
* @param cmdID {number} 実行コマンドID
* @param errID {number} エラータイプ
* @param errType {string} エラー種別
* @param errMsg {string} メッセージ内容
* @param info {number}
*/
constructor (id=0,cmdName="",cmdID=0,errID=0,errType="",errMsg="",info=0) {
super();
this.id= id;
this.cmdName=cmdName;
this.cmdID= cmdID;
this.errID=errID;
this.errType= errType;
this.errMsg= errMsg;
this.info= info;
}
}
module.exports = {
KMStructureBase:KMStructureBase,
KMVector2:KMVector2,
//KMVector3:KMVector3,
KMImuState:KMImuState,
KMLedState:KMLedState,
KMRotState:KMRotState,
KMDeviceInfo:KMDeviceInfo,
KMMotorLog:KMMotorLog
};
/***/ }),
/* 1 */
/***/ (function(module, exports, __webpack_require__) {
"use strict";
/***
* KMUtl.js
* CCreated by Harada Hiroshi on 2017/12/07.
*
* Copyright (c) 2017 Keigan Inc. https://keigan-motor.com/
* This software is released under the MIT License.
* http://opensource.org/licenses/mit-license.php
*/
/**
* @classdesc ユーティリティ
*/
class KMUtl{
/**
* 数値にキャストする関数
* 数値以外は0を返す<br>
* Infinityも0とする
* @param {number} val
* @param {number} defaultval valが数値に変換出来ない場合のデフォルト
* @returns {number}
*/
static toNumber(val, defaultval = 0) {
let v = parseFloat(val, 10);
return (!isFinite(v) ? defaultval : v);
};
/**
* 数値にキャストする関数 int固定
* 数値以外は0を返す<br>
* Infinityも0とする
* @param {number} val
* @param {number} defaultval valが数値に変換出来ない場合のデフォルト
* @returns {number}
*/
static toIntNumber(val, defaultval = 0) {
let v = parseInt(val, 10);
return (!isFinite(v) ? defaultval : v);
};
/**
* 角度の単位変換 degree >> radian
* @param {number} degree 度
* @returns {number} radian
*/
static degreeToRadian(degree) {
return degree * 0.017453292519943295;
};
/**
* 角度の単位変換 radian >> degree
* @param {number} radian radian角
* @returns {number} 度
*/
static radianToDegree(radian) {
return radian / 0.017453292519943295;
};
/**
* 速度 rpm ->radian/sec に変換
* @param {number} rpm
* @returns {number} radian/sec
*/
static rpmToRadianSec(rpm) {
//速度 rpm ->radian/sec(Math.PI*2/60)
return rpm * 0.10471975511965977;
};
/**
* 2点間の距離と角度を求める
* @param {number} from_x
* @param {number} from_y
* @param {number} to_x
* @param {number} to_y
* @returns {number}
*/
static twoPointDistanceAngle(from_x, from_y, to_x, to_y) {
let distance = Math.sqrt(Math.pow(from_x - to_x, 2) + Math.pow(from_y - to_y, 2));
let radian = Math.atan2(from_y - to_y, from_x - to_x);
return {dist: distance, radi: radian, deg: KMUtl.radianToDegree(radian)};
};
/**
* コマンドのチェックサムを計算
* @ignore
* @desc 右送り CRC-16-CCITT (x16 + x12 + x5 + 1) Start:0x0000 XOROut:0x0000 ByteOrder:Little-Endian
* @param uint8arrayBuffer
* @returns {number}
* @constructor
*/
static CreateCommandCheckSumCRC16(uint8arrayBuffer){
const crc16table= __crc16table;
let crc = 0;// Start:0x0000
let len=uint8arrayBuffer.length;
for (let i = 0; i < uint8arrayBuffer.length; i++) {
let c = uint8arrayBuffer[i];
crc = (crc >> 8) ^ crc16table[(crc ^ c) & 0x00ff];
}
crc=((crc>>8)&0xFF)|((crc<<8)&0xFF00);// ByteOrder:Little-Endian
//crc=0xFFFF^crc;//XOROut:0x0000
return crc;
}
/**
* @# info:: KMComBLE.jsのDEVICE INFORMATION SERVICEのパースに使用
* utf.js - UTF-8 <=> UTF-16 convertion
*
* @ignore
* @param array
* @returns {string}
* @constructor
*
* @desc
* Copyright (C) 1999 Masanao Izumo <[email protected]>
* Version: 1.0
* LastModified: Dec 25 1999
* This library is free. You can redistribute it and/or modify it.
*/
static Utf8ArrayToStr(array) {
let out, i, len, c;
let char2, char3;
out = "";
len = array.length;
i = 0;
while(i < len) {
c = array[i++];
switch(c >> 4)
{
case 0: case 1: case 2: case 3: case 4: case 5: case 6: case 7:
// 0xxxxxxx
out += String.fromCharCode(c);
break;
case 12: case 13:
// 110x xxxx 10xx xxxx
char2 = array[i++];
out += String.fromCharCode(((c & 0x1F) << 6) | (char2 & 0x3F));
break;
case 14:
// 1110 xxxx 10xx xxxx 10xx xxxx
char2 = array[i++];
char3 = array[i++];
out += String.fromCharCode(((c & 0x0F) << 12) |
((char2 & 0x3F) << 6) |
((char3 & 0x3F) << 0));
break;
}
}
return out;
};
/**
* @# info:: デバッグ用
* uint8Array => UTF-16 Strings convertion
* @ignore
* @param uint8Array
* @returns {string}
* @constructor
*/
static Uint8ArrayToHexStr(uint8Array){
if(!uint8Array instanceof Uint8Array){return;}
let ar=[];
for(let i=0;i<uint8Array.length;i++){
ar.push(uint8Array[i].toString(16));
}
return ar.join(':');
}
/**
* @# info::Uint8のConcat
* Creates a new Uint8Array based on two different ArrayBuffers
* @param {ArrayBuffers} u8Array1 The first buffer.
* @param {ArrayBuffers} u8Array2 The second buffer.
* @return {ArrayBuffers} The new ArrayBuffer created out of the two.
* @ignore
*/
static Uint8ArrayConcat(u8Array1, u8Array2) {
let tmp = new Uint8Array(u8Array1.byteLength + u8Array2.byteLength);
tmp.set(new Uint8Array(u8Array1), 0);
tmp.set(new Uint8Array(u8Array2), u8Array1.byteLength);
return tmp;
}
}
/**
* CreateCommandCheckSumCRC16用 CRCテーブル
* @ignore
* @type {Uint16Array}
* @private
*/
const __crc16table=new Uint16Array([
0 , 0x1189 , 0x2312 , 0x329b , 0x4624 , 0x57ad , 0x6536 , 0x74bf ,
0x8c48 , 0x9dc1 , 0xaf5a , 0xbed3 , 0xca6c , 0xdbe5 , 0xe97e , 0xf8f7 ,
0x1081 , 0x0108 , 0x3393 , 0x221a , 0x56a5 , 0x472c , 0x75b7 , 0x643e ,
0x9cc9 , 0x8d40 , 0xbfdb , 0xae52 , 0xdaed , 0xcb64 , 0xf9ff , 0xe876 ,
0x2102 , 0x308b , 0x0210 , 0x1399 , 0x6726 , 0x76af , 0x4434 , 0x55bd ,
0xad4a , 0xbcc3 , 0x8e58 , 0x9fd1 , 0xeb6e , 0xfae7 , 0xc87c , 0xd9f5 ,
0x3183 , 0x200a , 0x1291 , 0x0318 , 0x77a7 , 0x662e , 0x54b5 , 0x453c ,
0xbdcb , 0xac42 , 0x9ed9 , 0x8f50 , 0xfbef , 0xea66 , 0xd8fd , 0xc974 ,
0x4204 , 0x538d , 0x6116 , 0x709f , 0x0420 , 0x15a9 , 0x2732 , 0x36bb ,
0xce4c , 0xdfc5 , 0xed5e , 0xfcd7 , 0x8868 , 0x99e1 , 0xab7a , 0xbaf3 ,
0x5285 , 0x430c , 0x7197 , 0x601e , 0x14a1 , 0x0528 , 0x37b3 , 0x263a ,
0xdecd , 0xcf44 , 0xfddf , 0xec56 , 0x98e9 , 0x8960 , 0xbbfb , 0xaa72 ,
0x6306 , 0x728f , 0x4014 , 0x519d , 0x2522 , 0x34ab , 0x0630 , 0x17b9 ,
0xef4e , 0xfec7 , 0xcc5c , 0xddd5 , 0xa96a , 0xb8e3 , 0x8a78 , 0x9bf1 ,
0x7387 , 0x620e , 0x5095 , 0x411c , 0x35a3 , 0x242a , 0x16b1 , 0x0738 ,
0xffcf , 0xee46 , 0xdcdd , 0xcd54 , 0xb9eb , 0xa862 , 0x9af9 , 0x8b70 ,
0x8408 , 0x9581 , 0xa71a , 0xb693 , 0xc22c , 0xd3a5 , 0xe13e , 0xf0b7 ,
0x0840 , 0x19c9 , 0x2b52 , 0x3adb , 0x4e64 , 0x5fed , 0x6d76 , 0x7cff ,
0x9489 , 0x8500 , 0xb79b , 0xa612 , 0xd2ad , 0xc324 , 0xf1bf , 0xe036 ,
0x18c1 , 0x0948 , 0x3bd3 , 0x2a5a , 0x5ee5 , 0x4f6c , 0x7df7 , 0x6c7e ,
0xa50a , 0xb483 , 0x8618 , 0x9791 , 0xe32e , 0xf2a7 , 0xc03c , 0xd1b5 ,
0x2942 , 0x38cb , 0x0a50 , 0x1bd9 , 0x6f66 , 0x7eef , 0x4c74 , 0x5dfd ,
0xb58b , 0xa402 , 0x9699 , 0x8710 , 0xf3af , 0xe226 , 0xd0bd , 0xc134 ,
0x39c3 , 0x284a , 0x1ad1 , 0x0b58 , 0x7fe7 , 0x6e6e , 0x5cf5 , 0x4d7c ,
0xc60c , 0xd785 , 0xe51e , 0xf497 , 0x8028 , 0x91a1 , 0xa33a , 0xb2b3 ,
0x4a44 , 0x5bcd , 0x6956 , 0x78df , 0x0c60 , 0x1de9 , 0x2f72 , 0x3efb ,
0xd68d , 0xc704 , 0xf59f , 0xe416 , 0x90a9 , 0x8120 , 0xb3bb , 0xa232 ,
0x5ac5 , 0x4b4c , 0x79d7 , 0x685e , 0x1ce1 , 0x0d68 , 0x3ff3 , 0x2e7a ,
0xe70e , 0xf687 , 0xc41c , 0xd595 , 0xa12a , 0xb0a3 , 0x8238 , 0x93b1 ,
0x6b46 , 0x7acf , 0x4854 , 0x59dd , 0x2d62 , 0x3ceb , 0x0e70 , 0x1ff9 ,
0xf78f , 0xe606 , 0xd49d , 0xc514 , 0xb1ab , 0xa022 , 0x92b9 , 0x8330 ,
0x7bc7 , 0x6a4e , 0x58d5 , 0x495c , 0x3de3 , 0x2c6a , 0x1ef1 , 0x0f78
]);
module.exports = KMUtl;
/***/ }),
/* 2 */
/***/ (function(module, exports, __webpack_require__) {
"use strict";
/***
* KMComWebBLE.js
* version 0.1.0 alpha
* Created by Harada Hiroshi on 2017/12/07.
*
* Copyright (c) 2017 Keigan Inc. https://keigan-motor.com/
* This software is released under the MIT License.
* http://opensource.org/licenses/mit-license.php
*/
const KMUtl = __webpack_require__(1);
const KMComBase = __webpack_require__(6);
const KMStructures=__webpack_require__(0);
/**
* @classdesc ブラウザ用WebBluetooh通信クラス(chrome依存)
* @ignore
*/
class KMComWebBLE extends KMComBase{
/********************************************
* 定数
********************************************/
/**
* constructor
*/
constructor(){
super();
this._deviceInfo.type="WEBBLE";
this._characteristics={};
this._bleSendingQue=Promise.resolve(true);
this._queCount=0;
//サービスUUID
this._MOTOR_BLE_SERVICE_UUID='f140ea35-8936-4d35-a0ed-dfcd795baa8c';
//キャラクタリスティクスUUID
this._MOTOR_BLE_CRS={
'MOTOR_TX':'f1400001-8936-4d35-a0ed-dfcd795baa8c',//旧 MOTOR_CONTROL
//'MOTOR_LED':'f1400003-8936-4d35-a0ed-dfcd795baa8c',//モーターのLEDを取り扱う info:: MOTOR_CONTROL::bleLedで代用
'MOTOR_MEASUREMENT':'f1400004-8936-4d35-a0ed-dfcd795baa8c',
'MOTOR_IMU_MEASUREMENT':'f1400005-8936-4d35-a0ed-dfcd795baa8c',
'MOTOR_RX':'f1400006-8936-4d35-a0ed-dfcd795baa8c',//旧 MOTOR_SETTING
};
this._DEVICE_INFORMATION_SERVICE_UUIDS={
"Service":0x180a
,"ManufacturerNameString":0x2a29
,"HardwareRevisionString":0x2a27
,"FirmwareRevisionString":0x2a26
};
/**
* BLE切断時
* @param eve
* @private
*/
this._onBleConnectionLost=(eve)=>{
this._deviceInfo.isConnect=false;
this._peripheral=null;
this._statusChange_isConnect(false);
};
/**
* @#---------------------------------------------------------
* モーター回転情報受信
* @param eve
* @private
* @#---------------------------------------------------------
* Notify Value
* byte[0]-byte[3] byte[4]-byte[7] byte[8]-byte[11]
* float * float * float *
* position:radians speed:radians/second torque:N・m
*/
this._onBleMotorMeasurement=(eve)=>{
if(!eve||!eve.target){return;}
let dv = eve.target.value;
if(!(dv instanceof DataView)){return;}//info::web bluetoohのみ node.jsはinstanceof Buffer
let position=dv.getFloat32(0,false);
let velocity=dv.getFloat32(4,false);
let torque=dv.getFloat32(8,false);
this._onMotorMeasurementCB(new KMStructures.KMRotState(position,velocity,torque));
};
/**
* @#---------------------------------------------------------
* モーターIMU情報受信
* @param eve
* @private
* @#
* -----------------------------------------------------------
* Notify Value
* accel_x, accel_y, accel_z, temp, gyro_x, gyro_y, gyro_z が全て返ってくる。取得間隔は100ms 固定
* byte(BigEndian) [0][1] [2][3] [4][5] [6][7] [8][9] [10][11] [12][13]
* int16_t int16_t int16_t int16_t int16_t int16_t int16_t
* accel-x accel-y accel-z temp gyro-x gyro-y gyro-z
*
* int16_t:-32,768〜32,768
* 机の上にモーターを置いた場合、加速度 z = 16000 程度となる。(重力方向のため)
*
* 単位変換)---------------------------------------------------------
* 加速度 value [G] = raw_value * 2 / 32,767
* 温度 value [℃] = raw_value / 333.87 + 21.00
* 角速度
* value [degree/second] = raw_value * 250 / 32,767
* value [radians/second] = raw_value * 0.00013316211
*
*/
this._onBleImuMeasurement=(eve)=>{
if(!eve||!eve.target){return;}
let dv = eve.target.value;
let tempCalibration=-5.7;//温度校正値
if(!(dv instanceof DataView)){return;}//info::web bluetoohのみ node.jsはinstanceof Buffer
//単位を扱い易いように変換
let accelX=dv.getInt16(0,false)*2/32767;
let accelY=dv.getInt16(2,false)*2/32767;
let accelZ=dv.getInt16(4,false)*2/32767;
let temp=(dv.getInt16(6,false)) / 333.87 + 21.00+tempCalibration;
let gyroX=dv.getInt16(8,false)*250/32767;
let gyroY=dv.getInt16(10,false)*250/32767;
let gyroZ=dv.getInt16(12,false)*250/32767;
this._onImuMeasurementCB(new KMStructures.KMImuState(accelX,accelY,accelZ,temp,gyroX,gyroY,gyroZ));
};
/**
* @#---------------------------------------------------------
* モーターログ情報取得
* @param {Buffer} data
* @private
* @#---------------------------------------------------------
* Notify value
* byte[0] byte[1-2] byte[3] byte[4-7] byte[8-11] byte[12-13]
* uint8_t:tx_type uint16_t:id uint8_t:command uint32_t:errorCode uint32_t:info uint16_t:CRC
*/
this._onBleMotorLog=(eve)=>{
if(!eve||!eve.target){return;}
let dv = eve.target.value;
if(!(dv instanceof DataView)){return;}//info::web bluetoohのみ node.jsはinstanceof Buffer
let txType=dv.getUint8(0);//エラーログタイプ:0xBE固定
if(txType!==0xBE){return;}
let id=dv.getUint16(1,false);//送信ID
let cmdID=dv.getUint8(3,false);
let errCode=dv.getUint32(4,false);
let info=dv.getUint32(8,false);
//ログ取得
this._onMotorLogCB(new KMStructures.KMMotorLog(id,null,cmdID,this._MOTOR_LOG_ERRORCODE[errCode].id,this._MOTOR_LOG_ERRORCODE[errCode].type,this._MOTOR_LOG_ERRORCODE[errCode].msg,info));
};
/**
* @#---------------------------------------------------------
* モーター設定情報取得
* @param eve
* @private
* @#---------------------------------------------------------
* Notify value
* byte[0] byte[1] byte[2] byte[3] byte[4]以降 byte[n-2] byte[n-1]
* uint8_t:tx_type uint16_t:id uint8_t:register uint8_t:value uint16_t:CRC
* 0x40 uint16_t (2byte) 0~65535 レジスタコマンド レジスタの値(コマンドによって変わる) uint16_t (2byte) 0~65535
*/
this._onBleMotorSetting=(eve)=>{
if(!eve||!eve.target){return;}
let dv = eve.target.value;//5+nバイト
let uint8Array=new Uint8Array(dv.buffer);//info::一旦コピーする必要がある
if(!(dv instanceof DataView)){return;}//info::web bluetoohのみ node.jsはinstanceof Buffer
//-------------
//データのparse
//-------------
let txLen=dv.byteLength;
let txType=dv.getUint8(0);//レジスタ情報通知コマンドID 0x40固定
if(txType!==0x40||txLen<5){return;}//レジスタ情報を含まないデータの破棄
let id=dv.getUint16(1,false);//送信ID
let registerCmd=dv.getUint8(3);//レジスタコマンド
let crc=dv.getUint16(txLen-2,false);//CRCの値 最後から2dyte
let res={};
//コマンド別によるレジスタの値の取得[4-n byte]
let startOffset=4;
switch(registerCmd){
case this._MOTOR_RX_READREGISTER_COMMAND.maxSpeed:
res.maxSpeed=dv.getFloat32(startOffset,false);
break;
case this._MOTOR_RX_READREGISTER_COMMAND.minSpeed:
res.minSpeed=dv.getFloat32(startOffset,false);
break;
case this._MOTOR_RX_READREGISTER_COMMAND.curveType:
res.curveType=dv.getUint8(startOffset);
break;
case this._MOTOR_RX_READREGISTER_COMMAND.acc:
res.acc=dv.getFloat32(startOffset,false);
break;
case this._MOTOR_RX_READREGISTER_COMMAND.dec:
res.dec=dv.getFloat32(startOffset,false);
break;
case this._MOTOR_RX_READREGISTER_COMMAND.maxTorque:
res.maxTorque=dv.getFloat32(startOffset,false);
break;
case this._MOTOR_RX_READREGISTER_COMMAND.teachingInterval:
res.teachingInterval=dv.getUint8(startOffset,false);//todo::バイト不足 Uint32->Uint8 2.24
break;
case this._MOTOR_RX_READREGISTER_COMMAND.playbackInterval:
res.playbackInterval=dv.getUint8(startOffset,false);//todo::バイト不足 Uint32->Uint8 2.24
break;
case this._MOTOR_RX_READREGISTER_COMMAND.qCurrentP:
res.qCurrentP=dv.getFloat32(startOffset,false);
break;
case this._MOTOR_RX_READREGISTER_COMMAND.qCurrentI:
res.qCurrentI=dv.getFloat32(startOffset,false);
break;
case this._MOTOR_RX_READREGISTER_COMMAND.qCurrentD:
res.qCurrentD=dv.getFloat32(startOffset,false);
break;
case this._MOTOR_RX_READREGISTER_COMMAND.speedP:
res.speedP=dv.getFloat32(startOffset,false);
break;
case this._MOTOR_RX_READREGISTER_COMMAND.speedI:
res.speedI=dv.getFloat32(startOffset,false);
break;
case this._MOTOR_RX_READREGISTER_COMMAND.speedD:
res.speedD=dv.getFloat32(startOffset,false);
break;
case this._MOTOR_RX_READREGISTER_COMMAND.positionP:
res.positionP=dv.getFloat32(startOffset,false);
break;
case this._MOTOR_RX_READREGISTER_COMMAND.positionI:
res.positionI=dv.getFloat32(startOffset,false);
break;
case this._MOTOR_RX_READREGISTER_COMMAND.positionD:
res.positionD=dv.getFloat32(startOffset,false);
break;
case this._MOTOR_RX_READREGISTER_COMMAND.posControlThreshold:
res.posControlThreshold=dv.getFloat32(startOffset,false);
break;
case this._MOTOR_RX_READREGISTER_COMMAND.motorMeasurementInterval:
res.motorMeasurementInterval=dv.getUint8(startOffset);
break;
case this._MOTOR_RX_READREGISTER_COMMAND.motorMeasurementByDefault:
res.motorMeasurementByDefault=dv.getUint8(startOffset);
break;
case this._MOTOR_RX_READREGISTER_COMMAND.interface:
res.interface=dv.getUint8(startOffset);
break;
case this._MOTOR_RX_READREGISTER_COMMAND.response://todo::値が取得出来ない 2.24
res.response=dv.getUint8(startOffset);
break;
case this._MOTOR_RX_READREGISTER_COMMAND.ownColor:
res.ownColor=('#000000' +Number(dv.getUint8(startOffset)<<16|dv.getUint8(startOffset+1)<<8|dv.getUint8(startOffset+2)).toString(16)).substr(-6);
break;
case this._MOTOR_RX_READREGISTER_COMMAND.iMUMeasurementInterval:
res.iMUMeasurementInterval=dv.getUint8(startOffset);
break;
case this._MOTOR_RX_READREGISTER_COMMAND.iMUMeasurementByDefault:
res.iMUMeasurementByDefault=dv.getUint8(startOffset);
break;
case this._MOTOR_RX_READREGISTER_COMMAND.deviceName:
res.deviceName=String.fromCharCode.apply("", uint8Array.slice(startOffset,-2));//可変バイト文字
break;
case this._MOTOR_RX_READREGISTER_COMMAND.deviceInfo:
res.deviceInfo=String.fromCharCode.apply("", uint8Array.slice(startOffset,-2));//可変バイト文字
break;
case this._MOTOR_RX_READREGISTER_COMMAND.speed:
res.speed=dv.getFloat32(startOffset,false);
break;
case this._MOTOR_RX_READREGISTER_COMMAND.positionOffset:
res.positionOffset=dv.getFloat32(startOffset,false);
break;
case this._MOTOR_RX_READREGISTER_COMMAND.moveTo:
res.moveTo=dv.getFloat32(startOffset,false);
break;
case this._MOTOR_RX_READREGISTER_COMMAND.hold:
res.hold=dv.getFloat32(startOffset,false);
break;
case this._MOTOR_RX_READREGISTER_COMMAND.status:
res.status=dv.getUint8(startOffset);
break;
case this._MOTOR_RX_READREGISTER_COMMAND.tasksetName:
res.tasksetName=String.fromCharCode.apply("", uint8Array.slice(startOffset,-2));//可変バイト文字
break;
case this._MOTOR_RX_READREGISTER_COMMAND.tasksetInfo:
res.tasksetInfo=dv.getUint16(startOffset,false);
break;
case this._MOTOR_RX_READREGISTER_COMMAND.tasksetUsage:
res.tasksetUsage=dv.getUint8(startOffset);
break;
case this._MOTOR_RX_READREGISTER_COMMAND.motionName:
res.motionName=String.fromCharCode.apply("", uint8Array.slice(startOffset,-2));//可変バイト文字
break;
case this._MOTOR_RX_READREGISTER_COMMAND.motionInfo:
res.motionInfo=dv.getUint16(startOffset,false);
break;
case this._MOTOR_RX_READREGISTER_COMMAND.motionUsage:
res.motionUsage=dv.getUint8(startOffset);
break;
case this._MOTOR_RX_READREGISTER_COMMAND.i2CSlaveAddress:
res.i2CSlaveAddress=dv.getUint8(startOffset);
break;
case this._MOTOR_RX_READREGISTER_COMMAND.uartBaudRate:
res.uartBaudRate=dv.getUint8(startOffset);
break;
case this._MOTOR_RX_READREGISTER_COMMAND.led:
res.led={state:dv.getUint8(startOffset),r:dv.getUint8(startOffset+1),g:dv.getUint8(startOffset+2),b:dv.getUint8(startOffset+3)};
break;
case this._MOTOR_RX_READREGISTER_COMMAND.enableCheckSum:
res.enableCheckSum=dv.getUint8(startOffset);
break;
case this._MOTOR_RX_READREGISTER_COMMAND.deviceSerial:
res.deviceSerial=String.fromCharCode.apply("", uint8Array.slice(startOffset,-2));//可変バイト文字
break;
}
//console.log(res);
this._onMotorSettingCB(registerCmd,res);
}
}
/********************************************
* section::公開メソッド
********************************************/
/**
* WebBluetoohでの接続を開始する
*/
connect(){
if (this._peripheral&& this._peripheral.gatt&&this._peripheral.gatt.connected ) {return}
let gat= (this._peripheral&& this._peripheral.gatt )?this._peripheral.gatt :undefined;//再接続用
this._bleConnect(gat).then(obj=>{//info:: resolve({deviceID,deviceName,bleDevice,characteristics});
this._peripheral=obj.bleDevice;
this._deviceInfo.id=this._peripheral.id;
this._deviceInfo.name=this._peripheral.name;
this._deviceInfo.isConnect=this._peripheral.gatt.connected;
this._deviceInfo.manufacturerName=obj.infomation.manufacturerName;
this._deviceInfo.hardwareRevision=obj.infomation.hardwareRevision;
this._deviceInfo.firmwareRevision=obj.infomation.firmwareRevision;
//
this._characteristics=obj.characteristics;
if(!gat){
this._peripheral.removeEventListener('gattserverdisconnected',this._onBleConnectionLost);
this._peripheral.addEventListener('gattserverdisconnected', this._onBleConnectionLost);
if(this._characteristics['MOTOR_MEASUREMENT']){
this._characteristics['MOTOR_MEASUREMENT'].removeEventListener('characteristicvaluechanged',this._onBleMotorMeasurement);
this._characteristics['MOTOR_MEASUREMENT'].addEventListener('characteristicvaluechanged',this._onBleMotorMeasurement);
this._characteristics['MOTOR_MEASUREMENT'].startNotifications().then(obj=>{