Skip to content
This repository has been archived by the owner on Dec 20, 2019. It is now read-only.

Feature/streaming quaternion #88

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions examples/stream-quaternions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
"use strict";

var sphero = require("../");
var orb = sphero(process.env.PORT);

orb.connect(function() {
orb.streamQuaternions();

orb.on("quaternions", function(data) {
console.log("quaternions:");
console.log(" sensor:", data.quaternionQ0.sensor);
console.log(" range:", data.quaternionQ0.range);
console.log(" units:", data.quaternionQ0.units);
console.log(" value:", data.quaternionQ0.value[0]);

console.log(" sensor:", data.quaternionQ1.sensor);
console.log(" range:", data.quaternionQ1.range);
console.log(" units:", data.quaternionQ1.units);
console.log(" value:", data.quaternionQ1.value[0]);

console.log(" sensor:", data.quaternionQ2.sensor);
console.log(" range:", data.quaternionQ2.range);
console.log(" units:", data.quaternionQ2.units);
console.log(" value:", data.quaternionQ2.value[0]);

console.log(" sensor:", data.quaternionQ3.sensor);
console.log(" range:", data.quaternionQ3.range);
console.log(" units:", data.quaternionQ3.units);
console.log(" value:", data.quaternionQ3.value[0]);
});

orb.roll(180, 0);
});
30 changes: 30 additions & 0 deletions lib/devices/custom.js
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,36 @@ module.exports = function custom(device) {
return device.setStabilization(1, callback);
};

/**
* Starts streaming of quaternions
*
* It uses sphero's data streaming command. User needs to listen
* for the `dataStreaming` or `quaternions` event to get the data.
*
* @param {Number} [sps=5] samples per second
* @param {Boolean} [remove=false] forces streamin of quaternions to stop
* @example
* orb.streamQuaternions();
*
* orb.on("quaternions", function(data) {
* console.log("data:");
* console.log(" quaternionQ0:", data.quaternionQ0);
* console.log(" quaternionQ1:", data.quaternionQ1);
* console.log(" quaternionQ2:", data.quaternionQ2);
* console.log(" quaternionQ3:", data.quaternionQ3);
* });
* @return {object} promise for command
*/
device.streamQuaternions = function(sps, remove) {
return device.streamData({
event: "quaternions",
mask2: 0xF0000000,
fields: ["quaternionQ0", "quaternionQ1", "quaternionQ2", "quaternionQ3"],
sps: sps,
remove: remove
});
};

/**
* Starts streaming of odometer data
*
Expand Down
15 changes: 15 additions & 0 deletions spec/lib/devices/custom.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,21 @@ describe("Custom Device Functions", function() {
device.streamData.restore();
});

it("#streamQuaternions calls #streamData with", function() {
var opts = {
event: "quaternions",
mask2: 0xF0000000,
fields: ["quaternionQ0", "quaternionQ1", "quaternionQ2", "quaternionQ3"],
sps: 2,
remove: false
};

device.streamQuaternions(2, false);

expect(device.streamData).to.be.calledOnce;
expect(device.streamData).to.be.calledWith(opts);
});

it("#streamOdometer calls #streamData with", function() {
var opts = {
event: "odometer",
Expand Down