Skip to content

Commit 48f367d

Browse files
HipsterBrownrwaldron
authored andcommitted
docs: update ESC examples with newer javascript syntax
1 parent 512e3a6 commit 48f367d

12 files changed

+90
-112
lines changed

docs/esc-PCA9685.md

+2-3
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,10 @@ node eg/esc-PCA9685.js
2929

3030

3131
```javascript
32-
const {Board, ESC, Sensor} = require("johnny-five");
32+
const { Board, ESC, Sensor } = require("johnny-five");
3333
const board = new Board();
3434

35-
board.on("ready", function() {
36-
35+
board.on("ready", () => {
3736
const esc = new ESC({
3837
controller: "PCA9685",
3938
device: "FORWARD",

docs/esc-array.md

+6-11
Original file line numberDiff line numberDiff line change
@@ -29,22 +29,17 @@ node eg/esc-array.js
2929

3030

3131
```javascript
32-
var five = require("johnny-five");
33-
var board = new five.Board();
32+
const {Board, ESC} = require('johnny-five');
33+
const board = new Board();
3434

35-
board.on("ready", function() {
36-
37-
var escs = new five.ESCs([9, 10]);
35+
board.on('ready', () => {
36+
const escs = new ESC.Collection([9, 10]);
3837

3938
// Set the motors to their max speed
40-
// This could be dangerous
39+
// This might be dangerous ¯\_(ツ)_/¯
4140
escs.throttle(100);
4241

43-
board.wait(2000, function() {
44-
// Set the motors to the min speed (stopped)
45-
escs.brake();
46-
});
47-
42+
board.wait(2000, esc.brake);
4843
});
4944

5045
```

docs/esc-bidirectional-forward-reverse.md

+12-14
Original file line numberDiff line numberDiff line change
@@ -29,26 +29,24 @@ node eg/esc-bidirectional-forward-reverse.js
2929

3030

3131
```javascript
32-
var five = require("../");
33-
var board = new five.Board();
32+
const {Board, Button, ESC, Sensor} = require('johnny-five');
33+
const board = new Board();
3434

35-
board.on("ready", function() {
36-
var start = Date.now();
37-
var esc = new five.ESC({
38-
device: "FORWARD_REVERSE",
35+
board.on('ready', () => {
36+
const start = Date.now();
37+
const esc = new ESC({
38+
device: 'FORWARD_REVERSE',
3939
neutral: 50,
40-
pin: 11
40+
pin: 11,
4141
});
42-
var throttle = new five.Sensor("A0");
43-
var brake = new five.Button(4);
42+
const throttle = new Sensor('A0');
43+
const brake = new Button(4);
4444

45-
brake.on("press", function() {
46-
esc.brake();
47-
});
45+
brake.on('press', esc.brake);
4846

49-
throttle.scale(0, 100).on("change", function() {
47+
throttle.scale(0, 100).on('change', () => {
5048
// 2 Seconds for arming.
51-
if (Date.now() - start < 2e3) {
49+
if (Date.now() - start < 2000) {
5250
return;
5351
}
5452

docs/esc-bidirectional.md

+3-5
Original file line numberDiff line numberDiff line change
@@ -29,20 +29,18 @@ node eg/esc-bidirectional.js
2929

3030

3131
```javascript
32-
const {Board, Button, ESC, Sensor} = require("../");
32+
const { Board, Button, ESC, Sensor } = require("johnny-five");
3333
const board = new Board();
3434

35-
board.on("ready", function() {
35+
board.on("ready", () => {
3636
const esc = new ESC({
3737
device: "FORWARD_REVERSE",
3838
pin: 11
3939
});
4040
const throttle = new Sensor("A0");
4141
const brake = new Button(4);
4242

43-
brake.on("press", () => {
44-
esc.brake();
45-
});
43+
brake.on("press", esc.brake);
4644

4745
throttle.on("change", () => {
4846
esc.throttle(throttle.scaleTo(esc.pwmRange));

docs/esc-dualshock.md

+20-22
Original file line numberDiff line numberDiff line change
@@ -29,53 +29,52 @@ node eg/esc-dualshock.js
2929

3030

3131
```javascript
32-
var five = require("../lib/johnny-five.js");
33-
var dualShock = require("dualshock-controller");
32+
const {Board, ESC, Fn} = require('johnny-five');
33+
const dualShock = require('dualshock-controller');
3434

35-
var board = new five.Board();
36-
var controller = dualShock({
37-
config: "dualShock3",
38-
analogStickSmoothing: false
35+
const board = new Board();
36+
const controller = dualShock({
37+
config: 'dualShock3',
38+
analogStickSmoothing: false,
3939
});
4040

41-
board.on("ready", function() {
41+
board.on('ready', () => {
42+
const esc = new ESC(9);
43+
let speed = 0;
44+
let last = null;
4245

43-
var esc = new five.ESC(9);
44-
var speed = 0;
45-
var last = null;
46-
47-
controller.on("connected", function() {
46+
controller.on('connected', () => {
4847
controller.isConnected = true;
4948
});
5049

51-
controller.on("dpadUp:press", function() {
52-
if (last !== "up") {
50+
controller.on('dpadUp:press', () => {
51+
if (last !== 'up') {
5352
speed = 0;
5453
} else {
5554
speed += 1;
5655
}
5756
esc.throttle(esc.neutral + speed);
58-
last = "up"
57+
last = 'up';
5958
});
6059

61-
controller.on("dpadDown:press", function() {
62-
if (last !== "down") {
60+
controller.on('dpadDown:press', () => {
61+
if (last !== 'down') {
6362
speed = 0;
6463
} else {
6564
speed += 1;
6665
}
6766
esc.throttle(esc.neutral - speed);
68-
last = "down"
67+
last = 'down';
6968
});
7069

71-
controller.on("circle:press", function() {
70+
controller.on('circle:press', () => {
7271
last = null;
7372
speed = 0;
7473
esc.brake();
7574
});
7675

77-
controller.on("right:move", function(position) {
78-
var y = five.Fn.scale(position.y, 255, 0, 0, 180) | 0;
76+
controller.on('right:move', position => {
77+
const y = Fn.scale(position.y, 255, 0, 0, 180) | 0;
7978

8079
if (y > 100) {
8180
// from the deadzone and up
@@ -86,7 +85,6 @@ board.on("ready", function() {
8685
controller.connect();
8786
});
8887

89-
9088
// Brushless motor breadboard diagram originally published here:
9189
// http://robotic-controls.com/learn/projects/dji-esc-and-brushless-motor
9290

docs/esc-keypress.md

+5-6
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,15 @@ node eg/esc-keypress.js
2929

3030

3131
```javascript
32-
const {Board, ESC, Fn, Led} = require("johnny-five");
32+
const { Board, ESC, Fn, Led } = require("johnny-five");
3333
const keypress = require("keypress");
3434
const board = new Board();
3535

36-
board.on("ready", function() {
36+
board.on("ready", () => {
3737
const led = new Led(13);
3838
const esc = new ESC({
3939
device: "FORWARD_REVERSE",
40-
pin: 11,
40+
pin: 11
4141
});
4242
let speed = 0;
4343
let last = null;
@@ -60,9 +60,8 @@ board.on("ready", function() {
6060
} else {
6161
speed += 1;
6262

63-
change = key.name === "up" ?
64-
esc.neutral + speed :
65-
esc.neutral - speed;
63+
change =
64+
key.name === "up" ? esc.neutral + speed : esc.neutral - speed;
6665
}
6766
last = key.name;
6867
}

eg/esc-PCA9685.js

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
const {Board, ESC, Sensor} = require("../lib/johnny-five.js");
1+
const { Board, ESC, Sensor } = require("../lib/johnny-five.js");
22
const board = new Board();
33

4-
board.on("ready", function() {
5-
4+
board.on("ready", () => {
65
const esc = new ESC({
76
controller: "PCA9685",
87
device: "FORWARD",

eg/esc-array.js

+3-4
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
1-
const {Board, ESC} = require("../lib/johnny-five.js");
1+
const { Board, ESC } = require("../lib/johnny-five.js");
22
const board = new Board();
33

4-
board.on("ready", function() {
5-
4+
board.on("ready", () => {
65
const escs = new ESC.Collection([9, 10]);
76

87
// Set the motors to their max speed
98
// This might be dangerous ¯\_(ツ)_/¯
109
escs.throttle(100);
1110

12-
board.wait(2000, () => escs.brake());
11+
board.wait(2000, escs.brake);
1312
});

eg/esc-bidirectional-forward-reverse.js

+12-14
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,21 @@
1-
var five = require('../');
2-
var board = new five.Board();
1+
const { Board, Button, ESC, Sensor } = require("../lib/johnny-five");
2+
const board = new Board();
33

4-
board.on('ready', function() {
5-
var start = Date.now();
6-
var esc = new five.ESC({
7-
device: 'FORWARD_REVERSE',
4+
board.on("ready", () => {
5+
const start = Date.now();
6+
const esc = new ESC({
7+
device: "FORWARD_REVERSE",
88
neutral: 50,
9-
pin: 11,
9+
pin: 11
1010
});
11-
var throttle = new five.Sensor('A0');
12-
var brake = new five.Button(4);
11+
const throttle = new Sensor("A0");
12+
const brake = new Button(4);
1313

14-
brake.on('press', function() {
15-
esc.brake();
16-
});
14+
brake.on("press", esc.brake);
1715

18-
throttle.scale(0, 100).on('change', function() {
16+
throttle.scale(0, 100).on("change", () => {
1917
// 2 Seconds for arming.
20-
if (Date.now() - start < 2e3) {
18+
if (Date.now() - start < 2000) {
2119
return;
2220
}
2321

eg/esc-bidirectional.js

+3-5
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,15 @@
1-
const {Board, Button, ESC, Sensor} = require("../");
1+
const { Board, Button, ESC, Sensor } = require("../lib/johnny-five");
22
const board = new Board();
33

4-
board.on("ready", function() {
4+
board.on("ready", () => {
55
const esc = new ESC({
66
device: "FORWARD_REVERSE",
77
pin: 11
88
});
99
const throttle = new Sensor("A0");
1010
const brake = new Button(4);
1111

12-
brake.on("press", () => {
13-
esc.brake();
14-
});
12+
brake.on("press", esc.brake);
1513

1614
throttle.on("change", () => {
1715
esc.throttle(throttle.scaleTo(esc.pwmRange));

eg/esc-dualshock.js

+17-19
Original file line numberDiff line numberDiff line change
@@ -1,60 +1,58 @@
1-
var five = require("../lib/johnny-five.js");
2-
var dualShock = require("dualshock-controller");
1+
const { Board, ESC, Fn } = require("../lib/johnny-five.js");
2+
const dualShock = require("dualshock-controller");
33

4-
var board = new five.Board();
5-
var controller = dualShock({
4+
const board = new Board();
5+
const controller = dualShock({
66
config: "dualShock3",
77
analogStickSmoothing: false
88
});
99

10-
board.on("ready", function() {
10+
board.on("ready", () => {
11+
const esc = new ESC(9);
12+
let speed = 0;
13+
let last = null;
1114

12-
var esc = new five.ESC(9);
13-
var speed = 0;
14-
var last = null;
15-
16-
controller.on("connected", function() {
15+
controller.on("connected", () => {
1716
controller.isConnected = true;
1817
});
1918

20-
controller.on("dpadUp:press", function() {
19+
controller.on("dpadUp:press", () => {
2120
if (last !== "up") {
2221
speed = 0;
2322
} else {
2423
speed += 1;
2524
}
2625
esc.throttle(esc.neutral + speed);
27-
last = "up"
26+
last = "up";
2827
});
2928

30-
controller.on("dpadDown:press", function() {
29+
controller.on("dpadDown:press", () => {
3130
if (last !== "down") {
3231
speed = 0;
3332
} else {
3433
speed += 1;
3534
}
3635
esc.throttle(esc.neutral - speed);
37-
last = "down"
36+
last = "down";
3837
});
3938

40-
controller.on("circle:press", function() {
39+
controller.on("circle:press", () => {
4140
last = null;
4241
speed = 0;
4342
esc.brake();
4443
});
4544

46-
controller.on("right:move", function(position) {
47-
var y = five.Fn.scale(position.y, 255, 0, 0, 180) | 0;
45+
controller.on("right:move", position => {
46+
const y = Fn.scale(position.y, 255, 0, 0, 180) | 0;
4847

4948
if (y > 100) {
5049
// from the deadzone and up
51-
esc.throttle(scale(y, 100, 180, 0, 100));
50+
esc.throttle(Fn.scale(y, 100, 180, 0, 100));
5251
}
5352
});
5453

5554
controller.connect();
5655
});
5756

58-
5957
// Brushless motor breadboard diagram originally published here:
6058
// http://robotic-controls.com/learn/projects/dji-esc-and-brushless-motor

0 commit comments

Comments
 (0)