Skip to content

Commit

Permalink
feat: Add support for DS packet heartbeat monitoring (#95)
Browse files Browse the repository at this point in the history
  • Loading branch information
zhiquanyeo authored Feb 21, 2021
1 parent 1914c74 commit f657f46
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 6 deletions.
6 changes: 3 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
},
"homepage": "https://github.com/wpilibsuite/wpilib-ws-robot-romi#readme",
"dependencies": {
"@wpilib/wpilib-ws-robot": "1.0.0",
"@wpilib/wpilib-ws-robot": "1.1.0",
"commander": "^6.1.0",
"cors": "^2.8.5",
"express": "^4.17.1",
Expand Down
18 changes: 16 additions & 2 deletions src/robot/romi-robot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,9 @@ export default class WPILibWSRomiRobot extends WPILibWSRobotBase {
// Keep track of whether or not the robot is DS enabled/disabled
private _dsEnabled: boolean = false;

// Keep track of the DS heartbeat
private _dsHeartbeatPresent: boolean = false;

// Take in the abstract bus, since this will allow us to
// write unit tests more easily
constructor(bus: QueuedI2CBus, address: number, romiConfig?: RomiConfiguration) {
Expand Down Expand Up @@ -134,9 +137,10 @@ export default class WPILibWSRomiRobot extends WPILibWSRobotBase {
this._resetToCleanState();

// Set up the heartbeat. Only send the heartbeat if we have
// an active WS connection
// an active WS connection, the robot is in enabled state
// AND we have a recent-ish DS packet
this._heartbeatTimer = setInterval(() => {
if (this._numWsConnections > 0 && this._dsEnabled) {
if (this._numWsConnections > 0 && this._dsEnabled && this._dsHeartbeatPresent) {
this._i2cHandle.writeByte(RomiDataBuffer.heartbeat.offset, 1)
.catch(err => {
this._i2cErrorDetector.addErrorInstance();
Expand Down Expand Up @@ -469,6 +473,16 @@ export default class WPILibWSRomiRobot extends WPILibWSRobotBase {
this._dsEnabled = false;
}

public onDSPacketTimeoutOccurred(): void {
console.log("[ROMI] DS Packet Heartbeat Lost");
this._dsHeartbeatPresent = false;
}

public onDSPacketTimeoutCleared(): void {
console.log("[ROMI] DS Packet Heartbeat Acquired");
this._dsHeartbeatPresent = true;
}

public async queryFirmwareIdent(): Promise<number> {
return this._i2cHandle.readByte(RomiDataBuffer.firmwareIdent.offset)
.then(fwIdent => {
Expand Down

0 comments on commit f657f46

Please sign in to comment.