Node.js and Browser JavaScript Library for Keigan Motor.
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
docs/kmconnector/0.2.0
examples
lib
.gitignore
.npmignore
KMConnectorBLE.js
KMConnectorBrowser.js
KMConnectorUSB.js
README.md
jsdoc.json
package.json
webpack.config.js

README.md

KMConnector

Node.js and Browser javascript Library for Keigan Motor.

Keigan Motor用のNode.jsとBrowser JavaScriptライブラリ

Description

You can control USB Serial and BLE in Node.js. Browser can control Web Bluetooth. https://www.keigan-motor.com/

Node.jsではUSBシリアル・BLE、ブラウザ(chrome)ではWebBluetoothを用いて接続出来ます。

DEMO:

Requirement

  • noble 1.9.1+
  • serialport 7.0.2+

Usage

  • KeiganMotor firmware needs 1.78 or more

  • BLE (Node.js only. Raspberrypi needs to run with administrator privilege to launch Bluetooh.(sudo))

    BLE Node.js用。RaspberryPiは管理者権限で実行する必要があります。

  • USB Serial (Node.js only) serialport 7.0.2+

  • Web Bluetooth (Browser only. andoroid or chrome on macos)

    Web Bluetoothは現在andoroid及びMacOSのchromeのみで動作します。

Installation

Node.js

npmからインストール

$ npm install kmconnector

Browser(Web Bluetooth)

ブラウザはhtmlのヘッダーにindexBrowser.jsを読み込んで下さい

<script src="kmconnector-js/KMConnectorBrowser.js"></script>

Examples (Exsample file is /examples/nodejs/)

サンプルファイルは /examples/nodejs/ にあります

USB (Example of connection to specified port)

指定したUSBポートに接続する例

const KMConnector = require('kmconnector/KMConnectorUSB');
const KMMotorOneUSBSerial=KMConnector.KMMotorOneUSBSerial;

let kMMotorOne=new KMMotorOneUSBSerial('/dev/serial/by-id/usb-FTDI_FT230X_Basic_UART_DM00KBZZ-if00-port0');
kMMotorOne.on(kMMotorOne.EVENT_TYPE.connect,function(kMDeviceInfo){
   if(kMMotorOne.isConnect){
       kMMotorOne.cmdLed(1,200,0,0);//led
   }
});

//Try Auto Reconnect
kMMotorOne.on(kMMotorOne.EVENT_TYPE.disconnect,(kMDeviceInfo)=>{
    setTimeout(()=>{kMMotorOne.connect();},5000);
});

kMMotorOne.on(kMMotorOne.EVENT_TYPE.connectFailure,(kMDeviceInfo,err)=>{
    setTimeout(()=>{kMMotorOne.connect();},5000);
});

kMMotorOne.connect();

USB (Example of all scanning USB port for connection)

全てのUSBポートをスキャンして接続する例

const KMConnector = require('kmconnector/KMConnectorUSB');
KMConnector.KMMotorOneUSBSerial.on(KMMotorOneUSBSerial.EVENT_TYPE.discoverNewMotor,function(kMMotorOne){
   kMMotorOne.on(kMMotorOne.EVENT_TYPE.connect,function(kMDeviceInfo){
       if(kMMotorOne.isConnect){
           kMMotorOne.cmdLed(1,200,0,0);//led
       }
   });
   kMMotorOne.connect();
});

KMConnector.KMMotorOneBLE.startScanToCreateInstance();

BLE

const KMConnector = require('kmconnector/KMConnectorBLE');
KMConnector.KMMotorOneBLE.on(KMConnector.KMMotorOneBLE.EVENT_TYPE.discoverNewMotor,function(kMMotorOneBLE){
   KMConnector.KMMotorOneBLE.stopScan();
   kMMotorOneBLE.on(kMMotorOneBLE.EVENT_TYPE.init,function(kMDeviceInfo){
           if(kMMotorOneBLE.isConnect){
               kMMotorOneBLE.cmdLed(1,200,0,0);
           }
   });
   kMMotorOneBLE.connect();
});  
KMConnector.KMMotorOneBLE.startScanToCreateInstance();

BLE (When directly using noble API)

BLE通信(noble)を既に使用している物に組み込む場合の例

const KMConnector = require('kmconnector/KMConnectorBLE');
const noble = require('noble');
noble.on('discover',(nobleperipheral)=>{
    noble.stopScanning();
    
    //connected to motor only
    let localName=nobleperipheral.advertisement?nobleperipheral.advertisement.localName:"";
    if(localName.startsWith('KM-1')){
        let kMMotorOneBLE= new KMConnector.KMMotorOneBLE(nobleperipheral);
        kMMotorOneBLE.on(kMMotorOneBLE.EVENT_TYPE.init,function(kMDeviceInfo){
                        if(kMMotorOneBLE.isConnect){
                            kMMotorOneBLE.cmdLed(1,200,0,0);
                        }
                });
        kMMotorOneBLE.connect();
    }
});

if(noble.state === 'poweredOn'){
    noble.startScanning();
}else{
    noble.once('stateChange',()=>{
        noble.startScanning();
    });
}

Web Bluetooth (Browser only. Chrome on Android or Mac)

Exsample file is /examples/browser_webbluetooh/

html

<head>
<script src="kmconnector/indexBrowser.js"></script>
</head>
<body>
    <a href="javascript:KMB.connect();">connect</a>
</body>

javascript

const KMB=new KMMotorOneWebBLE();
KMB.on(KMB.EVENT_TYPE.init,function(kMDeviceInfo){
    KMB.cmdEnable();
    KMB.cmdSpeed_rpm(10);
    KMB.cmdRunForward();
});

//KMB.connect();//For security reasons permission request error occurs unless it is ignited by user's click operation

Https(https://) connection is required for Web Bluetooth operation.

※Web Bluetoothはセキュリティの為、https://での接続が必須です

BLE connection Api (Node.js)

Node.jsでのBLE接続例

Methods

BLE Scanninng and Stopping (Static Methods)

BLEスキャンと停止

KMConnector.KMMotorOneBLE.startScanToCreateInstance(15000);
KMConnector.KMMotorOneBLE.stopScan();

Collective disconnection (Static Methods)

接続したモーターの全切断

 KMConnector.KMMotorOneBLE.allDisConnect();

Connect and disConnect(instance Methods)

個々のモーターの切断

kMMotorOneBLE.connect();
kMMotorOneBLE.disConnect();

Events(Scan)

discoverMotor (Static)

スキャン中のモーター発見時のイベント例

KMConnector.KMMotorOneBLE.on(KMConnector.KMMotorOneBLE.EVENT_TYPE.discoverMotor,function(kMMotorOneBLE){
    console.log('onDiscoverMotor:'+kMMotorOneBLE.deviceInfo.name);
});

discoverNewMotor (Static)

未接続の新規モーター発見時

Only when new motor is found.

KMConnector.KMMotorOneBLE.on(KMConnector.KMMotorOneBLE.EVENT_TYPE.discoverNewMotor,function(kMMotorOneBLE){
    KMConnector.KMMotorOneBLE.stopScan();//Depending on the adapter, it is necessary to initialize the motor after completing the scan. https://github.com/sandeepmistry/noble#notes
    //todo::Motor initialization processing
});

scanTimeout (Static)

スキャンがタイムアウトで終了した時

KMConnector.KMMotorOneBLE.on(KMConnector.KMMotorOneBLE.EVENT_TYPE.scanTimeout,function(){        
     //Connected to all motors
     Object.keys(KMConnector.KMMotorOneBLE.motors).forEach((key)=>{
            let motor=KMConnector.KMMotorOneBLE.motors[key];
            if(!motor.isConnect){
                motor.connect();
            }
     });
});

Events(Motors)

init

モーターの初期化完了時のイベント例

 kMMotorOneBLE.on(kMMotorOneBLE.EVENT_TYPE.init,function(kMDeviceInfo){
        //Motor operation
        kMMotorOneBLE.cmdEnable();
        kMMotorOneBLE.cmdSpeed_rpm(10);
        kMMotorOneBLE.cmdRunForward();
    });

Connect and disconnect

モーター接続・切断時イベント

kMMotorOneBLE.on(kMMotorOneBLE.EVENT_TYPE.connect,function(kMDeviceInfo){
    console.log("onConnect:"+kMDeviceInfo.isConnect);
});

kMMotorOneBLE.on(kMMotorOneBLE.EVENT_TYPE.disconnect,function(kMDeviceInfo){
     console.log("onDisconnect:"+kMDeviceInfo.isConnect);
});

kMMotorOneBLE.on(kMMotorOneBLE.EVENT_TYPE.connectFailure,function(kMDeviceInfo,err){
    console.log("onConnectFailure:"+err);
});

motorMeasurement

モーター回転情報受信時

kMMotorOneBLE.on(kMMotorOneBLE.EVENT_TYPE.motorMeasurement,function(kMRotState){
    console.log(kMRotState.GetValObj());//{position,velocity,torque}
});

imuMeasurement

ジャイロ情報受信時(受信するには別途cmdEnableIMU()を有効にする)

It is output only when the gyro is enabled (kMMotorOneBLE.cmdEnableIMU())

kMMotorOneBLE.on(kMMotorOneBLE.EVENT_TYPE.imuMeasurement,function(kMImuState){
    console.log(kMImuState.GetValObj());//{accelX,accelY,accelZ,temp,gyroX,gyroY,gyroZ}
});

Web Bluetooth Api (Browser)

ブラウザでの接続例

Methods

Connect and disConnect

接続・切断

kMMotorOneWebBLE.connect()
kMMotorOneWebBLE.disConnect()

Events

Node.jsと同様

init

When the motor is first connected and initialized

kMMotorOneWebBLE.on(KMB.EVENT_TYPE.init,function(kMDeviceInfo){
    console.log(kMDeviceInfo.GetValObj());//{type,id,name,isConnect,manufacturerName,firmwareRevision}
    kMMotorOneWebBLE.cmdEnable();//For safety, the motor operation at startup is disabled
    kMMotorOneWebBLE.cmdSpeed_rpm(10);
});

Connect and disconnect

kMMotorOneWebBLE.on(KMB.EVENT_TYPE.connect,function(kMDeviceInfo){
        console.log("onConnect:"+kMDeviceInfo.isConnect);
});
kMMotorOneWebBLE.on(KMB.EVENT_TYPE.disconnect,function(kMDeviceInfo){
    console.log("onDisconnect:"+kMDeviceInfo.isConnect);
});
kMMotorOneWebBLE.on(KMB.EVENT_TYPE.connectFailure,function(kMDeviceInfo,err){
    console.log("onConnectFailure:"+err);
});

motorMeasurement

kMMotorOneWebBLE.on(KMB.EVENT_TYPE.motorMeasurement,function(kMRotState){
    console.log(kMRotState.GetValObj());//{position,velocity,torque}
});

imuMeasurement

It is output only when the gyro is enabled (kMMotorOneWebBLE.cmdEnableIMU())

kMMotorOneWebBLE.on(KMB.EVENT_TYPE.imuMeasurement,function(kMImuState){
    console.log(kMImuState.GetValObj());//{accelX,accelY,accelZ,temp,gyroX,gyroY,gyroZ}
});

Motor command Api (common)

モーター操作コマンド

The command of the motor is defined by "cmd [CommandName] (prame, ...)"

操作コマンドは「cmdコマンド名()」の書式になります。

kMMotorOneBLE.cmdSpeed_rpm(10);//Set the speed 10rpm
kMMotorOneBLE.cmdEnable();//Enable motor action
kMMotorOneBLE.cmdRunForward();//Run forward (ccw)

For details of the command, see the following site

詳細なドキュメント

Author

Keigan Inc.

License

MIT