-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b784dc4
commit 00d36f4
Showing
8 changed files
with
138 additions
and
23 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,3 @@ | ||
export type { Slam } from './slam/slam'; | ||
export type { GetPositionResponse } from './slam/types'; | ||
export { SlamClient } from './slam/client'; |
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,100 @@ | ||
import pb from '../../gen/service/slam/v1/slam_pb'; | ||
import { SLAMServiceClient } from '../../gen/service/slam/v1/slam_pb_service'; | ||
import { RobotClient } from '../../robot'; | ||
import type { Options, StructType } from '../../types'; | ||
import { doCommandFromClient, promisify } from '../../utils'; | ||
import type { Slam } from './slam'; | ||
|
||
/** | ||
* A gRPC-web client for a SLAM service. | ||
* | ||
* @group Clients | ||
*/ | ||
export class SlamClient implements Slam { | ||
private client: SLAMServiceClient; | ||
private readonly name: string; | ||
private readonly options: Options; | ||
|
||
constructor(client: RobotClient, name: string, options: Options = {}) { | ||
this.client = client.createServiceClient(SLAMServiceClient); | ||
this.name = name; | ||
this.options = options; | ||
} | ||
|
||
private get service() { | ||
return this.client; | ||
} | ||
|
||
async getPosition() { | ||
const { service } = this; | ||
|
||
const request = new pb.GetPositionRequest(); | ||
request.setName(this.name); | ||
|
||
this.options.requestLogger?.(request); | ||
|
||
const response = await promisify< | ||
pb.GetPositionRequest, | ||
pb.GetPositionResponse | ||
>(service.getPosition.bind(service), request); | ||
|
||
return response.toObject(); | ||
} | ||
|
||
async getPointCloudMap() { | ||
const { service } = this; | ||
|
||
const request = new pb.GetPointCloudMapRequest(); | ||
request.setName(this.name); | ||
|
||
this.options.requestLogger?.(request); | ||
|
||
const response = await promisify< | ||
pb.GetPointCloudMapRequest, | ||
pb.GetPointCloudMapResponse | ||
>(service.getPointCloudMap.bind(service), request); | ||
|
||
return response.getPointCloudPcdChunk_asU8(); | ||
} | ||
|
||
async getInternalState() { | ||
const { service } = this; | ||
|
||
const request = new pb.GetInternalStateRequest(); | ||
request.setName(this.name); | ||
|
||
this.options.requestLogger?.(request); | ||
|
||
const response = await promisify< | ||
pb.GetInternalStateRequest, | ||
pb.GetInternalStateResponse | ||
>(service.getInternalState.bind(service), request); | ||
|
||
return response.getInternalStateChunk_asU8(); | ||
} | ||
|
||
async getLatestMapInfo() { | ||
const { service } = this; | ||
|
||
const request = new pb.GetLatestMapInfoRequest(); | ||
request.setName(this.name); | ||
|
||
this.options.requestLogger?.(request); | ||
|
||
const response = await promisify< | ||
pb.GetLatestMapInfoRequest, | ||
pb.GetLatestMapInfoResponse | ||
>(service.getLatestMapInfo.bind(service), request); | ||
|
||
const timestamp = response.getLastMapUpdate(); | ||
if (!timestamp) { | ||
throw new Error('no map update'); | ||
} | ||
return new Date(timestamp.getSeconds() * 1e3 + timestamp.getNanos() / 1e6); | ||
} | ||
|
||
async doCommand(command: StructType): Promise<StructType> { | ||
const { service } = this; | ||
return doCommandFromClient(service, this.name, command, this.options); | ||
} | ||
} |
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,26 @@ | ||
import type { Resource } from '../../types'; | ||
import type { GetPositionResponse } from './types'; | ||
|
||
/** | ||
* A service that allows your robot to create a map of its surroundings and find | ||
* its location within that map. | ||
*/ | ||
export interface Slam extends Resource { | ||
/** | ||
* Get the current position of the specified source component in the point | ||
* cloud SLAM map. | ||
*/ | ||
getPosition: () => Promise<GetPositionResponse>; | ||
|
||
/** Get the point cloud SLAM map. */ | ||
getPointCloudMap: () => Promise<Uint8Array>; | ||
|
||
/** | ||
* Get the internal state of the SLAM algorithm required to continue | ||
* mapping/localization. | ||
*/ | ||
getInternalState: () => Promise<Uint8Array>; | ||
|
||
/** Get the timestamp of the last update to the point cloud SLAM map. */ | ||
getLatestMapInfo: () => Promise<Date>; | ||
} |
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,3 @@ | ||
import pb from '../../gen/service/slam/v1/slam_pb'; | ||
|
||
export type GetPositionResponse = pb.GetPositionResponse.AsObject; |