The Stepper class constructs objects that represent a single stepper motor attached to the physical board. This class is new and should be considered unstable.

In order to use the Stepper class, your board must be flashed with AdvancedFirmata, which is available here: https://github.com/soundanalogous/AdvancedFirmata

Stepper motors generally require significantly involved hardware setup (that is, more than just plugging a single lead into a pin).

Parameters

  • options An object of property parameters.

    PropertyTypeValue/DescriptionDefaultRequired
    pinsObject *See Table. An object containing the named pin addresses for the 3 supported typesyes *
    pinsArray *See Table. An array containing the pin addresses for the 3 supported typesyes1
    stepsPerRevNumberSteps per revolution. This will differ by motor, refer to motor specs for valueyes
    typeconstantfive.Stepper.TYPE.DRIVER, five.Stepper.TYPE.TWO_WIRE, five.Stepper.TYPE.FOUR_WIREyes
    rpmNumberRevolutions per minute, used to calculate speed.180no
    directionconstantfive.Stepper.DIRECTION.CW, five.Stepper.DIRECTION.CCWno2

Pins

ConfigAs ObjectAs Array
DRIVER{ step, dir }[ step, dir ]
TWO_WIRE{ m1, m2 }[ m1, m2 ]
FOUR_WIRE{ m1, m2, m3, m4 }[ m1, m2, m3, m4 ]
  1. The pins property is required, but can be EITHER an object or an array.
  2. The direction property is not required, but if it is undefined then Stepper.step() will do nothing.

Shape

{
  id: A user definable id value. Defaults to a generated uid
  pins: Object containing the pin addresses for the Stepper
  rpm: Revolutions per minute, used to calculate speed. Defaluts to 180
  direction:
  speed:
  accel:
  decel:
}

Component Initialization

Driver

// Create a stepper motor
//
//   - Step on pin 11
//   - Direction on pin 12
//   - Steps per revolution: 200
//   - Uses a Driver board
//

new five.Stepper({
  type: five.Stepper.TYPE.DRIVER,
  stepsPerRev: 200,
  pins: [ 11, 12 ]
});

// Is the same as...

five.Stepper({
  type: five.Stepper.TYPE.DRIVER
  stepsPerRev: 200,
  pins: {
    step: 11
    dir: 12
  }
 });
five.Stepper({
  type: five.Stepper.TYPE.DRIVER
  stepsPerRev: number,
  pins: {
    step: number
    dir: number
  }
 });
five.Stepper({
  type: five.Stepper.TYPE.DRIVER
  stepsPerRev: number,
  pins: [ step, dir ]
});

Stepper.Stepper.TYPE.DRIVER

TWO WIRE

five.Stepper({
  type: five.Stepper.TYPE.TWO_WIRE
  stepsPerRev: number,
  pins: {
    motor1: number,
    motor2: number
  }
 });
five.Stepper({
  type: five.Stepper.TYPE.TWO_WIRE
  stepsPerRev: number,
  pins: [ motor1, motor2 ]
});

FOUR WIRE

five.Stepper({
  type: five.Stepper.TYPE.FOUR_WIRE
  stepsPerRev: number,
  pins: {
    motor1: number,
    motor2: number,
    motor3: number,
    motor4: number
  }
});
five.Stepper({
  type: five.Stepper.TYPE.FOUR_WIRE
  stepsPerRev: number,
  pins: [ motor1, motor2, motor3, motor4 ]
});

Stepper.Stepper.TYPE.DRIVER

Usage

var board = new five.Board();

board.on("ready", function() {
  var k = 0;
  var stepper = new five.Stepper({
    type: five.Stepper.TYPE.DRIVER,
    stepsPerRev: 200,
    pins: [11, 12]
  });

  stepper.rpm(180).ccw().step(2000, function() {
    console.log("done");
  });
});

API

  • step(stepsOrOpts, callback) Move a stepper motor.

    // stepsOrOpts
    {
      steps: number of steps to move
      direction: 1, 0 (CCW, CW)
      rpm: Revolutions per minute. Defaults to 180
      accel: Number of steps to accelerate
      decel: Number of steps to decelerate
    }
    
    //
    //   - 10 full revolutions
    //   - Clockwise
    //   - Accelerate over the first 1600 steps
    //   - Decelerate over the last 1600 steps
    //
    
    stepper.step({ steps: 2000, direction: 1, accel: 1600, decel: 1600 }, function() {
      console.log("Done stepping!");
    });
    
  • rpm() Get the rpm.

  • rpm(value) Set the rpm.

    stepper.rpm(180).step(2000, function() {
      console.log("Done stepping!");
    });
    
  • speed() Get the speed.

  • speed(value) Set the speed in 0.01 * rad/s.

    // 180 rpm
    stepper.speed(0.18850).step(2000, function() {
      console.log("Done stepping!");
    });
    
  • direction() Get the direction.

  • direction(value) Set the direction.

    stepper.direction(1).step(2000, function() {
      console.log("Done stepping!");
    });
    
    stepper.direction(0).step(2000, function() {
      console.log("Done stepping!");
    });
    

    Or...

    stepper.direction(five.Stepper.DIRECTION.CW).step(2000, function() {
      console.log("Done stepping!");
    });
    
    stepper.direction(five.Stepper.DIRECTION.CCW).step(2000, function() {
      console.log("Done stepping!");
    });
    
  • accel() Get the acceleration.

  • accel(value) Set the acceleration.

    stepper.accel(1600).step(2000, function() {
      console.log("Done stepping!");
    });
    
  • decel() Get the deceleration.

  • decel(value) Set the deceleration.

    stepper.decel(1600).step(2000, function() {
      console.log("Done stepping!");
    });
    
  • cw() Set the Stepper to move Clockwise.

    stepper.cw().step(2000);
    
  • ccw() Set the Stepper to move Counter-Clockwise.

    stepper.ccw().step(2000);
    

Static

Types

TypeValueConstant
DRIVER1Stepper.TYPE.DRIVER
DRIVER2Stepper.TYPE.TWO_WIRE
DRIVER4Stepper.TYPE.FOUR_WIRE

Run States

StateValueConstant
STOP0Stepper.RUNSTATE.STOP
DRIVE1Stepper.RUNSTATE.ACCEL
DRIVE2Stepper.RUNSTATE.DECEL
DRIVE3Stepper.RUNSTATE.RUN

Directions

DirectionValueConstant
CCW0Stepper.DIRECTION.CCW
CW1Stepper.DIRECTION.CW

Examples

Hi! The Johnny-Five community is building new projects every day. We made this newsletter to tell you about what's new, what's good, and what's next for Open Source robotics. Join us in exploring what we can make together.

Fork me on GitHub