BrickPi3 package for nodejs
With this package you can control your BrickPi3 with nodejs.
This package has the same interface as the python library and was copied 1:1 with sligth language adjustments (bitwise operators are a bit different and the return values are promises). So just look at their examples (https://github.com/DexterInd/BrickPi3/tree/master/Software/Python/Examples) to see, how you can archive different tasks. Just remember, that you get promises back from most of the methods.
If you find any bugs, please open an issue or submit a pull request. I couldn't test everything, because I only have ev3 large motors. So I need your help to know if everything else works :)
Install
npm install --save brickpi3
Usage
const brickpi3 = ; async { try let BP = ; //Make sure to stop and free all motors and sensors when the programm exits brickpi3utils; //Resetting offset position of motor A to 0 let encoder = await BP; await BP; await BP; catch err console; };
Utils
When you need to find the extreme offsets of the motor (f.e. an arm can only get from point a to point b but not beyond), there is a helper function available like explained in this video: https://youtu.be/d0bghBZZMUg?t=1m35s
const brickpi3 = ; async { try let BP = ; brickpi3utils; await brickpi3utils; await BP; console; catch err console; };
For easier working with motors and sensors, you can get an instance of each of them and then access their methods:
const brickpi3 = ; async { try let BP = ; brickpi3utils; //Get the instance of one motor and sensor let motor = brickpi3utils; //getSensor has an optional third parameter which allows to overwrite the default configuration time limit let sensor = brickpi3utils; //First set the type of the sensor await sensor; //You can also change the default configuration time limit (3000ms) for the sensor configuration after initialization sensor; //Reset the motors encoder to 0 await motor; //Rotates the motor one revolution - will resolve only when finished await motor; //Powers on the motor until the callback function is true (good to use with sensors f.e.) await motor; catch err console; };
If you want to calculate, what gear ratio some connected gears have, there is also a helper:
const brickpi3 = ; //A 8-teeth-gear drives a 24-teeth-gear, which is connected to another 8-teeth-gear, which drives another 24-teeth-gearconsole;// => 0.111 (so if you rotate the initial 8-teeth-gear one evolution, the last 24-teeth-gear will rotate 0.111 rounds in the same direction)
BrickPi3 Stacking
If you have multiple brickPi3's, you can stack them and control even more motors/sensors with a single raspberry pi.
First attach each brickPi separatly and execute the following script. You need to write down the id of each brickPi.
const brickpi3 = ; async { try let BP = ; console; catch err console; };
Then you can set the address for each of your brickPi's in the initializing part and access the four sensors and motors with each brickPi object instance.
const brickpi3 = ; async { try await brickpi3; await brickpi3; const BP1 = 1; const BP2 = 2; brickpi3utils; brickpi3utils; const motor1 = brickpi3utils; const motor2 = brickpi3utils; //Reset Motor A offset of your first brickPi await motor1; //Reset Motor A offset of your second brickPi await motor2; //Let the motor A of your first brickPi turn constantly await motor1; //Let the motor A of your second brickPi rotate by 45° every two seconds. let target = 0; const moveOn = async { target = target + 45; await motor2; ; }; ; catcherr console; };
Special thanks
Thanks to Sumit Kumar (http://twitter.com/tweetsofsumit) for providing an example on how to communicate with the brickPi3 in nodejs on his repository.