GithubHelp home page GithubHelp logo

node-soem's Introduction

Simple Open EtherCAT Master for Node.js

Installation

npm install node-soem

Make sure to have git and cmake installed on you system. The installation process builds a shared library from the Simple Open EtherCAT Master.

API

Basically following the C API.

Creating a master.

var soem = require('node-soem').NodeSoemMaster,
    master = soem(); // uses eth0 initially

Example

// initiate socket and stuff
if (master.init()) {

    // send out some ethercat frames to configure slaves
    master.configInit();

    // map the slave pdos to the internal ioMap
    master.configMap();

    // configure distributed clocks
    master.configDC();

    // take a look at the bus configuration
    var slaves = master.getSlaves();

    console.log(slaves);

    // get ready to send some initial data befor going to operational state
    master.sendProcessdata();
    maste.receiveProcessdata();

    // go to operational state
    master.writeState(0, 4); // 0=all slaves, 4=operational state

    // wait for the master to reach operational state

    var waitForOp = function () {
    
        var intv = setInterval(function () {
        
            master.sendProcessdata();
            master.receiveProcessdata();
            
            var state = master.statecheck(0, 4);
            
            // operational state reached
            if (state === 4) {
                clearInterval(intv);
                loop();    
            } 
            
        }, 50);    
        
    }

    var loop = function () {
   
        var counter = 0,
            slaves = master.getSlaves(),
            dv = new DataView(slaves[0].outputs);
    
        var intv = setInterval(function () {
 
            master.sendProcessdata();
            master.receiveProcessdata();
           
            dv.setUint16(0, counter++); 
                       
            
        }, 50);   
        
    }

    waitForOp();

}

License

Copyright (C) 2016 Stefan Poeter (Stefan.Poeter[at]cloud-automation.de)

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

node-soem's People

Contributors

jonwooding avatar jumo-sballing avatar stefanpoeter avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

node-soem's Issues

Ibytes / Obytes limited to 8 bytes ?

Hi Stefan, is there any reason why you limit Ibytes, same applies to Obytes, to 8 bytes ? shouldn't we just make sure that num bytes reported by slave matches the bits ?

numbytes = ec_slave[i].Ibytes;
if ((numbytes == 0) && (ec_slave[i].Ibits > 0)) numbytes = 1;
if (numbytes > 8) numbytes = 8;

Rgds

Problem addressing bit-orientated slaves

I am just prototyping with node-soem using a Beckhoff EL2622 (2-channel relay).

This device has two output bits:

{ name: 'EL2622',
    state: 18,
    ALStatusocde: 0,
    configadr: 4098,
    aliasadr: 0,
    Obits: 2,
    Obytes: 0,
    outputs: ArrayBuffer { byteLength: 0 },
    Ibits: 0,
    Ibytes: 0,
    inputs: ArrayBuffer { byteLength: 0 },
    pdelay: 145 }

Note that the outputs array is zero length, so the following code fails:

        var counter = 0,
            slaves = master.getSlaves(),
            dv = new DataView(slaves[1].outputs);

        var intv = setInterval(function () {

            master.sendProcessdata();
            master.receiveProcessdata();

            dv.setUint16(0, 1);

(the DataView is zero length)

According to the SOEM ethercatmain.h, if Obits is less than 8, Obytes will be zero, as in this case.

I hacked around this using the following code:

                uint16 bits=ec_slave[i].Obits;

                if ( bits > 0 && bits < 8 ) {
                        slave->Set(String::NewFromUtf8(isolate, "outputs"),
                                ArrayBuffer::New(isolate, (void*) ec_slave[i].outputs, 1));
                } else {
                        slave->Set(String::NewFromUtf8(isolate, "outputs"),
                                ArrayBuffer::New(isolate, (void*) ec_slave[i].outputs, ec_slave[i].Obytes));
                }

Then I could use dv.setUint8 in the Javascript and it worked ok.

I think there is more to do for a proper solution, because I didn't take care of the bit offset / mask. There is some further info on this here: https://openethercatsociety.github.io/doc/soem/tutorial_8txt.html.

It would be great if ultimately this library (or another) could abstract this type of detail away from the developer -- many Javascript developers are not used to doing bitwise manipulation, or even using advanced features such as DataView!

Anyway I got my little PoC working so thanks for the lib!

npm fails on Ubuntu 16.04LTS 32bit at "node-gyp rebuild"

Attempt to "sudo npm install node-soem" fails at "node-gyp rebuild".
Node.js is "v4.2.6". Also installed "nodejs-legacy" but no change.
SOEM was installed before this and seems to work fine.

Additional info - the first error is on building "../src/node-sooem-master.cc:8:22: fatal error: ethercat.h: No such file or directory". I will try to find why.

node-gyp build fails: "/usr/bin/ld: cannot find -lsoem"

npm install node-soem fails at node-gyp build.

SOLINK_MODULE(target) Release/obj.target/node-soem.node
**/usr/bin/ld: cannot find -lsoem**
collect2: error: ld returned 1 exit status
node-soem.target.mk:133: die Regel für Ziel „Release/obj.target/node-soem.node“ scheiterte
make: *** [Release/obj.target/node-soem.node] Fehler 1
make: Verzeichnis „/home/xxx/proj/node_modules/node-soem/build“ wird verlassen
gyp ERR! build error
gyp ERR! stack Error: 'make' failed with exit code: 2
gyp ERR! stack     at ChildProcess.onExit (/usr/lib/node_modules/node-gyp/lib/build.js:285:23)
gyp ERR! stack     at emitTwo (events.js:106:13)
gyp ERR! stack     at ChildProcess.emit (events.js:191:7)
gyp ERR! stack     at Process.ChildProcess._handle.onexit (internal/child_process.js:215:12)
gyp ERR! System Linux 4.4.0-78-generic
gyp ERR! command "/usr/bin/nodejs" "/usr/bin/node-gyp" "rebuild"
gyp ERR! cwd /home/xxx/proj//node_modules/node-soem
gyp ERR! node -v v6.10.3
gyp ERR! node-gyp -v v3.6.1
gyp ERR! not ok

gyp info using [email protected]
gyp info using [email protected] | linux | x64
Ubuntu 16.04.2 LTS

There is a mistake in the binding.gyp. The libsoem.so is not at lib/build but at ../lib/build. But this doesn't help. At execution of a Node.js app based on node-soem I get:

module.js:597
  return process.dlopen(module, path._makeLong(filename));
                 ^

Error: libsoem.so: cannot open shared object file: No such file or directory
    at Error (native)
    at Object.Module._extensions..node (module.js:597:18)
    at Module.load (module.js:487:32)
    at tryModuleLoad (module.js:446:12)
    at Function.Module._load (module.js:438:3)
    at Module.require (module.js:497:17)
    at require (internal/module.js:20:19)
    at Object.<anonymous> (/home/ballings/proj/spielwiese/nodeEtherCAT/example_01.js:1:74)
    at Module._compile (module.js:570:32)
    at Object.Module._extensions..js (module.js:579:10)

If I copy the libsoem manually to /usr/lib everything working fine (node-gyp build & execution of node.js app).

By the way... why you use an shared object of libsoem and not an static linked object?

passing/checking wrong number for OP State

In example_01.js and readme.md to switch to OP state we should pass to writeState/statecheck '8' and not '4'.
See typdef enum from https://github.com/OpenEtherCATsociety/SOEM/blob/master/soem/ethercattype.h :
/** Possible EtherCAT slave states */ typedef enum { /** No valid state. */ EC_STATE_NONE = 0x00, /** Init state*/ EC_STATE_INIT = 0x01, /** Pre-operational. */ EC_STATE_PRE_OP = 0x02, /** Boot state*/ EC_STATE_BOOT = 0x03, /** Safe-operational. */ EC_STATE_SAFE_OP = 0x04, /** Operational */ EC_STATE_OPERATIONAL = 0x08, /** Error or ACK error */ EC_STATE_ACK = 0x10, EC_STATE_ERROR = 0x10 } ec_state;
and https://infosys.beckhoff.com/index.php?content=../content/1031/tcsystemmanager/reference/ethercat/html/EtherCAT_Diagnostics.htm&id= (section Info Data).
Or I'm missing something ?

npm install fails with the last SOEM changes

Fails with:
SOLINK_MODULE(target) Release/obj.target/node-soem.node
/usr/bin/ld: ../lib/build/libsoem.a(ethercatmain.c.o): Die Umlagerung von
../lib/build/libsoem.a: error adding symbols: Ungültiger Wert
collect2: error: ld returned 1 exit status
node-soem.target.mk:132: die Regel für Ziel „Release/obj.target/node-soem.node“ scheiterte
make: *** [Release/obj.target/node-soem.node] Fehler 1
make: Verzeichnis „/home/helyman83/node-soem/build“ wird verlassen

In the last SOEM changes, they have cleaned up the CMakeLists.txt file.

In preinstall.sh this fails:
sed -i "/target_link_libraries(soem\ pthread\ rt)/a \ \ set_property(TARGET soem PROPERTY POSITION_INDEPENDENT_CODE ON)" CMakeLists.txt

because "target_link_libraries(soem pthread rt)" was replaced by "target_link_libraries(soem ${OS_LIBS})"

preinstall.sh works again if we replace it with:
sed -i '/target_link_libraries(soem\ ${OS_LIBS})/a \ \ set_property(TARGET soem PROPERTY POSITION_INDEPENDENT_CODE ON)' CMakeLists.txt

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.