Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add motor configuration #5

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
24 changes: 23 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,25 @@ Create a connection to the Hub. Internally calls `bleReadyAsync`, `hubFoundAsync
const hub = await boost.getHubAsync();
```

#### Motor configuration

Vernie's left motor is 'A' and right 'B'. Car's left motor is 'B' and right 'A'. By default `getHubAsync` will use Vernie's configuration.

```js
// These both use same the configuration
const hub = await boost.getHubAsync();

const config = { left: 'A', right: 'B' };
const hub = await boost.getHubAsync(config);
```

Boost has `motorConfig`-object with car and vernie configurations.

```js
const vernieConfig = boost.motorConfig.vernie; // { left: 'A', right: 'B' }
const carConfig = boost.motorConfig.car; // { left: 'B', right: 'A' }
```

### boost.bleReadyAsync()

Wait for BLE device to be ready.
Expand All @@ -92,12 +111,15 @@ Wait for MoveHub found event.
const connectDetails = await boost.hubFoundAsync();
```

### boost.connectAsync(connectDetails)
### boost.connectAsync(connectDetails, motorConfig = defaultConfig)

Initialize and wait for the connection to the Hub.

```js
const hub = await boost.connectAsync(connectDetails);

// connectAsync has an optional motorConfiguration
const hub = await boost.connectAsync(connectDetails, boost.motorConfig.car);
```

## Hub
Expand Down
51 changes: 42 additions & 9 deletions movehub-async.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,21 @@ const waitForValueToSet = function(valueName, compareFunc = (valueName) => this[
});
};

const defaultConfiguration = { left: 'B', right: 'A' };

const validateConfiguration = function(motorConfiguration){
const validMotors = ['A', 'B'];

if (!validMotors.includes(motorConfiguration.left))
throw Error('Define left port port correctly');

if (!validMotors.includes(motorConfiguration.right))
throw Error('Define right port port correctly');

if (motorConfiguration.left === motorConfiguration.right)
throw Error('Left and right motor can not be same');
}

/**
* Disconnect Hub
* @method Hub#disconnectAsync
Expand All @@ -30,8 +45,12 @@ Hub.prototype.disconnectAsync = function() {
/**
* Execute this method after new instance of Hub is created
* @method Hub#afterInitialization
* @param {object} [configuration = defaultConfiguration] Boost device's engine configuration
*/
Hub.prototype.afterInitialization = function() {
Hub.prototype.afterInitialization = function(configuration = defaultConfiguration) {
validateConfiguration(configuration);
this.configuration = configuration;

this.hubDisconnected = null;
this.ports = {
A: { angle: 0 },
Expand Down Expand Up @@ -193,8 +212,8 @@ Hub.prototype.setFrictionModifier = function(modifier) {
*/
Hub.prototype.drive = function(distance, wait = true) {
const angle = Math.abs(distance) * ((this.useMetric ? METRIC_MODIFIER : IMPERIAL_MODIFIER) * this.modifier);
const dutyCycleA = DRIVE_SPEED * (distance > 0 ? 1 : -1);
const dutyCycleB = DRIVE_SPEED * (distance > 0 ? 1 : -1);
const dutyCycleA = DRIVE_SPEED * (distance > 0 ? 1 : -1) * (this.configuration.left === 'A' ? 1 : -1);
const dutyCycleB = DRIVE_SPEED * (distance > 0 ? 1 : -1) * (this.configuration.left === 'A' ? 1 : -1);
return this.motorAngleMultiAsync(angle, dutyCycleA, dutyCycleB, wait);
}

Expand All @@ -207,8 +226,10 @@ Hub.prototype.drive = function(distance, wait = true) {
*/
Hub.prototype.turn = function(degrees, wait = true) {
const angle = Math.abs(degrees) * TURN_MODIFIER;
const dutyCycleA = TURN_SPEED * (degrees > 0 ? 1 : -1);
const dutyCycleB = TURN_SPEED * (degrees > 0 ? -1 : 1);
const leftTurn = TURN_SPEED * (degrees > 0 ? 1 : -1);
const rightTurn = TURN_SPEED * (degrees > 0 ? -1 : 1);
const dutyCycleA = this.configuration.left === 'A' ? leftTurn : rightTurn;
const dutyCycleB = this.configuration.left === 'A' ? rightTurn : leftTurn;
return this.motorAngleMultiAsync(angle, dutyCycleA, dutyCycleB, wait);
}

Expand Down Expand Up @@ -281,15 +302,16 @@ Boost.prototype.hubFoundAsync = function() {
* @param {string} hubDetails.uuid
* @param {string} hubDetails.address
* @param {string} hubDetails.localName
* @param {object} [configuration = defaultConfiguration] Boost device's engine configuration
* @returns {Promise<Hub>} Hub object
*/
Boost.prototype.connectAsync = function(hubDetails) {
Boost.prototype.connectAsync = function(hubDetails, configuration) {
return new Promise((resolve, reject) => {
this.connect(hubDetails.address, async (err, hub) => {
if (err) {
reject(err);
} else {
hub.afterInitialization();
hub.afterInitialization(configuration);
await waitForValueToSet.bind(hub)('connected');
resolve(hub);
}
Expand All @@ -300,12 +322,15 @@ Boost.prototype.connectAsync = function(hubDetails) {
/**
* Connect to a MoveHub and get Hub instance
* @method Boost#getHubAsync
* @param {object} [configuration = defaultConfiguration] Boost device's engine configuration
* @returns {Promise<Hub>} Hub object
*/
Boost.prototype.getHubAsync = async function() {
Boost.prototype.getHubAsync = async function(configuration = defaultConfiguration) {
validateConfiguration(configuration);

await this.bleReadyAsync();
const connectDetails = await this.hubFoundAsync();
return await this.connectAsync(connectDetails);
return await this.connectAsync(connectDetails, configuration);
};

/**
Expand All @@ -320,5 +345,13 @@ Boost.prototype.afterInitialization = function() {
this.on('hub-found', hubDetails => (this.hubDetails = hubDetails));
};

/**
* Hub motor configuration for car and Vernie mode
*/
Boost.prototype.motorConfig = {
car: { left: 'B', right : 'A' },
vernie : { left: 'A', right : 'B' }
};

module.exports.Boost = Boost;
module.exports.Hub = Hub;
71 changes: 67 additions & 4 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,35 +37,98 @@ describe('Hub', function() {
assert.equal(hub.useMetric, false);
});
});

describe('#afterInitialization', function() {
it('correct configuration AB', function() {
const hub = new Hub();
hub.afterInitialization({ left: 'A', right: 'B'});
});
it('correct configuration BA', function() {
const hub = new Hub();
hub.afterInitialization({ left: 'B', right: 'A'});
});
it('invalid configuration AA', function() {
const hub = new Hub();
assert.throws(() => hub.afterInitialization({ left: 'A', right: 'A'}), Error);
});
it('invalid configuration BB', function() {
const hub = new Hub();
assert.throws(() => hub.afterInitialization({ left: 'B', right: 'B'}), Error);
});
it('invalid configuration CB', function() {
const hub = new Hub();
assert.throws(() => hub.afterInitialization({ left: 'C', right: 'B'}), Error);
});
it('invalid configuration AC', function() {
const hub = new Hub();
assert.throws(() => hub.afterInitialization({ left: 'A', right: 'C'}), Error);
});
});

describe('#drive', function() {
it('correct values to motorAngleMultiAsync', function() {
it('correct values to motorAngleMultiAsync (motor AB)', function() {
let values = [];
Hub.prototype.motorAngleMultiAsync = (...rest) => values = [ ...rest ];

const config = new Boost().motorConfig.vernie;

const hub = new Hub();
hub.afterInitialization();
hub.afterInitialization(config);

hub.drive(100);
assert.equal(values[0], 2850);
assert.equal(values[1], 25);
assert.equal(values[2], 25);
assert.equal(values[3], true);
});
it('correct values to motorAngleMultiAsync (motor BA)', function() {
let values = [];
Hub.prototype.motorAngleMultiAsync = (...rest) => values = [ ...rest ];

const config = new Boost().motorConfig.car;

const hub = new Hub();
hub.afterInitialization(config);

hub.drive(100);
assert.equal(values[0], 2850);
assert.equal(values[1], -25);
assert.equal(values[2], -25);
assert.equal(values[3], true);
});
});

describe('#turn', function() {
it('correct values to motorAngleMultiAsync', function() {
it('correct values to motorAngleMultiAsync (motor AB)', async function() {
let values = [];
Hub.prototype.motorAngleMultiAsync = (...rest) => values = [ ...rest ];

const config = new Boost().motorConfig.vernie;

const hub = new Hub();
hub.afterInitialization();
hub.afterInitialization(config);

hub.turn(90);
assert.equal(values[0], 230.4);
assert.equal(values[1], 20);
assert.equal(values[2], -20);
assert.equal(values[3], true);
});
it('correct values to motorAngleMultiAsync (motor BA)', async function() {
let values = [];
Hub.prototype.motorAngleMultiAsync = (...rest) => values = [ ...rest ];

const config = new Boost().motorConfig.car;

const hub = new Hub();
hub.afterInitialization(config);

hub.turn(90);
assert.equal(values[0], 230.4);
assert.equal(values[1], -20);
assert.equal(values[2], 20);
assert.equal(values[3], true);
});
});
});

Expand Down