-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add prefix topics & fix: 2025 robot communication (#158)
* feat: add prefix topics * feat: update readme * feat(topic)!: remove immediateNotify BREAKING CHANGE: removes immediateNotify from topic subscription * chore: update changelog * chore: bump version * fix(readme): subscribe fn params type * fix: integer overflow on UID generator * fix: remove RTT and only heartbeat on v4.0 * fix: start publish timeout after robot connected * feat: add example client * feat: count sub/pub ids from 0 * chore: update deps
- Loading branch information
1 parent
383a361
commit b276b31
Showing
54 changed files
with
1,732 additions
and
1,055 deletions.
There are no files selected for viewing
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,20 @@ | ||
{ | ||
"extends": ["../../.eslintrc.json"], | ||
"ignorePatterns": ["!**/*"], | ||
"overrides": [ | ||
{ | ||
"files": ["*.ts", "*.tsx", "*.js", "*.jsx"], | ||
"rules": { | ||
"no-console": "off" | ||
} | ||
}, | ||
{ | ||
"files": ["*.ts", "*.tsx"], | ||
"rules": {} | ||
}, | ||
{ | ||
"files": ["*.js", "*.jsx"], | ||
"rules": {} | ||
} | ||
] | ||
} |
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,61 @@ | ||
{ | ||
"name": "example-client", | ||
"$schema": "../../node_modules/nx/schemas/project-schema.json", | ||
"sourceRoot": "apps/example-client/src", | ||
"projectType": "application", | ||
"tags": [], | ||
"targets": { | ||
"build": { | ||
"executor": "@nx/esbuild:esbuild", | ||
"outputs": ["{options.outputPath}"], | ||
"defaultConfiguration": "production", | ||
"options": { | ||
"platform": "node", | ||
"outputPath": "dist/apps/example-client", | ||
"format": ["cjs"], | ||
"bundle": false, | ||
"main": "apps/example-client/src/main.ts", | ||
"tsConfig": "apps/example-client/tsconfig.app.json", | ||
"assets": ["apps/example-client/src/assets"], | ||
"generatePackageJson": true, | ||
"esbuildOptions": { | ||
"sourcemap": true, | ||
"outExtension": { | ||
".js": ".js" | ||
} | ||
} | ||
}, | ||
"configurations": { | ||
"development": {}, | ||
"production": { | ||
"esbuildOptions": { | ||
"sourcemap": false, | ||
"outExtension": { | ||
".js": ".js" | ||
} | ||
} | ||
} | ||
} | ||
}, | ||
"serve": { | ||
"executor": "@nx/js:node", | ||
"defaultConfiguration": "development", | ||
"dependsOn": ["build"], | ||
"options": { | ||
"buildTarget": "example-client:build", | ||
"runBuildTargetDependencies": false | ||
}, | ||
"configurations": { | ||
"development": { | ||
"buildTarget": "example-client:build:development" | ||
}, | ||
"production": { | ||
"buildTarget": "example-client:build:production" | ||
} | ||
} | ||
}, | ||
"lint": { | ||
"executor": "@nx/eslint:lint" | ||
} | ||
} | ||
} |
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,88 @@ | ||
// eslint-disable-next-line @nx/enforce-module-boundaries | ||
import { NetworkTables, NetworkTablesTypeInfos } from '../../../packages/ntcore-ts-client/src'; | ||
|
||
// Get or create the NT client instance | ||
const ntcore = NetworkTables.getInstanceByURI('localhost'); | ||
|
||
// ------------------------------------------------ // | ||
// Example of using a topic to subscribe to a value // | ||
// ------------------------------------------------ // | ||
|
||
// Create the gyro topic | ||
const gyroTopic = ntcore.createTopic<number>('/MyTable/Gyro', NetworkTablesTypeInfos.kDouble); | ||
|
||
// Subscribe and immediately call the callback with the current value | ||
gyroTopic.subscribe((value) => { | ||
console.log(`Got Gyro Value: ${value}`); | ||
}); | ||
|
||
// Or you can use the topic's announce parameters to get more info, like the topic ID | ||
gyroTopic.subscribe((value, params) => { | ||
console.log(`Got Gyro Value: ${value} at from topic id ${params.id}`); | ||
}); | ||
|
||
// ---------------------------------------------- // | ||
// Example of using a topic to publish to a value // | ||
// --------------------------------------------- | ||
|
||
// Create the autoMode topic w/ a default return value of 'No Auto' | ||
(async () => { | ||
const autoModeTopic = ntcore.createTopic<string>('/MyTable/autoMode', NetworkTablesTypeInfos.kString, 'No Auto'); | ||
|
||
// Make us the publisher | ||
await autoModeTopic.publish(); | ||
|
||
// Set a new value, this will error if we aren't the publisher! | ||
autoModeTopic.setValue('25 Ball Auto and Climb'); | ||
})(); | ||
|
||
// --------------------------------------------------------------- // | ||
// Example of using a prefix topic to subscribe to multiple topics // | ||
// --------------------------------------------------------------- // | ||
|
||
// Create the accelerator topic | ||
const accelerometerTopic = ntcore.createPrefixTopic('/MyTable/Accelerometer/'); | ||
|
||
let x, y, z: any; | ||
|
||
// Subscribe to all topics under the prefix /MyTable/Accelerometer/ | ||
accelerometerTopic.subscribe((value, params) => { | ||
console.log(`Got Accelerometer Value: ${value} from topic ${params.name}`); // i.e. Got Accelerometer Value: 9.81 from topic /MyTable/Accelerometer/Y | ||
|
||
// You can also use the topic name to determine which value to set | ||
if (params.name.endsWith('X')) { | ||
x = value; | ||
} else if (params.name.endsWith('Y')) { | ||
y = value; | ||
} else if (params.name.endsWith('Z')) { | ||
z = value; | ||
} | ||
|
||
// Since there can be THAT many different types in subtopics, | ||
// you can use the type information for other checks... | ||
if (params.type === 'int') { | ||
console.warn('Hmm... the accelerometer seems low precision'); | ||
} else if (params.type === 'double') { | ||
console.log('The accelerometer is high precision'); | ||
|
||
const typedX = x as number; | ||
const typedY = y as number; | ||
const typedZ = z as number; | ||
|
||
console.log(`Latest update: X: ${typedX}, Y: ${typedY}, Z: ${typedZ}`); | ||
} | ||
}); | ||
|
||
// x, y, and z will be updated as new values come in | ||
|
||
// ---------------------------------------------------------- // | ||
// Example of using a prefix topic to subscribe to all topics // | ||
// ---------------------------------------------------------- // | ||
|
||
// Create a prefix for all topics | ||
const allTopics = ntcore.createPrefixTopic(''); | ||
|
||
// Sub scribe to all topics | ||
allTopics.subscribe((value, params) => { | ||
console.log(`Got Value: ${value} from topic ${params.name}`); | ||
}); |
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,9 @@ | ||
{ | ||
"extends": "./tsconfig.json", | ||
"compilerOptions": { | ||
"outDir": "../../dist/out-tsc", | ||
"module": "ESNext", | ||
"types": ["node"] | ||
}, | ||
"include": ["src/main.ts"] | ||
} |
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,13 @@ | ||
{ | ||
"extends": "../../tsconfig.base.json", | ||
"files": [], | ||
"include": [], | ||
"references": [ | ||
{ | ||
"path": "./tsconfig.app.json" | ||
} | ||
], | ||
"compilerOptions": { | ||
"esModuleInterop": true | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.