-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update johnny-five, include throttle, servo examples
- Loading branch information
1 parent
129404e
commit 7deb5d8
Showing
4 changed files
with
140 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,15 @@ | ||
const five = require('johnny-five'); | ||
const {Board, Led} = require('johnny-five'); | ||
|
||
const board = new five.Board({ | ||
const board = new Board({ | ||
port: '', // path to bluetooth connection, i.e. /dev/tty.ROBOT_NAME-SPPDev or COMX | ||
}); | ||
|
||
board.on('ready', () => { | ||
const led = new five.Led(13); // use built-in led on Arduino | ||
const led = new Led(13); // use built-in led on Arduino | ||
led.blink(); | ||
}); | ||
|
||
board.on('error', error => { | ||
console.error(error); | ||
process.exit(1); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
const {Board, ESC, Fn, Led, Servo} = require('johnny-five'); | ||
const keypress = require('keypress'); | ||
|
||
const board = new Board({ | ||
port: '', // path to bluetooth connection, i.e. /dev/tty.ROBOT_NAME-SPPDev or COMX | ||
}); | ||
|
||
board.on('ready', () => { | ||
const led = new Led(13); | ||
const esc = new ESC({ | ||
device: 'FORWARD_REVERSE', | ||
pin: 11, | ||
}); | ||
const servo = new five.Servo(10); | ||
let speed = 0; | ||
let last = null; | ||
|
||
servo.center(); | ||
|
||
// 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 (key.name === 'left') { | ||
servo.to(45, 1000); | ||
} | ||
|
||
if (key.name === 'right') { | ||
servo.to(135, 1000); | ||
} | ||
|
||
if (key.name === 'c') { | ||
servo.center(); | ||
} | ||
} | ||
|
||
if (change) { | ||
esc.throttle(change); | ||
} | ||
} | ||
} | ||
|
||
keypress(process.stdin); | ||
|
||
process.stdin.on('keypress', controller); | ||
process.stdin.setRawMode(true); | ||
process.stdin.resume(); | ||
}); | ||
|
||
board.on('error', error => { | ||
console.error(error); | ||
process.exit(1); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
const {Board, ESC, Fn, Led} = require('johnny-five'); | ||
const keypress = require('keypress'); | ||
|
||
const board = new Board({ | ||
port: '', // path to bluetooth connection, i.e. /dev/tty.ROBOT_NAME-SPPDev or COMX | ||
}); | ||
|
||
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(); | ||
}); | ||
|
||
board.on('error', error => { | ||
console.error(error); | ||
process.exit(1); | ||
}); |