API Docs for: 0.5.12
Show:

PhidgetStepper Class

Extends Phidget
Defined in: lib/phidgets.js:1655
Module: phidgets

The PhidgetStepper class is used to control the stepper motors connected to a PhidgetStepper board. It can be used to set a target position for the motors while controlling their maximum velocity and acceleration.

Here is a simple example that moves the motor connected to output 0 to its 200 position and then brings it back to position 0. Before making it move, you must first "engage" the motor. Also, you should always explicitely set the desired acceleration and velocity (speed).

var phidgets = require('phidgets');
var ps = new phidgets.PhidgetStepper();

ps.addListener("opened", onReady);

function onReady() {

   // Engage motor and set desired properties
   ps.engageMotor(0, true);
   ps.setAcceleration(0, ps.maximumAcceleration);
   ps.setTargetVelocity(0, ps.maximumVelocity);

   // Makes the motor move
   ps.setTargetPosition(0, 200);

   // Triggered when the target position is reached
   ps.once("target", function(e, data) {
     ps.setTargetPosition(0, 0);
   });

}

ps.open();

The PhidgetStepper object adds 5 events to the basic ones inherited by all Phidgets. They are:

  • position: triggered each time the motor's position changes
  • start: triggered when a motor starts moving
  • stop: triggered when a motor stops moving
  • target: triggered when the motor has reached its target position
  • input: triggered when a digital input changes (not available on all boards)

This object extends the Phidget object which itself extends Node.js' events.EventEmitter object. See that object's documentation for more inherited methods and properties.

Constructor

PhidgetStepper

()

Methods

_forceBetween

(
  • value
  • min
  • max
)
protected

Inherited from Phidget: lib/phidgets.js:848

Returns the value after making sure it falls between min and max.

Parameters:

  • value Int | Number

    The value to check

  • min Int

    The minimum value desired

  • max Int

    The maximum value desired

Returns:

int

_handleData

(
  • chunk
)
private

Inherited from Phidget: lib/phidgets.js:511

This function is called each time data is received from the Phidget WebSerice. It adds the data to the input buffer and checks if full lines (separated by '\n') can be reconstructed. If full lines are found they are handed over to the _parseLineInput() method for processing.

Parameters:

  • chunk String

    A chunk of utf8 encoded text to parse

_makePckString

(
  • keyword
  • [index]
)
private

Inherited from Phidget: lib/phidgets.js:791

Returns a /PCK string built from the specified parameters. PCK strings are the keys sent out to control the board.

Parameters:

  • keyword String

    The operation keyword to use

  • [index] Int optional

    The index of the output to use

_parseLineInput

(
  • line
)
private

Inherited from Phidget: lib/phidgets.js:553

Parses a single line of data typically received from the Phidget WebService. If the line is a report line, the function hands it off to the _parsePskKey() method. Otherwise, it deals with it locally.

Parameters:

  • line String

    A non-terminated line of utf8 text

_parsePhidgetSpecificData

(
  • data
)
protected

Inherited from Phidget: lib/phidgets.js:761

Parses Phidget-specific data received from the Phidget WebService. This function is meant to be overridden by subclasses.

Parameters:

  • data Object

    An object containing the received data

    • device String

      The device identifier (e.g. PhidgetInterfaceKey, PhidgetLED, etc.).

    • label String

      The custom label set for the device.

    • serial Int

      The serial number of the device.

    • keyword String

      A keyword identifying the type of information conveyed. It could be 'Input', 'Version', 'DataRate', etc.

    • index Int

      The numerical index (for indexed keys only)

    • value String

      The actual value.

    • status String

      The status of the key. It could be: 'added', 'changed', 'removing', etc.

_sendLine

(
  • line
)
private

Inherited from Phidget: lib/phidgets.js:824

Sends a line of data to the webservice

Parameters:

  • line String

    A non-terminated line of data to send

_sendPck

(
  • key
  • value
  • [persistent=false]
)
private

Inherited from Phidget: lib/phidgets.js:807

Sends the /PCK string (with attached value) to the webservice.

Parameters:

  • key String

    A /PCK string (typically form the _makePckString() method)

  • value Int | String

    The value to set

  • [persistent=false] Boolean optional

    Whether the value should persist or whether its for the session only.

_setPhidgetSpecificInitialState

() protected

Inherited from Phidget: lib/phidgets.js:781

Sets phidget-specific state before the 'opened' event is triggered. This is a good place for subclasses to assign initial values to the board. This is meant to be overridden by subclasses.

addListener

(
  • event
  • listener
)
chainable

Inherited from Phidget: lib/phidgets.js:262

This is an alias for the on() method.

Parameters:

  • event String

    The event to add the listener for.

  • listener Function

    The callback function to execute when the event is triggered.

close

() Phidget chainable

Inherited from Phidget: lib/phidgets.js:396

Closes a previously opened connection to a Phidget device.

Returns:

Phidget:

Returns the Phidget to allow method chaining.

engageMotor

(
  • index
  • value
)
PhidgetStepper chainable

Starts or stops power from being sent to the motor connected to a specific output (or array of outputs). By default, outputs do not power connected motors. Before moving the motor, you must therefore engage the motor first.

To reduce the motor's power consumption, you can disengage it once it's reached its target position. If you are concerned about keeping accurate track of position, the motor should not be disengaged until the motor is stopped.

Parameters:

  • index Int | Array

    The motor's index number (or an array of motor numbers)

  • value Boolean

    The boolean status to use.

Returns:

PhidgetStepper:

Returns the PhidgetStepper object to allow method chaining.

on

(
  • event
  • listener
)
chainable

Inherited from Phidget: lib/phidgets.js:271

Adds a listener to the end of the listeners array for the specified event. No checks are made to see if the listener has already been added. Multiple calls passing the same combination of event and listener will result in the listener being added multiple times.

This method is inherited from Node.js' events.EventEmitter object. See that object's documentation for more details methods.

Parameters:

  • event String

    The event to add the listener for.

  • listener Function

    The callback function to execute when the event is triggered.

once

(
  • event
  • listener
)
chainable

Inherited from Phidget: lib/phidgets.js:288

Adds a one time listener for the event. This listener is invoked only the next time the event is fired, after which it is removed.

This method is inherited from Node.js' events.EventEmitter object. See that object's documentation for more details methods.

Parameters:

  • event String

    The event to add the listener for.

  • listener Function

    The callback function to execute when the event is triggered.

open

(
  • [options={}]
)
Phidget chainable

Inherited from Phidget: lib/phidgets.js:333

Opens a connection to a Phidget device. Opening a connection is a two-step process. First, a connection to the Phidget WebService (which must be running) is established. Then, a session to the specified device (which must be plugged in) is opened.

Parameters:

  • [options={}] Object optional

    Options

    • [host="127.0.0.1"] String optional

      Hostname or IP address to connect to

    • [port=5001] Int optional

      Port to connect to

    • [serial] Int optional

      Serial number of the device to connect to

    • [label] String optional

      Label of the device to connect to (can be set in the Phidgets control panel).

Returns:

Phidget:

Returns the Phidget to allow method chaining.

removeAllListeners

(
  • [event]
)
chainable

Inherited from Phidget: lib/phidgets.js:303

Removes all listeners, or those of the specified event.

This method is inherited from Node.js' events.EventEmitter object. See that object's documentation for more details methods.

Parameters:

  • [event] String optional

    The event to remove the listeners for.

removeListener

(
  • [event]
  • listener
)
chainable

Inherited from Phidget: lib/phidgets.js:315

Removes a listener from the listener array for the specified event. removeListener() will remove, at most, one instance of a listener from the listener array. If any single listener has been added multiple times to the listener array for the specified event, then removeListener() must be called multiple times to remove each instance.

This method is inherited from Node.js' events.EventEmitter object. See that object's documentation for more details methods.

Parameters:

  • [event] String optional

    The event to remove the listeners for.

  • listener Function

    The callback function to execute when the event is triggered.

setAcceleration

(
  • index
  • [value]
)
PhidgetStepper chainable

Sets the acceleration for the motor connected to the specified output. The motor will both accelerate and decelarate at this rate. For the 1062 board, this is specified in half-steps.

The minimum and maximum acceleration values can be viewed in the minimumAcceleration and maximumAcceleration properties.

The acceleration should be explicitely set as part of initialization because otherwise it will remain unknown.

Parameters:

  • index Int | Array

    The output's index number (or an array of output numbers)

  • [value] Number optional

    The desired acceleration specified in half-steps. If not specified (or invalid), the maximum acceleration will be used.

Returns:

PhidgetStepper:

Returns the PhidgetStepper to allow method chaining.

setCurrentLimit

(
  • index
  • value
)
PhidgetStepper chainable

Sets the upper current limit for the motor connected to the specified output. Note that not all stepper controllers support current limiting.

Parameters:

  • index Int | Array

    The output's index number (or an array of output numbers)

  • value Number

    The target position specified in half-steps.

Returns:

PhidgetStepper:

Returns the PhidgetStepper to allow method chaining.

setPosition

(
  • index
  • [value]
)
PhidgetStepper chainable

Sets the current position of the motor connected to the specified output (or array of outputs). Setting the position does not actually move the motor, it merely sets the reference that will be used when moving to a target position.

Parameters:

  • index Int | Array

    The output's index number (or an array of output numbers).

  • [value] Number optional

    The desired velocity specified in half-steps. If not specified (or invalid), the maximum velocity will be used.

Returns:

PhidgetStepper:

Returns the PhidgetStepper to allow method chaining.

setTargetPosition

(
  • index
  • [value=1000]
)
PhidgetStepper chainable

Sets a new target position for the motor connected to the specified output. The motor will immediately start moving towards this position.

Note that calling setTargetPosition() will override a previous call to setTargetPosition() and the motor will begin tracking to the new position immediately. The velocity of the motor will be ramped appropriately.

Parameters:

  • index Int | Array

    The output's index number (or an array of output numbers)

  • [value=1000] Number optional

    The target position specified in half-steps.

Returns:

PhidgetStepper:

Returns the PhidgetStepper to allow method chaining.

setTargetVelocity

(
  • index
  • [value]
)
PhidgetStepper chainable

Sets the target speed (velocity) for the motor connected to the specified output (or array of outputs). If the targetVelocity is set to 0, the motor will not move.

Note that this is not necessarily the speed that the motor is being turned at. The motor is accelerated towards the target velocity and then decelerated as it approaches the target position. If the target position is close enough, it may never reach the target velocity.

Parameters:

  • index Int | Array

    The output's index number (or an array of output numbers)

  • [value] Number optional

    The desired velocity specified in half-steps. If not specified (or invalid), the maximum velocity will be used.

Returns:

PhidgetStepper:

Returns the PhidgetStepper to allow method chaining.

Properties

host

String

Inherited from Phidget: lib/phidgets.js:117

The host name or address of the Phidgets WebService to connect to.

Default: 127.0.0.1

inputs

Object

[read-only] An object containing information about the digital inputs of the PhidgetStepper board. If PhidgetStepper.inputs.count equals 0, it simply means that your board does not have any digital inputs.

PhidgetStepper.inputs[0].value  // Digital input 0's boolean value
PhidgetStepper.outputs.count    // Number of digital inputs on the device

Sub-properties:

  • count Int

    The total number of digital inputs on the device.

  • outputs[int].value Boolean

    The current boolean value of the input.

interReportPeriod

Int

Inherited from Phidget: lib/phidgets.js:226

The delay (in milliseconds) between report updates sent from the webservice.

Default: 8

label

String

Inherited from Phidget: lib/phidgets.js:143

The unique label of the device. The label must have a maximum length of 10 characters. If you try to set a longer label, the remainder will be truncated. Labels are supported only on newer devices and are remembered even when the device is unplugged. A label can only be set after a Phidget has been 'opened'. Trying to set the label before that will fail silently.

Default: undefined

maximumAcceleration

Number

[Read-only] The maximum acceleration value that can be set on outputs

maximumCurrent

Number

[Read-only] The maximum current that an output can be set to. Current limits are not supported by all stepper controllers.

maximumPosition

Number

[Read-only] The maximum position that an output can travel to.

maximumVelocity

Number

[Read-only] The maximum velocity that an output can be set to.

minimumAcceleration

Number

[Read-only] The minimum acceleration value that can be set on outputs

minimumCurrent

Number

[Read-only] The minimum current that an output can be set to. Current limits are not supported by all stepper controllers.

minimumPosition

Number

[Read-only] The minimum position that an output can travel to.

minimumVelocity

Number

[Read-only] The minimum velocity that an output can be set to.

name

String

Inherited from Phidget: lib/phidgets.js:194

[read-only] Human-readable version of the board's name (i.e. "Phidget InterfaceKit 8/8/8". This information is only available some time after the connection has been successfully opened.

Default: undefined

outputs

Object

[read-only] An object containing information about the motor outputs of the device. Here are a few examples of how to retrieve information in that object:

PhidgetStepper.outputs[5].currentPosition  // Motor 5's current position
PhidgetStepper.outputs[5].stopped          // Is motor 5 stopped?
PhidgetStepper.outputs.count               // Total number of outputs on the device

Sub-properties:

  • count Int

    The total number of outputs on the device.

  • outputs[int].position Number

    The position of the motor hooked up to that output. This value remains between sessions. It is used when calculating the movement needed to reach the target position. It can be manually set with setPosition().

  • outputs[int].targetPosition Number

    The last set target position.

  • outputs[int].acceleration Number

    The last set acceleration value (also used as deceleration value). This property should be set as part of initialization because otherwise, it will remain unknown.

  • outputs[int].currentLimit Number

    The last set current limit. Current limit is not supported by all stepper controllers.

  • outputs[int].current Number

    The actual current draw for the motor connected to that output. Current sense is not supported by all stepper controllers.

  • outputs[int].targetVelocity Number

    The desired velocity (speed) for the motor on that output. Sometimes referred to as the "velocity limit".

  • outputs[int].velocity Number

    The actual current velocity for the motor on that output.

  • outputs[int].engaged Boolean

    The engaged state. This is whether or not the motor connected to the output is currently powered.

  • outputs[int].stopped Boolean

    Whether the motor connected to that output is currently stopped. If this is true, it indicates that the motor is not moving, and there are no outstanding commands.

password

String private

Inherited from Phidget: lib/phidgets.js:182

The password to connect to the WebService. If specified, it will be used when opening a new connection. As soon as connected the password property will be erased. THIS IS CURRENTLY SET TO PRIVATE BECAUSE IT'S NOT IMPLEMENTED YET!

Default: undefined

port

Int

Inherited from Phidget: lib/phidgets.js:125

The port of the Phidgets webservice to connect to.

Default: 5001

ready

Boolean

Inherited from Phidget: lib/phidgets.js:94

[read-only] Whether the device is ready for use or not. A device must be 'opened' before it can be used.

reopen

Boolean

Inherited from Phidget: lib/phidgets.js:109

Whether to try to automatically reopen the device if it gets remotely closed.

Default: true

serial

Int

Inherited from Phidget: lib/phidgets.js:133

The unique serial number of the device. If specified, it will be used to connect to the matching device.

Default: undefined

serverId

Int

Inherited from Phidget: lib/phidgets.js:167

[read-only] The unique ID of the Phidget WebService the device is currently connected to.

Default: undefined

supportedDevices

String[]

Inherited from Phidget: lib/phidgets.js:58

[read-only] Array of all the devices supported by this library.

type

String

Inherited from Phidget: lib/phidgets.js:81

[read-only] The type of device (i.e. PhidgetInterfaceKit, PhidgetLED, etc.).

updateInterval

Int

The duration (in milliseconds) between update notifications (must be multiple of 8). The shorter the interval is, the more frequent the updates will be sent by the device.

Default: 16

version

String

Inherited from Phidget: lib/phidgets.js:210

[read-only] This number distinguishes between revisions of a specific type of Phidget. It is only useful for debugging purposes. This information is only available some time after the connection has been successfully opened.

Default: undefined

Events

closed

Inherited from Phidget: lib/phidgets.js:435

Event emitted when the connection to a phidget has been remotely closed.

Event Payload:

  • emitter Phidget

    The actual object that emitted the event.

error

Inherited from Phidget: lib/phidgets.js:493

Event emitted when an error occurs while trying to open a phidget

Event Payload:

  • emitter Phidget

    The actual object that emitted the event.

  • error Error

    The error object

    • address String

      The network address

    • code String

      The error code

    • errno String

      The error number

    • message String

      The error message

    • port String

      The network port

input

Event emitted when digital input data is received.

Event Payload:

  • emitter PhidgetStepper

    The actual PhidgetStepper object that emitted the event.

  • data Object

    An object containing the input data and related information

    • index Int

      The input's index number

    • value Boolean

      The input's received value

opened

Inherited from Phidget: lib/phidgets.js:585

Event emitted when a phidget is successfully opened.

Event Payload:

  • emitter Phidget

    The actual Phidget object that emitted the event.

opening

Inherited from Phidget: lib/phidgets.js:359

Event emitted when an attempt to open a Phidget has been initiated.

Event Payload:

  • emitter Phidget

    The actual Phidget object that emitted the event.

position

Event emitted to report that the position of a motor connected to one of the board's outputs has changed.

Event Payload:

  • emitter PhidgetStepper

    The actual PhidgetStepper object that emitted the event.

  • data Object

    An object containing information about the event.

    • index Int

      The output's index number.

    • index Number

      The motor's new position.

received

private

Inherited from Phidget: lib/phidgets.js:535

Event emitted when a new line of data has been received from the web service. This is mostly useful for debugging purposes (hence the @private denomination). It will let you view all data coming in.

Event Payload:

  • emitter Phidget

    The actual Phidget object that emitted the event.

  • data String

    The actual string data received.

reopening

Inherited from Phidget: lib/phidgets.js:461

Event emitted when an attempt to automatically re-open a closed Phidget is being carried on.

Event Payload:

  • emitter Phidget

    The actual Phidget object that emitted the event.

  • data Object

    Additional data regarding the event.

    • attempt Int

      The number of re-opening attempts performed.

    • max Int

      The maximum number of attempts that will be tried before failing.

sent

private

Inherited from Phidget: lib/phidgets.js:835

Event emitted when a new line of data has been sent to the Phidget WebService. This is mostly useful for debugging purposes (hence the @private denomination). It will let you view all data going out.

Event Payload:

  • emitter Phidget

    The actual Phidget object that emitted the event.

  • data String

    The actual string of sent data.

start

Event emitted when a motor connected to one of the board's outputs starts moving.

Event Payload:

  • emitter PhidgetStepper

    The actual PhidgetStepper object that emitted the event.

  • data Object

    An object containing information about the event.

    • index Int

      The output's index number.

stop

Event emitted when a motor connected to one of the board's outputs stops moving.

Event Payload:

  • emitter PhidgetStepper

    The actual PhidgetStepper object that emitted the event.

  • data Object

    An object containing information about the event.

    • index Int

      The output's index number.

target

Event emitted when a motor connected to one of the board's outputs has reached its target position.

Event Payload:

  • emitter PhidgetStepper

    The actual PhidgetStepper object that emitted the event.

  • data Object

    An object containing information about the event.

    • index Int

      The output's index number.

    • position Int

      The motor's position

timeout

Inherited from Phidget: lib/phidgets.js:638

Event emitted when an attempt to open a Phidget times out.

Event Payload:

  • emitter Phidget

    The actual Phidget object that emitted the event.