-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add optional ANT+ Bike Power output.
- Loading branch information
Showing
11 changed files
with
206 additions
and
4 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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,112 @@ | ||
import Ant from 'gd-ant-plus'; | ||
import util from 'util'; | ||
import {Timer} from '../../util/timer'; | ||
|
||
const debuglog = util.debuglog('gymnasticon:servers:ant'); | ||
|
||
const DEVICE_TYPE = 0x0b; // power meter | ||
const DEVICE_NUMBER = 1; | ||
const PERIOD = 8182; // 8182/32768 ~4hz | ||
const RF_CHANNEL = 57; // 2457 MHz | ||
const BROADCAST_INTERVAL = PERIOD / 32768; // seconds | ||
|
||
const defaults = { | ||
deviceId: 11234, | ||
channel: 1, | ||
} | ||
|
||
/** | ||
* Handles communication with apps (e.g. Zwift) using the ANT+ Bicycle Power | ||
* profile (instantaneous cadence and power). | ||
*/ | ||
export class AntServer { | ||
/** | ||
* Create an AntServer instance. | ||
* @param {Ant.USBDevice} antStick - ANT+ device instance | ||
* @param {object} options | ||
* @param {number} options.channel - ANT+ channel | ||
* @param {number} options.deviceId - ANT+ device id | ||
*/ | ||
constructor(antStick, options = {}) { | ||
const opts = {...defaults, ...options}; | ||
this.stick = antStick; | ||
this.deviceId = opts.deviceId; | ||
this.eventCount = 0; | ||
this.accumulatedPower = 0; | ||
this.channel = opts.channel; | ||
|
||
this.power = 0; | ||
this.cadence = 0; | ||
|
||
this.broadcastInterval = new Timer(BROADCAST_INTERVAL); | ||
this.broadcastInterval.on('timeout', this.onBroadcastInterval.bind(this)); | ||
} | ||
|
||
/** | ||
* Start the ANT+ server (setup channel and start broadcasting). | ||
*/ | ||
start() { | ||
const {stick, channel, deviceId} = this; | ||
const messages = [ | ||
Ant.Messages.assignChannel(channel, 'transmit'), | ||
Ant.Messages.setDevice(channel, deviceId, DEVICE_TYPE, DEVICE_NUMBER), | ||
Ant.Messages.setFrequency(channel, RF_CHANNEL), | ||
Ant.Messages.setPeriod(channel, PERIOD), | ||
Ant.Messages.openChannel(channel), | ||
]; | ||
debuglog(`ANT+ server start [deviceId=${deviceId} channel=${channel}]`); | ||
for (let m of messages) { | ||
stick.write(m); | ||
} | ||
this.broadcastInterval.reset(); | ||
} | ||
|
||
/** | ||
* Stop the ANT+ server (stop broadcasting and unassign channel). | ||
*/ | ||
stop() { | ||
const {stick, channel} = this; | ||
this.broadcastInterval.cancel(); | ||
const messages = [ | ||
Ant.Messages.closeChannel(channel), | ||
Ant.Messages.unassignChannel(channel), | ||
]; | ||
for (let m of messages) { | ||
stick.write(m); | ||
} | ||
} | ||
|
||
/** | ||
* Update instantaneous power and cadence. | ||
* @param {object} measurement | ||
* @param {number} measurement.power - power in watts | ||
* @param {number} measurement.cadence - cadence in rpm | ||
*/ | ||
updateMeasurement({ power, cadence }) { | ||
this.power = power; | ||
this.cadence = cadence; | ||
} | ||
|
||
/** | ||
* Broadcast instantaneous power and cadence. | ||
*/ | ||
onBroadcastInterval() { | ||
const {stick, channel, power, cadence} = this; | ||
this.accumulatedPower += power; | ||
this.accumulatedPower &= 0xffff; | ||
const data = [ | ||
channel, | ||
0x10, // power only | ||
this.eventCount, | ||
0xff, // pedal power not used | ||
cadence, | ||
...Ant.Messages.intToLEHexArray(this.accumulatedPower, 2), | ||
...Ant.Messages.intToLEHexArray(power, 2), | ||
]; | ||
const message = Ant.Messages.broadcastData(data); | ||
debuglog(`ANT+ broadcast power=${power}W cadence=${cadence}rpm accumulatedPower=${this.accumulatedPower}W eventCount=${this.eventCount} message=${message.toString('hex')}`); | ||
stick.write(message); | ||
this.eventCount++; | ||
this.eventCount &= 0xff; | ||
} | ||
} |
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
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
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,12 @@ | ||
import Ant from 'gd-ant-plus'; | ||
|
||
/** | ||
* Create ANT+ stick. | ||
*/ | ||
export function createAntStick() { | ||
let stick = new Ant.GarminStick3; // 0fcf:1009 | ||
if (!stick.is_present()) { | ||
stick = new Ant.GarminStick2; // 0fcf:1008 | ||
} | ||
return stick; | ||
} |