Skip to content

Commit

Permalink
Merge 5de2f5a into 944ccad
Browse files Browse the repository at this point in the history
  • Loading branch information
dtex committed Apr 30, 2018
2 parents 944ccad + 5de2f5a commit 1ab82b1
Show file tree
Hide file tree
Showing 12 changed files with 229 additions and 83 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,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 362 example programs with code and diagrams!**
**There are presently 363 example programs with code and diagrams!**

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

Expand Down Expand Up @@ -224,6 +224,7 @@ To interactively navigate the examples, visit the [Johnny-Five examples](http://
- [Servo](https://github.com/rwaldron/johnny-five/blob/master/docs/servo.md)
- [Servo - Continuous](https://github.com/rwaldron/johnny-five/blob/master/docs/servo-continuous.md)
- [Servo - Drive](https://github.com/rwaldron/johnny-five/blob/master/docs/servo-drive.md)
- [Servo - Multi-Turn](https://github.com/rwaldron/johnny-five/blob/master/docs/servo-multi-turn.md)
- [Servo - PCA9685](https://github.com/rwaldron/johnny-five/blob/master/docs/servo-PCA9685.md)
- [Servo - Prompt](https://github.com/rwaldron/johnny-five/blob/master/docs/servo-prompt.md)
- [Servo - Slider control](https://github.com/rwaldron/johnny-five/blob/master/docs/servo-slider.md)
Expand Down
86 changes: 86 additions & 0 deletions docs/servo-multi-turn.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
<!--remove-start-->

# Servo - Multi-Turn

<!--remove-end-->








Run this example from the command line with:
```bash
node eg/servo-multi-turn.js
```


```javascript
var five = require("johnny-five");
var board = new five.Board();

board.on("ready", function() {
var servo = new five.Servo({
pin: 10,
// Cheapo servo has limited PWM range
pwmRange: [600, 2370],
// This multi-turn servo can turn 2430 degrees or 6.75 revolutions
deviceRange: [0, 2430]
});

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


// Servo API

// min()
//
// move the servo to its minimum position
//
// eg. servo.min();

// max()
//
// move the servo to its maximum position of 6.75 turns from the minimum
//
// eg. servo.max();

// center()
//
// centers the servo at 3 3/8 turns from the minimum
//
// servo.center();

// to( deg )
//
// Moves the servo to 2 whole turns from the minimum
//
// servo.to( 720 );

});

```








&nbsp;

<!--remove-start-->

## License
Copyright (c) 2012, 2013, 2014 Rick Waldron <waldron.rick@gmail.com>
Licensed under the MIT license.
Copyright (c) 2018 The Johnny-Five Contributors
Licensed under the MIT license.

<!--remove-end-->
45 changes: 45 additions & 0 deletions eg/servo-multi-turn.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
var five = require("../lib/johnny-five.js");
var board = new five.Board();

board.on("ready", function() {
var servo = new five.Servo({
pin: 10,
// Cheapo servo has limited PWM range
pwmRange: [600, 2370],
// This multi-turn servo can turn 2430 degrees or 6.75 revolutions
deviceRange: [0, 2430]
});

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


// Servo API

// min()
//
// move the servo to its minimum position
//
// eg. servo.min();

// max()
//
// move the servo to its maximum position of 6.75 turns from the minimum
//
// eg. servo.max();

// center()
//
// centers the servo at 3 3/8 turns from the minimum
//
// servo.center();

// to( deg )
//
// Moves the servo to 2 whole turns from the minimum
//
// servo.to( 720 );

});
15 changes: 8 additions & 7 deletions lib/esc.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ var Controllers = {
},
write: {
writable: true,
value: function(pin, degrees) {
value: function(pin, microseconds) {
var state = priv.get(this);
state.expander.servoWrite(pin, degrees);
state.expander.servoWrite(pin, microseconds);
}
}
},
Expand All @@ -56,8 +56,9 @@ var Controllers = {
},
write: {
writable: true,
value: function(pin, degrees) {
this.io.servoWrite(pin, degrees);
value: function(pin, microseconds) {
microseconds |= 0;
this.io.servoWrite(pin, microseconds);
}
}
}
Expand Down Expand Up @@ -295,7 +296,7 @@ ESC.prototype.speed = function(speed) {
}

if (noInterval) {
this.write(this.pin, Fn.fscale(speed, 0, 100, 0, 180));
this.write(this.pin, Fn.fscale(speed, 0, 100, this.pwmRange[0], this.pwmRange[1]));

history.push({
timestamp: Date.now(),
Expand All @@ -314,7 +315,7 @@ ESC.prototype.speed = function(speed) {
throttle--;
}

this.write(this.pin, (throttle * 180 / 100));
this.write(this.pin, Fn.fscale(throttle, 0, 100, this.pwmRange[0], this.pwmRange[1]));

history.push({
timestamp: Date.now(),
Expand Down Expand Up @@ -397,7 +398,7 @@ ESC.prototype.stop = function() {
var history = state.history;
var speed = this.type === "bidirectional" ? this.neutral : 0;

this.write(this.pin, Fn.fscale(speed, 0, 100, 0, 180));
this.write(this.pin, Fn.fscale(speed, 0, 100, this.pwmRange[0], this.pwmRange[1]));

history.push({
timestamp: Date.now(),
Expand Down
11 changes: 9 additions & 2 deletions lib/expander.js
Original file line number Diff line number Diff line change
Expand Up @@ -681,9 +681,16 @@ var Controllers = {
servoWrite: {
value: function(pin, value) {

value = Board.constrain(value, 0, 180);
var off;

var off = Fn.map(value, 0, 180, this.pwmRange[0] / 4, this.pwmRange[1] / 4);
if (value < 544) {
value = Board.constrain(value, 0, 180);
off = Fn.map(value, 0, 180, this.pwmRange[0] / 4, this.pwmRange[1] / 4);
} else {
off = value / 4;
}

off |= 0;

this.io.i2cWrite(this.address, [
this.REGISTER.BASE + 4 * pin,
Expand Down
37 changes: 25 additions & 12 deletions lib/servo.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ var Controllers = {
},
update: {
writable: true,
value: function(degrees) {
value: function(microseconds) {
var state = priv.get(this);
state.expander.servoWrite(this.pin, degrees);
state.expander.servoWrite(this.pin, microseconds);
}
}
},
Expand All @@ -62,15 +62,23 @@ var Controllers = {
update: {
writable: true,
value: function(degrees) {
// Servo is restricted to integers
degrees |= 0;

// If same degrees return immediately.
if (this.last && this.last.degrees === degrees) {
return this;
}

this.io.servoWrite(this.pin, degrees);
// Map value from degreeRange to pwmRange
var microseconds = Fn.map(
degrees,
this.degreeRange[0], this.degreeRange[1],
this.pwmRange[0], this.pwmRange[1]
);

// Restrict values to integers
microseconds |= 0;

this.io.servoWrite(this.pin, microseconds);
}
}
}
Expand All @@ -97,6 +105,9 @@ function Servo(opts) {
this, opts = Board.Options(opts)
);

this.degreeRange = opts.degreeRange || [0, 180];
this.pwmRange = opts.pwmRange || [600, 2400];
this.range = opts.range || this.degreeRange;
this.deadband = opts.deadband || [90, 90];
this.fps = opts.fps || 100;
this.offset = opts.offset || 0;
Expand Down Expand Up @@ -139,10 +150,7 @@ function Servo(opts) {
}
this.invert = opts.isInverted || opts.invert || false;

// Allow "setup"instructions to come from
// constructor options properties
this.startAt = 90;


// Collect all movement history for this servo
// history = [
// {
Expand All @@ -151,6 +159,9 @@ function Servo(opts) {
// }
// ];

// Allow "setup"instructions to come from
// constructor options properties

if (opts.controller && typeof opts.controller === "string") {
controller = Controllers[opts.controller.toUpperCase()];
} else {
Expand Down Expand Up @@ -189,9 +200,11 @@ function Servo(opts) {

// If "startAt" is defined and center is falsy
// set servo to min or max degrees
if (opts.startAt !== undefined) {
if (typeof opts.startAt !== "undefined") {
this.startAt = opts.startAt;
this.to(opts.startAt);
} else {
this.startAt = (this.degreeRange[1] - this.degreeRange[0]) / 2 + this.degreeRange[0];
}

// If "center" true set servo to 90deg
Expand Down Expand Up @@ -282,8 +295,8 @@ Servo.prototype.to = function(degrees, time, rate) {
if (this.invert) {
degrees = Fn.map(
degrees,
0, 180,
180, 0
this.degreeRange[0], this.degreeRange[1],
this.degreeRange[1], this.degreeRange[0]
);
}

Expand Down
2 changes: 1 addition & 1 deletion test/animation.js
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ exports["Animation -- Servo"] = {

this.animation.target["@@render"](val);
test.equal(testContext.servoWrite.args[1][0], 3);
test.equal(testContext.servoWrite.args[1][1], 80);
test.equal(testContext.servoWrite.args[1][1], 1404);

test.done();
},
Expand Down
4 changes: 3 additions & 1 deletion test/board.js
Original file line number Diff line number Diff line change
Expand Up @@ -490,7 +490,7 @@ exports["Board"] = {
},

snapshot: function(test) {
test.expect(69);
test.expect(71);

new Multi({
controller: "BME280",
Expand Down Expand Up @@ -653,6 +653,8 @@ exports["Board"] = {
pin: 10
}, {
deadband: [90, 90],
degreeRange: [ 0, 180 ],
pwmRange: [ 600, 2400 ],
startAt: 90,
value: null,
position: -1,
Expand Down
9 changes: 4 additions & 5 deletions test/esc.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,23 +96,22 @@ exports["ESC"] = {
this.clock.tick(120);
test.equal(this.servoWrite.callCount, 10);
// (10 * 180 / 100) | 0 = 18
test.equal(this.servoWrite.lastCall.args[1], 18);
test.equal(this.servoWrite.lastCall.args[1], 729);

this.servoWrite.reset();

this.esc.speed(9);
this.clock.tick(10);
test.equal(this.servoWrite.callCount, 1);
// (9 * 180 / 100) = 16.2
test.equal(this.servoWrite.lastCall.args[1], 16.200000762939453);
test.equal(this.servoWrite.lastCall.args[1], 711);

this.servoWrite.reset();

this.esc.speed(12);
this.clock.tick(30);
test.equal(this.servoWrite.callCount, 3);
// (12 * 180 / 100) = 21.6
test.equal(this.servoWrite.lastCall.args[1], 21.6);
test.equal(this.servoWrite.lastCall.args[1], 766);

test.done();
},
Expand Down Expand Up @@ -463,7 +462,7 @@ exports["ESC - FORWARD_REVERSE"] = {

test.ok(spy.calledOnce);
test.equal(spy.getCall(0).args[0], 11);
test.equal(spy.getCall(0).args[1], 90);
test.equal(spy.getCall(0).args[1], 1472);

spy.restore();
test.done();
Expand Down
8 changes: 4 additions & 4 deletions test/servo.collection.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,12 +95,12 @@ exports["Servo.Collection"] = {
}]);

this.servos.to(180);
test.ok(this.servoWrite.calledWith(9, 180));
test.ok(this.servoWrite.calledWith(11, 180));
test.ok(this.servoWrite.calledWith(9, 2400));
test.ok(this.servoWrite.calledWith(11, 2400));

this.servos.home();
test.ok(this.servoWrite.calledWith(9, 40));
test.ok(this.servoWrite.calledWith(11, 20));
test.ok(this.servoWrite.calledWith(9, 1000));
test.ok(this.servoWrite.calledWith(11, 800));

test.done();
},
Expand Down
Loading

0 comments on commit 1ab82b1

Please sign in to comment.