Skip to content

Commit

Permalink
Examples: Update Servo Syntax
Browse files Browse the repository at this point in the history
  • Loading branch information
dtex authored and rwaldron committed Aug 21, 2019
1 parent 89c70b7 commit 2ce0505
Show file tree
Hide file tree
Showing 25 changed files with 226 additions and 248 deletions.
24 changes: 11 additions & 13 deletions docs/servo-PCA9685.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,30 +29,28 @@ node eg/servo-PCA9685.js


```javascript
var five = require("johnny-five");
var board = new five.Board();
const {Board, Servo} = require("johnny-five");
const board = new Board();
const controller = "PCA9685";

board.on("ready", function() {
board.on("ready", () => {
console.log("Connected");

// Initialize the servo instance
var a = new five.Servo({
address: 0x40,
controller: "PCA9685",
const a = new Servo({
controller,
pin: 0,
});

var b = new five.Servo({
address: 0x40,
controller: "PCA9685",
const b = new Servo({
controller,
range: [0, 180],
pin: 1,
});

var degrees = 0;

a.to(degrees);
b.to(degrees);
a.to(0);
b.to(0);

});

```
Expand Down
41 changes: 19 additions & 22 deletions docs/servo-animation-leg.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,53 +31,55 @@ node eg/servo-animation-leg.js


```javascript
var five = require("johnny-five"),
ph = {
state: "sleep"
};
const {Animation, Board, Servo, Servos} = require("johnny-five");
let ph = {
state: "sleep"
};

const board = new Board();

var board = new five.Board().on("ready", function() {
board.on("ready", () => {

/**
* This animation controls three servos
* The servos are the coxa, femur and tibia of a single
* leg on a hexapod. A full hexapod might need 18
* servo instances (assuming 3 degrees of freedom)
*/
ph.coxa = new five.Servo({
ph.coxa = new Servo({
pin: 9,
startAt: 45
});
ph.femur = new five.Servo({
ph.femur = new Servo({
pin: 10,
startAt: 180
});
ph.tibia = new five.Servo({
ph.tibia = new Servo({
pin: 11,
startAt: 180
});

// Create a Servos instance for those leg parts
ph.leg = new five.Servos([ph.coxa, ph.femur, ph.tibia]);
ph.leg = new Servos([ph.coxa, ph.femur, ph.tibia]);

/**
* Create an Animation(target) object. A newly initialized
* animation object is essentially an empty queue (array) for
* animation segments that will run asynchronously.
* @param {target} A Servo or Servos instance to be animated
*/
var legAnimation = new five.Animation(ph.leg);
const legAnimation = new Animation(ph.leg);

/**
* This object describes an animation segment and is passed into
* our animation with the enqueue method. The only required
* property is keyFrames. See the Animation wiki page for a full
* list of available properties
*/
var sleep = {
const sleep = {
duration: 500,
cuePoints: [0, 0.5, 1.0],
oncomplete: function() {
oncomplete() {
ph.state = "sleep";
},
keyFrames: [
Expand All @@ -90,11 +92,11 @@ var board = new five.Board().on("ready", function() {
/**
* Another animation segment
*/
var stand = {
const stand = {
duration: 500,
loop: false,
cuePoints: [0, 0.1, 0.3, 0.7, 1.0],
oncomplete: function() {
oncomplete() {
ph.state = "stand";
},
keyFrames: [
Expand All @@ -105,19 +107,14 @@ var board = new five.Board().on("ready", function() {
};

// Functions we can call from the REPL
ph.sleep = function() {
legAnimation.enqueue(sleep);
};

ph.stand = function() {
legAnimation.enqueue(stand);
};
ph.sleep = () => legAnimation.enqueue(sleep);
ph.stand = () => legAnimation.enqueue(stand);

// Inject the `servo` hardware into;
// the Repl instance's context;
// allows direct command line access
board.repl.inject({
ph: ph
ph
});

console.log("Try running ph.stand() or ph.sleep()");
Expand Down
14 changes: 7 additions & 7 deletions docs/servo-animation.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,16 @@ node eg/servo-animation.js


```javascript
var five = require("johnny-five");
var board = new five.Board();
const {Animation, Board, Servo} = require("johnny-five");
const board = new Board();

board.on("ready", function() {
board.on("ready", () => {

// Create a new `servo` hardware instance.
var servo = new five.Servo(10);
const servo = new Servo(10);

// Create a new `animation` instance.
var animation = new five.Animation(servo);
const animation = new Animation(servo);

// Enqueue an animation segment with options param
// See Animation example and docs for details
Expand All @@ -54,8 +54,8 @@ board.on("ready", function() {
// the Repl instance's context;
// allows direct command line access
board.repl.inject({
servo: servo,
animation: animation
servo,
animation
});
});

Expand Down
14 changes: 6 additions & 8 deletions docs/servo-array.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,23 +31,21 @@ node eg/servo-array.js


```javascript
var five = require("johnny-five");
var board = new five.Board();
const {Board, Servos} = require("johnny-five");
const board = new Board();

board.on("ready", function() {
board.on("ready", () => {

// Initialize a Servo collection
var servos = new five.Servos([9, 10]);

const servos = new Servos([9, 10]);

servos.center();


// Inject the `servo` hardware into
// the Repl instance's context;
// allows direct command line access
this.repl.inject({
servos: servos
board.repl.inject({
servos
});


Expand Down
12 changes: 6 additions & 6 deletions docs/servo-continuous.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,24 +31,24 @@ node eg/servo-continuous.js


```javascript
var five = require("johnny-five");
var keypress = require("keypress");
const {Board, Servo} = require("johnny-five");
const keypress = require("keypress");

keypress(process.stdin);

var board = new five.Board();
const board = new Board();

board.on("ready", function() {
board.on("ready", () => {

console.log("Use Up and Down arrows for CW and CCW respectively. Space to stop.");

var servo = new five.Servo.Continuous(10);
const servo = new Servo.Continuous(10);

process.stdin.resume();
process.stdin.setEncoding("utf8");
process.stdin.setRawMode(true);

process.stdin.on("keypress", function(ch, key) {
process.stdin.on("keypress", (ch, key) => {

if (!key) {
return;
Expand Down
26 changes: 13 additions & 13 deletions docs/servo-drive.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,16 @@ node eg/servo-drive.js


```javascript
var five = require("johnny-five"),
board, wheels;
const {Board, Servo, Servos} = require("johnny-five");

board = new five.Board();
const board = new Board();

board.on("ready", function() {
board.on("ready", () => {

wheels = {};
let wheels = {};

// Create two servos as our wheels
wheels.left = new five.Servo({
wheels.left = new Servo({
pin: 9,
// `type` defaults to standard servo.
// For continuous rotation servos, override the default
Expand All @@ -50,7 +49,7 @@ board.on("ready", function() {

});

wheels.right = new five.Servo({
wheels.right = new Servo({
pin: 10,
// `type` defaults to standard servo.
// For continuous rotation servos, override the default
Expand All @@ -59,11 +58,14 @@ board.on("ready", function() {
invert: true // one wheel mounted inverted of the other
});

wheels.both = new five.Servos().stop(); // reference both together
// reference both together
wheels.both = new Servos([wheels.left, wheels.right]);

wheels.stop();

// Add servos to REPL (optional)
this.repl.inject({
wheels: wheels
board.repl.inject({
wheels
});

// Drive forwards
Expand All @@ -72,9 +74,7 @@ board.on("ready", function() {
wheels.both.cw();

// Stop driving after 3 seconds
this.wait(3000, function() {
wheels.both.stop();
});
board.wait(3000, wheels.both.stop);

});

Expand Down
12 changes: 6 additions & 6 deletions docs/servo-multi-turn.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ node eg/servo-multi-turn.js


```javascript
var five = require("johnny-five");
var board = new five.Board();
const {Board, Servo} = require("johnny-five");
const board = new Board();

board.on("ready", function() {
var servo = new five.Servo({
board.on("ready", () => {
const servo = new Servo({
pin: 10,
// Cheapo servo has limited PWM range
pwmRange: [600, 2370],
Expand All @@ -31,8 +31,8 @@ board.on("ready", function() {
});

// Add servo to REPL (optional)
this.repl.inject({
servo: servo
board.repl.inject({
servo
});


Expand Down
18 changes: 9 additions & 9 deletions docs/servo-prompt.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,26 +31,26 @@ node eg/servo-prompt.js


```javascript
var five = require("johnny-five");
var readline = require("readline");
const {Board, Servo} = require("johnny-five");
const readline = require("readline");

var rl = readline.createInterface({
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});

five.Board().on("ready", function() {
var servo = new five.Servo(10);
const board = new Board();

board.on("ready", () => {
const servo = new Servo(10);

rl.setPrompt("SERVO TEST (0-180)> ");
rl.prompt();

rl.on("line", function(line) {
rl.on("line", (line) => {
servo.to(+line.trim());
rl.prompt();
}).on("close", function() {
process.exit(0);
});
}).on("close", () => process.exit(0));
});

```
Expand Down
16 changes: 7 additions & 9 deletions docs/servo-slider.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,18 +29,16 @@ node eg/servo-slider.js


```javascript
var five = require("johnny-five");
var board = new five.Board();
const {Board, Sensor, Servo} = require("johnny-five");
const board = new Board();

board.on("ready", function() {
board.on("ready", () => {

var slider = new five.Sensor("A0");
var tilt = new five.Servo(9);
const slider = new Sensor("A0");
const tilt = new Servo(9);

slider.scale([0, 180]).on("slide", function() {

// The slider's value will be scaled to match the tilt servo range
tilt.to(this.value);
slider.on("slide", () => {
tilt.to(slider.scaleTo(0, 180));
});
});

Expand Down
Loading

0 comments on commit 2ce0505

Please sign in to comment.