Skip to content

Node Module to control Adafruits MotorHAT for the RaspberryPi

License

Notifications You must be signed in to change notification settings

jcane86/motor-hat

Repository files navigation

🏁 motor-hat 🎩

NPM version Build Status Dependency Status Coverage percentage semantic-release Commitizen friendly Gitter badge

Node Module to control Adafruit's MotorHAT for the Raspberry Pi http://jcane86.github.io/motor-hat

Installation & Basic Usage

$ npm install --save motor-hat
var motorHat = require('motor-hat')({steppers: [{ W1: 'M1', W2: 'M2' }]}).init();
motorHat.steppers[0].setSpeed({pps:100});
motorHat.steppers[0].step('back', 2048, (err, result) => {
  if (err) return console.log('Oh no, there was an error', err);
  console.log(`Did ${result.steps} steps ${result.dir} in ${result.duration/1000} seconds. I had to retry ${result.retried} steps because you set me up quicker than your poor board can handle.`); 
});

DOCS

Notes about 2.0

Some changes will need to be made to transition to the async version of the library in 2.0:

Main library:

  • Instance needs to be init()'d
  • Servo and Stepper instances exposed in servos and steppers arrays are already init()'d.

DC Motors:

  • Methods are now async, and need a callback as last parameter.
  • Old Sync methods remain, just call them as stopSync(), etc..
  • Instance needs to be init()'d

Servo Motors:

  • No changes, everything is still sync (I didn't feel it was necessary, feel free to open an issue or send a PR otherwise).

Stepper Motors:

  • Most methods already had the Sync suffix. Only setFrequency is now setFrequencySync.
  • Async methods added.
  • Release and current methods added (actually in 1.3).
  • Instance needs to be init()'d

Advanced usage

// get a motor-hat instance with the following initialized:
// * a non-default I2C address for the motor hat (default is 0x6F)
// * a stepper with winding one on 'M1' and winding two on 'M2' ports
// * a dc motor on port 'M4'
// * a servo on channel 0
// * a servo on channel 14
let spec = {
    address: 0x60,
    steppers: [{ W1: 'M1', W2: 'M2' }],
    dcs: ['M4'],
    servos: [0,14]
};
var motorHat = require('motor-hat')(spec);

// Since MotorHat 2.0, the instance needs to be initialized.
// This is to enable async initialization, feel free to open an issue if this is a pain.
motorHat.init();

// For steppers, set speed in rpm or pps (pulses per second) or sps (steps per second).
// To set it in rpm, set you steps/rev first (default 200)
// If you set it in pps, the speed will not be constant for different styles or number of microsteps.
motorHat.steppers[0].setSteps(2048);
motorHat.steppers[0].setSpeed({rpm:5});

// Move the motor one full turn fwds synchronously, one back async.
// step[Sync] and oneStep[Sync] take number of steps as input, 
// depending on selected style. To do 2048 full steps fwd (sync), 2048 back (async):
motorHat.steppers[0].stepSync('fwd', 2048);
motorHat.steppers[0].step('back', 2048, function(err, result) {
  if (err) {
    console.log('Oh no, there was an error');
  } else {
    // Move on..
  }
});

Further configuration

// Supported syles are 'single', 'double' (default), 'interleaved', and 'microstep'
motorHat.steppers[0].setStyle('microstep');
// Supported number of microsteps are 8 and 16 (8 by default)
motorHat.steppers[0].setMicrosteps(16);
// step[Sync] and oneStep[Sync] take number of steps/halfsteps/microsteps as input, 
// depending on selected style. To do 16 microsteps fwd:
motorHat.steppers[0].stepSync('back', 16);
// Set current at 50% to avoid overheating or to run at lower torques
motorHat.steppers[0].setCurrent(0.5);
// Release motor after moving it to avoid overheating or to let it move freely.
motorHat.steppers[0].release((err) => !err && console.log("IT'S FREE!!"));


// Calibrate the servo output. Pass in PWM frequency, position 0 pulse duration in ms,
// position 100 pulse duration in ms.
motorHat.servos[0].calibrate(50, 1, 2);
// Move to position 0
motorHat.servos[0].moveTo(0);
// Move to position 100
motorHat.servos[0].moveTo(100);


// Start dc motor forward (by default at 100% speed)
motorHat.dcs[0].runSync('fwd');
// Set DC motor speed to 50%
motorHat.dcs[0].setSpeedSync(50);
// reverse the dc motor to back direction
motorHat.dcs[0].runSync('back');
// stop the dc motor
motorHat.dcs[0].stopSync();

As seen in

Some of our friends made cool stuff using motor-hat. Drop us a PR to add your project to this list.

License

MIT © J. Cane