Skip to content

Commit

Permalink
ESC: refactor 2
Browse files Browse the repository at this point in the history
  • Loading branch information
rwaldron committed Aug 8, 2019
1 parent 12d242e commit 4108b28
Show file tree
Hide file tree
Showing 14 changed files with 384 additions and 496 deletions.
5 changes: 1 addition & 4 deletions README.md
Expand Up @@ -173,7 +173,7 @@ To get you up and running quickly, we provide a variety of examples for using ea

To interactively navigate the examples, visit the [Johnny-Five examples](http://johnny-five.io/examples/) page on the official website. If you want to link directly to the examples in this repo, you can use one of the following links.

**There are presently 363 example programs with code and diagrams!**
**There are presently 360 example programs with code and diagrams!**

<!--extract-start:examples-->

Expand Down Expand Up @@ -273,11 +273,8 @@ To interactively navigate the examples, visit the [Johnny-Five examples](http://

### ESC & Brushless Motor
- [ESC - Bidirectional](https://github.com/rwaldron/johnny-five/blob/master/docs/esc-bidirectional.md)
- [ESC - Bidirectional Forward-Reverse](https://github.com/rwaldron/johnny-five/blob/master/docs/esc-bidirectional-forward-reverse.md)
- [ESC - Dualshock controlled ESCs](https://github.com/rwaldron/johnny-five/blob/master/docs/esc-dualshock.md)
- [ESC - Keypress controlled ESCs](https://github.com/rwaldron/johnny-five/blob/master/docs/esc-keypress.md)
- [ESC - PCA9685](https://github.com/rwaldron/johnny-five/blob/master/docs/esc-PCA9685.md)
- [ESCs - An array of ESCs](https://github.com/rwaldron/johnny-five/blob/master/docs/esc-array.md)

### Button / Switch
- [Button](https://github.com/rwaldron/johnny-five/blob/master/docs/button.md)
Expand Down
13 changes: 7 additions & 6 deletions docs/esc-PCA9685.md
Expand Up @@ -29,20 +29,21 @@ node eg/esc-PCA9685.js


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

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

var esc = new five.ESC({
const esc = new ESC({
controller: "PCA9685",
device: "FORWARD",
pin: 1
});

var pot = new five.Sensor("A0");
const pot = new Sensor("A0");

pot.scale(0, 100).on("change", function() {
esc.speed(this.value);
pot.on("change", () => {
esc.throttle(pot.scaleTo(esc.pwmRange));
});
});

Expand Down
25 changes: 8 additions & 17 deletions docs/esc-bidirectional.md
Expand Up @@ -29,32 +29,23 @@ node eg/esc-bidirectional.js


```javascript
var five = require("../");
var board = new five.Board();
const {Board, Button, ESC, Sensor} = require("../");
const board = new Board();

board.on("ready", function() {
var start = Date.now();
var esc = new five.ESC({
const esc = new ESC({
device: "FORWARD_REVERSE",
neutral: 50,
pin: 11
});
var throttle = new five.Sensor("A0");
var brake = new five.Button(4);
const throttle = new Sensor("A0");
const brake = new Button(4);

brake.on("press", function() {
brake.on("press", () => {
esc.brake();
});

throttle.scale(0, 100).on("change", function() {
// 2 Seconds for arming.
if (Date.now() - start < 2e3) {
return;
}

if (esc.value !== this.value) {
esc.speed(this.value);
}
throttle.on("change", () => {
esc.throttle(throttle.scaleTo(esc.pwmRange));
});
});

Expand Down
61 changes: 37 additions & 24 deletions docs/esc-keypress.md
Expand Up @@ -29,34 +29,47 @@ node eg/esc-keypress.js


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

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

var esc = new five.ESC(9);

// Hold shift+arrow-up, shift+arrow-down to incrementally
// increase or decrease speed.

function controller(ch, key) {
var isThrottle = false;
var speed = esc.last ? esc.speed : 0;

if (key && key.shift) {
if (key.name === "up") {
speed += 0.01;
isThrottle = true;
}

if (key.name === "down") {
speed -= 0.01;
isThrottle = true;
const led = new Led(13);
const esc = new ESC({
device: "FORWARD_REVERSE",
pin: 11,
});
let speed = 0;
let last = null;

// just to make sure the program is running
led.blink(500);

function controller(_, key) {
let change = 0;

if (key) {
if (!key.shift) {
change = esc.neutral;
speed = 0;
} else {
if (key.name === "up" || key.name === "down") {
if (last !== key.name) {
change = esc.neutral;
speed = 0;
} else {
speed += 1;

change = key.name === "up" ?
esc.neutral + speed :
esc.neutral - speed;
}
last = key.name;
}
}

if (isThrottle) {
esc.speed(speed);
if (change) {
esc.throttle(change);
}
}
}
Expand Down
13 changes: 7 additions & 6 deletions eg/esc-PCA9685.js
@@ -1,16 +1,17 @@
var five = require("../lib/johnny-five.js");
var board = new five.Board();
const {Board, ESC, Sensor} = require("../lib/johnny-five.js");
const board = new Board();

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

var esc = new five.ESC({
const esc = new ESC({
controller: "PCA9685",
device: "FORWARD",
pin: 1
});

var pot = new five.Sensor("A0");
const pot = new Sensor("A0");

pot.scale(0, 100).on("change", function() {
esc.speed(this.value);
pot.on("change", () => {
esc.throttle(pot.scaleTo(esc.pwmRange));
});
});
15 changes: 6 additions & 9 deletions eg/esc-array.js
@@ -1,16 +1,13 @@
var five = require("../lib/johnny-five.js");
var board = new five.Board();
const {Board, ESC} = require("../lib/johnny-five.js");
const board = new Board();

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

var escs = new five.ESCs([9, 10]);
const escs = new ESC.Collection([9, 10]);

// Set the motors to their max speed
escs.max();

board.wait(2000, function() {
// Set the motors to the min speed (stopped)
escs.min();
});
// This might be dangerous ¯\_(ツ)_/¯
escs.throttle(100);

board.wait(2000, () => escs.brake());
});
25 changes: 8 additions & 17 deletions eg/esc-bidirectional.js
@@ -1,28 +1,19 @@
var five = require("../");
var board = new five.Board();
const {Board, Button, ESC, Sensor} = require("../");
const board = new Board();

board.on("ready", function() {
var start = Date.now();
var esc = new five.ESC({
const esc = new ESC({
device: "FORWARD_REVERSE",
neutral: 50,
pin: 11
});
var throttle = new five.Sensor("A0");
var brake = new five.Button(4);
const throttle = new Sensor("A0");
const brake = new Button(4);

brake.on("press", function() {
brake.on("press", () => {
esc.brake();
});

throttle.scale(0, 100).on("change", function() {
// 2 Seconds for arming.
if (Date.now() - start < 2e3) {
return;
}

if (esc.value !== this.value) {
esc.speed(this.value);
}
throttle.on("change", () => {
esc.throttle(throttle.scaleTo(esc.pwmRange));
});
});
57 changes: 57 additions & 0 deletions eg/esc-blobfish.js
@@ -0,0 +1,57 @@
const {Board, ESC, Fn, Led} = require("../lib/johnny-five.js");
const keypress = require("keypress");

const board = new Board();

board.on("error", error => {
console.error(error);
process.exit(1);
});

board.on("ready", () => {
const led = new Led(13);
const esc = new ESC({
device: "FORWARD_REVERSE",
pin: 11,
});
let speed = 0;
let last = null;

// just to make sure the program is running
led.blink(500);

function controller(_, key) {
let change = 0;

if (key) {
if (!key.shift) {
change = esc.neutral;
speed = 0;
} else {
if (key.name === "up" || key.name === "down") {
if (last !== key.name) {
change = esc.neutral;
speed = 0;
} else {
speed += 1;

change = key.name === "up" ?
esc.neutral + speed :
esc.neutral - speed;
}
last = key.name;
}
}

if (change) {
esc.throttle(change);
}
}
}

keypress(process.stdin);

process.stdin.on("keypress", controller);
process.stdin.setRawMode(true);
process.stdin.resume();
});
61 changes: 37 additions & 24 deletions eg/esc-keypress.js
@@ -1,31 +1,44 @@
var five = require("../lib/johnny-five.js");
var keypress = require("keypress");
var board = new five.Board();
const {Board, ESC, Fn, Led} = require("../lib/johnny-five.js");
const keypress = require("keypress");
const board = new Board();

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

var esc = new five.ESC(9);

// Hold shift+arrow-up, shift+arrow-down to incrementally
// increase or decrease speed.

function controller(ch, key) {
var isThrottle = false;
var speed = esc.last ? esc.speed : 0;

if (key && key.shift) {
if (key.name === "up") {
speed += 0.01;
isThrottle = true;
}

if (key.name === "down") {
speed -= 0.01;
isThrottle = true;
const led = new Led(13);
const esc = new ESC({
device: "FORWARD_REVERSE",
pin: 11,
});
let speed = 0;
let last = null;

// just to make sure the program is running
led.blink(500);

function controller(_, key) {
let change = 0;

if (key) {
if (!key.shift) {
change = esc.neutral;
speed = 0;
} else {
if (key.name === "up" || key.name === "down") {
if (last !== key.name) {
change = esc.neutral;
speed = 0;
} else {
speed += 1;

change = key.name === "up" ?
esc.neutral + speed :
esc.neutral - speed;
}
last = key.name;
}
}

if (isThrottle) {
esc.speed(speed);
if (change) {
esc.throttle(change);
}
}
}
Expand Down

0 comments on commit 4108b28

Please sign in to comment.