new <async> Servo( io )

Description
The Servo class allows for control of hobby servos
Parameters
Name Type Description
io number | string | object Pin identifier or IO Options (See Instantiating a Device)
Examples

Sweep a servo back and forth

import Servo from "j5e/servo";
			
			const servo = await new Servo(12);
			servo.sweep();
Fires

Members


<readonly> history

Description
The last five position updates
Properties
Name Type Description
timestamp date Timestamp of position update
target number The user requested position
degrees number The actual position (factors in offset and invert)
pulseWidth number The corrected pulseWidth (factors in offset and invert)
Returns
Details

<readonly> last

Description
The most recent position update
Properties
Name Type Description
timestamp date Timestamp of position update
target number The user requested position
degrees number The actual position (factors in offset and invert)
pulseWidth number The corrected pulseWidth (factors in offset and invert)
Returns
Details

<readonly> position

Description
The most recent request and corrected position (factors in offset and invert)
Returns
Details

Methods


configure( options ) → {Servo}

Description
Configure a Servo
Parameters
Name Type Description
options object Device configuration options
Name Type Attributes Default Description
pin number | string <optional>
If passing an object, a pin number or pin identifier
io string | constructor <optional>
builtin/pwm If passing an object, a string specifying a path to the IO provider or a constructor
type string <optional>
"standard" Type of servo ("standard" or "continuous")
pwmRange Array.<number> <optional>
[600, 2400] The pulse width range in microseconds
deadband Array.<number> <optional>
[90,90] The degree range at which a continuos motion servo will not turn
range Array.<number> <optional>
[0, 180] The allowed range of motion in degrees
deviceRange Array.<number> <optional>
[0, 180] The physical range (throw) of the servo in degrees
startAt number <optional>
"Any value within options.range" The desired start position of the servo
offset number <optional>
0 Adjust the position of the servo for trimming
invert boolean <optional>
false Reverses the direction of rotation
center boolean <optional>
false Center the servo on instantiation
Returns
The instance on which the method was called
Examples

Move a continuos rotation servo forward

import Servo from "j5e/servo";
			
			const servo = await new Servo({ pin: 12 });
			servo.configure({type: "continuous"});
			servo.cw();
Details

to( degrees [, time [, rate ] ] ) → {Servo}

Description
Set the servo's position to given degree over time.
Parameters
Name Type Attributes Default Description
degrees Number | Object Degrees to move servo to or an animation object (See j5e's Timeline and Tweening Tools)
time Number <optional>
Time to spend in motion. If degrees is a number and this value is ommitted, the servo will move to the requested degrees as quickly as possible.
rate Number <optional>
20 The target frame rate of the motion transiton
Returns
instance
Examples

Move a servo to 180° as quickly as possible

import Servo from "j5e/servo";
			
			const servo = await new Servo(12);
			servo.to(180);

Move a servo to 180° over one second

import Servo from "j5e/servo";
			
			const servo = await new Servo(12);
			servo.to(180, 1000);

Move a servo to 180° using an animation segment object

import Servo from "j5e/servo";
			import { inOutQuad } from "j5e/easing";
			
			const servo = await new Servo(12);
			servo.to({
			  duration: 1000,
			  cuePoints: [0, 1.0],
			  keyFrames: [ null, 180 ],
			  easing: inOutQuad
			});
Details

step( degrees [, time ] ) → {Servo}

Description
Update the servo position by specified degrees (over time)
Parameters
Name Type Attributes Description
degrees Number Degrees to turn servo to.
time Number <optional>
Time to spend in motion.
Returns
Examples

Move a servo to 120° as quickly as possible

import Servo from "j5e/servo";
			
			const servo = await new Servo(12);
			// The servo's default position is 90 degrees
			servo.step(30);

Move a servo to 120° over one second

import Servo from "j5e/servo";
			
			const servo = await new Servo(12);
			// The servo's default position is 90 degrees
			servo.step(30, 1000);
Details

min( [ time ] ) → {Servo}

Description
min Set Servo to minimum degrees, defaults to 0deg
Parameters
Name Type Attributes Description
time Number <optional>
Time to spend in motion.
Returns
Examples

Move a servo to 0° as quickly as possible

import Servo from "j5e/servo";
			
			const servo = await new Servo(12);
			servo.min();

Move a servo to 0° over one second

import Servo from "j5e/servo";
			
			const servo = await new Servo(12);
			servo.min(1000);
Details

max( [ time ] ) → {Object}

Description
Set Servo to maximum degrees, defaults to 180deg
Parameters
Name Type Attributes Description
time Number <optional>
Time to spend in motion.
Returns
instance
Examples

Move a standard servo to 180° as quickly as possible

import Servo from "j5e/servo";
			
			const servo = await new Servo(12);
			servo.max();

Move a standard servo to 180° over one second

import Servo from "j5e/servo";
			
			const servo = await new Servo(12);
			servo.max(1000);
Details

center( [ time ] ) → {Object}

Description
Set Servo to centerpoint, defaults to 90deg
Parameters
Name Type Attributes Description
time Number <optional>
Time to spend in motion.
Returns
instance
Examples

Move a servo to 180° and then back to 90° as quickly as possible

import Servo from "j5e/servo";
			import { timer } from "j5e/fn";
			
			const servo = await new Servo(12);
			servo.to(180);
			
			timer.setTimeout(function() {
			  servo.center();
			}, 1000);

Move a servo to 180° and then back to 90° over one second

import Servo from "j5e/servo";
			import { timer } from "j5e/fn";
			
			const servo = await new Servo(12);
			servo.to(180);
			
			timer.setTimeout(function() {
			  servo.center(1000);
			}, 1000);
Details

home() → {Servo}

Description
Return Servo to its startAt position
Returns
Examples

Move a servo to 180° and then back to 90° as quickly as possible

import Servo from "j5e/servo";
			import { timer } from "j5e/fn";
			
			const servo = await new Servo(12);
			servo.to(180);
			
			timer.setTimeout(function() {
			  servo.home();
			}, 1000);
Details

sweep( opts ) → {Servo}

Description
Sweep the servo between min and max or provided range
Parameters
Name Type Description
opts Array | Object An array describing the range of the sweep in degrees or an animation segment options object
Returns
Examples

Sweep a servo back and forth between 10° to 170°

import Servo from "j5e/servo";
			
			const servo = await new Servo(12);
			servo.sweep([10, 170]));
			
			example
			<caption>Sweep a servo back and forth between 10° to 170° with inOutCirc easing</caption>
			import Servo from "j5e/servo";
			import {inOutCirc} from "j5e/easing";
			
			const servo = await new Servo(12);
			servo.sweep({
			  keyFrames: [10, 170],
			  cuePoints: [0, 1],
			  easing: inOutCirc
			});
Details

stop()

Description
Stop a moving servo return {Servo}
Examples

Sweep a servo back and forth for two seconds then stop

import Servo from "j5e/servo";
			
			const servo = await new Servo(12);
			servo.sweep([10, 170]));
			
			timer.setTimeout(function() {
			  servo.stop();
			}, 2000);
Details

cw( speed ) → {Servo}

Description
Move a continuous rotation servo clockwise
Parameters
Name Type Default Description
speed number 1 Speed between 0 and 1.
Returns
Examples

Move a continuos rotation servo clockwise

import Servo from "j5e/servo";
			
			const servo = await new Servo({ pin: 12, type: "continuous"});
			servo.cw();

Move a continuos rotation servo clockwise at half speed

import Servo from "j5e/servo";
			
			const servo = await new Servo({ pin: 12, type: "continuous"});
			servo.cw(0.5);
Details

ccw( speed ) → {Servo}

Description
Move a continuous rotation servo counter-clockwise
Parameters
Name Type Default Description
speed number 1 Speed between 0 and 1.
Returns
Examples

Move a continuos rotation servo counter-clockwise

import Servo from "j5e/servo";
			
			const servo = await new Servo({ pin: 12, type: "continuous"});
			servo.ccw();

Move a continuos rotation servo counter-clockwise at half speed

import Servo from "j5e/servo";
			
			const servo = await new Servo({ pin: 12, type: "continuous"});
			servo.ccw(0.5);
Details

on( event, listener )

Description
Create an event listener
Parameters
Name Type Description
event string The name of the event to listen for
listener function A callback to run when the event is fired.

removeListener( event, listener )

Description
Remove an event listener
Parameters
Name Type Description
event string The name of the event that we are removing a listener from
listener function The callback that we are removing

once( event, listener )

Description
Create an event listener that will only fire one time.
Parameters
Name Type Description
event string The name of the event to listen for
listener function A callback to run when the event is fired.