npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

brickpi3

v0.9.8

Published

Control your BrickPi3 with nodejs

Downloads

14

Readme

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 = require('brickpi3');

(async () => {
    try {
        let BP = new brickpi3.BrickPi3();
        
        //Make sure to stop and free all motors and sensors when the programm exits
        brickpi3.utils.resetAllWhenFinished(BP);
        
        //Resetting offset position of motor A to 0
        let encoder = await BP.get_motor_encoder(BP.PORT_A);
        await BP.offset_motor_encoder(BP.PORT_A, encoder);
        await BP.set_motor_power(BP.PORT_A, 10);
    } catch (err) {
        console.log(err);        
    }
})();

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 = require('brickpi3');

(async () => {
    try {
        let BP = new brickpi3.BrickPi3();
        brickpi3.utils.resetAllWhenFinished(BP);
        
        await brickpi3.utils.resetMotorEncoder(BP, BP.PORT_A, brickpi3.utils.RESET_MOTOR_LIMIT.MIDPOINT_LIMIT);
        await BP.set_motor_position(BP.PORT_A, 0);
        console.log("Motor should now be in the middle of its two extremes");
    } catch (err) {
        console.log(err);
    }
})();

For easier working with motors and sensors, you can get an instance of each of them and then access their methods:

const brickpi3 = require('brickpi3');

(async () => {
    try {
        let BP = new brickpi3.BrickPi3();
        brickpi3.utils.resetAllWhenFinished(BP);
        
        //Get the instance of one motor and sensor
        let motor = brickpi3.utils.getMotor(BP, BP.PORT_A);
        //getSensor has an optional third parameter which allows to overwrite the default configuration time limit
        let sensor = brickpi3.utils.getSensor(BP, BP.PORT_2);
        
        //First set the type of the sensor
        await sensor.setType(BP.SENSOR_TYPE.EV3_TOUCH);
        
        //You can also change the default configuration time limit (3000ms) for the sensor configuration after initialization
        sensor.setConfigurationTimeLimit(4000);

        //Reset the motors encoder to 0
        await motor.resetEncoder();
        
        //Rotates the motor one revolution - will resolve only when finished
        await motor.setPosition(360);
        
        //Powers on the motor until the callback function is true (good to use with sensors f.e.)
        await motor.setPower(50, async () => {
            return await sensor.getValue() === 1;
        });
    } catch (err) {
        console.log(err);
    }
})();

If you want to calculate, what gear ratio some connected gears have, there is also a helper:

const brickpi3 = require('brickpi3');

//A 8-teeth-gear drives a 24-teeth-gear, which is connected to another 8-teeth-gear, which drives another 24-teeth-gear
console.log(new brickpi3.utils.Gear(8).drive(24).connect(8).drive(24).getFactor());
// => 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 = require('brickpi3');

(async () => {
    try {
        let BP = new brickpi3.BrickPi3();
        console.log(await BP.get_id());
    } catch (err) {
        console.log(err);
    }
})();

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 = require('brickpi3');

(async () => {
    try {
        await brickpi3.set_address(1, 'A778704A514D355934202020FF110722');
        await brickpi3.set_address(2, 'DF9E6AC3514D355934202020FF112718');
        
        const BP1 = new brickpi3.BrickPi3(1);
        const BP2 = new brickpi3.BrickPi3(2);
            
        brickpi3.utils.resetAllWhenFinished(BP1);
        brickpi3.utils.resetAllWhenFinished(BP2);
        
        const motor1 = brickpi3.utils.getMotor(BP1, BP1.PORT_A);
        const motor2 = brickpi3.utils.getMotor(BP2, BP2.PORT_A);
            
        //Reset Motor A offset of your first brickPi
        await motor1.resetEncoder();
            
        //Reset Motor A offset of your second brickPi
        await motor2.resetEncoder();
            
        //Let the motor A of your first brickPi turn constantly
        await motor1.setPower(10);
            
        //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.setPosition(target);
    
            setTimeout(moveOn, 2000);
        };
        moveOn();
            
    } catch(err) {
        console.log(err);
    }
})();

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.