Skip to content

Commit

Permalink
Merge pull request #56 from dtex/servoTests
Browse files Browse the repository at this point in the history
Servo tests
  • Loading branch information
dtex authored Jul 5, 2020
2 parents 182da27 + 6abebe0 commit 4471c4a
Show file tree
Hide file tree
Showing 9 changed files with 245 additions and 110 deletions.
55 changes: 2 additions & 53 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
## j5e Repo Anatomy
One goal of j5e is write once, run anywhere. We are building toward this goal before TC-53 IOCP conformant platforms exist. To that end, the structure of this repo may change to help us support more platforms. The JS is the same across platforms, but the different implementations of ECMAScript Modules may require special concessions (package.json vs manifest.json and varying module resolution schemes).
One goal of j5e is write once, run anywhere. We are building toward this goal before TC-53 IOCP conformant platforms exist. To that end, the structure of this repo may change to help us support more platforms. The JS is the same across platforms, but the different implementations of ECMAScript Modules may require special concessions (package.json vs manifest.json and varying module resolution schemes for example).

### Special Scaffolding
j5e is designed to import just the parts it needs, so subpath exports in node.js are absolutely necessary. To make everything link up you will need to symlink your dev folder to itself. From the root run ```npm link``` and then ```npm link j5e```. This will allow tests to import subpaths, and for subpaths to import their siblings.
Expand All @@ -11,55 +11,4 @@ j5e is designed to import just the parts it needs, so subpath exports in node.js
Tests are run using Mocha, Chai, Sinon, and node's built-in Assert module. Each class has its own js file in the ```./tests/``` folder. You can run tests by running ```npm run test``` or on a specific module by running ```npm run test ./test/rgb``` for example.

**Test Organiztion**
Test files should be organized with the following heirarchy

````js
describe('className', function() {

describe('Instantiation', function() {
// All tests related to default instantiation
/* describe('Options', function() {
describe('someOptionProperty', async function() {
it('should be configured appropriately for the option', async function() {
// ...
});
// [ All other tests related to sink ]
});
}); */
// [ All other options, each with it's own describe ]
}

describe('Properties', function() {
/* describe('someProperty', function() {
it('should respong with the property value', async function() {
// ...
});
[ all other tests related to someProperty ]
});
*/
// [ All other properties, each with it's own describe ]
});

describe('Methods', function() {
/* describe('doSomething', function() {
it('should doSomething', async function() {
// ...
});
// [ all other tests related to doSomething ]
}); */
// [ All other methods, each with it's own describe ]
});

describe('Events', function() {
/* describe('someEvent', function() {
it('should fire the event at the right time', async function() {
// ...
});
// [ all other tests related to the event ]
}); */
// [ All other Events, each with it's own describe ]
});

});
````
Test files should be organized with the heirarchy described in [/build/templates/test.js](build/templates/test.js).
77 changes: 77 additions & 0 deletions build/templates/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import assert from "assert";
import sinon from "sinon";
import { Digital, PWM } from "@dtex/mock-io";
import ClassName from "j5e/classname";

describe("ClassName", function() {

describe("Instantiation", function() {

// All tests related to default instantiation

describe("Options", function() {

describe("someOptionProperty", async function() {

it("should be configured appropriately for the option", async function() {
// ...
});

// [ All other tests related to this option ]

});
});

// [ All other options, each with it's own describe ]

});

describe("Properties", function() {

describe("someProperty", function() {

it("should respond with the property value", async function() {
// ...
});

// [ all other tests related to someProperty ]

});

// [ All other properties, each with it's own describe ]

});

describe("Methods", function() {

describe("someMethod", function() {

it("should do the right thing", async function() {
// ...
});

// [ all other tests related to someMethod ]

});

// [ All other methods, each with it's own describe ]

});

describe("Events", function() {

describe("someEvent", function() {

it("should emit the event at the appropriate time", async function() {
// ...
});

// [ all other tests related to someEvent ]

});

// [ All other events, each with it's own describe ]

});

});
33 changes: 20 additions & 13 deletions docs/module-j5e_servo-Servo.html
Original file line number Diff line number Diff line change
Expand Up @@ -170,9 +170,9 @@ <h5>Parameters</h5>
<td><code>deadband</code></td>
<td><span class="param-type">Array.&lt;number></span></td>
<td class="attributes">&lt;optional&gt;<br/></td>
<td>[1500,1500]</td>
<td>[90,90]</td>
<td class="last">
The range at which a continuos motion servo will not turn
The degree range at which a continuos motion servo will not turn
</td>
</tr>
<tr>
Expand Down Expand Up @@ -321,6 +321,13 @@ <h5>Properties</h5>
The corrected position (factors in offset and invert)
</td>
</tr>
<tr>
<td><code>pulseWidth</code></td>
<td><span class="param-type">Array.&lt;object></span></td>
<td class="last">
The corrected pulseWidth (factors in offset and invert)
</td>
</tr>
</tbody>
</table>
</td>
Expand Down Expand Up @@ -384,7 +391,7 @@ <h5>Properties</h5>
<div class="form-group">
<label class="col-sm-3 control-label">Source</label>
<div class="col-sm-9">
<p class="form-control-static"><a href="servo_index.js.html">servo/index.js</a>, <a href="servo_index.js.html#line-68">line 68</a></p>
<p class="form-control-static"><a href="servo_index.js.html">servo/index.js</a>, <a href="servo_index.js.html#line-69">line 69</a></p>
</div>
</div>

Expand Down Expand Up @@ -503,7 +510,7 @@ <h5>Parameters</h5>
<div class="form-group">
<label class="col-sm-3 control-label">Source</label>
<div class="col-sm-9">
<p class="form-control-static"><a href="servo_index.js.html">servo/index.js</a>, <a href="servo_index.js.html#line-213">line 213</a></p>
<p class="form-control-static"><a href="servo_index.js.html">servo/index.js</a>, <a href="servo_index.js.html#line-208">line 208</a></p>
</div>
</div>

Expand Down Expand Up @@ -595,7 +602,7 @@ <h5>Parameters</h5>
<div class="form-group">
<label class="col-sm-3 control-label">Source</label>
<div class="col-sm-9">
<p class="form-control-static"><a href="servo_index.js.html">servo/index.js</a>, <a href="servo_index.js.html#line-314">line 314</a></p>
<p class="form-control-static"><a href="servo_index.js.html">servo/index.js</a>, <a href="servo_index.js.html#line-310">line 310</a></p>
</div>
</div>

Expand Down Expand Up @@ -677,7 +684,7 @@ <h5>Parameters</h5>
<div class="form-group">
<label class="col-sm-3 control-label">Source</label>
<div class="col-sm-9">
<p class="form-control-static"><a href="servo_index.js.html">servo/index.js</a>, <a href="servo_index.js.html#line-336">line 336</a></p>
<p class="form-control-static"><a href="servo_index.js.html">servo/index.js</a>, <a href="servo_index.js.html#line-332">line 332</a></p>
</div>
</div>

Expand Down Expand Up @@ -759,7 +766,7 @@ <h5>Parameters</h5>
<div class="form-group">
<label class="col-sm-3 control-label">Source</label>
<div class="col-sm-9">
<p class="form-control-static"><a href="servo_index.js.html">servo/index.js</a>, <a href="servo_index.js.html#line-358">line 358</a></p>
<p class="form-control-static"><a href="servo_index.js.html">servo/index.js</a>, <a href="servo_index.js.html#line-354">line 354</a></p>
</div>
</div>

Expand Down Expand Up @@ -851,7 +858,7 @@ <h5>Parameters</h5>
<div class="form-group">
<label class="col-sm-3 control-label">Source</label>
<div class="col-sm-9">
<p class="form-control-static"><a href="servo_index.js.html">servo/index.js</a>, <a href="servo_index.js.html#line-390">line 390</a></p>
<p class="form-control-static"><a href="servo_index.js.html">servo/index.js</a>, <a href="servo_index.js.html#line-386">line 386</a></p>
</div>
</div>

Expand Down Expand Up @@ -908,7 +915,7 @@ <h4 id="home" class="function public"><span class="name">home</span><span class=
<div class="form-group">
<label class="col-sm-3 control-label">Source</label>
<div class="col-sm-9">
<p class="form-control-static"><a href="servo_index.js.html">servo/index.js</a>, <a href="servo_index.js.html#line-409">line 409</a></p>
<p class="form-control-static"><a href="servo_index.js.html">servo/index.js</a>, <a href="servo_index.js.html#line-405">line 405</a></p>
</div>
</div>

Expand Down Expand Up @@ -993,7 +1000,7 @@ <h5>Parameters</h5>
<div class="form-group">
<label class="col-sm-3 control-label">Source</label>
<div class="col-sm-9">
<p class="form-control-static"><a href="servo_index.js.html">servo/index.js</a>, <a href="servo_index.js.html#line-436">line 436</a></p>
<p class="form-control-static"><a href="servo_index.js.html">servo/index.js</a>, <a href="servo_index.js.html#line-432">line 432</a></p>
</div>
</div>

Expand Down Expand Up @@ -1042,7 +1049,7 @@ <h4 id="stop" class="function public"><span class="name">stop</span><span class=
<div class="form-group">
<label class="col-sm-3 control-label">Source</label>
<div class="col-sm-9">
<p class="form-control-static"><a href="servo_index.js.html">servo/index.js</a>, <a href="servo_index.js.html#line-479">line 479</a></p>
<p class="form-control-static"><a href="servo_index.js.html">servo/index.js</a>, <a href="servo_index.js.html#line-475">line 475</a></p>
</div>
</div>

Expand Down Expand Up @@ -1124,7 +1131,7 @@ <h5>Parameters</h5>
<div class="form-group">
<label class="col-sm-3 control-label">Source</label>
<div class="col-sm-9">
<p class="form-control-static"><a href="servo_index.js.html">servo/index.js</a>, <a href="servo_index.js.html#line-514">line 514</a></p>
<p class="form-control-static"><a href="servo_index.js.html">servo/index.js</a>, <a href="servo_index.js.html#line-510">line 510</a></p>
</div>
</div>

Expand Down Expand Up @@ -1206,7 +1213,7 @@ <h5>Parameters</h5>
<div class="form-group">
<label class="col-sm-3 control-label">Source</label>
<div class="col-sm-9">
<p class="form-control-static"><a href="servo_index.js.html">servo/index.js</a>, <a href="servo_index.js.html#line-538">line 538</a></p>
<p class="form-control-static"><a href="servo_index.js.html">servo/index.js</a>, <a href="servo_index.js.html#line-534">line 534</a></p>
</div>
</div>

Expand Down
38 changes: 17 additions & 21 deletions docs/servo_index.js.html
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ <h1><span class="name">servo/index.js</span></h1>
* @param {(string|constructor)} [options.io=builtin/pwm] - If passing an object, a string specifying a path to the IO provider or a constructor
* @param {string} [options.type="standard"] - Type of servo ("standard" or "continuous")
* @param {number[]} [options.pwmRange=[600, 2400]] - The pulse width range in microseconds
* @param {number[]} [options.deadband=[1500,1500]] - The range at which a continuos motion servo will not turn
* @param {number[]} [options.deadband=[90,90]] - The degree range at which a continuos motion servo will not turn
* @param {number[]} [options.range=[0, 180]] - The allowed range of motion in degrees
* @param {number[]} [options.deviceRange=[0, 180]] - The physical range of the servo in degrees
* @param {number} [options.startAt="Any value within options.range"] - The desired start position of the servo
Expand All @@ -146,6 +146,7 @@ <h1><span class="name">servo/index.js</span></h1>
* @property {object[]} last.timestamp - Timestamp of position update
* @property {object[]} last.target - The user requested position
* @property {object[]} last.degrees - The corrected position (factors in offset and invert)
* @property {object[]} last.pulseWidth - The corrected pulseWidth (factors in offset and invert)
* @property {number} position - The most recent request and corrected position (factors in offset and invert)
* @example
* &lt;caption>Sweep a servo back and forth&lt;/caption>
Expand Down Expand Up @@ -175,14 +176,8 @@ <h1><span class="name">servo/index.js</span></h1>
});

this.#state.pwmRange = options.pwmRange || [600, 2400];

// This line is a hack until we can write in µs
this.#state.pwmRange = [34, 120];
this.#state.degreeRange = options.degreeRange || [0, 180];
this.#state.deadband = options.deadband || [1500, 1500];

// This line is a hack until we can write in µs
this.#state.deadband = options.deadband || [77, 77];
this.#state.deadband = options.deadband || [90, 90];
this.#state.offset = options.offset || 0;
this.#state.startAt = options.startAt || (this.#state.degreeRange[1] - this.#state.degreeRange[0]) / 2;
this.#state.range = options.range || [0 - this.offset, 180 - this.offset];
Expand Down Expand Up @@ -218,8 +213,6 @@ <h1><span class="name">servo/index.js</span></h1>

this.initialize(options);

// If "startAt" is defined and center is falsy
// set servo to min or max degrees
if (typeof options.startAt !== "undefined") {
this.to(options.startAt);
} else {
Expand Down Expand Up @@ -248,7 +241,7 @@ <h1><span class="name">servo/index.js</span></h1>

/**
* Calls the write param on the IO instance for this servo.
* @param {number} degrees - The absolute position to move a servo to
* @param {number} pulseWidth - The target pulseWidth
* @returns {Servo}
* @ignore
*/
Expand All @@ -259,17 +252,19 @@ <h1><span class="name">servo/index.js</span></h1>
return this;
}

// Map value from degreeRange to pwmRange
let microseconds = map(
degrees,
this.#state.degreeRange[0], this.#state.degreeRange[1],
this.#state.pwmRange[0], this.#state.pwmRange[1]
);
// Presumably not all IO's will support writeMicroseconds
if (this.io.writeMicroseconds) {

let microseconds = map(degrees, this.#state.degreeRange[0], this.#state.degreeRange[1], this.#state.pwmRange[0], this.#state.pwmRange[1]);
this.io.write(microseconds | 0);

} else {

let value = map(degrees, this.#state.degreeRange[0], this.#state.degreeRange[1], 0, this.io.resolution ** 2 - 1);
this.io.write(value | 0);

// Restrict values to integers
microseconds |= 0;
}

this.io.write(microseconds);
}

/**
Expand Down Expand Up @@ -378,6 +373,7 @@ <h1><span class="name">servo/index.js</span></h1>
degrees: degrees,
target: target
});

}
}

Expand Down Expand Up @@ -609,7 +605,7 @@ <h1><span class="name">servo/index.js</span></h1>
*/
cw(speed = 1) {
speed = constrain(speed, 0, 1);
speed = map(speed, 0, 1, this.#state.deadband[1] + 1, this.#state.pwmRange[1]);
speed = map(speed, 0, 1, this.#state.deadband[1] + 1, this.#state.degreeRange[1]);
return this.to(speed);
}

Expand Down
4 changes: 4 additions & 0 deletions examples/servo-sweep/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import Servo from "j5e/servo";

const servo = await new Servo(12);
servo.sweep();
9 changes: 9 additions & 0 deletions examples/servo-sweep/manifest.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"include": [
"$(MODDABLE)/modules/io/manifest.json",
"$(j5e)/lib/servo/manifest.json"
],
"modules": {
"*": "./main"
}
}
Loading

0 comments on commit 4471c4a

Please sign in to comment.