Skip to content
Rick Waldron edited this page Aug 5, 2013 · 1 revision

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 then just plugging a single lead into a pin).

Parameters

  • options An object of property parameters.
Property Name Type Value(s) Description Required
pins Object * ```js Type: DRIVER { step, dir } ```
    ```js
    Type: TWO_WIRE
    { motor1, motor2 }
    ```

    ```js
    Type: FOUR_WIRE
    { motor1, motor2, motor3, motor4 }
    ```

  </td>
  <td>An object containing the named pin addresses for the 3 supported types</td>
  <td>yes *</td>
</tr>
<tr>
  <td>pins</td>
  <td>Array *</td>
  <td>
    ```js
    Type: DRIVER
    [ step, dir ]
    ```

    ```js
    Type: TWO_WIRE
    [ motor1, motor2 ]
    ```

    ```js
    Type: FOUR_WIRE
    [ motor1, motor2, motor3, motor4 ]
    ```
  </td>

  <td>An array containing the pin addresses for the 3 supported types</td>
  <td>yes *</td>
</tr>


<tr>
  <td>stepsPerRev</td>
  <td>Number</td>
  <td>#</td>
  <td>Steps per revolution. This will differ by motor, refer to motor specs for value</td>
  <td>yes</td>
</tr>
<tr>
  <td>type</td>
  <td>constant</td>
  <td>DRIVER, TWO_WIRE, FOUR_WIRE</td>
  <td>five.Stepper.TYPE.DRIVER, five.Stepper.TYPE.TWO_WIRE, five.Stepper.TYPE.FOUR_WIRE</td>
  <td>yes</td>
</tr>
<tr>
  <td>rpm</td>
  <td>Number</td>
  <td>Per device</td>
  <td>Revolutions per minute, used to calculate speed. Defaluts to 180</td>
  <td>no</td>
</tr>
<tr>
  <td>direction</td>
  <td>Number</td>
  <td>-1, 0, 1</td>
  <td>
    Counter-Clockwise: 0, Clockwise: 1, Default: -1

    - five.Stepper.DIRECTION.CW
    - five.Stepper.DIRECTION.CCW

  </td>
  <td>no</td>
</tr>
```js // Create a stepper motor // // - Step on pin 11 // - Direction on pin 12 // - Steps per revolution: 200 // - Uses a Driver board //

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

// Is the same as...

var stepper = five.Stepper({ type: five.Stepper.TYPE.DRIVER stepsPerRev: 200, pins: { step: 11 dir: 12 } });



* The **pins** property is required, but can be EITHER an object or an array.

### 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: }




### Usage

```js
var stepper = five.Stepper({
  type: five.Stepper.TYPE.DRIVER
  stepsPerRev: number,
  pins: {
    step: number
    dir: number
  }
 });
var stepper = five.Stepper({
  type: five.Stepper.TYPE.DRIVER
  stepsPerRev: number,
  pins: [ step, dir ]
}); 
var stepper = five.Stepper({
 type: five.Stepper.TYPE.TWO_WIRE
 stepsPerRev: number,
 pins: {
   motor1: number,
   motor2: number
 }
});
var stepper = five.Stepper({
  type: five.Stepper.TYPE.TWO_WIRE
  stepsPerRev: number,
  pins: [ motor1, motor2 ]
}); 
var stepper = five.Stepper({
 type: five.Stepper.TYPE.FOUR_WIRE
 stepsPerRev: number,
 pins: {
   motor1: number,
   motor2: number,
   motor3: number,
   motor4: number
 }
});
var stepper = five.Stepper({
  type: five.Stepper.TYPE.FOUR_WIRE
  stepsPerRev: number,
  pins: [ motor1, motor2, motor3, motor4 ]
}); 

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!" );
});
  • 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

Type Value Constant
DRIVER 1 Stepper.TYPE.DRIVER
DRIVER 2 Stepper.TYPE.TWO_WIRE
DRIVER 4 Stepper.TYPE.FOUR_WIRE

Run States

State Value Constant
STOP 0 Stepper.RUNSTATE.STOP
DRIVER 1 Stepper.RUNSTATE.ACCEL
DRIVER 2 Stepper.RUNSTATE.DECEL
DRIVER 3 Stepper.RUNSTATE.RUN

Directions

Direction Value Constant
CCW 0 Stepper.DIRECTION.CCW
CW 1 Stepper.DIRECTION.CW
Clone this wiki locally