Motor Hat Save

Node Module to control Adafruits MotorHAT for the RaspberryPi

Project README

🏁 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

Installation & Basic Usage

$ npm install --save motor-hat
var motorHat = require('motor-hat')({steppers: [{ W1: 'M1', W2: 'M2' }]}).init();
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.`); 


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.

// 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.

// 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'
// Supported number of microsteps are 8 and 16 (8 by default)
// 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
// 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
// Move to position 100

// Start dc motor forward (by default at 100% speed)
// Set DC motor speed to 50%
// reverse the dc motor to back direction
// stop the dc motor

As seen in

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


MIT © J. Cane

Open Source Agenda is not affiliated with "Motor Hat" Project. README Source: jcane86/motor-hat

Open Source Agenda Badge

Open Source Agenda Rating